Skip to content

Commit

Permalink
Set both Symbol.species and "@@species" on Concast constructor (#7403)
Browse files Browse the repository at this point in the history
An attempt to address the problems I speculated about in
#6520 (comment)

Theory: in the Hermes JS engine, Symbol.species is not defined, so
Object.defineProperty was not called, but perhaps "@@species" was getting
set somewhere else by a polyfill library, causing the zen-observable
library to fall back to "@@species" instead of using/ignoring the
nonexistent Symbol.species symbol.
  • Loading branch information
benjamn committed Dec 3, 2020
1 parent 8d265e6 commit 2a2843c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/utilities/observables/Concast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,19 @@ export class Concast<T> extends Observable<T> {
// Those methods assume (perhaps unwisely?) that they can call the
// subtype's constructor with an observer registration function, but the
// Concast constructor uses a different signature. Defining this
// Symbol.species getter function on the Concast constructor function is
// a hint to generic Observable code to use the default constructor
// instead of trying to do `new Concast(observer => ...)`.
// Symbol.species property on the Concast constructor function is a hint
// to generic Observable code to use the default constructor instead of
// trying to do `new Concast(observer => ...)`.
function setSpecies(key: symbol | string) {
// Object.defineProperty is necessary because Concast[Symbol.species]
// is a getter by default in modern JS environments, so we can't
// assign to it with a normal assignment expression.
Object.defineProperty(Concast, key, { value: Observable });
}
if (typeof Symbol === "function" && Symbol.species) {
Object.defineProperty(Concast, Symbol.species, {
value: Observable,
});
setSpecies(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.
setSpecies("@@species");

0 comments on commit 2a2843c

Please sign in to comment.