@margoengine/ecs 0.0.0-stage → 1.0.0
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +486 -2
- package/dist/changes.d.ts +39 -0
- package/dist/changes.js +63 -0
- package/dist/coroutines.d.ts +82 -0
- package/dist/coroutines.js +97 -0
- package/dist/engine.d.ts +80 -0
- package/dist/engine.js +164 -0
- package/dist/growable.d.ts +23 -0
- package/dist/growable.js +28 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.js +166 -0
- package/dist/objects.d.ts +43 -0
- package/dist/objects.js +78 -0
- package/dist/parallel-runtime.d.ts +43 -0
- package/dist/parallel.d.ts +102 -0
- package/dist/parallel.js +280 -0
- package/dist/parallel.worker.d.ts +1 -0
- package/dist/query.d.ts +42 -0
- package/dist/query.js +61 -0
- package/dist/reactive.d.ts +59 -0
- package/dist/reactive.js +106 -0
- package/dist/relation-query.d.ts +77 -0
- package/dist/relation-query.js +123 -0
- package/dist/relations.d.ts +72 -0
- package/dist/relations.js +99 -0
- package/dist/schedule.d.ts +82 -0
- package/dist/schedule.js +109 -0
- package/dist/scripts.d.ts +182 -0
- package/dist/scripts.js +278 -0
- package/dist/serialize-codec.d.ts +34 -0
- package/dist/serialize.d.ts +34 -0
- package/dist/serialize.js +355 -0
- package/dist/workers/parallel.js +1 -0
- package/package.json +105 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Schedule as e } from "./schedule.js";
|
|
2
|
+
import { ChangeTracker as t } from "./changes.js";
|
|
3
|
+
import { Query as n } from "./query.js";
|
|
4
|
+
import { Relation as r } from "./relations.js";
|
|
5
|
+
import { MargoError as i } from "@margoengine/math/errors";
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
var a = 1e6, o = class {
|
|
8
|
+
constructor(e, t, n = !1) {
|
|
9
|
+
if (this.world = e, this.size = 0, this.observers = [], !n && !Object.keys(t).length) throw TypeError("Empty component schema; give at least one field, e.g. {x: Float32Array}");
|
|
10
|
+
this.sparse = e.allocate(Int32Array, -1), this.entities = e.allocate(Uint32Array), this.fields = Object.fromEntries(Object.entries(t).map(([t, n]) => {
|
|
11
|
+
if (![
|
|
12
|
+
Float32Array,
|
|
13
|
+
Float64Array,
|
|
14
|
+
Int32Array,
|
|
15
|
+
Uint32Array,
|
|
16
|
+
Uint8Array
|
|
17
|
+
].includes(n)) throw TypeError("Unsupported component storage; use Float32Array, Float64Array, Int32Array, Uint32Array or Uint8Array");
|
|
18
|
+
return [t, e.allocate(n)];
|
|
19
|
+
})), this.columns = Object.values(this.fields);
|
|
20
|
+
}
|
|
21
|
+
index(e) {
|
|
22
|
+
if (!this.world.alive(e)) return -1;
|
|
23
|
+
let t = this.sparse[e % this.world.stride];
|
|
24
|
+
return t >= 0 && this.entities[t] === e ? t : -1;
|
|
25
|
+
}
|
|
26
|
+
has(e) {
|
|
27
|
+
return this.index(e) >= 0;
|
|
28
|
+
}
|
|
29
|
+
add(e, t = {}) {
|
|
30
|
+
this.world.check(e), this.validateValues(t);
|
|
31
|
+
let n = Object.keys(t), r = this.index(e), i = r < 0;
|
|
32
|
+
i && (r = this.size, this.initializeRow(r, t), this.size++, this.sparse[e % this.world.stride] = r, this.entities[r] = e);
|
|
33
|
+
for (let e of n) this.fields[e][r] = t[e];
|
|
34
|
+
let a = this.observers;
|
|
35
|
+
if (i) for (let t = 0; t < a.length; t++) a[t].onAdd(e);
|
|
36
|
+
else if (n.length) for (let t = 0; t < a.length; t++) a[t].onChange(e);
|
|
37
|
+
return r;
|
|
38
|
+
}
|
|
39
|
+
validateValues(e) {
|
|
40
|
+
let t = Object.keys(e);
|
|
41
|
+
for (let n of t) {
|
|
42
|
+
let t = e[n], r = this.fields[n];
|
|
43
|
+
if (!Object.hasOwn(this.fields, n) || typeof t != "number" || !Number.isFinite(t) || r instanceof Float32Array && !Number.isFinite(Math.fround(t)) || r instanceof Int32Array && (!Number.isInteger(t) || t < -2147483648 || t > 2147483647) || r instanceof Uint32Array && (!Number.isInteger(t) || t < 0 || t > 4294967295) || r instanceof Uint8Array && (!Number.isInteger(t) || t < 0 || t > 255)) throw RangeError("Invalid component data; pass finite numbers that fit the field type");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
initializeRow(e, t = {}) {
|
|
47
|
+
for (let t of this.columns) t[e] = 0;
|
|
48
|
+
}
|
|
49
|
+
moveRow(e, t) {
|
|
50
|
+
if (e !== t) for (let n of this.columns) n[e] = n[t];
|
|
51
|
+
}
|
|
52
|
+
touch(e) {
|
|
53
|
+
if (this.index(e) < 0) return !1;
|
|
54
|
+
let t = this.observers;
|
|
55
|
+
for (let n = 0; n < t.length; n++) t[n].onChange(e);
|
|
56
|
+
return !0;
|
|
57
|
+
}
|
|
58
|
+
remove(e) {
|
|
59
|
+
let t = this.index(e);
|
|
60
|
+
if (t < 0) return !1;
|
|
61
|
+
let n = --this.size;
|
|
62
|
+
if (this.sparse[e % this.world.stride] = -1, t !== n) {
|
|
63
|
+
let e = this.entities[n];
|
|
64
|
+
this.entities[t] = e, this.sparse[e % this.world.stride] = t;
|
|
65
|
+
}
|
|
66
|
+
this.moveRow(t, n);
|
|
67
|
+
let r = this.observers;
|
|
68
|
+
for (let t = 0; t < r.length; t++) r[t].onRemove(e);
|
|
69
|
+
return !0;
|
|
70
|
+
}
|
|
71
|
+
}, s = class {
|
|
72
|
+
constructor(e = 4096, t = e) {
|
|
73
|
+
if (this.capacity = e, this.stride = t, this.components = /* @__PURE__ */ new Set(), !Number.isSafeInteger(t) || t < 1 || t > a) throw RangeError("Invalid ECS limit; use an integer from 1 to 1000000");
|
|
74
|
+
if (!Number.isSafeInteger(e) || e < 1 || e > t) throw RangeError(`Invalid ECS capacity; use an integer from 1 to ${t}`);
|
|
75
|
+
this.live = this.allocate(Uint8Array), this.generations = this.allocate(Uint32Array), this.free = this.allocate(Uint32Array), this.freeCount = 0, this.release(0, e);
|
|
76
|
+
}
|
|
77
|
+
allocate(e, t = 0) {
|
|
78
|
+
let n = new e(this.capacity);
|
|
79
|
+
return t && n.fill(t), n;
|
|
80
|
+
}
|
|
81
|
+
get available() {
|
|
82
|
+
return this.freeCount;
|
|
83
|
+
}
|
|
84
|
+
reserve(e) {
|
|
85
|
+
if (!Number.isSafeInteger(e) || e < 0 || e > this.stride) throw RangeError("Invalid ECS reservation; use a free-slot count from zero to world.stride");
|
|
86
|
+
for (; this.freeCount < e;) this.grow();
|
|
87
|
+
}
|
|
88
|
+
entities(e) {
|
|
89
|
+
let t = 0;
|
|
90
|
+
for (let n = 0; n < this.capacity; n++) if (this.live[n] === 1) {
|
|
91
|
+
if (t === e.length) throw RangeError("Entity output is too small; allocate world.capacity entries");
|
|
92
|
+
e[t++] = this.generations[n] * this.stride + n;
|
|
93
|
+
}
|
|
94
|
+
return t;
|
|
95
|
+
}
|
|
96
|
+
release(e, t) {
|
|
97
|
+
for (let n = t - 1; n >= e; n--) this.free[this.freeCount++] = n;
|
|
98
|
+
}
|
|
99
|
+
grow() {
|
|
100
|
+
throw RangeError("Entity capacity exhausted; destroy entities or create the World with a larger capacity");
|
|
101
|
+
}
|
|
102
|
+
alive(e) {
|
|
103
|
+
if (!Number.isSafeInteger(e) || e < 0 || e > 4294967295) return !1;
|
|
104
|
+
let t = e % this.stride;
|
|
105
|
+
return !!this.live[t] && Math.floor(e / this.stride) === this.generations[t];
|
|
106
|
+
}
|
|
107
|
+
destroying(e) {
|
|
108
|
+
return this.alive(e) && this.live[e % this.stride] === 2;
|
|
109
|
+
}
|
|
110
|
+
check(e) {
|
|
111
|
+
if (!this.alive(e)) throw new i("state", "Entity is stale or destroyed; check world.alive(entity) before using a stored id");
|
|
112
|
+
}
|
|
113
|
+
create() {
|
|
114
|
+
this.freeCount || this.grow();
|
|
115
|
+
let e = this.free[--this.freeCount];
|
|
116
|
+
return this.live[e] = 1, this.generations[e] * this.stride + e;
|
|
117
|
+
}
|
|
118
|
+
destroy(e) {
|
|
119
|
+
this.check(e);
|
|
120
|
+
let t = e % this.stride;
|
|
121
|
+
if (this.live[t] !== 2) {
|
|
122
|
+
this.live[t] = 2;
|
|
123
|
+
for (let t of this.components) t.remove(e);
|
|
124
|
+
this.live[t] = 0, (this.generations[t] + 1) * this.stride + t <= 4294967295 && (this.generations[t]++, this.free[this.freeCount++] = t);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
attach(e) {
|
|
128
|
+
this.components.add(e);
|
|
129
|
+
}
|
|
130
|
+
detach(e) {
|
|
131
|
+
this.components.delete(e);
|
|
132
|
+
}
|
|
133
|
+
component(e) {
|
|
134
|
+
let t = new o(this, e);
|
|
135
|
+
return this.components.add(t), t;
|
|
136
|
+
}
|
|
137
|
+
tag() {
|
|
138
|
+
let e = new o(this, {}, !0);
|
|
139
|
+
return this.components.add(e), e;
|
|
140
|
+
}
|
|
141
|
+
query(e, t) {
|
|
142
|
+
if (!e.length || e.some((e) => e.world !== this)) throw TypeError("Query needs components of this world");
|
|
143
|
+
let n = e[0];
|
|
144
|
+
for (let t = 1; t < e.length; t++) e[t].size < n.size && (n = e[t]);
|
|
145
|
+
let r = 0, i = this.stride, a = this.live, o = this.generations, s = n.entities, c = n.size, l = e.length;
|
|
146
|
+
for (let n = 0; n < c; n++) {
|
|
147
|
+
let c = s[n], u = c % i;
|
|
148
|
+
if (!a[u] || Math.floor(c / i) !== o[u]) continue;
|
|
149
|
+
let d = !0;
|
|
150
|
+
for (let t = 0; t < l; t++) {
|
|
151
|
+
let n = e[t], r = n.sparse[u];
|
|
152
|
+
if (r < 0 || n.entities[r] !== c) {
|
|
153
|
+
d = !1;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (d) {
|
|
158
|
+
if (r === t.length) throw RangeError("Query output is too small; pass a Uint32Array at least as long as the entity count (world.capacity always fits)");
|
|
159
|
+
t[r++] = c;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return r;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
//#endregion
|
|
166
|
+
export { t as ChangeTracker, o as Component, n as Query, r as Relation, e as Schedule, s as World };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Component, type ArrayConstructor, type Entity, type World } from './index.js';
|
|
2
|
+
/** A reference field with a fresh default per row. Values retain identity, including functions and cycles. */
|
|
3
|
+
export interface ObjectField<T = unknown> {
|
|
4
|
+
/** Called for each new row; default is undefined. */
|
|
5
|
+
readonly create: () => T;
|
|
6
|
+
}
|
|
7
|
+
/** Declare a JS value field. Pass a factory for independent mutable defaults. */
|
|
8
|
+
export declare function objectField<T = unknown>(create?: () => T): ObjectField<T>;
|
|
9
|
+
/**
|
|
10
|
+
* A weak reference field: each row holds a `WeakRef` to its value, so the garbage collector may take the value once
|
|
11
|
+
* nothing else holds it, and the row then reads `undefined`. Values are objects, functions, unregistered symbols or
|
|
12
|
+
* `undefined`.
|
|
13
|
+
*/
|
|
14
|
+
export interface WeakObjectField<T extends WeakKey = WeakKey> {
|
|
15
|
+
/** Called for each new row; default is undefined. */
|
|
16
|
+
readonly create: () => T | undefined;
|
|
17
|
+
/** Marks the field as weak. */
|
|
18
|
+
readonly weak: true;
|
|
19
|
+
}
|
|
20
|
+
/** Declare a weak reference field. Pass a factory for a default per row. */
|
|
21
|
+
export declare function weakObjectField<T extends WeakKey = WeakKey>(create?: () => T | undefined): WeakObjectField<T>;
|
|
22
|
+
/** Mixed reference/numeric component schema. */
|
|
23
|
+
export type ObjectSchema = Record<string, ArrayConstructor | ObjectField | WeakObjectField>;
|
|
24
|
+
/** Value of one field of a mixed schema. */
|
|
25
|
+
export type ObjectValue<T> = T extends WeakObjectField<infer W> ? W | undefined : T extends ObjectField<infer V> ? V : number;
|
|
26
|
+
/** Packed mixed fields, indexed with component.index(entity). */
|
|
27
|
+
export type ObjectFields<S extends ObjectSchema> = {
|
|
28
|
+
[K in keyof S]: S[K] extends ArrayConstructor ? InstanceType<S[K]> : ObjectValue<S[K]>[];
|
|
29
|
+
};
|
|
30
|
+
/** A query-compatible component with arbitrary JS fields beside packed numeric arrays. */
|
|
31
|
+
export declare class ObjectComponent<S extends ObjectSchema> extends Component<Record<never, never>> {
|
|
32
|
+
readonly fields: ObjectFields<S>;
|
|
33
|
+
private readonly objects;
|
|
34
|
+
/** @throws TypeError for an empty schema, invalid factory or unsupported numeric constructor. */
|
|
35
|
+
constructor(world: World, schema: S);
|
|
36
|
+
/** Add or update a row atomically after validation; omitted new object fields use their factories. */
|
|
37
|
+
add(entity: Entity, values?: Partial<{
|
|
38
|
+
[K in keyof S]: ObjectValue<S[K]>;
|
|
39
|
+
}>): number;
|
|
40
|
+
protected validateValues(values: Record<string, unknown>): void;
|
|
41
|
+
protected initializeRow(row: number, values?: Record<string, unknown>): void;
|
|
42
|
+
protected moveRow(row: number, last: number): void;
|
|
43
|
+
}
|
package/dist/objects.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Component as e } from "./index.js";
|
|
2
|
+
//#region src/objects.ts
|
|
3
|
+
function t(e = (() => void 0)) {
|
|
4
|
+
if (typeof e != "function") throw TypeError("Invalid object field factory; pass a function");
|
|
5
|
+
return Object.freeze({ create: e });
|
|
6
|
+
}
|
|
7
|
+
function n(e) {
|
|
8
|
+
return typeof e == "object" && !!e || typeof e == "function" || typeof e == "symbol" && Symbol.keyFor(e) === void 0;
|
|
9
|
+
}
|
|
10
|
+
function r(e) {
|
|
11
|
+
if (e !== void 0 && !n(e)) throw TypeError("Invalid weak object field value; pass an object, a function, an unregistered symbol or undefined");
|
|
12
|
+
}
|
|
13
|
+
function i(e = () => void 0) {
|
|
14
|
+
if (typeof e != "function") throw TypeError("Invalid object field factory; pass a function");
|
|
15
|
+
return Object.freeze({
|
|
16
|
+
create: e,
|
|
17
|
+
weak: !0
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
var a = (e) => typeof e == "string" && e !== "" && String(e >>> 0) === e && +e != 4294967295;
|
|
21
|
+
function o() {
|
|
22
|
+
return new Proxy([], {
|
|
23
|
+
get(e, t, n) {
|
|
24
|
+
return a(t) ? e[+t]?.deref() : Reflect.get(e, t, n);
|
|
25
|
+
},
|
|
26
|
+
set(e, t, n, i) {
|
|
27
|
+
return a(t) ? (r(n), e[+t] = n === void 0 ? void 0 : new WeakRef(n), !0) : Reflect.set(e, t, n, i);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
var s = class extends e {
|
|
32
|
+
constructor(e, t) {
|
|
33
|
+
let n = [], r = {};
|
|
34
|
+
for (let [e, i] of Object.entries(t)) typeof i == "object" && i && typeof i.create == "function" ? n.push([e, i]) : Object.defineProperty(r, e, {
|
|
35
|
+
value: i,
|
|
36
|
+
enumerable: !0
|
|
37
|
+
});
|
|
38
|
+
if (!Object.keys(t).length) throw TypeError("Empty object schema; declare at least one field");
|
|
39
|
+
super(e, r, !0), this.objects = n;
|
|
40
|
+
for (let [e, t] of n) Object.defineProperty(this.fields, e, {
|
|
41
|
+
value: "weak" in t ? o() : [],
|
|
42
|
+
enumerable: !0
|
|
43
|
+
});
|
|
44
|
+
e.attach(this);
|
|
45
|
+
}
|
|
46
|
+
add(e, t = {}) {
|
|
47
|
+
return super.add(e, t);
|
|
48
|
+
}
|
|
49
|
+
validateValues(e) {
|
|
50
|
+
let t = {};
|
|
51
|
+
for (let [n, i] of Object.entries(e)) {
|
|
52
|
+
if (!Object.hasOwn(this.fields, n)) throw RangeError("Unknown object component field; use a schema field");
|
|
53
|
+
let e = this.objects.find(([e]) => e === n)?.[1];
|
|
54
|
+
e && "weak" in e ? r(i) : e || Object.defineProperty(t, n, {
|
|
55
|
+
value: i,
|
|
56
|
+
enumerable: !0
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
super.validateValues(t);
|
|
60
|
+
}
|
|
61
|
+
initializeRow(e, t = {}) {
|
|
62
|
+
let n = this.objects.map(([e, n]) => {
|
|
63
|
+
let i = Object.hasOwn(t, e) ? t[e] : n.create();
|
|
64
|
+
return "weak" in n && r(i), i;
|
|
65
|
+
});
|
|
66
|
+
super.initializeRow(e);
|
|
67
|
+
for (let t = 0; t < this.objects.length; t++) this.fields[this.objects[t][0]][e] = n[t];
|
|
68
|
+
}
|
|
69
|
+
moveRow(e, t) {
|
|
70
|
+
super.moveRow(e, t);
|
|
71
|
+
for (let [n] of this.objects) {
|
|
72
|
+
let r = this.fields[n];
|
|
73
|
+
e !== t && (r[e] = r[t]), r.length = t;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { s as ObjectComponent, t as objectField, i as weakObjectField };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { NumericArray } from './index.js';
|
|
2
|
+
/** Stable packed shared component view for one system execution. */
|
|
3
|
+
export interface SharedComponentView {
|
|
4
|
+
/** Read the first count ids. Structural edits are confined to the calling thread between updates. */
|
|
5
|
+
readonly entities: Uint32Array;
|
|
6
|
+
/** Shared sparse row lookup by entity slot. Use sharedIndex to check the generation. */
|
|
7
|
+
readonly rows: Int32Array;
|
|
8
|
+
/** Slot spacing in entity ids. */
|
|
9
|
+
readonly stride: number;
|
|
10
|
+
/** Occupied packed rows. */
|
|
11
|
+
readonly count: number;
|
|
12
|
+
/** Shared numeric field arrays. Only declared writes may be mutated. */
|
|
13
|
+
readonly fields: Readonly<Record<string, NumericArray>>;
|
|
14
|
+
}
|
|
15
|
+
/** Resolve a live packed row from a worker view, rejecting stale generations. */
|
|
16
|
+
export declare function sharedIndex(view: SharedComponentView, entity: number): number;
|
|
17
|
+
/** Worker system invocation; retain neither views nor asynchronous work after the function settles. */
|
|
18
|
+
export interface ParallelContext {
|
|
19
|
+
/** Seconds in this update. */
|
|
20
|
+
readonly delta: number;
|
|
21
|
+
/** Accumulated successful update time, including this update. */
|
|
22
|
+
readonly elapsed: number;
|
|
23
|
+
/** Zero-based successful update index. */
|
|
24
|
+
readonly tick: number;
|
|
25
|
+
/** Only the named components declared in reads or writes. */
|
|
26
|
+
readonly components: Readonly<Record<string, SharedComponentView>>;
|
|
27
|
+
}
|
|
28
|
+
/** Browser Worker or an equivalent app-provided worker endpoint. */
|
|
29
|
+
export interface ParallelEndpoint {
|
|
30
|
+
/** Send a structured-clone message. Shared buffers are shared, never transferred. */
|
|
31
|
+
postMessage(message: unknown): void;
|
|
32
|
+
/** Receive messages or endpoint failures. */
|
|
33
|
+
addEventListener(type: string, listener: EventListener): void;
|
|
34
|
+
/** Detach a listener. */
|
|
35
|
+
removeEventListener(type: string, listener: EventListener): void;
|
|
36
|
+
}
|
|
37
|
+
/** Owned worker endpoint. Termination must stop all shared-memory writes before resolving. */
|
|
38
|
+
export interface ParallelWorker extends ParallelEndpoint {
|
|
39
|
+
/** Stop immediately. Promise-returning hosts are awaited before releasing the world lock. */
|
|
40
|
+
terminate(): void | Promise<unknown>;
|
|
41
|
+
}
|
|
42
|
+
/** Install the worker-side module dispatcher on a dedicated worker endpoint; returns its cleanup function. */
|
|
43
|
+
export declare function serveParallelSystems(endpoint: ParallelEndpoint): () => void;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Component, World, type Entity, type NumericArray, type Schema, type ComponentStore } from './index.js';
|
|
2
|
+
import type { ParallelWorker } from './parallel-runtime.js';
|
|
3
|
+
export { serveParallelSystems, sharedIndex } from './parallel-runtime.js';
|
|
4
|
+
export type { ParallelWorker, ParallelEndpoint, ParallelContext, SharedComponentView } from './parallel-runtime.js';
|
|
5
|
+
/** A numeric component whose membership can change only between shared worker updates. */
|
|
6
|
+
export declare class SharedComponent<S extends Schema = Schema> extends Component<S> {
|
|
7
|
+
/** Shared entity-slot lookup, maintained before observers run. Resolve generations with sharedIndex. */
|
|
8
|
+
readonly rows: Int32Array;
|
|
9
|
+
private adding?;
|
|
10
|
+
private removing?;
|
|
11
|
+
/** Prefer SharedWorld.component/tag, which register this store for cleanup. */
|
|
12
|
+
constructor(world: SharedWorld, schema: S, tag?: boolean);
|
|
13
|
+
add(entity: Entity, values?: Partial<Record<keyof S, number>>): number;
|
|
14
|
+
remove(entity: Entity): boolean;
|
|
15
|
+
protected initializeRow(row: number, values?: Partial<Record<keyof S, number>>): void;
|
|
16
|
+
protected moveRow(row: number, last: number): void;
|
|
17
|
+
touch(entity: Entity): boolean;
|
|
18
|
+
}
|
|
19
|
+
/** Fixed-capacity world with SharedArrayBuffer fields and guarded structural mutation during worker updates. */
|
|
20
|
+
export declare class SharedWorld extends World {
|
|
21
|
+
/** Allocate fixed-length shared storage; requires cross-origin isolation in browsers. */
|
|
22
|
+
allocate<T extends NumericArray>(Type: new (length: number) => T, fill?: number): T;
|
|
23
|
+
create(): Entity;
|
|
24
|
+
destroy(entity: Entity): void;
|
|
25
|
+
reserve(count: number): void;
|
|
26
|
+
attach(store: {
|
|
27
|
+
remove(entity: Entity): unknown;
|
|
28
|
+
}): void;
|
|
29
|
+
detach(store: {
|
|
30
|
+
remove(entity: Entity): unknown;
|
|
31
|
+
}): void;
|
|
32
|
+
component<S extends Schema>(schema: S): SharedComponent<S>;
|
|
33
|
+
tag(): SharedComponent<Record<never, never>>;
|
|
34
|
+
}
|
|
35
|
+
/** One module system and its declared memory accesses. */
|
|
36
|
+
export interface ParallelSystem {
|
|
37
|
+
/** Unique system name. */
|
|
38
|
+
name: string;
|
|
39
|
+
/** Absolute module URL; native module imports are cached inside workers. */
|
|
40
|
+
module: string | URL;
|
|
41
|
+
/** Named function export. Default 'default'. */
|
|
42
|
+
exportName?: string;
|
|
43
|
+
/** Read-only component names. Default none. */
|
|
44
|
+
reads?: readonly string[];
|
|
45
|
+
/** Read/write component names. Default none. Conflicts serialize in dependency order. */
|
|
46
|
+
writes?: readonly string[];
|
|
47
|
+
/** Explicit successors. Default none. */
|
|
48
|
+
before?: readonly string[];
|
|
49
|
+
/** Explicit predecessors. Default none. */
|
|
50
|
+
after?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
/** Worker pool ownership, capacity and deadlines. */
|
|
53
|
+
export interface ParallelOptions {
|
|
54
|
+
/** Worker count, 1 to 32. Default 2. */
|
|
55
|
+
workers?: number;
|
|
56
|
+
/** Hard deadline per dispatched system in milliseconds. Range 1 to 2147483647; default 30000. Failure closes the schedule. */
|
|
57
|
+
timeout?: number;
|
|
58
|
+
/** Worker factory, for embedding or Node adapters. Default a dedicated browser worker. */
|
|
59
|
+
workerFactory?: () => ParallelWorker;
|
|
60
|
+
}
|
|
61
|
+
/** Conflict-aware worker system scheduler over stable shared numeric component rows. */
|
|
62
|
+
export declare class ParallelSchedule {
|
|
63
|
+
readonly world: SharedWorld;
|
|
64
|
+
private readonly systems;
|
|
65
|
+
private readonly stores;
|
|
66
|
+
private readonly slots;
|
|
67
|
+
private readonly workerCount;
|
|
68
|
+
private readonly timeout;
|
|
69
|
+
private readonly factory;
|
|
70
|
+
private stopping?;
|
|
71
|
+
private stopped;
|
|
72
|
+
private running;
|
|
73
|
+
private sequence;
|
|
74
|
+
private tick;
|
|
75
|
+
private elapsed;
|
|
76
|
+
private failure?;
|
|
77
|
+
private terminationFailed;
|
|
78
|
+
/** @throws RangeError for invalid worker count/deadline; TypeError for nonshared or foreign components. */
|
|
79
|
+
constructor(world: SharedWorld, components: Readonly<Record<string, ComponentStore>>, options?: ParallelOptions);
|
|
80
|
+
/** Add a module system. Missing dependency names/cycles reject when compiling order or running an update. */
|
|
81
|
+
add(system: ParallelSystem): this;
|
|
82
|
+
private graph;
|
|
83
|
+
private levels;
|
|
84
|
+
/** Return concurrent groups after explicit dependencies and read/write hazards are applied. */
|
|
85
|
+
order(): string[][];
|
|
86
|
+
private slot;
|
|
87
|
+
private dispatch;
|
|
88
|
+
/**
|
|
89
|
+
* Run independent systems concurrently and serialize declared conflicts. Locks structure until every worker settles.
|
|
90
|
+
* Callers must not write raw shared arrays or retain worker tasks across the returned promise.
|
|
91
|
+
* After success, written components are touched for change trackers. Failure/abort terminates all workers,
|
|
92
|
+
* closes this schedule and can leave partial field writes; no system is silently repeated.
|
|
93
|
+
* @throws MargoError 'state' for concurrent updates, worker failures or cycles, 'timeout' for deadlines, 'disposed' after stop.
|
|
94
|
+
*/
|
|
95
|
+
update(delta: number, { signal }?: {
|
|
96
|
+
signal?: AbortSignal;
|
|
97
|
+
}): Promise<void>;
|
|
98
|
+
/** Terminate owned workers and reject pending calls; await termination before reusing shared fields. Idempotent. */
|
|
99
|
+
stop(): Promise<void>;
|
|
100
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
101
|
+
private check;
|
|
102
|
+
}
|