-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b492e9
commit a106db0
Showing
2 changed files
with
57 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |