@atlaspack/workers 2.14.21-typescript-bc4459c37.0 → 2.14.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/index.d.ts +2 -94
  3. package/lib/Handle.js +3 -0
  4. package/lib/Worker.js +1 -7
  5. package/lib/WorkerFarm.js +7 -17
  6. package/lib/backend.js +1 -5
  7. package/lib/bus.js +1 -1
  8. package/lib/child.js +4 -11
  9. package/lib/cpuCount.js +2 -6
  10. package/lib/process/ProcessChild.js +1 -6
  11. package/lib/process/ProcessWorker.js +2 -9
  12. package/lib/threads/ThreadsChild.js +0 -3
  13. package/lib/threads/ThreadsWorker.js +2 -10
  14. package/lib/web/WebChild.js +1 -6
  15. package/lib/web/WebWorker.js +4 -20
  16. package/package.json +12 -16
  17. package/src/{Handle.ts → Handle.js} +11 -11
  18. package/src/{Worker.ts → Worker.js} +54 -66
  19. package/src/{WorkerFarm.ts → WorkerFarm.js} +141 -198
  20. package/src/{backend.ts → backend.js} +3 -6
  21. package/src/{bus.ts → bus.js} +3 -2
  22. package/src/{child.ts → child.js} +43 -55
  23. package/src/{childState.ts → childState.js} +2 -1
  24. package/src/{cpuCount.ts → cpuCount.js} +7 -10
  25. package/src/{index.ts → index.js} +1 -0
  26. package/src/process/{ProcessChild.ts → ProcessChild.js} +3 -5
  27. package/src/process/{ProcessWorker.ts → ProcessWorker.js} +7 -10
  28. package/src/threads/{ThreadsChild.ts → ThreadsChild.js} +2 -2
  29. package/src/threads/{ThreadsWorker.ts → ThreadsWorker.js} +8 -14
  30. package/src/{types.ts → types.js} +35 -34
  31. package/src/web/{WebChild.ts → WebChild.js} +2 -6
  32. package/src/web/{WebWorker.ts → WebWorker.js} +7 -19
  33. package/test/{cpuCount.test.ts → cpuCount.test.js} +1 -0
  34. package/test/{workerfarm.test.js → workerfarm.test.cjs} +2 -4
  35. package/LICENSE +0 -201
  36. package/lib/Handle.d.ts +0 -19
  37. package/lib/Worker.d.ts +0 -40
  38. package/lib/WorkerFarm.d.ts +0 -93
  39. package/lib/backend.d.ts +0 -4
  40. package/lib/bus.d.ts +0 -6
  41. package/lib/child.d.ts +0 -43
  42. package/lib/childState.d.ts +0 -3
  43. package/lib/cpuCount.d.ts +0 -2
  44. package/lib/index.d.ts +0 -6
  45. package/lib/process/ProcessChild.d.ts +0 -9
  46. package/lib/process/ProcessWorker.d.ts +0 -16
  47. package/lib/threads/ThreadsChild.d.ts +0 -8
  48. package/lib/threads/ThreadsWorker.d.ts +0 -15
  49. package/lib/types.d.ts +0 -52
  50. package/lib/web/WebChild.d.ts +0 -8
  51. package/lib/web/WebWorker.d.ts +0 -15
  52. package/tsconfig.json +0 -4
@@ -1,21 +1,24 @@
1
+ // @flow strict-local
1
2
  import {registerSerializableClass} from '@atlaspack/build-cache';
3
+ // $FlowFixMe
2
4
  import packageJson from '../package.json';
3
5
 
4
6
  let HANDLE_ID = 0;
7
+ // $FlowFixMe
5
8
  export type HandleFunction = (...args: Array<any>) => any;
6
9
 
7
- type HandleOpts = {
8
- fn?: HandleFunction;
9
- childId?: number | null | undefined;
10
- id?: number;
11
- };
10
+ type HandleOpts = {|
11
+ fn?: HandleFunction,
12
+ childId?: ?number,
13
+ id?: number,
14
+ |};
12
15
 
