-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Backport v3.4 Symbol.species fix for Concast and ObservableQuery. #7660
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab6b60a
Set both Symbol.species and "@@species" on Concast constructor (#7403)
benjamn 6d7a8eb
Fix minor type error for private Concast#resolve function.
benjamn 78d9074
Extract fixObservableSubclass helper function.
benjamn f1f9248
Allow Concast<T> constructor to be invoked with a Subscriber<T>.
benjamn 1b89257
Use fixObservableSubclass for ObservableQuery, too.
benjamn 7648cf6
Increase bundlesize limit from 25.5kB to 25.6kB.
benjamn fc57af8
Mention PRs #7403 and #7660 in CHANGELOG.md.
benjamn 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Observable } from "../Observable"; | ||
import { Concast } from "../Concast"; | ||
|
||
function toArrayPromise<T>(observable: Observable<T>): Promise<T[]> { | ||
return new Promise<T[]>((resolve, reject) => { | ||
const values: T[] = []; | ||
observable.subscribe({ | ||
next(value) { | ||
values.push(value); | ||
}, | ||
error: reject, | ||
complete() { | ||
resolve(values); | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
describe("Observable subclassing", () => { | ||
it("Symbol.species is defined for Concast subclass", () => { | ||
const concast = new Concast([ | ||
Observable.of(1, 2, 3), | ||
Observable.of(4, 5), | ||
]); | ||
expect(concast).toBeInstanceOf(Concast); | ||
|
||
const mapped = concast.map(n => n * 2); | ||
expect(mapped).toBeInstanceOf(Observable); | ||
expect(mapped).not.toBeInstanceOf(Concast); | ||
|
||
return toArrayPromise(mapped).then(doubles => { | ||
expect(doubles).toEqual([2, 4, 6, 8, 10]); | ||
}); | ||
}); | ||
|
||
it("Inherited Concast.of static method returns a Concast", () => { | ||
const concast = Concast.of("asdf", "qwer", "zxcv"); | ||
expect(concast).toBeInstanceOf(Observable); | ||
expect(concast).toBeInstanceOf(Concast); | ||
|
||
return toArrayPromise(concast).then(values => { | ||
expect(values).toEqual(["asdf", "qwer", "zxcv"]); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Observable } from "./Observable"; | ||
|
||
// Generic implementations of Observable.prototype methods like map and | ||
// filter need to know how to create a new Observable from an Observable | ||
// subclass (like Concast or ObservableQuery). Those methods assume | ||
// (perhaps unwisely?) that they can call the subtype's constructor with a | ||
// Subscriber function, even though the subclass constructor might expect | ||
// different parameters. Defining this static Symbol.species property on | ||
// the subclass is a hint to generic Observable code to use the default | ||
// constructor instead of trying to do `new Subclass(observer => ...)`. | ||
export function fixObservableSubclass< | ||
S extends new (...args: any[]) => Observable<any>, | ||
>(subclass: S): S { | ||
function set(key: symbol | string) { | ||
// Object.defineProperty is necessary because the Symbol.species | ||
// property is a getter by default in modern JS environments, so we | ||
// can't assign to it with a normal assignment expression. | ||
Object.defineProperty(subclass, key, { value: Observable }); | ||
} | ||
if (typeof Symbol === "function" && Symbol.species) { | ||
set(Symbol.species); | ||
} | ||
// The "@@species" string is used as a fake Symbol.species value in some | ||
// polyfill systems (including the SymbolSpecies variable used by | ||
// zen-observable), so we should set it as well, to be safe. | ||
set("@@species"); | ||
return subclass; | ||
} |
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 was the best way I could find to express that
subclass
should be a constructor function that creates a subtype ofObservable
.