@open-core/framework 0.1.0-alpha.1
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.
- package/LICENSE +373 -0
- package/README.md +360 -0
- package/dist/client/client-bootstrap.d.ts +1 -0
- package/dist/client/client-bootstrap.js +50 -0
- package/dist/client/client-container.d.ts +2 -0
- package/dist/client/client-container.js +6 -0
- package/dist/client/client-core.d.ts +1 -0
- package/dist/client/client-core.js +7 -0
- package/dist/client/controllers/spawner.controller.d.ts +12 -0
- package/dist/client/controllers/spawner.controller.js +51 -0
- package/dist/client/decorators/controller.d.ts +3 -0
- package/dist/client/decorators/controller.js +14 -0
- package/dist/client/decorators/export.d.ts +7 -0
- package/dist/client/decorators/export.js +15 -0
- package/dist/client/decorators/gameEvent.d.ts +47 -0
- package/dist/client/decorators/gameEvent.js +54 -0
- package/dist/client/decorators/index.d.ts +10 -0
- package/dist/client/decorators/index.js +26 -0
- package/dist/client/decorators/interval.d.ts +7 -0
- package/dist/client/decorators/interval.js +15 -0
- package/dist/client/decorators/key.d.ts +2 -0
- package/dist/client/decorators/key.js +10 -0
- package/dist/client/decorators/localEvent.d.ts +7 -0
- package/dist/client/decorators/localEvent.js +15 -0
- package/dist/client/decorators/nui.d.ts +1 -0
- package/dist/client/decorators/nui.js +9 -0
- package/dist/client/decorators/onNet.d.ts +1 -0
- package/dist/client/decorators/onNet.js +9 -0
- package/dist/client/decorators/resourceLifecycle.d.ts +11 -0
- package/dist/client/decorators/resourceLifecycle.js +24 -0
- package/dist/client/decorators/tick.d.ts +1 -0
- package/dist/client/decorators/tick.js +9 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.js +23 -0
- package/dist/client/interfaces/appearance.interface.d.ts +19 -0
- package/dist/client/interfaces/appearance.interface.js +2 -0
- package/dist/client/player/player.d.ts +262 -0
- package/dist/client/player/player.js +480 -0
- package/dist/client/player/player.loader.d.ts +1 -0
- package/dist/client/player/player.loader.js +22 -0
- package/dist/client/services/appearance.service.d.ts +6 -0
- package/dist/client/services/appearance.service.js +89 -0
- package/dist/client/services/blip.service.d.ts +112 -0
- package/dist/client/services/blip.service.js +215 -0
- package/dist/client/services/index.d.ts +9 -0
- package/dist/client/services/index.js +25 -0
- package/dist/client/services/marker.service.d.ts +94 -0
- package/dist/client/services/marker.service.js +153 -0
- package/dist/client/services/notification.service.d.ts +76 -0
- package/dist/client/services/notification.service.js +111 -0
- package/dist/client/services/ped.service.d.ts +182 -0
- package/dist/client/services/ped.service.js +302 -0
- package/dist/client/services/progress.service.d.ts +82 -0
- package/dist/client/services/progress.service.js +210 -0
- package/dist/client/services/spawn.service.d.ts +73 -0
- package/dist/client/services/spawn.service.js +261 -0
- package/dist/client/services/streaming.service.d.ts +165 -0
- package/dist/client/services/streaming.service.js +341 -0
- package/dist/client/services/textui.service.d.ts +82 -0
- package/dist/client/services/textui.service.js +156 -0
- package/dist/client/services/vehicle.service.d.ts +168 -0
- package/dist/client/services/vehicle.service.js +296 -0
- package/dist/client/system/metadata-client.keys.d.ts +13 -0
- package/dist/client/system/metadata-client.keys.js +16 -0
- package/dist/client/system/processors/export.processor.d.ts +7 -0
- package/dist/client/system/processors/export.processor.js +39 -0
- package/dist/client/system/processors/gameEvent.processor.d.ts +10 -0
- package/dist/client/system/processors/gameEvent.processor.js +58 -0
- package/dist/client/system/processors/interval.processor.d.ts +7 -0
- package/dist/client/system/processors/interval.processor.js +43 -0
- package/dist/client/system/processors/key.processor.d.ts +8 -0
- package/dist/client/system/processors/key.processor.js +27 -0
- package/dist/client/system/processors/localEvent.processor.d.ts +7 -0
- package/dist/client/system/processors/localEvent.processor.js +38 -0
- package/dist/client/system/processors/netEvent.processor.d.ts +7 -0
- package/dist/client/system/processors/netEvent.processor.js +38 -0
- package/dist/client/system/processors/nui.processor.d.ts +7 -0
- package/dist/client/system/processors/nui.processor.js +40 -0
- package/dist/client/system/processors/resourceLifecycle.processor.d.ts +9 -0
- package/dist/client/system/processors/resourceLifecycle.processor.js +69 -0
- package/dist/client/system/processors/tick.processor.d.ts +5 -0
- package/dist/client/system/processors/tick.processor.js +37 -0
- package/dist/client/system/processors.register.d.ts +1 -0
- package/dist/client/system/processors.register.js +27 -0
- package/dist/client/types/game-events.d.ts +126 -0
- package/dist/client/types/game-events.js +83 -0
- package/dist/client/types/index.d.ts +1 -0
- package/dist/client/types/index.js +17 -0
- package/dist/client/ui-bridge.d.ts +116 -0
- package/dist/client/ui-bridge.js +201 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +41 -0
- package/dist/server/bootstrap.d.ts +16 -0
- package/dist/server/bootstrap.js +57 -0
- package/dist/server/bus/core-event-bus.d.ts +6 -0
- package/dist/server/bus/core-event-bus.js +31 -0
- package/dist/server/configs/api.config.d.ts +71 -0
- package/dist/server/configs/api.config.js +81 -0
- package/dist/server/configs/config.base.d.ts +63 -0
- package/dist/server/configs/config.base.js +64 -0
- package/dist/server/configs/index.d.ts +2 -0
- package/dist/server/configs/index.js +18 -0
- package/dist/server/container.d.ts +2 -0
- package/dist/server/container.js +6 -0
- package/dist/server/controllers/chat.controller.d.ts +10 -0
- package/dist/server/controllers/chat.controller.js +50 -0
- package/dist/server/controllers/command.controller.d.ts +7 -0
- package/dist/server/controllers/command.controller.js +47 -0
- package/dist/server/controllers/session.controller.d.ts +9 -0
- package/dist/server/controllers/session.controller.js +70 -0
- package/dist/server/core.d.ts +1 -0
- package/dist/server/core.js +7 -0
- package/dist/server/database/adapters/oxmysql.adapter.d.ts +89 -0
- package/dist/server/database/adapters/oxmysql.adapter.js +149 -0
- package/dist/server/database/database.contract.d.ts +128 -0
- package/dist/server/database/database.contract.js +29 -0
- package/dist/server/database/database.service.d.ts +216 -0
- package/dist/server/database/database.service.js +301 -0
- package/dist/server/database/index.d.ts +53 -0
- package/dist/server/database/index.js +70 -0
- package/dist/server/database/types.d.ts +67 -0
- package/dist/server/database/types.js +7 -0
- package/dist/server/database.d.ts +7 -0
- package/dist/server/database.js +23 -0
- package/dist/server/decorators/bind.d.ts +2 -0
- package/dist/server/decorators/bind.js +15 -0
- package/dist/server/decorators/command.d.ts +49 -0
- package/dist/server/decorators/command.js +23 -0
- package/dist/server/decorators/controller.d.ts +25 -0
- package/dist/server/decorators/controller.js +36 -0
- package/dist/server/decorators/export.d.ts +39 -0
- package/dist/server/decorators/export.js +47 -0
- package/dist/server/decorators/guard.d.ts +56 -0
- package/dist/server/decorators/guard.js +82 -0
- package/dist/server/decorators/index.d.ts +10 -0
- package/dist/server/decorators/index.js +29 -0
- package/dist/server/decorators/onFiveMEvent.d.ts +6 -0
- package/dist/server/decorators/onFiveMEvent.js +14 -0
- package/dist/server/decorators/onFrameworkEvent.d.ts +22 -0
- package/dist/server/decorators/onFrameworkEvent.js +29 -0
- package/dist/server/decorators/onNet.d.ts +58 -0
- package/dist/server/decorators/onNet.js +57 -0
- package/dist/server/decorators/onTick.d.ts +32 -0
- package/dist/server/decorators/onTick.js +40 -0
- package/dist/server/decorators/public.d.ts +27 -0
- package/dist/server/decorators/public.js +36 -0
- package/dist/server/decorators/requiresState.d.ts +56 -0
- package/dist/server/decorators/requiresState.js +63 -0
- package/dist/server/decorators/throttle.d.ts +48 -0
- package/dist/server/decorators/throttle.js +63 -0
- package/dist/server/decorators/utils.d.ts +57 -0
- package/dist/server/decorators/utils.js +63 -0
- package/dist/server/entities/index.d.ts +1 -0
- package/dist/server/entities/index.js +17 -0
- package/dist/server/entities/player.d.ts +157 -0
- package/dist/server/entities/player.js +217 -0
- package/dist/server/error-handler.d.ts +2 -0
- package/dist/server/error-handler.js +43 -0
- package/dist/server/helpers/resolve-method.d.ts +5 -0
- package/dist/server/helpers/resolve-method.js +18 -0
- package/dist/server/index.d.ts +10 -0
- package/dist/server/index.js +31 -0
- package/dist/server/loaders/exports.loader.d.ts +0 -0
- package/dist/server/loaders/exports.loader.js +23 -0
- package/dist/server/loaders/playerSession.loader.d.ts +1 -0
- package/dist/server/loaders/playerSession.loader.js +51 -0
- package/dist/server/services/access-control.service.d.ts +56 -0
- package/dist/server/services/access-control.service.js +99 -0
- package/dist/server/services/chat.service.d.ts +7 -0
- package/dist/server/services/chat.service.js +31 -0
- package/dist/server/services/command.service.d.ts +15 -0
- package/dist/server/services/command.service.js +77 -0
- package/dist/server/services/config.service.d.ts +75 -0
- package/dist/server/services/config.service.js +116 -0
- package/dist/server/services/default/default-security.handler.d.ts +6 -0
- package/dist/server/services/default/default-security.handler.js +26 -0
- package/dist/server/services/http/http.service.d.ts +50 -0
- package/dist/server/services/http/http.service.js +126 -0
- package/dist/server/services/index.d.ts +10 -0
- package/dist/server/services/index.js +26 -0
- package/dist/server/services/parallel/index.d.ts +49 -0
- package/dist/server/services/parallel/index.js +67 -0
- package/dist/server/services/parallel/parallel-compute.service.d.ts +132 -0
- package/dist/server/services/parallel/parallel-compute.service.js +449 -0
- package/dist/server/services/parallel/types.d.ts +188 -0
- package/dist/server/services/parallel/types.js +7 -0
- package/dist/server/services/parallel/worker-pool.d.ts +83 -0
- package/dist/server/services/parallel/worker-pool.js +350 -0
- package/dist/server/services/parallel/worker.d.ts +19 -0
- package/dist/server/services/parallel/worker.js +49 -0
- package/dist/server/services/persistence.service.d.ts +59 -0
- package/dist/server/services/persistence.service.js +166 -0
- package/dist/server/services/player.service.d.ts +96 -0
- package/dist/server/services/player.service.js +132 -0
- package/dist/server/services/rate-limiter.service.d.ts +5 -0
- package/dist/server/services/rate-limiter.service.js +39 -0
- package/dist/server/services/registers.d.ts +1 -0
- package/dist/server/services/registers.js +18 -0
- package/dist/server/setup.d.ts +9 -0
- package/dist/server/setup.js +28 -0
- package/dist/server/system/metadata-server.keys.d.ts +10 -0
- package/dist/server/system/metadata-server.keys.js +13 -0
- package/dist/server/system/processors/command.processor.d.ts +9 -0
- package/dist/server/system/processors/command.processor.js +30 -0
- package/dist/server/system/processors/coreEvent.processor.d.ts +7 -0
- package/dist/server/system/processors/coreEvent.processor.js +41 -0
- package/dist/server/system/processors/export.processor.d.ts +7 -0
- package/dist/server/system/processors/export.processor.js +30 -0
- package/dist/server/system/processors/fivemEvent.processor.d.ts +7 -0
- package/dist/server/system/processors/fivemEvent.processor.js +40 -0
- package/dist/server/system/processors/netEvent.processor.d.ts +11 -0
- package/dist/server/system/processors/netEvent.processor.js +103 -0
- package/dist/server/system/processors/tick.processor.d.ts +5 -0
- package/dist/server/system/processors/tick.processor.js +36 -0
- package/dist/server/system/processors.register.d.ts +1 -0
- package/dist/server/system/processors.register.js +23 -0
- package/dist/server/system/schema-generator.d.ts +2 -0
- package/dist/server/system/schema-generator.js +34 -0
- package/dist/server/templates/admin/admin.controller-template.d.ts +12 -0
- package/dist/server/templates/admin/admin.controller-template.js +2 -0
- package/dist/server/templates/auth/auth-provider.contract.d.ts +58 -0
- package/dist/server/templates/auth/auth-provider.contract.js +23 -0
- package/dist/server/templates/index.d.ts +8 -0
- package/dist/server/templates/index.js +21 -0
- package/dist/server/templates/persistence/index.d.ts +30 -0
- package/dist/server/templates/persistence/index.js +34 -0
- package/dist/server/templates/persistence/player-persistence.contract.d.ts +86 -0
- package/dist/server/templates/persistence/player-persistence.contract.js +52 -0
- package/dist/server/templates/repository/index.d.ts +57 -0
- package/dist/server/templates/repository/index.js +61 -0
- package/dist/server/templates/repository/repository.contract.d.ts +224 -0
- package/dist/server/templates/repository/repository.contract.js +342 -0
- package/dist/server/templates/repository/repository.types.d.ts +51 -0
- package/dist/server/templates/repository/repository.types.js +7 -0
- package/dist/server/templates/security/permission.types.d.ts +32 -0
- package/dist/server/templates/security/permission.types.js +2 -0
- package/dist/server/templates/security/principal-provider.contract.d.ts +43 -0
- package/dist/server/templates/security/principal-provider.contract.js +19 -0
- package/dist/server/templates/security/security-handler.contract.d.ts +5 -0
- package/dist/server/templates/security/security-handler.contract.js +6 -0
- package/dist/server/types/core-events.d.ts +22 -0
- package/dist/server/types/core-events.js +2 -0
- package/dist/server/types/security.types.d.ts +7 -0
- package/dist/server/types/security.types.js +2 -0
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.js +17 -0
- package/dist/shared/logger/core-logger.d.ts +35 -0
- package/dist/shared/logger/core-logger.js +52 -0
- package/dist/shared/logger/index.d.ts +11 -0
- package/dist/shared/logger/index.js +26 -0
- package/dist/shared/logger/logger.config.d.ts +47 -0
- package/dist/shared/logger/logger.config.js +33 -0
- package/dist/shared/logger/logger.service.d.ts +161 -0
- package/dist/shared/logger/logger.service.js +279 -0
- package/dist/shared/logger/logger.types.d.ts +85 -0
- package/dist/shared/logger/logger.types.js +74 -0
- package/dist/shared/logger/transports/buffered.transport.d.ts +88 -0
- package/dist/shared/logger/transports/buffered.transport.js +174 -0
- package/dist/shared/logger/transports/console.transport.d.ts +37 -0
- package/dist/shared/logger/transports/console.transport.js +134 -0
- package/dist/shared/logger/transports/index.d.ts +3 -0
- package/dist/shared/logger/transports/index.js +19 -0
- package/dist/shared/logger/transports/transport.interface.d.ts +40 -0
- package/dist/shared/logger/transports/transport.interface.js +2 -0
- package/dist/system/class-constructor.d.ts +1 -0
- package/dist/system/class-constructor.js +2 -0
- package/dist/system/decorator-processor.d.ts +4 -0
- package/dist/system/decorator-processor.js +2 -0
- package/dist/system/metadata.scanner.d.ts +7 -0
- package/dist/system/metadata.scanner.js +45 -0
- package/dist/utils/errors.d.ts +14 -0
- package/dist/utils/errors.js +25 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/result.d.ts +12 -0
- package/dist/utils/result.js +10 -0
- package/dist/utils/rgb.d.ts +5 -0
- package/dist/utils/rgb.js +2 -0
- package/dist/utils/vector3.d.ts +13 -0
- package/dist/utils/vector3.js +27 -0
- package/package.json +98 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ParallelCompute Service
|
|
4
|
+
*
|
|
5
|
+
* Provides an ergonomic API for parallel computation.
|
|
6
|
+
* Uses virtual workers in FiveM environment, with async execution
|
|
7
|
+
* that yields to the event loop for better performance.
|
|
8
|
+
*
|
|
9
|
+
* Automatically decides whether to run synchronously or asynchronously
|
|
10
|
+
* based on estimated computational cost.
|
|
11
|
+
*/
|
|
12
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
13
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
14
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
15
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
16
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.findClosest = exports.sortByDistance = exports.filterByDistance = exports.ParallelComputeService = void 0;
|
|
20
|
+
exports.getParallelComputeService = getParallelComputeService;
|
|
21
|
+
exports.initParallelCompute = initParallelCompute;
|
|
22
|
+
exports.shutdownParallelCompute = shutdownParallelCompute;
|
|
23
|
+
exports.defineTask = defineTask;
|
|
24
|
+
exports.defineBatchTransform = defineBatchTransform;
|
|
25
|
+
exports.defineBatchFilter = defineBatchFilter;
|
|
26
|
+
exports.defineBatchReduce = defineBatchReduce;
|
|
27
|
+
const tsyringe_1 = require("tsyringe");
|
|
28
|
+
const uuid_1 = require("uuid");
|
|
29
|
+
const worker_pool_1 = require("./worker-pool");
|
|
30
|
+
const DEFAULT_WORKER_THRESHOLD = 10000;
|
|
31
|
+
/**
|
|
32
|
+
* Service for managing parallel computation
|
|
33
|
+
*/
|
|
34
|
+
let ParallelComputeService = class ParallelComputeService {
|
|
35
|
+
constructor() {
|
|
36
|
+
this.pool = null;
|
|
37
|
+
this.metrics = {
|
|
38
|
+
totalTasks: 0,
|
|
39
|
+
syncTasks: 0,
|
|
40
|
+
parallelTasks: 0,
|
|
41
|
+
failedTasks: 0,
|
|
42
|
+
avgExecutionTime: 0,
|
|
43
|
+
estimatedTimeSaved: 0,
|
|
44
|
+
activeWorkers: 0,
|
|
45
|
+
peakWorkers: 0,
|
|
46
|
+
};
|
|
47
|
+
this.totalExecutionTime = 0;
|
|
48
|
+
this.isInitialized = false;
|
|
49
|
+
this.poolConfig = {};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Initialize the worker pool
|
|
53
|
+
* Must be called before using parallel execution
|
|
54
|
+
*/
|
|
55
|
+
initialize(config = {}) {
|
|
56
|
+
if (this.isInitialized)
|
|
57
|
+
return;
|
|
58
|
+
this.poolConfig = config;
|
|
59
|
+
this.pool = new worker_pool_1.WorkerPool(config);
|
|
60
|
+
this.pool.on('taskCompleted', (data) => {
|
|
61
|
+
const { executionTime } = data;
|
|
62
|
+
this.updateMetrics(executionTime, 'parallel');
|
|
63
|
+
});
|
|
64
|
+
this.pool.on('workerSpawned', () => {
|
|
65
|
+
var _a;
|
|
66
|
+
const stats = (_a = this.pool) === null || _a === void 0 ? void 0 : _a.getStats();
|
|
67
|
+
if (stats) {
|
|
68
|
+
this.metrics.activeWorkers = stats.totalWorkers;
|
|
69
|
+
this.metrics.peakWorkers = Math.max(this.metrics.peakWorkers, stats.totalWorkers);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
this.pool.on('workerExit', () => {
|
|
73
|
+
var _a;
|
|
74
|
+
const stats = (_a = this.pool) === null || _a === void 0 ? void 0 : _a.getStats();
|
|
75
|
+
if (stats) {
|
|
76
|
+
this.metrics.activeWorkers = stats.totalWorkers;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
this.isInitialized = true;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if initialized
|
|
83
|
+
*/
|
|
84
|
+
get initialized() {
|
|
85
|
+
return this.isInitialized;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if using native workers
|
|
89
|
+
*/
|
|
90
|
+
get isNative() {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
return (_b = (_a = this.pool) === null || _a === void 0 ? void 0 : _a.isNative) !== null && _b !== void 0 ? _b : false;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Shutdown the service and worker pool
|
|
96
|
+
*/
|
|
97
|
+
async shutdown() {
|
|
98
|
+
if (this.pool) {
|
|
99
|
+
await this.pool.shutdown();
|
|
100
|
+
this.pool = null;
|
|
101
|
+
}
|
|
102
|
+
this.isInitialized = false;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get current metrics
|
|
106
|
+
*/
|
|
107
|
+
getMetrics() {
|
|
108
|
+
return Object.assign({}, this.metrics);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get worker pool statistics
|
|
112
|
+
*/
|
|
113
|
+
getPoolStats() {
|
|
114
|
+
var _a, _b;
|
|
115
|
+
return (_b = (_a = this.pool) === null || _a === void 0 ? void 0 : _a.getStats()) !== null && _b !== void 0 ? _b : null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Reset metrics
|
|
119
|
+
*/
|
|
120
|
+
resetMetrics() {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
this.metrics = {
|
|
123
|
+
totalTasks: 0,
|
|
124
|
+
syncTasks: 0,
|
|
125
|
+
parallelTasks: 0,
|
|
126
|
+
failedTasks: 0,
|
|
127
|
+
avgExecutionTime: 0,
|
|
128
|
+
estimatedTimeSaved: 0,
|
|
129
|
+
activeWorkers: (_b = (_a = this.pool) === null || _a === void 0 ? void 0 : _a.getStats().totalWorkers) !== null && _b !== void 0 ? _b : 0,
|
|
130
|
+
peakWorkers: 0,
|
|
131
|
+
};
|
|
132
|
+
this.totalExecutionTime = 0;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Execute a compute function with automatic mode selection
|
|
136
|
+
*/
|
|
137
|
+
async run(options, input) {
|
|
138
|
+
var _a, _b, _c;
|
|
139
|
+
const cost = (_b = (_a = options.estimateCost) === null || _a === void 0 ? void 0 : _a.call(options, input)) !== null && _b !== void 0 ? _b : 0;
|
|
140
|
+
const threshold = (_c = options.workerThreshold) !== null && _c !== void 0 ? _c : DEFAULT_WORKER_THRESHOLD;
|
|
141
|
+
if (cost > threshold && this.pool) {
|
|
142
|
+
return this.parallel(options, input);
|
|
143
|
+
}
|
|
144
|
+
return this.sync(options, input);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Execute synchronously on main thread
|
|
148
|
+
*/
|
|
149
|
+
sync(options, input) {
|
|
150
|
+
const startTime = performance.now();
|
|
151
|
+
try {
|
|
152
|
+
const result = options.compute(input);
|
|
153
|
+
const executionTime = performance.now() - startTime;
|
|
154
|
+
this.updateMetrics(executionTime, 'sync');
|
|
155
|
+
return {
|
|
156
|
+
result,
|
|
157
|
+
executionTime,
|
|
158
|
+
mode: 'sync',
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
this.metrics.failedTasks++;
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Execute on worker (async with yielding in FiveM)
|
|
168
|
+
*/
|
|
169
|
+
async parallel(options, input) {
|
|
170
|
+
if (!this.pool) {
|
|
171
|
+
// Fallback to sync if pool not initialized
|
|
172
|
+
return this.sync(options, input);
|
|
173
|
+
}
|
|
174
|
+
const startTime = performance.now();
|
|
175
|
+
try {
|
|
176
|
+
const message = {
|
|
177
|
+
id: (0, uuid_1.v4)(),
|
|
178
|
+
taskName: options.name,
|
|
179
|
+
functionBody: options.compute.toString(),
|
|
180
|
+
input,
|
|
181
|
+
};
|
|
182
|
+
const result = (await this.pool.execute(message));
|
|
183
|
+
const executionTime = performance.now() - startTime;
|
|
184
|
+
return {
|
|
185
|
+
result,
|
|
186
|
+
executionTime,
|
|
187
|
+
mode: 'parallel',
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
this.metrics.failedTasks++;
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Execute distributed across multiple workers
|
|
197
|
+
*/
|
|
198
|
+
async distributed(options, input, workerCount) {
|
|
199
|
+
var _a;
|
|
200
|
+
if (!options.chunker || !options.merger) {
|
|
201
|
+
throw new Error('Distributed execution requires chunker and merger functions');
|
|
202
|
+
}
|
|
203
|
+
if (!this.pool) {
|
|
204
|
+
// Fallback to sync if pool not initialized
|
|
205
|
+
return this.sync(options, input);
|
|
206
|
+
}
|
|
207
|
+
const startTime = performance.now();
|
|
208
|
+
const actualWorkerCount = (_a = workerCount !== null && workerCount !== void 0 ? workerCount : this.poolConfig.maxWorkers) !== null && _a !== void 0 ? _a : 4;
|
|
209
|
+
try {
|
|
210
|
+
const chunks = options.chunker(input, actualWorkerCount);
|
|
211
|
+
const results = await Promise.all(chunks.map(async (chunk) => {
|
|
212
|
+
const message = {
|
|
213
|
+
id: (0, uuid_1.v4)(),
|
|
214
|
+
taskName: options.name,
|
|
215
|
+
functionBody: options.compute.toString(),
|
|
216
|
+
input: chunk,
|
|
217
|
+
};
|
|
218
|
+
return this.pool.execute(message);
|
|
219
|
+
}));
|
|
220
|
+
const mergedResult = options.merger(results);
|
|
221
|
+
const executionTime = performance.now() - startTime;
|
|
222
|
+
this.updateMetrics(executionTime, 'distributed');
|
|
223
|
+
return {
|
|
224
|
+
result: mergedResult,
|
|
225
|
+
executionTime,
|
|
226
|
+
mode: 'distributed',
|
|
227
|
+
workerCount: chunks.length,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
this.metrics.failedTasks++;
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Update internal metrics
|
|
237
|
+
*/
|
|
238
|
+
updateMetrics(executionTime, mode) {
|
|
239
|
+
this.metrics.totalTasks++;
|
|
240
|
+
this.totalExecutionTime += executionTime;
|
|
241
|
+
this.metrics.avgExecutionTime = this.totalExecutionTime / this.metrics.totalTasks;
|
|
242
|
+
if (mode === 'sync') {
|
|
243
|
+
this.metrics.syncTasks++;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
this.metrics.parallelTasks++;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
exports.ParallelComputeService = ParallelComputeService;
|
|
251
|
+
exports.ParallelComputeService = ParallelComputeService = __decorate([
|
|
252
|
+
(0, tsyringe_1.injectable)()
|
|
253
|
+
], ParallelComputeService);
|
|
254
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
255
|
+
// Standalone Functions (no DI required)
|
|
256
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
257
|
+
let globalService = null;
|
|
258
|
+
/**
|
|
259
|
+
* Get or create the global ParallelCompute service instance
|
|
260
|
+
*/
|
|
261
|
+
function getParallelComputeService() {
|
|
262
|
+
if (!globalService) {
|
|
263
|
+
globalService = new ParallelComputeService();
|
|
264
|
+
}
|
|
265
|
+
return globalService;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Initialize the global ParallelCompute service
|
|
269
|
+
*/
|
|
270
|
+
function initParallelCompute(config = {}) {
|
|
271
|
+
const service = getParallelComputeService();
|
|
272
|
+
service.initialize(config);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Shutdown the global ParallelCompute service
|
|
276
|
+
*/
|
|
277
|
+
async function shutdownParallelCompute() {
|
|
278
|
+
if (globalService) {
|
|
279
|
+
await globalService.shutdown();
|
|
280
|
+
globalService = null;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Define a parallel task with the given options
|
|
285
|
+
* Returns a task object with run, sync, parallel, and distributed methods
|
|
286
|
+
*/
|
|
287
|
+
function defineTask(options) {
|
|
288
|
+
const service = getParallelComputeService();
|
|
289
|
+
return {
|
|
290
|
+
name: options.name,
|
|
291
|
+
options,
|
|
292
|
+
async run(input) {
|
|
293
|
+
const result = await service.run(options, input);
|
|
294
|
+
return result.result;
|
|
295
|
+
},
|
|
296
|
+
sync(input) {
|
|
297
|
+
const result = service.sync(options, input);
|
|
298
|
+
return result.result;
|
|
299
|
+
},
|
|
300
|
+
async parallel(input) {
|
|
301
|
+
const result = await service.parallel(options, input);
|
|
302
|
+
return result.result;
|
|
303
|
+
},
|
|
304
|
+
async distributed(input, workerCount) {
|
|
305
|
+
const result = await service.distributed(options, input, workerCount);
|
|
306
|
+
return result.result;
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Built-in task: Filter entities by distance from a position
|
|
312
|
+
*/
|
|
313
|
+
exports.filterByDistance = defineTask({
|
|
314
|
+
name: 'filterByDistance',
|
|
315
|
+
estimateCost: (input) => input.entities.length,
|
|
316
|
+
workerThreshold: 1000,
|
|
317
|
+
compute: (input) => {
|
|
318
|
+
const { entities, position, radius } = input;
|
|
319
|
+
const radiusSquared = radius * radius;
|
|
320
|
+
return entities.filter((entity) => {
|
|
321
|
+
const dx = entity.x - position.x;
|
|
322
|
+
const dy = entity.y - position.y;
|
|
323
|
+
const dz = entity.z - position.z;
|
|
324
|
+
return dx * dx + dy * dy + dz * dz <= radiusSquared;
|
|
325
|
+
});
|
|
326
|
+
},
|
|
327
|
+
chunker: (input, workerCount) => {
|
|
328
|
+
const { entities, position, radius } = input;
|
|
329
|
+
const chunkSize = Math.ceil(entities.length / workerCount);
|
|
330
|
+
const chunks = [];
|
|
331
|
+
for (let i = 0; i < entities.length; i += chunkSize) {
|
|
332
|
+
chunks.push({
|
|
333
|
+
entities: entities.slice(i, i + chunkSize),
|
|
334
|
+
position,
|
|
335
|
+
radius,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
return chunks;
|
|
339
|
+
},
|
|
340
|
+
merger: (results) => [].concat(...results),
|
|
341
|
+
});
|
|
342
|
+
/**
|
|
343
|
+
* Built-in task: Sort entities by distance from a position
|
|
344
|
+
*/
|
|
345
|
+
exports.sortByDistance = defineTask({
|
|
346
|
+
name: 'sortByDistance',
|
|
347
|
+
estimateCost: (input) => input.entities.length * Math.log(input.entities.length),
|
|
348
|
+
workerThreshold: 500,
|
|
349
|
+
compute: (input) => {
|
|
350
|
+
const { entities, position } = input;
|
|
351
|
+
return [...entities].sort((a, b) => {
|
|
352
|
+
const dxA = a.x - position.x;
|
|
353
|
+
const dyA = a.y - position.y;
|
|
354
|
+
const dzA = a.z - position.z;
|
|
355
|
+
const distA = dxA * dxA + dyA * dyA + dzA * dzA;
|
|
356
|
+
const dxB = b.x - position.x;
|
|
357
|
+
const dyB = b.y - position.y;
|
|
358
|
+
const dzB = b.z - position.z;
|
|
359
|
+
const distB = dxB * dxB + dyB * dyB + dzB * dzB;
|
|
360
|
+
return distA - distB;
|
|
361
|
+
});
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
/**
|
|
365
|
+
* Built-in task: Find closest entity to a position
|
|
366
|
+
*/
|
|
367
|
+
exports.findClosest = defineTask({
|
|
368
|
+
name: 'findClosest',
|
|
369
|
+
estimateCost: (input) => input.entities.length,
|
|
370
|
+
workerThreshold: 5000,
|
|
371
|
+
compute: (input) => {
|
|
372
|
+
const { entities, position } = input;
|
|
373
|
+
if (entities.length === 0)
|
|
374
|
+
return null;
|
|
375
|
+
let closest = entities[0];
|
|
376
|
+
let minDistSquared = Infinity;
|
|
377
|
+
for (const entity of entities) {
|
|
378
|
+
const dx = entity.x - position.x;
|
|
379
|
+
const dy = entity.y - position.y;
|
|
380
|
+
const dz = entity.z - position.z;
|
|
381
|
+
const distSquared = dx * dx + dy * dy + dz * dz;
|
|
382
|
+
if (distSquared < minDistSquared) {
|
|
383
|
+
minDistSquared = distSquared;
|
|
384
|
+
closest = entity;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return closest;
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
/**
|
|
391
|
+
* Built-in task: Batch transform items
|
|
392
|
+
*/
|
|
393
|
+
function defineBatchTransform(name, transform, threshold = 1000) {
|
|
394
|
+
return defineTask({
|
|
395
|
+
name,
|
|
396
|
+
estimateCost: (input) => input.length,
|
|
397
|
+
workerThreshold: threshold,
|
|
398
|
+
compute: (input) => input.map(transform),
|
|
399
|
+
chunker: (input, workerCount) => {
|
|
400
|
+
const chunkSize = Math.ceil(input.length / workerCount);
|
|
401
|
+
const chunks = [];
|
|
402
|
+
for (let i = 0; i < input.length; i += chunkSize) {
|
|
403
|
+
chunks.push(input.slice(i, i + chunkSize));
|
|
404
|
+
}
|
|
405
|
+
return chunks;
|
|
406
|
+
},
|
|
407
|
+
merger: (results) => [].concat(...results),
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Built-in task: Batch filter items
|
|
412
|
+
*/
|
|
413
|
+
function defineBatchFilter(name, predicate, threshold = 1000) {
|
|
414
|
+
return defineTask({
|
|
415
|
+
name,
|
|
416
|
+
estimateCost: (input) => input.length,
|
|
417
|
+
workerThreshold: threshold,
|
|
418
|
+
compute: (input) => input.filter(predicate),
|
|
419
|
+
chunker: (input, workerCount) => {
|
|
420
|
+
const chunkSize = Math.ceil(input.length / workerCount);
|
|
421
|
+
const chunks = [];
|
|
422
|
+
for (let i = 0; i < input.length; i += chunkSize) {
|
|
423
|
+
chunks.push(input.slice(i, i + chunkSize));
|
|
424
|
+
}
|
|
425
|
+
return chunks;
|
|
426
|
+
},
|
|
427
|
+
merger: (results) => [].concat(...results),
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Built-in task: Batch reduce items
|
|
432
|
+
*/
|
|
433
|
+
function defineBatchReduce(name, reducer, initial, merger, threshold = 1000) {
|
|
434
|
+
return defineTask({
|
|
435
|
+
name,
|
|
436
|
+
estimateCost: (input) => input.length,
|
|
437
|
+
workerThreshold: threshold,
|
|
438
|
+
compute: (input) => input.reduce(reducer, initial),
|
|
439
|
+
chunker: (input, workerCount) => {
|
|
440
|
+
const chunkSize = Math.ceil(input.length / workerCount);
|
|
441
|
+
const chunks = [];
|
|
442
|
+
for (let i = 0; i < input.length; i += chunkSize) {
|
|
443
|
+
chunks.push(input.slice(i, i + chunkSize));
|
|
444
|
+
}
|
|
445
|
+
return chunks;
|
|
446
|
+
},
|
|
447
|
+
merger,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ParallelCompute Module Types
|
|
3
|
+
*
|
|
4
|
+
* Provides type definitions for the parallel computation system.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Task execution mode
|
|
8
|
+
*/
|
|
9
|
+
export type ExecutionMode = 'auto' | 'sync' | 'parallel';
|
|
10
|
+
/**
|
|
11
|
+
* Worker pool configuration
|
|
12
|
+
*/
|
|
13
|
+
export interface WorkerPoolConfig {
|
|
14
|
+
/** Minimum number of workers to keep alive */
|
|
15
|
+
minWorkers: number;
|
|
16
|
+
/** Maximum number of workers to spawn */
|
|
17
|
+
maxWorkers: number;
|
|
18
|
+
/** Time in ms before idle workers are terminated */
|
|
19
|
+
idleTimeout: number;
|
|
20
|
+
/** Maximum time in ms a task can run before timeout */
|
|
21
|
+
taskTimeout: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Options for defining a parallel task
|
|
25
|
+
*/
|
|
26
|
+
export interface ParallelTaskOptions<TInput, TOutput> {
|
|
27
|
+
/** Unique name for the task (used for debugging and metrics) */
|
|
28
|
+
name: string;
|
|
29
|
+
/**
|
|
30
|
+
* Function to estimate computational cost based on input.
|
|
31
|
+
* Higher values = more likely to use worker.
|
|
32
|
+
* Return a number representing relative complexity.
|
|
33
|
+
*/
|
|
34
|
+
estimateCost?: (input: TInput) => number;
|
|
35
|
+
/**
|
|
36
|
+
* If estimated cost exceeds this threshold, automatically use worker.
|
|
37
|
+
* Default: 10000
|
|
38
|
+
*/
|
|
39
|
+
workerThreshold?: number;
|
|
40
|
+
/**
|
|
41
|
+
* The compute function to execute.
|
|
42
|
+
* MUST be pure (no closures, no external dependencies).
|
|
43
|
+
* Will be serialized and sent to worker.
|
|
44
|
+
*/
|
|
45
|
+
compute: (input: TInput) => TOutput;
|
|
46
|
+
/**
|
|
47
|
+
* Optional function to split input into chunks for multiple workers.
|
|
48
|
+
* Enables parallel processing across multiple workers.
|
|
49
|
+
*/
|
|
50
|
+
chunker?: (input: TInput, workerCount: number) => TInput[];
|
|
51
|
+
/**
|
|
52
|
+
* Optional function to merge results from multiple chunks.
|
|
53
|
+
* Required if chunker is provided.
|
|
54
|
+
*/
|
|
55
|
+
merger?: (results: TOutput[]) => TOutput;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A defined parallel task that can be executed
|
|
59
|
+
*/
|
|
60
|
+
export interface ParallelTask<TInput, TOutput> {
|
|
61
|
+
/** Task name */
|
|
62
|
+
readonly name: string;
|
|
63
|
+
/** Task options */
|
|
64
|
+
readonly options: ParallelTaskOptions<TInput, TOutput>;
|
|
65
|
+
/**
|
|
66
|
+
* Execute the task with automatic mode selection.
|
|
67
|
+
* Uses worker if estimated cost exceeds threshold.
|
|
68
|
+
*/
|
|
69
|
+
run(input: TInput): Promise<TOutput>;
|
|
70
|
+
/**
|
|
71
|
+
* Force execution on main thread (synchronous).
|
|
72
|
+
*/
|
|
73
|
+
sync(input: TInput): TOutput;
|
|
74
|
+
/**
|
|
75
|
+
* Force execution on worker thread.
|
|
76
|
+
*/
|
|
77
|
+
parallel(input: TInput): Promise<TOutput>;
|
|
78
|
+
/**
|
|
79
|
+
* Execute with multiple workers (requires chunker/merger).
|
|
80
|
+
*/
|
|
81
|
+
distributed(input: TInput, workerCount?: number): Promise<TOutput>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Message sent to worker
|
|
85
|
+
*/
|
|
86
|
+
export interface WorkerMessage {
|
|
87
|
+
id: string;
|
|
88
|
+
taskName: string;
|
|
89
|
+
functionBody: string;
|
|
90
|
+
input: unknown;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Response from worker
|
|
94
|
+
*/
|
|
95
|
+
export interface WorkerResponse {
|
|
96
|
+
id: string;
|
|
97
|
+
success: boolean;
|
|
98
|
+
result?: unknown;
|
|
99
|
+
error?: string;
|
|
100
|
+
executionTime: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Worker status
|
|
104
|
+
*/
|
|
105
|
+
export type WorkerStatus = 'idle' | 'busy' | 'terminated';
|
|
106
|
+
/**
|
|
107
|
+
* Worker info for pool management
|
|
108
|
+
*/
|
|
109
|
+
export interface WorkerInfo {
|
|
110
|
+
id: number;
|
|
111
|
+
status: WorkerStatus;
|
|
112
|
+
currentTaskId: string | null;
|
|
113
|
+
tasksCompleted: number;
|
|
114
|
+
lastActiveAt: number;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Metrics for parallel compute service
|
|
118
|
+
*/
|
|
119
|
+
export interface ParallelComputeMetrics {
|
|
120
|
+
/** Total tasks executed */
|
|
121
|
+
totalTasks: number;
|
|
122
|
+
/** Tasks executed on main thread */
|
|
123
|
+
syncTasks: number;
|
|
124
|
+
/** Tasks executed on workers */
|
|
125
|
+
parallelTasks: number;
|
|
126
|
+
/** Tasks that failed */
|
|
127
|
+
failedTasks: number;
|
|
128
|
+
/** Average execution time in ms */
|
|
129
|
+
avgExecutionTime: number;
|
|
130
|
+
/** Total time saved by parallelization (estimated) */
|
|
131
|
+
estimatedTimeSaved: number;
|
|
132
|
+
/** Current active workers */
|
|
133
|
+
activeWorkers: number;
|
|
134
|
+
/** Peak worker count */
|
|
135
|
+
peakWorkers: number;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Task execution result with timing info
|
|
139
|
+
*/
|
|
140
|
+
export interface TaskResult<T> {
|
|
141
|
+
result: T;
|
|
142
|
+
executionTime: number;
|
|
143
|
+
mode: 'sync' | 'parallel' | 'distributed';
|
|
144
|
+
workerCount?: number;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Built-in compute functions for common operations
|
|
148
|
+
*/
|
|
149
|
+
export interface BuiltInComputes {
|
|
150
|
+
/**
|
|
151
|
+
* Filter entities by distance from a position
|
|
152
|
+
*/
|
|
153
|
+
filterByDistance: <T extends {
|
|
154
|
+
x: number;
|
|
155
|
+
y: number;
|
|
156
|
+
z: number;
|
|
157
|
+
}>(input: {
|
|
158
|
+
entities: T[];
|
|
159
|
+
position: {
|
|
160
|
+
x: number;
|
|
161
|
+
y: number;
|
|
162
|
+
z: number;
|
|
163
|
+
};
|
|
164
|
+
radius: number;
|
|
165
|
+
}) => T[];
|
|
166
|
+
/**
|
|
167
|
+
* Sort entities by distance from a position
|
|
168
|
+
*/
|
|
169
|
+
sortByDistance: <T extends {
|
|
170
|
+
x: number;
|
|
171
|
+
y: number;
|
|
172
|
+
z: number;
|
|
173
|
+
}>(input: {
|
|
174
|
+
entities: T[];
|
|
175
|
+
position: {
|
|
176
|
+
x: number;
|
|
177
|
+
y: number;
|
|
178
|
+
z: number;
|
|
179
|
+
};
|
|
180
|
+
}) => T[];
|
|
181
|
+
/**
|
|
182
|
+
* Batch process items with a transform function
|
|
183
|
+
*/
|
|
184
|
+
batchProcess: <T, R>(input: {
|
|
185
|
+
items: T[];
|
|
186
|
+
transform: string;
|
|
187
|
+
}) => R[];
|
|
188
|
+
}
|