-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support subclassing Observable with non-class constructor functions.
- Loading branch information
Showing
1 changed file
with
26 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
export { Observable } from "zen-observable/esm.js"; | ||
import { Observable } from "zen-observable/esm.js"; | ||
|
||
// When a non-native class constructor function attempts to subclass | ||
// Observable, compilers like Babel and TypeScript may compile the | ||
// super(subscriber) expression to Observable.call(this, subscriber) or | ||
// Observable.apply(this, arguments). Calling a native class constructor | ||
// in this way is forbidden by the ECMAScript specification, but we can | ||
// preserve backwards compatibility by overriding the Observable.call and | ||
// Observable.apply methods with an implementation that calls | ||
// Reflect.construct instead, when available. | ||
|
||
Observable.call = function (obs, sub) { | ||
return construct(this, obs, sub); | ||
}; | ||
|
||
Observable.apply = function (obs, args) { | ||
return construct(this, obs, args[0]); | ||
}; | ||
|
||
function construct(Super, self, subscriber) { | ||
return typeof Reflect === 'object' | ||
? Reflect.construct(Super, [subscriber], self.constructor) | ||
: Function.prototype.call.call(Super, self, subscriber) || self; | ||
} | ||
|
||
export { Observable } |