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. #7640

Merged
merged 2 commits 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"symbol-observable": "^2.0.0",
"ts-invariant": "^0.6.0",
"tslib": "^1.10.0",
"zen-observable-ts": "^1.0.0-beta.4"
"zen-observable-ts": "^1.0.0-beta.5"
},
"devDependencies": {
"@babel/parser": "7.12.11",
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/observables/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Observable,
Observer,
Subscription as ObservableSubscription,
Subscriber,
} from 'zen-observable-ts';

// This simplified polyfill attempts to follow the ECMAScript Observable
Expand All @@ -11,6 +12,7 @@ import 'symbol-observable';
export type {
Observer,
ObservableSubscription,
Subscriber,
};

// Use global module augmentation to add RxJS interop functionality. By
Expand Down
69 changes: 69 additions & 0 deletions src/utilities/observables/__tests__/Observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Observable, Subscriber } from '../Observable';

describe('Observable', () => {
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).toBe("function");
expect((obs as any).sub).toBe(subscriber);

expect(obs).toBeInstanceOf(Observable);
expect(obs).toBeInstanceOf(constructor);
expect(obs.constructor).toBe(constructor);

return new Promise((resolve, reject) => {
obs.subscribe({
next: resolve,
error: reject,
});
}).then(value => {
expect(value).toBe(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));
});
});
});