Skip to content

Commit

Permalink
feat(pool-time-core): add a flushing mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
louisscruz committed Jun 29, 2020
1 parent 9360565 commit 0ce998e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions projects/pool-time-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"repository": "https://github.com/pool-time/pool-time",
"scripts": {
"build": "yarn clean && rollup -c",
"build:watch": "yarn clean && rollup -c -w",
"clean": "rm -rf dist",
"release": "semantic-release",
"test": "yarn test:base --watch",
Expand Down
50 changes: 50 additions & 0 deletions projects/pool-time-core/src/__tests__/poolTimeCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,56 @@ describe('PoolTime', () => {
});
});

describe('#flushInitialEmitQueue', () => {
it('emits the correct least common duration to the appropriate handlers', () => {
const poolTime = new PoolTime({ configuration });
const firstMockHandler = jest.fn();
const secondMockHandler = jest.fn();

poolTime.register(ONE_SECOND.key);

poolTime.subscribeToLeastCommonDurationChange(firstMockHandler);
poolTime.subscribeToLeastCommonDurationChange(secondMockHandler);

expect(firstMockHandler).not.toHaveBeenCalled();
expect(secondMockHandler).not.toHaveBeenCalled();

poolTime.flushInitialEmitQueue();

expect(firstMockHandler).toHaveBeenCalledTimes(1);
expect(firstMockHandler).toHaveBeenLastCalledWith({
upTo: ONE_MINUTE,
within: ONE_SECOND,
});
expect(secondMockHandler).toHaveBeenCalledTimes(1);
expect(secondMockHandler).toHaveBeenLastCalledWith({
upTo: ONE_MINUTE,
within: ONE_SECOND,
});
});

it('does not emit any kind of tick', () => {
const poolTime = new PoolTime({ configuration });
const firstMockHandler = jest.fn();
const secondMockHandler = jest.fn();

poolTime.register(ONE_SECOND.key);

poolTime.subscribeToTick(firstMockHandler);
poolTime.subscribeToTick(secondMockHandler);

poolTime.startTicking();

expect(firstMockHandler).not.toHaveBeenCalled();
expect(secondMockHandler).not.toHaveBeenCalled();

poolTime.flushInitialEmitQueue();

expect(firstMockHandler).not.toHaveBeenCalled();
expect(secondMockHandler).not.toHaveBeenCalled();
});
});

describe('#getTimes', () => {
it('returns the correct default times', () => {
const poolTime = new PoolTime({ configuration });
Expand Down
11 changes: 7 additions & 4 deletions projects/pool-time-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class PoolTime<T extends AdditionalTimeProperties<T>> {
this.times = this.generateTimes();
}

flushInitialEmitQueue(): void {
this.emitLeastCommonDurationChange(this.leastCommonDuration);
}

subscribeToLeastCommonDurationChange(
callback: (duration: CoreAccuracyEntry<T>) => void
): unsubscribeFunction {
Expand Down Expand Up @@ -325,9 +329,9 @@ class PoolTime<T extends AdditionalTimeProperties<T>> {
private emitLeastCommonDurationChange(
leastCommonDuration: CoreAccuracyEntry<T>
): void {
this.leastCommonDurationChangeSubscriptions.forEach((callback) =>
callback(leastCommonDuration)
);
this.leastCommonDurationChangeSubscriptions.forEach((callback) => {
callback(leastCommonDuration);
});
}

private emitTick(times: TimeState<T>): void {
Expand Down Expand Up @@ -373,7 +377,6 @@ class PoolTime<T extends AdditionalTimeProperties<T>> {
if (applicableNumberOfRegistrations === 0) {
const currentLeastCommonDuration = this.leastCommonDuration;
const nextLeastCommonDuration = this.findLeastCommonDuration();

if (nextLeastCommonDuration !== currentLeastCommonDuration) {
this.leastCommonDuration = nextLeastCommonDuration;
this.emitLeastCommonDurationChange(this.leastCommonDuration);
Expand Down

0 comments on commit 0ce998e

Please sign in to comment.