MCPcopy
hub / github.com/yjs/yjs / flushRandomMessage

Method flushRandomMessage

tests/testHelper.js:186–210  ·  view source on GitHub ↗

* Choose random connection and flush a random message from a random sender. * * If this function was unable to flush a message, because there are no more messages to flush, it returns false. true otherwise. * @return {boolean}

()

Source from the content-addressed store, hash-verified

184 * @return {boolean}
185 */
186 flushRandomMessage () {
187 const gen = this.prng
188 const conns = Array.from(this.onlineConns).filter(conn => conn.receiving.size > 0)
189 if (conns.length > 0) {
190 const receiver = prng.oneOf(gen, conns)
191 const [sender, messages] = prng.oneOf(gen, Array.from(receiver.receiving))
192 const m = messages.shift()
193 if (messages.length === 0) {
194 receiver.receiving.delete(sender)
195 }
196 if (m === undefined) {
197 return this.flushRandomMessage()
198 }
199 const encoder = encoding.createEncoder()
200 // console.log('receive (' + sender.userID + '->' + receiver.userID + '):\n', syncProtocol.stringifySyncMessage(decoding.createDecoder(m), receiver))
201 // do not publish data created when this function is executed (could be ss2 or update message)
202 syncProtocol.readSyncMessage(decoding.createDecoder(m), encoder, receiver, receiver.tc)
203 if (encoding.length(encoder) > 0) {
204 // send reply message
205 sender._receive(encoding.toUint8Array(encoder), receiver)
206 }
207 return true
208 }
209 return false
210 }
211
212 /**
213 * @return {boolean} True iff this function actually flushed something

Callers 2

flushAllMessagesMethod · 0.95
applyRandomTestsFunction · 0.80

Calls 5

fromMethod · 0.80
lengthMethod · 0.80
_receiveMethod · 0.80
deleteMethod · 0.45
toUint8ArrayMethod · 0.45

Tested by

no test coverage detected