Skip to content
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

Support subclassing Observable with non-class constructor functions. #1

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import Observable = require("zen-observable");
export { Observable };
export type Observer<T> = ZenObservable.Observer<T>;
export type Subscription = ZenObservable.Subscription;
export type Subscriber<T> = ZenObservable.Subscriber<T>;
30 changes: 29 additions & 1 deletion module.js
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
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 (instance, subscriber) {
// Since Observable.call is a static method, 'this' will typically be the
// Observable constructor function, though it could be a subclass of the
// Observable constructor, which is why we don't just hard-code it here.
return construct(this, instance, subscriber);
};

Observable.apply = function (instance, args) {
return construct(this, instance, args[0]);
};

function construct(Super, instance, subscriber) {
return typeof Reflect === 'object'
? Reflect.construct(Super, [subscriber], instance.constructor)
: Function.prototype.call.call(Super, instance, subscriber) || instance;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use Function.prototype.call.call(Super, ...) instead of Super.call(...), since Super is often the Observable constructor, and we do not want to call back into the Observable.call implementation above.

}

export { Observable }
73 changes: 72 additions & 1 deletion tests/Observable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,80 @@
const { expect } = require("chai");
const { expect } = require("chai") as typeof import("chai");

import type {
Observable,
Subscriber,
} from "..";

export default function (Observable: typeof import("..").Observable) {
describe("Observable", () => {
it("Should be a constructor function", () => {
expect(typeof Observable).to.equal("function");
});

describe('subclassing by non-class constructor functions', () => {
function check(constructor: new <T>(sub: Subscriber<T>) => Observable<T>) {
constructor.prototype = Object.create(Observable.prototype, {
constructor: {
value: constructor,
},
});

const subscriber: Subscriber<number> = observer => {
observer.next(123);
observer.complete();
};

const obs = new constructor(subscriber) as Observable<number>;

expect(typeof (obs as any).sub).to.equal("function");
expect((obs as any).sub).to.equal(subscriber);

expect(obs).to.be.instanceOf(Observable);
expect(obs).to.be.instanceOf(constructor);
expect(obs.constructor).to.equal(constructor);

return new Promise((resolve, reject) => {
obs.subscribe({
next: resolve,
error: reject,
});
}).then(value => {
expect(value).to.equal(123);
});
}

function newify(
constructor: <T>(sub: Subscriber<T>) => void,
): new <T>(sub: Subscriber<T>) => Observable<T> {
return constructor as any;
}

it('simulating super(sub) with Observable.call(this, sub)', () => {
function SubclassWithSuperCall<T>(sub: Subscriber<T>) {
const self = Observable.call(this, sub) || this;
self.sub = sub;
return self;
}
return check(newify(SubclassWithSuperCall));
});

it('simulating super(sub) with Observable.apply(this, arguments)', () => {
function SubclassWithSuperApplyArgs<T>(_sub: Subscriber<T>) {
const self = Observable.apply(this, arguments) || this;
self.sub = _sub;
return self;
}
return check(newify(SubclassWithSuperApplyArgs));
});

it('simulating super(sub) with Observable.apply(this, [sub])', () => {
function SubclassWithSuperApplyArray<T>(...args: [Subscriber<T>]) {
const self = Observable.apply(this, args) || this;
self.sub = args[0];
return self;
}
return check(newify(SubclassWithSuperApplyArray));
});
});
});
}