以ä¸ã®ä¾ã¯ãèªã¿åãå¯è½ãªãã¤ãã¹ããªã¼ã ã®ä½¿ç¨ããåã£ããã®ã§ãã
ã¾ããã¹ããªã¼ã ä¸ã§ ReadableStream.getReader()
ã使ç¨ãã¦ãªã¼ãã¼ã使ãã¾ãã options 弿°ã« mode: "byob"
ãæå®ãã¾ãã ãã㯠"Bring Your Own Buffer" ãªã¼ãã¼ãªã®ã§ãèªã¿è¾¼ãããã® ArrayBuffer
ã使ããå¿
è¦ãããã¾ãã
const reader = stream.getReader({ mode: "byob" });
let buffer = new ArrayBuffer(4000);
ãªã¼ãã¼ã使ç¨ãã颿°ãä¸è¨ã«ç¤ºãã¾ãã ãã㯠read()
ã¡ã½ãããå帰çã«å¼ã³åºãã¦ããã¼ã¿ããããã¡ã¼ã«èªã¿è¾¼ã¿ã¾ãã ãã®ã¡ã½ãã㯠Uint8Array
åä»ãé
åãåããããã ããã¯ãå
ã®é
åãããã¡ã¼ã§ã¾ã æ¸ãè¾¼ã¾ãã¦ããªãé¨åã®ãã¥ã¼ã§ãã ãã¥ã¼ã®å¼æ°ã¯ãååå¼ã³åºãããéã«åãåã£ããã¼ã¿ããè¨ç®ãããå
é
åãããã¡ã¼ã¸ã®ãªãã»ãããå®ç¾©ãã¾ãã
readStream(reader);
function readStream(reader) {
let bytesReceived = 0;
let offset = 0;
while (offset < buffer.byteLength) {
// read() returns a promise that resolves when a value has been received
reader
.read(new Uint8Array(buffer, offset, buffer.byteLength - offset))
.then(function processBytes({ done, value }) {
// Result objects contain two properties:
// done - true if the stream has already given all its data.
// value - some data. Always undefined when done is true.
if (done) {
// There is no more data in the stream
return;
}
buffer = value.buffer;
offset += value.byteLength;
bytesReceived += value.byteLength;
// Read some more, and call this function again
return reader
.read(new Uint8Array(buffer, offset, buffer.byteLength - offset))
.then(processBytes);
});
}
}
ã¹ããªã¼ã ã«ãã¼ã¿ããªããªãã¨ãread()
ã¡ã½ããã¯ãããã㣠done
ã true
ã«è¨å®ãããªãã¸ã§ã¯ãã§è§£æ±ºãã颿°ãè¿ãã¾ãã
ReadableStreamBYOBReader.closed
ããããã£ã¯ãããã¹ãè¿ããã¹ããªã¼ã ãéããããããã¨ã©ã¼ã¨ããããããªã¼ãã¼ããã¯ãè§£æ¾ãããããããã¨ãç£è¦ããããã«ä½¿ç¨ãããã¨ãã§ãã¾ãã
reader.closed
.then(() => {
// Resolved - code to handle stream closing
})
.catch(() => {
// Rejected - code to handle error
});
ã¹ããªã¼ã ãåãæ¶ãã«ã¯ ReadableStreamBYOBReader.cancel()
ãå¼ã³åºãã¾ãããªãã·ã§ã³ã§çç±ãæå®ãã¾ãã ããã¯ãã¹ããªã¼ã ãåãæ¶ãããå¯è½æ§ã解決ãããããã¹ãè¿ãã¾ãã ã¹ããªã¼ã ããã£ã³ã»ã«ãããã¨ãã³ã³ããã¼ã©ã¯æ¬¡ã« cancel()
ãåºç¤ã¨ãªãã½ã¼ã¹ä¸ã§å¼ã³åºãã¾ãã
èªã¿åãå¯è½ãªãã¤ãã¹ããªã¼ã ã®ä½¿ç¨ã®ä¾ã§ã¯ã次ã®ããã«ãã¿ã³ãæ¼ãããã¨ãã« cancel ã¡ã½ãããå¼ã³åºãã¾ãã
button.addEventListener("click", () => {
reader.cancel("user choice").then(() => console.log("cancel complete"));
});
ã³ã³ã·ã¥ã¼ãã¼ã¯ releaseLock()
ãå¼ã³åºãã¦ãã¹ããªã¼ã ä¸ã®ãªã¼ãã¼ã®ä¿æãè§£æ¾ãããã¨ãã§ãã¾ãããããã¯èªã¿è¾¼ã¿ãå¾
æ©ãã¦ããªãå ´åã«éããã¾ãã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4