13
16
  const handleById: Map<number, Handle> = new Map();
14
17
 
15
18
  export default class Handle {
16
19
  id: number;
17
- childId: number | null | undefined;
18
- fn: HandleFunction | null | undefined;
20
+ childId: ?number;
21
+ fn: ?HandleFunction;
19
22
 
20
23
  constructor(opts: HandleOpts) {
21
24
  this.id = opts.id ?? ++HANDLE_ID;
@@ -28,10 +31,7 @@ export default class Handle {
28
31
  handleById.delete(this.id);
29
32
  }
30
33
 
31
- serialize(): {
32
- childId: number | null | undefined;
33
- id: number;
34
- } {
34
+ serialize(): {|childId: ?number, id: number|} {
35
35
  return {
36
36
  id: this.id,
37
37
  childId: this.childId,
@@ -1,3 +1,5 @@
1
+ // @flow
2
+
1
3
  import type {FilePath} from '@atlaspack/types-internal';
2
4
  import type {BackendType, WorkerImpl, WorkerMessage} from './types';
3
5
  import type {SharedReference} from './WorkerFarm';
@@ -7,34 +9,33 @@ import EventEmitter from 'events';
7
9
  import ThrowableDiagnostic from '@atlaspack/diagnostic';
8
10
  import {getWorkerBackend} from './backend';
9
11
 
10
- export type WorkerCall = {
11
- method?: string;
12
- handle?: number;
13
- args: ReadonlyArray<any>;
14
- retries: number;
15
- skipReadyCheck?: boolean;
16
- resolve: (result: Promise<any> | any) => void;
17
- reject: (error?: any) => void;
18
- };
19
-
20
- type WorkerOpts = {
21
- forcedKillTime: number;
22
- backend: BackendType;
23
- shouldPatchConsole?: boolean;
24
- shouldTrace?: boolean;
25
- sharedReferences: ReadonlyMap<SharedReference, unknown>;
26
- };
12
+ export type WorkerCall = {|
13
+ method?: string,
14
+ handle?: number,
15
+ args: $ReadOnlyArray<any>,
16
+ retries: number,
17
+ skipReadyCheck?: boolean,
18
+ resolve: (result: Promise<any> | any) => void,
19
+ reject: (error: any) => void,
20
+ |};
21
+
22
+ type WorkerOpts = {|
23
+ forcedKillTime: number,
24
+ backend: BackendType,
25
+ shouldPatchConsole?: boolean,
26
+ shouldTrace?: boolean,
27
+ sharedReferences: $ReadOnlyMap<SharedReference, mixed>,
28
+ |};
27
29
 
28
30
  let WORKER_ID = 0;
29
31
  export default class Worker extends EventEmitter {
30
- readonly options: WorkerOpts;
31
- // @ts-expect-error TS2564
32
+ +options: WorkerOpts;
32
33
  worker: WorkerImpl;
33
34
  id: number = WORKER_ID++;
34
35
  sentSharedReferences: Set<SharedReference> = new Set();
35
36
 
36
37
  calls: Map<number, WorkerCall> = new Map();
37
- exitCode: number | null | undefined = null;
38
+ exitCode: ?number = null;
38
39
  callId: number = 0;
39
40
 
40
41
  ready: boolean = false;
@@ -47,7 +48,7 @@ export default class Worker extends EventEmitter {
47
48
  }
48
49
 
49
50
  async fork(forkModule: FilePath) {
50
- let filteredArgs: Array<string> | Array<any | string> = [];
51
+ let filteredArgs = [];
51
52
  if (process.execArgv) {
52
53
  filteredArgs = process.execArgv.filter(
53
54
  (v) =>
@@ -93,13 +94,13 @@ export default class Worker extends EventEmitter {
93
94
  }
94
95
  }
95
96
 
96
- let onMessage = (data: WorkerMessage) => this.receive(data);
97
- let onExit = (code: number) => {
97
+ let onMessage = (data) => this.receive(data);
98
+ let onExit = (code) => {
98
99
  this.exitCode = code;
99
100
  this.emit('exit', code);
100
101
  };
101
102
 
102
- let onError = (err: Error) => {
103
+ let onError = (err) => {
103
104
  this.emit('error', err);
104
105
  };
105
106
 
@@ -107,27 +108,22 @@ export default class Worker extends EventEmitter {
107
108
  this.worker = new WorkerBackend(filteredArgs, onMessage, onError, onExit);
108
109
  await this.worker.start();
109
110
 
110
- await new Promise(
111
- (
112
- resolve: (result: Promise<any> | any) => void,
113
- reject: (error?: any) => void,
114
- ) => {
115
- this.call({
116
- method: 'childInit',
117
- args: [
118
- forkModule,
119
- {
120
- shouldPatchConsole: !!this.options.shouldPatchConsole,
121
- shouldTrace: !!this.options.shouldTrace,
122
- },
123
- ],
124
- retries: 0,
125
- skipReadyCheck: true,
126
- resolve,
127
- reject,
128
- });
129
- },
130
- );
111
+ await new Promise((resolve, reject) => {
112
+ this.call({
113
+ method: 'childInit',
114
+ args: [
115
+ forkModule,
116
+ {
117
+ shouldPatchConsole: !!this.options.shouldPatchConsole,
118
+ shouldTrace: !!this.options.shouldTrace,
119
+ },
120
+ ],
121
+ retries: 0,
122
+ skipReadyCheck: true,
123
+ resolve,
124
+ reject,
125
+ });
126
+ });
131
127
 
132
128
  let sharedRefs = this.options.sharedReferences;
133
129
  let refsShared = new Set();
@@ -135,9 +131,8 @@ export default class Worker extends EventEmitter {
135
131
  while (refsShared.size < sharedRefs.size) {
136
132
  await Promise.all(
137
133
  [...sharedRefs]
138
- // @ts-expect-error TS2769
139
- .filter(([ref]: [any]) => !refsShared.has(ref))
140
- .map(async ([ref, value]: [any, any]) => {
134
+ .filter(([ref]) => !refsShared.has(ref))
135
+ .map(async ([ref, value]) => {
141
136
  await this.sendSharedReference(ref, value);
142
137
  refsShared.add(ref);
143
138
  }),
@@ -148,23 +143,18 @@ export default class Worker extends EventEmitter {
148
143
  this.emit('ready');
149
144
  }
150
145
 
151
- sendSharedReference(ref: SharedReference, value: unknown): Promise<any> {
146
+ sendSharedReference(ref: SharedReference, value: mixed): Promise<any> {
152
147
  this.sentSharedReferences.add(ref);
153
- return new Promise(
154
- (
155
- resolve: (result: Promise<any> | any) => void,
156
- reject: (error?: any) => void,
157
- ) => {
158
- this.call({
159
- method: 'createSharedReference',
160
- args: [ref, value],
161
- resolve,
162
- reject,
163
- retries: 0,
164
- skipReadyCheck: true,
165
- });
166
- },
167
- );
148
+ return new Promise((resolve, reject) => {
149
+ this.call({
150
+ method: 'createSharedReference',
151
+ args: [ref, value],
152
+ resolve,
153
+ reject,
154
+ retries: 0,
155
+ skipReadyCheck: true,
156
+ });
157
+ });
168
158
  }
169
159
 
170
160
  send(data: WorkerMessage): void {
@@ -189,10 +179,8 @@ export default class Worker extends EventEmitter {
189
179
  };
190
180
 
191
181
  if (this.ready || call.skipReadyCheck === true) {
192
- // @ts-expect-error TS2345
193
182
  this.send(msg);
194
183
  } else {
195
- // @ts-expect-error TS2345
196
184
  this.once('ready', () => this.send(msg));
197
185
  }
198
186
  }