Skip to content

Commit 0cddd29

Browse files
committed
v0.5.15
1 parent d33bc66 commit 0cddd29

19 files changed

+257
-74
lines changed

CHANGE.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
## v0.5.15
2+
3+
- New tests are added to the platform lib.
4+
15
## v0.5.14
26

37
- concurrent.module is renamed to concurrent.import.
48

59
## v0.5.13
610

7-
- Unit tests are implemented for some of the platform core classes.
11+
- New tests are added to the platform lib.
812
- The mechanism of loading a module is simplified.
913
- The mechanism of allocating a thread is simplified.
1014
- Exclusive thread allocation is removed.

dist/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "module",
33
"name": "@bitair/concurrent.js",
4-
"version": "0.5.14",
4+
"version": "0.5.15",
55
"description": "Easy Multithreading for JavaScript (Node.js, Deno & Browser)",
66
"main": "src/node/index.js",
77
"browser": "src/browser/index.js",

dist/src/browser/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ var _ThreadedObject = class {
199199
this.id = id;
200200
this.target = target;
201201
}
202-
static async disposeObject(id, thread) {
203-
const task = new Task(9 /* DisposeObject */, [id]);
204-
await thread.run(task);
205-
}
206202
static async create(thread, moduleSrc, exportName, ctorArgs) {
207203
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
208204
const [id, properties] = await thread.run(task);
@@ -229,8 +225,10 @@ var _ThreadedObject = class {
229225
var ThreadedObject = _ThreadedObject;
230226
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
231227
const thread = threadRef.deref();
232-
if (thread)
233-
_ThreadedObject.disposeObject(id, thread).finally();
228+
if (thread) {
229+
const task = new Task(9 /* DisposeObject */, [id]);
230+
thread.run(task).finally();
231+
}
234232
}));
235233

236234
// libs/platform/src/core/concurrent_module.ts

dist/src/browser/worker_script.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ var _ThreadedObject = class {
139139
this.id = id;
140140
this.target = target;
141141
}
142-
static async disposeObject(id, thread) {
143-
const task = new Task(9 /* DisposeObject */, [id]);
144-
await thread.run(task);
145-
}
146142
static async create(thread, moduleSrc, exportName, ctorArgs) {
147143
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
148144
const [id, properties] = await thread.run(task);
@@ -169,8 +165,10 @@ var _ThreadedObject = class {
169165
var ThreadedObject = _ThreadedObject;
170166
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
171167
const thread = threadRef.deref();
172-
if (thread)
173-
_ThreadedObject.disposeObject(id, thread).finally();
168+
if (thread) {
169+
const task = new Task(9 /* DisposeObject */, [id]);
170+
thread.run(task).finally();
171+
}
174172
}));
175173

176174
// libs/platform/src/core/worker_manager.ts

dist/src/deno/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ var _ThreadedObject = class {
208208
this.id = id;
209209
this.target = target;
210210
}
211-
static async disposeObject(id, thread) {
212-
const task = new Task(9 /* DisposeObject */, [id]);
213-
await thread.run(task);
214-
}
215211
static async create(thread, moduleSrc, exportName, ctorArgs) {
216212
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
217213
const [id, properties] = await thread.run(task);
@@ -238,8 +234,10 @@ var _ThreadedObject = class {
238234
var ThreadedObject = _ThreadedObject;
239235
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
240236
const thread = threadRef.deref();
241-
if (thread)
242-
_ThreadedObject.disposeObject(id, thread).finally();
237+
if (thread) {
238+
const task = new Task(9 /* DisposeObject */, [id]);
239+
thread.run(task).finally();
240+
}
243241
}));
244242

245243
// libs/platform/src/core/concurrent_module.ts

dist/src/deno/worker_script.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ var _ThreadedObject = class {
139139
this.id = id;
140140
this.target = target;
141141
}
142-
static async disposeObject(id, thread) {
143-
const task = new Task(9 /* DisposeObject */, [id]);
144-
await thread.run(task);
145-
}
146142
static async create(thread, moduleSrc, exportName, ctorArgs) {
147143
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
148144
const [id, properties] = await thread.run(task);
@@ -169,8 +165,10 @@ var _ThreadedObject = class {
169165
var ThreadedObject = _ThreadedObject;
170166
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
171167
const thread = threadRef.deref();
172-
if (thread)
173-
_ThreadedObject.disposeObject(id, thread).finally();
168+
if (thread) {
169+
const task = new Task(9 /* DisposeObject */, [id]);
170+
thread.run(task).finally();
171+
}
174172
}));
175173

176174
// libs/platform/src/core/worker_manager.ts

dist/src/node/index.cjs

+4-6
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ var _ThreadedObject = class {
235235
this.id = id;
236236
this.target = target;
237237
}
238-
static async disposeObject(id, thread) {
239-
const task = new Task(9 /* DisposeObject */, [id]);
240-
await thread.run(task);
241-
}
242238
static async create(thread, moduleSrc, exportName, ctorArgs) {
243239
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
244240
const [id, properties] = await thread.run(task);
@@ -265,8 +261,10 @@ var _ThreadedObject = class {
265261
var ThreadedObject = _ThreadedObject;
266262
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
267263
const thread = threadRef.deref();
268-
if (thread)
269-
_ThreadedObject.disposeObject(id, thread).finally();
264+
if (thread) {
265+
const task = new Task(9 /* DisposeObject */, [id]);
266+
thread.run(task).finally();
267+
}
270268
}));
271269

272270
// libs/platform/src/core/concurrent_module.ts

dist/src/node/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,6 @@ var _ThreadedObject = class {
211211
this.id = id;
212212
this.target = target;
213213
}
214-
static async disposeObject(id, thread) {
215-
const task = new Task(9 /* DisposeObject */, [id]);
216-
await thread.run(task);
217-
}
218214
static async create(thread, moduleSrc, exportName, ctorArgs) {
219215
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
220216
const [id, properties] = await thread.run(task);
@@ -241,8 +237,10 @@ var _ThreadedObject = class {
241237
var ThreadedObject = _ThreadedObject;
242238
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
243239
const thread = threadRef.deref();
244-
if (thread)
245-
_ThreadedObject.disposeObject(id, thread).finally();
240+
if (thread) {
241+
const task = new Task(9 /* DisposeObject */, [id]);
242+
thread.run(task).finally();
243+
}
246244
}));
247245

248246
// libs/platform/src/core/concurrent_module.ts

dist/src/node/worker_script.cjs

+4-6
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ var _ThreadedObject = class {
158158
this.id = id;
159159
this.target = target;
160160
}
161-
static async disposeObject(id, thread) {
162-
const task = new Task(9 /* DisposeObject */, [id]);
163-
await thread.run(task);
164-
}
165161
static async create(thread, moduleSrc, exportName, ctorArgs) {
166162
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
167163
const [id, properties] = await thread.run(task);
@@ -188,8 +184,10 @@ var _ThreadedObject = class {
188184
var ThreadedObject = _ThreadedObject;
189185
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
190186
const thread = threadRef.deref();
191-
if (thread)
192-
_ThreadedObject.disposeObject(id, thread).finally();
187+
if (thread) {
188+
const task = new Task(9 /* DisposeObject */, [id]);
189+
thread.run(task).finally();
190+
}
193191
}));
194192

195193
// libs/platform/src/core/worker_manager.ts

dist/src/node/worker_script.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ var _ThreadedObject = class {
157157
this.id = id;
158158
this.target = target;
159159
}
160-
static async disposeObject(id, thread) {
161-
const task = new Task(9 /* DisposeObject */, [id]);
162-
await thread.run(task);
163-
}
164160
static async create(thread, moduleSrc, exportName, ctorArgs) {
165161
const task = new Task(5 /* InstantiateObject */, [moduleSrc, exportName, ctorArgs]);
166162
const [id, properties] = await thread.run(task);
@@ -187,8 +183,10 @@ var _ThreadedObject = class {
187183
var ThreadedObject = _ThreadedObject;
188184
__publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id, threadRef }) => {
189185
const thread = threadRef.deref();
190-
if (thread)
191-
_ThreadedObject.disposeObject(id, thread).finally();
186+
if (thread) {
187+
const task = new Task(9 /* DisposeObject */, [id]);
188+
thread.run(task).finally();
189+
}
192190
}));
193191

194192
// libs/platform/src/core/worker_manager.ts

libs/platform/src/core/constants.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export const ErrorMessage = {
2727
WorkerNotSupported: { code: 507, text: "This browser doesn't support web workers." },
2828
ThreadAllocationTimeout: { code: 508, text: 'Thread allocation failed due to timeout.' },
2929
MethodAssignment: { code: 509, text: "Can't assign a method." },
30-
NotAccessibleExport: { code: 510, text: "Can't access an export of type '%{0}'. Only top level functions and classes are imported." },
30+
NotAccessibleExport: {
31+
code: 510,
32+
text: "Can't access an export of type '%{0}'. Only top level functions and classes are imported."
33+
},
3134
ThreadPoolTerminated: { code: 511, text: 'Thread pool has been terminated.' },
3235
ThreadTerminated: { code: 512, text: 'Thread has been terminated.' }
3336
}

libs/platform/src/core/master.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ConcurrentModule } from './concurrent_module.js'
2+
import { defaultConcurrencySettings } from './constants.js'
23
import { ThreadPool } from './thread_pool.js'
34
import { getBoolean, getNumber } from './utils.js'
45

56
import type { ConcurrencySettings, IConcurrent, IConcurrentModule } from '../index.d.js'
67
import type { IWorkerFactory } from './types.js'
7-
import { defaultConcurrencySettings } from './constants.js'
88

99
export class Master implements IConcurrent {
1010
private settings: ConcurrencySettings

libs/platform/src/core/threaded_object.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ declare type ObjectRegistryEntry = {
1818
}
1919

2020
export class ThreadedObject {
21-
static objectRegistry = new FinalizationRegistry(({ id, threadRef }: ObjectRegistryEntry) => {
21+
private static objectRegistry = new FinalizationRegistry(({ id, threadRef }: ObjectRegistryEntry) => {
2222
const thread = threadRef.deref()
23-
if (thread) this.disposeObject(id, thread).finally()
23+
if (thread) {
24+
const task = new Task<DisposeObjectData>(TaskType.DisposeObject, [id])
25+
thread.run(task).finally()
26+
}
2427
})
2528

2629
private constructor(private thread: Thread, private id: number, public target: object) {}
2730

28-
static async disposeObject(id: number, thread: Thread) {
29-
const task = new Task<DisposeObjectData>(TaskType.DisposeObject, [id])
30-
await thread.run(task)
31-
}
32-
3331
static async create(thread: Thread, moduleSrc: string, exportName: string, ctorArgs: unknown[]) {
3432
const task = new Task<InstantiateObjectData>(TaskType.InstantiateObject, [moduleSrc, exportName, ctorArgs])
3533
const [id, properties] = (await thread.run(task)) as InstantiateObjectResult

libs/platform/src/core/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export declare interface IConcurrencyError {
2121
message: string
2222
}
2323

24-
export declare type InvokeFunctionData = [moduleSrc: string, functionName: string, args: unknown[]]
24+
export declare type InvokeFunctionData = [moduleSrc: string, exportName: string, args: unknown[]]
2525
export declare type GetStaticPropertyData = [moduleSrc: string, exportName: string, propName: string]
2626
export declare type SetStaticPropertyData = [moduleSrc: string, exportName: string, propName: string, value: unknown]
2727
export declare type InvokeStaticMethodData = [

libs/platform/src/core/utils.ts

-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ import { ValueType } from './constants.js'
22

33
import type { Dict } from './types.js'
44

5-
export function sleep(seconds: number | undefined = 0) {
6-
return new Promise(resolve => {
7-
const timer = setTimeout(() => {
8-
clearInterval(timer)
9-
resolve(true)
10-
}, seconds * 1000)
11-
})
12-
}
13-
145
export function isBoolean(val: unknown) {
156
return typeof val === 'boolean'
167
}

libs/platform/test/core/common/helpers.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-empty-function */
22
/* eslint-disable @typescript-eslint/no-unused-vars */
3+
import { defaultThreadPoolSettings, TaskType } from '../../../src/core/constants.js'
4+
import { Task } from '../../../src/core/task.js'
5+
import { Thread } from '../../../src/core/thread.js'
6+
import { ThreadPool } from '../../../src/core/thread_pool.js'
37
import { WorkerBase } from '../../../src/core/worker_base.js'
48

59
import type { Coroutine } from '../../../src/core/coroutine.js'
6-
import type { Thread } from '../../../src/core/thread.js'
710
import type {
811
IWorker,
912
WorkerMessageHandler,
@@ -12,11 +15,17 @@ import type {
1215
IWorkerFactory,
1316
InstantiateObjectData
1417
} from '../../../src/core/types.js'
15-
import { defaultThreadPoolSettings, TaskType } from '../../../src/core/constants.js'
16-
import { Task } from '../../../src/core/task.js'
17-
import { ThreadPool } from '../../../src/core/thread_pool.js'
1818
import type { ThreadPoolSettings } from '../../../src/index.js'
1919

20+
export function sleep(seconds: number | undefined = 0) {
21+
return new Promise(resolve => {
22+
const timer = setTimeout(() => {
23+
clearInterval(timer)
24+
resolve(true)
25+
}, seconds * 1000)
26+
})
27+
}
28+
2029
export function createSampleValueDict(): Dict<unknown> {
2130
return {
2231
undefined: undefined,
@@ -60,6 +69,10 @@ export function createWorker() {
6069
})
6170
}
6271

72+
export function createThread() {
73+
return new Thread(createWorkerFactory())
74+
}
75+
6376
export function createInstantiateObjectTask() {
6477
const taskType = TaskType.InstantiateObject
6578
const taskData: InstantiateObjectData = ['moduleSrc', 'exportName', []]

libs/platform/test/core/thread_pool.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { ThreadPool } from '../../src/core/thread_pool.js'
1010

1111
let pool: ThreadPool
1212

13-
describe('Testing ThreadPool Class', () => {
13+
describe('Testing ThreadPool', () => {
1414
beforeEach(() => {
1515
pool = createThreadPool()
1616
})

0 commit comments

Comments
 (0)