diff --git a/src/persisters/persister-sync.ts b/src/persisters/persister-sync.ts index 150b9718054..9929d21552e 100644 --- a/src/persisters/persister-sync.ts +++ b/src/persisters/persister-sync.ts @@ -1,11 +1,7 @@ import { Bus, - BusStats, MessageType, - Receive, - Send, SyncPersister, - createLocalBus as createLocalBusDecl, createSyncPersister as createSyncPersisterDecl, } from '../types/persisters/persister-sync'; import { @@ -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; @@ -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 = 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; diff --git a/src/persisters/sync/bus-local.ts b/src/persisters/sync/bus-local.ts new file mode 100644 index 00000000000..5c0e4200c1a --- /dev/null +++ b/src/persisters/sync/bus-local.ts @@ -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 = 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;