worker.markAsUntransferable(object)
添加於:v14.5.0、v12.19.0
將對象標記為不可轉移。如果 object
出現在
調用的傳輸列表中,則將其忽略。port.postMessage()
特別是,這對於可以被克隆而不是傳輸的對象以及被發送方的其他對象使用的對象是有意義的。例如,Node.js 標記ArrayBuffer
s 它用於其Buffer.allocUnsafe(size)有了這個。
此操作無法撤消。
const { MessageChannel, markAsUntransferable } = require('node:worker_threads');
const pooledBuffer = new ArrayBuffer(8);
const typedArray1 = new Uint8Array(pooledBuffer);
const typedArray2 = new Float64Array(pooledBuffer);
markAsUntransferable(pooledBuffer);
const { port1 } = new MessageChannel();
port1.postMessage(typedArray1, [ typedArray1.buffer ]);
// The following line prints the contents of typedArray1 -- it still owns
// its memory and has been cloned, not transferred. Without
// `markAsUntransferable()`, this would print an empty Uint8Array.
// typedArray2 is intact as well.
console.log(typedArray1);
console.log(typedArray2);
瀏覽器中沒有與此 API 等效的函數。
相關用法
- Node.js worker.receiveMessageOnPort(port)用法及代碼示例
- Node.js worker.workerData用法及代碼示例
- Node.js worker.SHARE_ENV用法及代碼示例
- Node.js worker.isMainThread用法及代碼示例
- Node.js worker.parentPort用法及代碼示例
- Node.js worker.getEnvironmentData(key)用法及代碼示例
- Node.js writeStream.getColorDepth()用法及代碼示例
- Node.js writeStream.cursorTo()用法及代碼示例
- Node.js writable._construct(callback)用法及代碼示例
- Node.js writeStream.clearLine()用法及代碼示例
- Node.js writeStream.moveCursor()用法及代碼示例
- Node.js writeStream.getWindowSize()用法及代碼示例
- Node.js writeStream.columns用法及代碼示例
- Node.js writeStream.clearScreenDown()用法及代碼示例
- Node.js writeStream.isTTY用法及代碼示例
- Node.js writeStream.hasColors()用法及代碼示例
- Node.js writeStream.rows用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2.Http2ServerRequest request.url用法及代碼示例
- Node.js request.socket用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js tlsSocket.authorized用法及代碼示例
- Node.js zlib.deflateRaw()用法及代碼示例
- Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
- Node.js Console用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 worker.markAsUntransferable(object)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。