11'use strict' ;
22
3+ const {
4+ AtomicsNotify,
5+ AtomicsStore,
6+ AtomicsWait,
7+ AtomicsWaitAsync,
8+ Int32Array,
9+ ObjectKeys,
10+ } = primordials ;
11+
12+ const {
13+ codes : {
14+ ERR_WORKER_MESSAGING_TIMEOUT ,
15+ } ,
16+ } = require ( 'internal/errors' ) ;
17+
318const { read, write } = require ( 'internal/worker/everysync/objects' ) ;
419const {
520 OFFSET ,
@@ -11,18 +26,17 @@ const {
1126 * Creates a synchronous API facade from a shared memory buffer.
1227 * This function is meant to be used in the main thread to communicate with
1328 * a worker thread that has called `wire()` on the same shared memory.
14- *
1529 * @param {SharedArrayBuffer } data - The shared memory buffer for communication
16- * @param {Object } [opts={}] - Options object
30+ * @param {object } [opts={}] - Options object
1731 * @param {number } [opts.timeout=1000] - Timeout in milliseconds for synchronous operations
18- * @returns {Object } - An object with methods that match the ones exposed by the worker
32+ * @returns {object } - An object with methods that match the ones exposed by the worker
1933 */
2034function makeSync ( data , opts = { } ) {
2135 const timeout = opts . timeout || 1000 ;
2236 const metaView = new Int32Array ( data ) ;
2337
24- const res = Atomics . wait ( metaView , TO_WORKER , 0 , timeout ) ;
25- Atomics . store ( metaView , TO_WORKER , 0 ) ;
38+ const res = AtomicsWait ( metaView , TO_WORKER , 0 , timeout ) ;
39+ AtomicsStore ( metaView , TO_WORKER , 0 ) ;
2640
2741 if ( res === 'ok' ) {
2842 const obj = read ( data , OFFSET ) ;
@@ -31,59 +45,56 @@ function makeSync(data, opts = {}) {
3145 for ( const key of obj ) {
3246 api [ key ] = ( ...args ) => {
3347 write ( data , { key, args } , OFFSET ) ;
34- Atomics . store ( metaView , TO_MAIN , 1 ) ;
35- Atomics . notify ( metaView , TO_MAIN , 1 ) ;
36- const res = Atomics . wait ( metaView , TO_WORKER , 0 , timeout ) ;
37- Atomics . store ( metaView , TO_WORKER , 0 ) ;
48+ AtomicsStore ( metaView , TO_MAIN , 1 ) ;
49+ AtomicsNotify ( metaView , TO_MAIN , 1 ) ;
50+ const res = AtomicsWait ( metaView , TO_WORKER , 0 , timeout ) ;
51+ AtomicsStore ( metaView , TO_WORKER , 0 ) ;
3852 if ( res === 'ok' ) {
3953 const obj = read ( data , OFFSET ) ;
4054 return obj ;
41- } else {
42- throw new Error ( `The response timed out after ${ timeout } ms` ) ;
4355 }
56+ throw new ERR_WORKER_MESSAGING_TIMEOUT ( ) ;
4457 } ;
4558 }
4659
4760 return api ;
48- } else {
49- throw new Error ( `The initialization timed out after ${ timeout } ms` ) ;
5061 }
62+ throw new ERR_WORKER_MESSAGING_TIMEOUT ( ) ;
5163}
5264
5365/**
5466 * Wires up a shared memory buffer to invoke methods on an object.
5567 * This function is meant to be used in a worker thread to expose methods
5668 * to the main thread that has called `makeSync()` on the same shared memory.
57- *
5869 * @param {SharedArrayBuffer } data - The shared memory buffer for communication
59- * @param {Object } obj - Object with methods to expose to the main thread
70+ * @param {object } obj - Object with methods to expose to the main thread
6071 * @returns {Promise<void> } - A promise that never resolves unless there's an error
6172 */
6273async function wire ( data , obj ) {
63- write ( data , Object . keys ( obj ) , OFFSET ) ;
74+ write ( data , ObjectKeys ( obj ) , OFFSET ) ;
6475
6576 const metaView = new Int32Array ( data ) ;
6677
67- Atomics . store ( metaView , TO_WORKER , 1 ) ;
68- Atomics . notify ( metaView , TO_WORKER ) ;
78+ AtomicsStore ( metaView , TO_WORKER , 1 ) ;
79+ AtomicsNotify ( metaView , TO_WORKER ) ;
6980
7081 while ( true ) {
71- const waitAsync = Atomics . waitAsync ( metaView , TO_MAIN , 0 ) ;
82+ const waitAsync = AtomicsWaitAsync ( metaView , TO_MAIN , 0 ) ;
7283 const res = await waitAsync . value ;
73- Atomics . store ( metaView , TO_MAIN , 0 ) ;
84+ AtomicsStore ( metaView , TO_MAIN , 0 ) ;
7485
7586 if ( res === 'ok' ) {
7687 const { key, args } = read ( data , OFFSET ) ;
7788 // This is where the magic happens - invoke the requested method
7889 const result = await obj [ key ] ( ...args ) ;
7990 write ( data , result , OFFSET ) ;
80- Atomics . store ( metaView , TO_WORKER , 1 ) ;
81- Atomics . notify ( metaView , TO_WORKER , 1 ) ;
91+ AtomicsStore ( metaView , TO_WORKER , 1 ) ;
92+ AtomicsNotify ( metaView , TO_WORKER , 1 ) ;
8293 }
8394 }
8495}
8596
8697module . exports = {
8798 makeSync,
88- wire
89- } ;
99+ wire,
100+ } ;
0 commit comments