Skip to content

Commit

Permalink
Export EventOrderer type and fix falsy check
Browse files Browse the repository at this point in the history
  • Loading branch information
zknill committed Sep 28, 2023
1 parent db2d1f6 commit ebf9b2b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/SlidingWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ describe('SlidingWindow', () => {
const onExpire = vi.fn();
const sliding = new SlidingWindow(1, onExpire);

const msg2a = createMessage(2);
const msg2b = createMessage(2);
const msg1a = createMessage(2);
const msg1b = createMessage(2);

sliding.addMessage(msg2a);
sliding.addMessage(msg2b);
sliding.addMessage(msg1a);
sliding.addMessage(msg1b);

await new Promise((resolve) => setTimeout(resolve, 1));

expect(onExpire).toHaveBeenCalledTimes(1);
expect(onExpire).toHaveBeenNthCalledWith(1, msg2a);
expect(onExpire).toHaveBeenNthCalledWith(1, msg1a);
});
});
3 changes: 2 additions & 1 deletion src/SlidingWindow.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Types as AblyTypes } from 'ably';

Check failure on line 1 in src/SlidingWindow.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import type { EventOrderer } from './types/mutations';

export default class SlidingWindow {
private messages: AblyTypes.Message[] = [];

constructor(
private readonly windowSizeMs: number,
private onExpire: (message: AblyTypes.Message) => void,
private readonly eventOrderer: (a: AblyTypes.Message, b: AblyTypes.Message) => number = defaultOrderLexicoId,
private readonly eventOrderer: EventOrderer = defaultOrderLexicoId,
) {}

public addMessage(message: AblyTypes.Message) {
Expand Down
3 changes: 1 addition & 2 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Subject, Subscription } from 'rxjs';

import SlidingWindow from './SlidingWindow.js';
import type { StandardCallback } from './types/callbacks';
import type { EventOrderer } from './types/mutations.js';
import EventEmitter from './utilities/EventEmitter.js';

/**
Expand Down Expand Up @@ -61,8 +62,6 @@ export type EventBufferOptions = {
eventOrderer?: EventOrderer;
};

type EventOrderer = (a: AblyTypes.Message, b: AblyTypes.Message) => number;

/**
* A state transition emitted as an event from the stream describing a change to the stream's lifecycle.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/StreamRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class StreamRegistry implements IStreamRegistry {
* @param {Pick<StreamOptions, 'ably' | 'logger'>} options - The default options used when instantiating a stream.
*/
constructor(readonly options: Pick<StreamOptions, 'ably' | 'logger' | 'eventBufferOptions'>) {
if (options.eventBufferOptions !== null) {
if (options.eventBufferOptions) {
const bufferMs = options.eventBufferOptions?.bufferMs || 0;
if (bufferMs < 0) {
throw new Error(`EventBufferOptions bufferMs cannot be less than zero: ${bufferMs}`);
Expand Down
8 changes: 8 additions & 0 deletions src/types/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Types as AblyTypes } from 'ably';

Check failure on line 1 in src/types/mutations.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import type { Event } from './model';

/**
Expand All @@ -6,6 +7,13 @@ import type { Event } from './model';
*/
export type EventComparator = (optimistic: Event, confirmed: Event) => boolean;

/**
* EventOrderer is used to determine the order of elements in the event buffer. It expects
* to return a negative value of the first argument is less than the second argument, zero
* if they are equal, and a positive value otherwise.
*/
export type EventOrderer = (a: AblyTypes.Message, b: AblyTypes.Message) => number;

/**
* MutationOptions can be used to configure options on individual mutations.
*/
Expand Down

0 comments on commit ebf9b2b

Please sign in to comment.