MCPcopy Index your code
hub / github.com/MultithreadedJSBook/code-samples / read

Method read

ch6-ring-buffer/ring-buffer.js:60–84  ·  view source on GitHub ↗
(bytes)

Source from the content-addressed store, hash-verified

58 }
59
60 read(bytes) {
61 if (bytes > this.length) { // <1>
62 bytes = this.length;
63 }
64 if (bytes === 0) {
65 return new Uint8Array(0);
66 }
67 let readData;
68 if (
69 this.head > this.tail || this.buffer.length - this.tail >= bytes // <2>
70 ) {
71 // The data is in a contiguous chunk.
72 readData = this.buffer.slice(this.tail, bytes)
73 this.tail += bytes;
74 } else { // <3>
75 // Read from the end and the beginning.
76 readData = new Uint8Array(bytes);
77 const endBytesToRead = this.buffer.length - this.tail;
78 readData.set(this.buffer.subarray(this.tail, this.buffer.length));
79 readData.set(this.buffer.subarray(0, bytes - endBytesToRead), endBytesToRead);
80 this.tail = bytes - endBytesToRead;
81 }
82 this.length -= bytes;
83 return readData;
84 }
85}
86
87const Mutex = require('../ch6-mutex/mutex.js');

Callers 2

readMethod · 0.45
ring-buffer.jsFile · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected