source: trip-planner-front/node_modules/piscina/src/common.ts@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1import type { MessagePort } from 'worker_threads';
2
3export interface StartupMessage {
4 filename : string | null;
5 name : string;
6 port : MessagePort;
7 sharedBuffer : Int32Array;
8 useAtomics : boolean;
9 niceIncrement : number;
10}
11
12export interface RequestMessage {
13 taskId : number;
14 task : any;
15 filename: string;
16 name : string;
17}
18
19export interface ReadyMessage {
20 ready: true
21};
22
23export interface ResponseMessage {
24 taskId : number;
25 result : any;
26 error: Error | null;
27}
28
29export 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
36const kMovable = Symbol('Piscina.kMovable');
37export const kTransferable = Symbol.for('Piscina.transferable');
38export const kValue = Symbol.for('Piscina.valueOf');
39export const kQueueOptions = Symbol.for('Piscina.queueOptions');
40
41// True if the object implements the Transferable interface
42export 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
51export function isMovable (value : any) : boolean {
52 return isTransferable(value) && value[kMovable] === true;
53}
54
55export 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
64export interface Transferable {
65 readonly [kTransferable] : object;
66 readonly [kValue] : object;
67}
68
69export interface Task {
70 readonly [kQueueOptions] : object | null;
71}
72
73export interface TaskQueue {
74 readonly size : number;
75 shift () : Task | null;
76 remove (task : Task) : void;
77 push (task : Task) : void;
78}
79
80export 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
89export const kRequestCountField = 0;
90export const kResponseCountField = 1;
91export const kFieldCount = 2;
Note: See TracBrowser for help on using the repository browser.