Skip to content

Commit

Permalink
fix: add delayOnMicrotaskQueue()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Jul 16, 2019
1 parent 22ecf5b commit 656e943
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { fakeAsync, flushMicrotasks } from "@angular/core/testing";
import { Subject } from "rxjs";
import { expectSingleCallAndReset } from "s-ng-dev-utils";
import {
testCompletionPropagation,
testErrorPropagation,
testUnsubscribePropagation,
} from "../../test-helpers/misc-helpers";
import { delayOnMicrotaskQueue } from "./delay-on-microtask-queue";

describe("delayOnMicrotaskQueue()", () => {
it("passes along values asynchronously", fakeAsync(() => {
const source = new Subject<number>();
const spy = jasmine.createSpy();
source.pipe(delayOnMicrotaskQueue()).subscribe(spy);

source.next(1);
expect(spy).not.toHaveBeenCalled();
flushMicrotasks();
expectSingleCallAndReset(spy, 1);

source.next(2);
expect(spy).not.toHaveBeenCalled();
flushMicrotasks();
expectSingleCallAndReset(spy, 2);
}));

it(
"passes along unsubscribes synchronously",
testUnsubscribePropagation(delayOnMicrotaskQueue),
);

it(
"passes along errors synchronously",
testErrorPropagation(delayOnMicrotaskQueue),
);

it(
"passes along completion synchronously",
testCompletionPropagation(delayOnMicrotaskQueue),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { asapScheduler } from "rxjs";
import { delay } from "rxjs/operators";

/**
* Delays the emission of items from the source Observable using the microtask queue.
*/
export function delayOnMicrotaskQueue() {
return delay(0, asapScheduler);
}
1 change: 1 addition & 0 deletions projects/s-rxjs-utils/src/lib/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { cache } from "./cache";
export { delayOnMicrotaskQueue } from "./delay-on-microtask-queue";
export { distinctUntilKeysChanged } from "./distinct-until-keys-changed";
export { filterBehavior } from "./filter-behavior";
export { logValues } from "./log-values";
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Subject } from "rxjs";
import {
cache,
createOperatorFunction,
delayOnMicrotaskQueue,
distinctUntilKeysChanged,
filterBehavior,
logValues,
Expand All @@ -28,6 +29,7 @@ export class AppComponent {
new Subject<number>().pipe(
cache(),
createOperatorFunction(noop),
delayOnMicrotaskQueue(),
filterBehavior(() => true),
logValues(),
skipAfter(new Subject()),
Expand Down

0 comments on commit 656e943

Please sign in to comment.