MCPcopy Index your code
hub / github.com/tinyplex/tinybase / createPayloadReceiver

Function createPayloadReceiver

src/synchronizers/common.ts:60–109  ·  view source on GitHub ↗
(
  receive: Receive,
  fragmentTimeoutSeconds: number = 1,
)

Source from the content-addressed store, hash-verified

58 );
59
60export const createPayloadReceiver = (
61 receive: Receive,
62 fragmentTimeoutSeconds: number = 1,
63) => {
64 const buffer: IdMap<Pending> = mapNew();
65
66 const setPendingTimeout = (bufferKey: Id) =>
67 startTimeout(() => collDel(buffer, bufferKey), fragmentTimeoutSeconds);
68
69 const delPending = (bufferKey: Id, pending: Pending) => {
70 stopTimeout(pending[3]);
71 collDel(buffer, bufferKey);
72 };
73
74 return (payload: string) =>
75 ifPayloadValid(payload, (fromClientId, remainder) => {
76 const [, messageId, indexStr, totalStr, fragment] =
77 strMatch(remainder, FRAGMENT) ?? [];
78 if (messageId) {
79 const index = parseInt(indexStr);
80 const total = parseInt(totalStr);
81 if (total > 0 && index >= 0 && index < total) {
82 const bufferKey = fromClientId + MESSAGE_SEPARATOR + messageId;
83 const pending = mapEnsure(buffer, bufferKey, (): Pending => [
84 [],
85 total,
86 total,
87 setPendingTimeout(bufferKey),
88 ]);
89 const [fragments] = pending;
90 if (total == pending[2] && isUndefined(fragments[index])) {
91 stopTimeout(pending[3]);
92 pending[3] = setPendingTimeout(bufferKey);
93 fragments[index] = fragment;
94 pending[1]--;
95 }
96 if (pending[1] == 0) {
97 delPending(bufferKey, pending);
98 receivePayloadRemainder(
99 fromClientId,
100 arrayJoin(fragments),
101 receive,
102 );
103 }
104 }
105 } else {
106 receivePayloadRemainder(fromClientId, remainder, receive);
107 }
108 });
109};
110
111export const createPayload = (
112 toClientId: IdOrNull,

Callers 3

constructorMethod · 0.90
registerReceiveFunction · 0.90
configureServerClientFunction · 0.90

Calls 9

mapNewFunction · 0.90
strMatchFunction · 0.90
mapEnsureFunction · 0.90
isUndefinedFunction · 0.90
arrayJoinFunction · 0.90
ifPayloadValidFunction · 0.70
setPendingTimeoutFunction · 0.70
delPendingFunction · 0.70
receivePayloadRemainderFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…