Skip to content

Commit

Permalink
Set Concast["@@species"] = Observable as well as Symbol.species.
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 2, 2020
1 parent 8d265e6 commit c8cdc6e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/utilities/observables/Concast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,23 @@ 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) {
if (key) {
// 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 might as well make sure we set it as well.
setSpecies("@@species");

0 comments on commit c8cdc6e

Please sign in to comment.