-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(fromEvent): match targets properly; fix result selector type #6208
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea57c6a
test(fromEvent): make dtslint tests fail
cartant fcf7d34
fix(fromEvent): use methods not props
cartant ad8a36d
refactor: remove redundant optional qualifiers
cartant c8f7075
fix: result selector and target cannot both use T
cartant 36a8230
chore: update api_guardian
cartant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,75 @@ | ||
import { fromEvent } from 'rxjs'; | ||
import { JQueryStyleEventEmitter } from '../../src/internal/observable/fromEvent'; | ||
import { A, B } from '../helpers'; | ||
import { | ||
HasEventTargetAddRemove, | ||
NodeStyleEventEmitter, | ||
NodeCompatibleEventEmitter, | ||
JQueryStyleEventEmitter | ||
} from '../../src/internal/observable/fromEvent'; | ||
import { B } from '../helpers'; | ||
|
||
declare const eventTargetSource: EventTarget; | ||
|
||
it('should support an event target source', () => { | ||
const source: HasEventTargetAddRemove<Event> = eventTargetSource; | ||
const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event> | ||
}); | ||
|
||
it('should support an event target source result selector', () => { | ||
const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable<string> | ||
}); | ||
|
||
declare const documentSource: HTMLDocument; | ||
|
||
it('should support a document source', () => { | ||
const source: HasEventTargetAddRemove<Event> = documentSource; | ||
const a = fromEvent(documentSource, "click"); // $ExpectType Observable<Event> | ||
}); | ||
|
||
interface NodeStyleSource { | ||
addListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this; | ||
removeListener: (eventName: string | symbol, handler: (...args: any[]) => void) => this; | ||
}; | ||
it('should support a document source result selector', () => { | ||
const a = fromEvent(documentSource, "click", () => "clunk"); // $ExpectType Observable<string> | ||
}); | ||
|
||
declare const nodeStyleSource : NodeStyleSource; | ||
// Pick the parts that will match NodeStyleEventEmitter. If this isn't done, it | ||
// will match JQueryStyleEventEmitter - because of the `on` and `off` methods - | ||
// despite the latter being declared last in the EventTargetLike union. | ||
declare const nodeStyleSource: Pick<typeof process, 'addListener' | 'removeListener'>; | ||
|
||
it('should support a node-style source', () => { | ||
const a = fromEvent(nodeStyleSource, "something"); // $ExpectType Observable<unknown> | ||
const b = fromEvent<B>(nodeStyleSource, "something"); // $ExpectType Observable<B> | ||
const source: NodeStyleEventEmitter = nodeStyleSource; | ||
const a = fromEvent(nodeStyleSource, "exit"); // $ExpectType Observable<unknown> | ||
const b = fromEvent<B>(nodeStyleSource, "exit"); // $ExpectType Observable<B> | ||
}); | ||
|
||
it('should support a node-style source result selector', () => { | ||
const a = fromEvent(nodeStyleSource, "exit", () => "bye"); // $ExpectType Observable<string> | ||
}); | ||
|
||
declare const nodeCompatibleSource: { | ||
addListener: (eventName: string, handler: (...args: any[]) => void) => void; | ||
removeListener: (eventName: string, handler: (...args: any[]) => void) => void; | ||
const nodeCompatibleSource = { | ||
addListener(eventName: "something", handler: () => void) {}, | ||
removeListener(eventName: "something", handler: () => void) {} | ||
}; | ||
|
||
it('should support a node-compatible source', () => { | ||
const source: NodeCompatibleEventEmitter = nodeCompatibleSource; | ||
const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectType Observable<unknown> | ||
const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectType Observable<B> | ||
}); | ||
|
||
declare const jQueryStyleSource: JQueryStyleEventEmitter<A, B>; | ||
it('should support a node-compatible source result selector', () => { | ||
const a = fromEvent(nodeCompatibleSource, "something", () => "something else"); // $ExpectType Observable<string> | ||
}); | ||
|
||
const jQueryStyleSource = { | ||
on(eventName: "something", handler: (this: any, b: B) => any) {}, | ||
off(eventName: "something", handler: (this: any, b: B) => any) {} | ||
}; | ||
|
||
it('should support a jQuery-style source', () => { | ||
const source: JQueryStyleEventEmitter<any, any> = jQueryStyleSource; | ||
const a = fromEvent(jQueryStyleSource, "something"); // $ExpectType Observable<B> | ||
const b = fromEvent<B>(jQueryStyleSource, "something"); // $ExpectType Observable<B> | ||
}); | ||
|
||
it('should support a jQuery-style source result selector', () => { | ||
const a = fromEvent(jQueryStyleSource, "something", () => "something else"); // $ExpectType Observable<string> | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to make sense. We're loosing up the type here, because really it's the
T
that matters. However, part of me feels like we should undeprecate this signature (as discussed in #5824), and in that case, we should try to get the types correct that are being passed to the result selector. I suppose we can do that in another pass though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. That's something that can be done after it's split into separate signatures. ATM,
FromEventTarget
makes the function look simpler than it really is and theT
is really only applicable to some of the signatures.