Skip to content

Commit

Permalink
[sync] Move LocalBus out
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgpearce committed Apr 11, 2024
1 parent 4b492e9 commit a106db0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
51 changes: 4 additions & 47 deletions src/persisters/persister-sync.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {
Bus,
BusStats,
MessageType,
Receive,
Send,
SyncPersister,
createLocalBus as createLocalBusDecl,
createSyncPersister as createSyncPersisterDecl,
} from '../types/persisters/persister-sync';
import {
Expand All @@ -17,15 +13,17 @@ import {
TablesStamp,
ValuesStamp,
} from '../types/mergeable-store';
import {DEBUG, ifNotUndefined, isUndefined, promiseNew} from '../common/other';
import {Id, IdOrNull} from '../types/common';
import {IdMap, mapGet, mapNew, mapSet} from '../common/map';
import {collDel, collForEach, collSize} from '../common/coll';
import {ifNotUndefined, isUndefined, promiseNew} from '../common/other';
import {EMPTY_STRING} from '../common/strings';
import {PersisterListener} from '../types/persisters';
import {collDel} from '../common/coll';
import {createCustomPersister} from '../persisters';
import {getHlcFunctions} from '../mergeable-store/hlc';

export {createLocalBus} from './sync/bus-local';

const RESPONSE = 0;
const CONTENT_HASHES = 1;
const GET_CONTENT_HASHES = 2;
Expand Down Expand Up @@ -200,44 +198,3 @@ export const createSyncPersister = ((
) as SyncPersister;
return persister;
}) as typeof createSyncPersisterDecl;

export const createLocalBus = (() => {
let sends = 0;
let receives = 0;
const stores: IdMap<Receive> = mapNew();

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;
53 changes: 53 additions & 0 deletions src/persisters/sync/bus-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
Bus,
BusStats,
MessageType,
Receive,
Send,
createLocalBus as createLocalBusDecl,
} from '../../types/persisters/persister-sync';
import {DEBUG, isUndefined} from '../../common/other';
import {Id, IdOrNull} from '../../types/common';
import {IdMap, mapGet, mapNew, mapSet} from '../../common/map';
import {collDel, collForEach, collSize} from '../../common/coll';

export const createLocalBus = (() => {
let sends = 0;
let receives = 0;
const stores: IdMap<Receive> = mapNew();

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;

0 comments on commit a106db0

Please sign in to comment.