( receive: Receive, fragmentTimeoutSeconds: number = 1, )
| 58 | ); |
| 59 | |
| 60 | export 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 | |
| 111 | export const createPayload = ( |
| 112 | toClientId: IdOrNull, |
no test coverage detected
searching dependent graphs…