Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Feb 8, 2022
2 parents 69147b9 + 0ab91eb commit 7214daa
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion integration/side-effects/snapshots/esm/ajax.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@

const _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm/fetch.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

const _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

const _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm/operators.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

const _bind = Function.prototype.bind;
2 changes: 1 addition & 1 deletion integration/side-effects/snapshots/esm/testing.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@

const _bind = Function.prototype.bind;
2 changes: 1 addition & 1 deletion integration/side-effects/snapshots/esm/websocket.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@

const _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/ajax.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/fetch.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/operators.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/testing.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
4 changes: 3 additions & 1 deletion integration/side-effects/snapshots/esm5/websocket.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "tslib";
import 'tslib';

var _bind = Function.prototype.bind;
28 changes: 28 additions & 0 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,34 @@ describe('Observable', () => {
});

describe('subscribe', () => {
it('should work with handlers with hacked bind methods', () => {
const source = of('Hi');
const results: any[] = [];
const next = function (value: string) {
results.push(value);
}
next.bind = () => { /* lol */};

const complete = function () {
results.push('done');
}
complete.bind = () => { /* lol */};

source.subscribe({ next, complete });
expect(results).to.deep.equal(['Hi', 'done']);
});

it('should work with handlers with hacked bind methods, in the error case', () => {
const source = throwError(() => 'an error');
const results: any[] = [];
const error = function (value: string) {
results.push(value);
}

source.subscribe({ error });
expect(results).to.deep.equal(['an error']);
});

it('should be synchronous', () => {
let subscribed = false;
let nexted: string;
Expand Down
18 changes: 15 additions & 3 deletions src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
}
}

/**
* This bind is captured here because we want to be able to have
* compatibility with monoid libraries that tend to use a method named
* `bind`. In particular, a library called Monio requires this.
*/
const _bind = Function.prototype.bind;

function bind<Fn extends (...args: any[]) => any>(fn: Fn, thisArg: any): Fn {
return _bind.call(fn, thisArg);
}

export class SafeSubscriber<T> extends Subscriber<T> {
constructor(observerOrNext?: Partial<Observer<T>> | ((value: T) => void) | null) {
super();
Expand All @@ -152,9 +163,10 @@ export class SafeSubscriber<T> extends Subscriber<T> {
// going to put them all in a new destination with ensured methods
// for `next`, `error`, and `complete`. That's part of what makes this
// the "Safe" Subscriber.
next = observerOrNext.next?.bind(observerOrNext);
error = observerOrNext.error?.bind(observerOrNext);
complete = observerOrNext.complete?.bind(observerOrNext);
({ next, error, complete } = observerOrNext);
next = next && bind(next, observerOrNext);
error = error && bind(error, observerOrNext);
complete = complete && bind(complete, observerOrNext);
}

// Once we set the destination, the superclass `Subscriber` will
Expand Down

0 comments on commit 7214daa

Please sign in to comment.