Skip to content

Commit

Permalink
[sync] Bus as object
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgpearce committed Apr 11, 2024
1 parent 7f1e949 commit 4b492e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
71 changes: 36 additions & 35 deletions src/persisters/persister-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const createSyncPersister = ((
): SyncPersister => {
let persisterListener: PersisterListener | undefined;

const [join] = bus;
const [getHlc] = getHlcFunctions(store.getId());
const pendingRequests: IdMap<
[
Expand Down Expand Up @@ -86,7 +85,7 @@ export const createSyncPersister = ((
}
};

const [send] = join(store.getId(), receive);
const [send] = bus.join(store.getId(), receive);

const request = async <Response>(
toStoreId: IdOrNull,
Expand Down Expand Up @@ -206,37 +205,39 @@ export const createLocalBus = (() => {
let sends = 0;
let receives = 0;
const stores: IdMap<Receive> = mapNew();
return [
(storeId: Id, receive: Receive): [Send, () => void] => {
mapSet(stores, storeId, receive);
const send = (
requestId: IdOrNull,
toStoreId: IdOrNull,
messageType: MessageType,
messageBody: any,
): void => {
if (DEBUG) {
sends++;
receives += isUndefined(toStoreId) ? collSize(stores) - 1 : 1;
}
isUndefined(toStoreId)
? collForEach(stores, (receive, otherStoreId) =>
otherStoreId != storeId
? receive(requestId, storeId, messageType, messageBody)
: 0,
)
: mapGet(stores, toStoreId)?.(
requestId,
storeId,
messageType,
messageBody,
);
};
const leave = (): void => {
collDel(stores, storeId);
};
return [send, leave];
},
(): BusStats => (DEBUG ? {sends, receives} : {}),
];

const join = (storeId: Id, receive: Receive): [Send, () => void] => {
mapSet(stores, storeId, receive);
const send = (
requestId: IdOrNull,
toStoreId: IdOrNull,
messageType: MessageType,
messageBody: any,
): void => {
if (DEBUG) {
sends++;
receives += isUndefined(toStoreId) ? collSize(stores) - 1 : 1;
}
isUndefined(toStoreId)
? collForEach(stores, (receive, otherStoreId) =>
otherStoreId != storeId
? receive(requestId, storeId, messageType, messageBody)
: 0,
)
: mapGet(stores, toStoreId)?.(
requestId,
storeId,
messageType,
messageBody,
);
};
const leave = (): void => {
collDel(stores, storeId);
};
return [send, leave];
};

const getStats = (): BusStats => (DEBUG ? {sends, receives} : {});

return {join, getStats} as Bus;
}) as typeof createLocalBusDecl;
8 changes: 4 additions & 4 deletions src/types/persisters/persister-sync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export type Send = (
export type BusStats = {sends?: number; receives?: number};

/// Bus
export type Bus = [
join: (storeId: Id, receive: Receive) => [send: Send, leave: () => void],
getStats: () => BusStats,
];
export type Bus = {
join: (storeId: Id, receive: Receive) => [send: Send, leave: () => void];
getStats: () => BusStats;
};

/// SyncPersister
export interface SyncPersister extends Persister<true> {
Expand Down
8 changes: 4 additions & 4 deletions src/types/with-schemas/persisters/persister-sync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export type Send = (
export type BusStats = {sends?: number; receives?: number};

/// Bus
export type Bus = [
join: (storeId: Id, receive: Receive) => [send: Send, leave: () => void],
getStats: () => BusStats,
];
export type Bus = {
join: (storeId: Id, receive: Receive) => [send: Send, leave: () => void];
getStats: () => BusStats;
};

/// SyncPersister
export interface SyncPersister<Schemas extends OptionalSchemas>
Expand Down
4 changes: 2 additions & 2 deletions test/unit/persisters/persister-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const expectEachToHaveContent = async (
} else {
expect(store2.getMergeableContent()).toEqual(store1.getMergeableContent());
}
expect(bus[1]()).toMatchSnapshot();
expect(bus.getStats()).toMatchSnapshot();
};

beforeEach(() => {
Expand Down Expand Up @@ -380,7 +380,7 @@ describe('Multidirectional', () => {
expect(store.getMergeableContent()).toEqual(mergeableContent);
}
});
expect(bus[1]()).toMatchSnapshot();
expect(bus.getStats()).toMatchSnapshot();
};

beforeEach(async () => {
Expand Down

0 comments on commit 4b492e9

Please sign in to comment.