GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/test/parallel/test-abortsignal-cloneable.js at main · styfle/node Expand file tree/ Copy pathtest-abortsignal-cloneable.jsMore file actions 83 lines (67 loc) · 2.33 KB / Copy pathtest-abortsignal-cloneable.jsFile metadata and controls83 lines (67 loc) · 2.33 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 'use strict'; const common = require('../common'); const { ok, strictEqual } = require('assert'); const { setImmediate: pause } = require('timers/promises'); const { transferableAbortSignal, transferableAbortController, } = require('util'); function deferred() { let res; const promise = new Promise((resolve) => res = resolve); return { res, promise }; } (async () => { const ac = transferableAbortController(); const mc = new MessageChannel(); const deferred1 = deferred(); const deferred2 = deferred(); const resolvers = [deferred1, deferred2]; mc.port1.onmessage = common.mustCall(({ data }) => { data.addEventListener('abort', common.mustCall(() => { strictEqual(data.reason, 'boom'); })); resolvers.shift().res(); }, 2); mc.port2.postMessage(ac.signal, [ac.signal]); // Can be cloned/transferd multiple times and they all still work mc.port2.postMessage(ac.signal, [ac.signal]); mc.port2.close(); // Although we're using transfer semantics, the local AbortSignal // is still usable locally. ac.signal.addEventListener('abort', common.mustCall(() => { strictEqual(ac.signal.reason, 'boom'); })); await Promise.all([ deferred1.promise, deferred2.promise ]); ac.abort('boom'); // Because the postMessage used by the underlying AbortSignal // takes at least one turn of the event loop to be processed, // and because it is unref'd, it won't, by itself, keep the // event loop open long enough for the test to complete, so // we schedule two back to back turns of the event to ensure // the loop runs long enough for the test to complete. await pause(); await pause(); })().then(common.mustCall()); { const signal = transferableAbortSignal(AbortSignal.abort('boom')); ok(signal.aborted); strictEqual(signal.reason, 'boom'); const mc = new MessageChannel(); mc.port1.onmessage = common.mustCall(({ data }) => { ok(data instanceof AbortSignal); ok(data.aborted); strictEqual(data.reason, 'boom'); mc.port1.close(); }); mc.port2.postMessage(signal, [signal]); } { // The cloned AbortSignal does not keep the event loop open // waiting for the abort to be triggered. const ac = transferableAbortController(); const mc = new MessageChannel(); mc.port1.onmessage = common.mustCall(); mc.port2.postMessage(ac.signal, [ac.signal]); mc.port2.close(); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |