1 | import type { MessagePort } from 'worker_threads';
|
---|
2 |
|
---|
3 | export interface StartupMessage {
|
---|
4 | filename : string | null;
|
---|
5 | name : string;
|
---|
6 | port : MessagePort;
|
---|
7 | sharedBuffer : Int32Array;
|
---|
8 | useAtomics : boolean;
|
---|
9 | niceIncrement : number;
|
---|
10 | }
|
---|
11 |
|
---|
12 | export interface RequestMessage {
|
---|
13 | taskId : number;
|
---|
14 | task : any;
|
---|
15 | filename: string;
|
---|
16 | name : string;
|
---|
17 | }
|
---|
18 |
|
---|
19 | export interface ReadyMessage {
|
---|
20 | ready: true
|
---|
21 | };
|
---|
22 |
|
---|
23 | export interface ResponseMessage {
|
---|
24 | taskId : number;
|
---|
25 | result : any;
|
---|
26 | error: Error | null;
|
---|
27 | }
|
---|
28 |
|
---|
29 | export const commonState = {
|
---|
30 | isWorkerThread: false,
|
---|
31 | workerData: undefined
|
---|
32 | };
|
---|
33 |
|
---|
34 | // Internal symbol used to mark Transferable objects returned
|
---|
35 | // by the Piscina.move() function
|
---|
36 | const kMovable = Symbol('Piscina.kMovable');
|
---|
37 | export const kTransferable = Symbol.for('Piscina.transferable');
|
---|
38 | export const kValue = Symbol.for('Piscina.valueOf');
|
---|
39 | export const kQueueOptions = Symbol.for('Piscina.queueOptions');
|
---|
40 |
|
---|
41 | // True if the object implements the Transferable interface
|
---|
42 | export function isTransferable (value : any) : boolean {
|
---|
43 | return value != null &&
|
---|
44 | typeof value === 'object' &&
|
---|
45 | kTransferable in value &&
|
---|
46 | kValue in value;
|
---|
47 | }
|
---|
48 |
|
---|
49 | // True if object implements Transferable and has been returned
|
---|
50 | // by the Piscina.move() function
|
---|
51 | export function isMovable (value : any) : boolean {
|
---|
52 | return isTransferable(value) && value[kMovable] === true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | export function markMovable (value : object) : void {
|
---|
56 | Object.defineProperty(value, kMovable, {
|
---|
57 | enumerable: false,
|
---|
58 | configurable: true,
|
---|
59 | writable: true,
|
---|
60 | value: true
|
---|
61 | });
|
---|
62 | }
|
---|
63 |
|
---|
64 | export interface Transferable {
|
---|
65 | readonly [kTransferable] : object;
|
---|
66 | readonly [kValue] : object;
|
---|
67 | }
|
---|
68 |
|
---|
69 | export interface Task {
|
---|
70 | readonly [kQueueOptions] : object | null;
|
---|
71 | }
|
---|
72 |
|
---|
73 | export interface TaskQueue {
|
---|
74 | readonly size : number;
|
---|
75 | shift () : Task | null;
|
---|
76 | remove (task : Task) : void;
|
---|
77 | push (task : Task) : void;
|
---|
78 | }
|
---|
79 |
|
---|
80 | export function isTaskQueue (value : any) : boolean {
|
---|
81 | return typeof value === 'object' &&
|
---|
82 | value !== null &&
|
---|
83 | 'size' in value &&
|
---|
84 | typeof value.shift === 'function' &&
|
---|
85 | typeof value.remove === 'function' &&
|
---|
86 | typeof value.push === 'function';
|
---|
87 | }
|
---|
88 |
|
---|
89 | export const kRequestCountField = 0;
|
---|
90 | export const kResponseCountField = 1;
|
---|
91 | export const kFieldCount = 2;
|
---|