@daneren2005/shared-memory-ecs 1.10.2 → 1.10.4

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/dist/index.js CHANGED
@@ -1,17 +1,17 @@
1
- import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as m, t as h, u as ee, v as g, y as _ } from "./create-system-worker-BU5CnyVh.js";
2
- import te from "@daneren2005/shared-memory-objects/memory-heap";
3
- import { MAX_BYTE_OFFSET_LENGTH as v } from "@daneren2005/shared-memory-objects/utils/pointer";
1
+ import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as ee, t as te, u as ne, v as m, y as h } from "./create-system-worker-BU5CnyVh.js";
2
+ import re from "@daneren2005/shared-memory-objects/memory-heap";
3
+ import { MAX_BYTE_OFFSET_LENGTH as ie } from "@daneren2005/shared-memory-objects/utils/pointer";
4
4
  //#region \0rolldown/runtime.js
5
- var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDescriptor, re = Object.getOwnPropertyNames, ie = Object.getPrototypeOf, x = Object.prototype.hasOwnProperty, ae = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), oe = (e, t, n, r) => {
6
- if (t && typeof t == "object" || typeof t == "function") for (var i = re(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !x.call(e, s) && s !== n && b(e, s, {
5
+ var ae = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescriptor, oe = Object.getOwnPropertyNames, v = Object.getPrototypeOf, y = Object.prototype.hasOwnProperty, b = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), se = (e, t, n, r) => {
6
+ if (t && typeof t == "object" || typeof t == "function") for (var i = oe(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !y.call(e, s) && s !== n && g(e, s, {
7
7
  get: ((e) => t[e]).bind(null, s),
8
- enumerable: !(r = ne(t, s)) || r.enumerable
8
+ enumerable: !(r = _(t, s)) || r.enumerable
9
9
  });
10
10
  return e;
11
- }, S = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : y(ie(e)), oe(t || !e || !e.__esModule || !x.call(e, "default") ? b(n, "default", {
11
+ }, x = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : ae(v(e)), se(t || !e || !e.__esModule || !y.call(e, "default") ? g(n, "default", {
12
12
  value: e,
13
13
  enumerable: !0
14
- }) : n, e)))((/* @__PURE__ */ ae(((e, t) => {
14
+ }) : n, e)))((/* @__PURE__ */ b(((e, t) => {
15
15
  var n = Object.prototype.hasOwnProperty, r = "~";
16
16
  function i() {}
17
17
  Object.create && (i.prototype = Object.create(null), new i().__proto__ || (r = !1));
@@ -98,7 +98,7 @@ var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDesc
98
98
  var t;
99
99
  return e ? (t = r ? r + e : e, this._events[t] && s(this, t)) : (this._events = new i(), this._eventsCount = 0), this;
100
100
  }, c.prototype.off = c.prototype.removeListener, c.prototype.addListener = c.prototype.on, c.prefixed = r, c.EventEmitter = c, t !== void 0 && (t.exports = c);
101
- })))(), 1), C = class extends S.default {
101
+ })))(), 1), S = class extends x.default {
102
102
  world;
103
103
  name;
104
104
  currentDelta = 0;
@@ -174,13 +174,13 @@ var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDesc
174
174
  this.removeAllListeners();
175
175
  }
176
176
  };
177
- function w(e, t, n) {
177
+ function C(e, t, n) {
178
178
  let r = e.groups.map(t);
179
179
  for (let t = 0; t < e.entities.length; t++) r[e.groupIndexes[t]] && n(e.entities[t]);
180
180
  }
181
181
  //#endregion
182
182
  //#region src/systems/iterable-system.ts
183
- var T = class extends C {
183
+ var w = class extends S {
184
184
  remainingInstancesToRun = [];
185
185
  remainingInstancesStartTime = null;
186
186
  iterationsPerCheck;
@@ -230,7 +230,7 @@ var T = class extends C {
230
230
  }
231
231
  beforeRunIterables() {}
232
232
  getIterableEntityId(e) {}
233
- }, E = class e extends T {
233
+ }, T = class e extends w {
234
234
  entities = /* @__PURE__ */ new Map();
235
235
  options;
236
236
  constructor(e, t = { name: "EntitySystem" }) {
@@ -268,7 +268,7 @@ var T = class extends C {
268
268
  t.entities.forEach((e) => this.onEntityAdded(e));
269
269
  return;
270
270
  }
271
- w(t, (e) => this.hasComponents(e), (e) => {
271
+ C(t, (e) => this.hasComponents(e), (e) => {
272
272
  this.filterEntity(e) && (this.entities.set(e.eid, e), this.options.updateEntityOnAdd && this.updateEntity(e, 0));
273
273
  });
274
274
  }
@@ -300,9 +300,9 @@ var T = class extends C {
300
300
  ...this.reads ?? []
301
301
  ] : void 0;
302
302
  }
303
- }, D = class {
303
+ }, E = class {
304
304
  onmessage(e, t = []) {}
305
- }, O = class extends D {
305
+ }, D = class extends E {
306
306
  updateFunction;
307
307
  entities = new e();
308
308
  workState = new a();
@@ -461,7 +461,7 @@ var T = class extends C {
461
461
  onMessageTyped(e) {
462
462
  this.onmessage({ data: e });
463
463
  }
464
- }, k = "___main", A = class e extends C {
464
+ }, O = "___main", k = class e extends S {
465
465
  entities = /* @__PURE__ */ new Map();
466
466
  options;
467
467
  worker;
@@ -494,13 +494,13 @@ var T = class extends C {
494
494
  type: "grow-buffer",
495
495
  buffer: e
496
496
  });
497
- })) : (this.worker = new O(t.updateFunction, e, !!t.createsEntities, !!t.addsComponents), this.isWorkerThread = !1), this.initWorker();
497
+ })) : (this.worker = new D(t.updateFunction, e, !!t.createsEntities, !!t.addsComponents), this.isWorkerThread = !1), this.initWorker();
498
498
  }
499
499
  initWorker() {
500
500
  let e = (e) => {
501
501
  console.error(e, e.cause), this.initialized || (this.initError = e, this.initPromise?.reject(e));
502
502
  };
503
- if (!(this.worker instanceof O)) {
503
+ if (!(this.worker instanceof D)) {
504
504
  let t = (t) => {
505
505
  e(Error(`Worker ${this.name} failed to load`, { cause: t }));
506
506
  };
@@ -514,7 +514,7 @@ var T = class extends C {
514
514
  else if (n.type === "grow-buffer-from-worker") this.world.addGrownBuffer(n.buffer);
515
515
  else if (n.type === "run-complete") {
516
516
  if (n.generation !== this.generation) {
517
- this.world.scheduler.prepareEffects && this.discardWorkerAllocations(n);
517
+ this.world.scheduler.prepareEffects && this.discardWorkerAllocations(n), this.world.notifyAbandonedRunFinished(this);
518
518
  return;
519
519
  }
520
520
  let e = this.timingBatch && {
@@ -605,7 +605,7 @@ var T = class extends C {
605
605
  type: "load",
606
606
  data: this.options.getInitData?.(),
607
607
  gameTime: this.gameTime,
608
- entities: this.buildQueryDelta(k, this.options),
608
+ entities: this.buildQueryDelta(O, this.options),
609
609
  queries: n,
610
610
  heap: this.isWorkerThread ? this.world.heap.getSharedMemory() : void 0,
611
611
  sharedMemory: this.isWorkerThread ? this.world.getSharedComponentMemory() : void 0,
@@ -705,7 +705,7 @@ var T = class extends C {
705
705
  first: t.first,
706
706
  last: t.last
707
707
  }, this.isRunning = !0, this.completeEffects = this.world.scheduler.prepareEffects?.(this.world, this);
708
- let i = this.buildQueryDelta(k, this.options), a = {};
708
+ let i = this.buildQueryDelta(O, this.options), a = {};
709
709
  Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
710
710
  a[e] = this.buildQueryDelta(e, t);
711
711
  });
@@ -774,7 +774,7 @@ var T = class extends C {
774
774
  }
775
775
  checkAddEntity(e) {
776
776
  let t = this.tracksMainQuery && this.matchesQuery(e, this.options);
777
- return this.updateEntityList(k, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
777
+ return this.updateEntityList(O, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
778
778
  let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
779
779
  this.updateEntityList(t, r, e, this.matchesQuery(e, n));
780
780
  }), t;
@@ -784,19 +784,19 @@ var T = class extends C {
784
784
  t.entities.forEach((e) => this.checkAddEntity(e));
785
785
  return;
786
786
  }
787
- this.tracksMainQuery && this.addBatchToQuery(k, this.entities, this.options, t), Object.entries(this.options.queries ?? {}).forEach(([e, n]) => {
787
+ this.tracksMainQuery && this.addBatchToQuery(O, this.entities, this.options, t), Object.entries(this.options.queries ?? {}).forEach(([e, n]) => {
788
788
  let r = this.queryEntities[e] ?? (this.queryEntities[e] = /* @__PURE__ */ new Map());
789
789
  this.addBatchToQuery(e, r, n, t);
790
790
  });
791
791
  }
792
792
  addBatchToQuery(e, t, n, r) {
793
793
  let i = this.getQueryDelta(e);
794
- w(r, (e) => this.matchesComponents(e, n), (e) => {
794
+ C(r, (e) => this.matchesComponents(e, n), (e) => {
795
795
  e.components.entity.dead || n.filter && !n.filter(e) || (t.set(e.eid, e), this.markAdded(i, e));
796
796
  });
797
797
  }
798
798
  removeEntity(e) {
799
- this.updateEntityList(k, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
799
+ this.updateEntityList(O, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
800
800
  this.updateEntityList(t, n, e, !1);
801
801
  });
802
802
  }
@@ -811,7 +811,7 @@ var T = class extends C {
811
811
  destroy() {
812
812
  super.destroy(), this.pendingEffects?.discard(), "terminate" in this.worker && this.worker.terminate();
813
813
  }
814
- }, j = class extends S.default {
814
+ }, A = class extends x.default {
815
815
  eid;
816
816
  config;
817
817
  world;
@@ -912,7 +912,7 @@ var T = class extends C {
912
912
  assertComponentPropertyMutable(e, t) {
913
913
  if (e === "entity" && (t === "type" || t === "class")) throw Error(`Entity ${t} is immutable`);
914
914
  }
915
- }, M = class e {
915
+ }, j = class e {
916
916
  world;
917
917
  configs;
918
918
  classes;
@@ -929,7 +929,7 @@ var T = class extends C {
929
929
  this.world = e, this.forbiddenProperties.clear();
930
930
  for (let t of Object.keys(this.classes)) {
931
931
  let n = this.classes[t].entity;
932
- if (n !== j && !(n.prototype instanceof j)) throw Error(`Entity class ${t} must extend BaseEntity`);
932
+ if (n !== A && !(n.prototype instanceof A)) throw Error(`Entity class ${t} must extend BaseEntity`);
933
933
  for (let n of this.classes[t].components) if (n === "entity" || !(n in e.registry)) throw Error(`Entity class ${t} contains unknown or reserved component ${n}`);
934
934
  }
935
935
  for (let e of Object.keys(this.configs)) this.validateTemplate(e, this.configs[e]);
@@ -955,7 +955,7 @@ var T = class extends C {
955
955
  createAdoptedEntity(e, t, n) {
956
956
  if (!t) {
957
957
  if (this.hasClasses) throw Error(`Entity type ${e} has no class`);
958
- return new j(this.world, void 0, n);
958
+ return new A(this.world, void 0, n);
959
959
  }
960
960
  let r = this.classes[t];
961
961
  if (!r) throw Error(`Unknown entity class: ${t}`);
@@ -981,7 +981,7 @@ var T = class extends C {
981
981
  return e ? this.getAllowedComponents(e) : void 0;
982
982
  }
983
983
  createEntity(e) {
984
- return new j(this.world, e);
984
+ return new A(this.world, e);
985
985
  }
986
986
  get hasClasses() {
987
987
  return this.allowedComponents.size > 0;
@@ -1046,48 +1046,48 @@ var T = class extends C {
1046
1046
  }
1047
1047
  throw Error(`Entity type ${e} class ${t} loads a component outside its class`);
1048
1048
  }
1049
- }, N = 45, P = "!", F = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO", I = "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}", L = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
1050
- for (let e = 0; e < N; e++) L[F.charCodeAt(e)] = e, L[I.charCodeAt(e)] = e + N;
1051
- function R(e) {
1049
+ }, M = 45, N = "!", P = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO", F = "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}", I = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
1050
+ for (let e = 0; e < M; e++) I[P.charCodeAt(e)] = e, I[F.charCodeAt(e)] = e + M;
1051
+ function L(e) {
1052
1052
  let t = "";
1053
- for (; e >= N;) t += I[e % N], e = Math.floor(e / N);
1054
- return t + F[e];
1053
+ for (; e >= M;) t += F[e % M], e = Math.floor(e / M);
1054
+ return t + P[e];
1055
1055
  }
1056
- var z = /* @__PURE__ */ new Float32Array(1), B = new Uint32Array(z.buffer);
1057
- function se(e, t) {
1056
+ var R = /* @__PURE__ */ new Float32Array(1), z = new Uint32Array(R.buffer);
1057
+ function ce(e, t) {
1058
1058
  switch (e.type) {
1059
- case "boolean": return typeof t == "boolean" ? F[Number(t)] : void 0;
1060
- case "string": return t === null ? F[0] : typeof t == "string" && t.length ? R(t.length) + t : void 0;
1061
- case "float32": return typeof t != "number" || !Number.isFinite(t) || Math.fround(t) !== t ? void 0 : (z[0] = t, R(B[0]));
1059
+ case "boolean": return typeof t == "boolean" ? P[Number(t)] : void 0;
1060
+ case "string": return t === null ? P[0] : typeof t == "string" && t.length ? L(t.length) + t : void 0;
1061
+ case "float32": return typeof t != "number" || !Number.isFinite(t) || Math.fround(t) !== t ? void 0 : (R[0] = t, L(z[0]));
1062
1062
  default: {
1063
- let n = V(e, t);
1064
- return n === void 0 ? void 0 : R(n);
1063
+ let n = le(e, t);
1064
+ return n === void 0 ? void 0 : L(n);
1065
1065
  }
1066
1066
  }
1067
1067
  }
1068
- function V(e, t) {
1068
+ function le(e, t) {
1069
1069
  if (typeof t != "number") return;
1070
1070
  let n = Math.round((t - (e.offset ?? 0)) * 10 ** (e.decimals ?? 0) / (e.step ?? 1));
1071
- if (!Number.isSafeInteger(n) || H(e, n) !== t) return;
1071
+ if (!Number.isSafeInteger(n) || B(e, n) !== t) return;
1072
1072
  if (e.type === "uint") return n >= 0 ? n : void 0;
1073
1073
  let r = n >= 0 ? n * 2 : -n * 2 - 1;
1074
1074
  return Number.isSafeInteger(r) ? r : void 0;
1075
1075
  }
1076
- function H(e, t) {
1076
+ function B(e, t) {
1077
1077
  return t * (e.step ?? 1) / 10 ** (e.decimals ?? 0) + (e.offset ?? 0);
1078
1078
  }
1079
- function U(e, t) {
1079
+ function V(e, t) {
1080
1080
  switch (e.type) {
1081
1081
  case "boolean": return t !== 0;
1082
- case "float32": return B[0] = t, z[0];
1083
- case "uint": return H(e, t);
1084
- default: return H(e, t % 2 == 0 ? t / 2 : -(t + 1) / 2);
1082
+ case "float32": return z[0] = t, R[0];
1083
+ case "uint": return B(e, t);
1084
+ default: return B(e, t % 2 == 0 ? t / 2 : -(t + 1) / 2);
1085
1085
  }
1086
1086
  }
1087
- function W(e) {
1087
+ function H(e) {
1088
1088
  return e.types === void 0 ? [e.type] : e.types;
1089
1089
  }
1090
- var G = class {
1090
+ var U = class {
1091
1091
  encoders = /* @__PURE__ */ new Map();
1092
1092
  used = /* @__PURE__ */ new Set();
1093
1093
  constructor(e) {
@@ -1098,10 +1098,10 @@ var G = class {
1098
1098
  definition: n,
1099
1099
  indices: new Map(n.properties.map((e, t) => [e.name, t]))
1100
1100
  };
1101
- for (let r of W(n)) {
1101
+ for (let r of H(n)) {
1102
1102
  if (this.encoders.has(r)) throw Error(`Duplicate compressed entity definition for ${r}`);
1103
1103
  this.encoders.set(r, {
1104
- prefix: R(t++),
1104
+ prefix: L(t++),
1105
1105
  group: e
1106
1106
  });
1107
1107
  }
@@ -1129,25 +1129,25 @@ var G = class {
1129
1129
  if (r === "type" || i === void 0) continue;
1130
1130
  let t = e.group.indices.get(r);
1131
1131
  if (t === void 0) return;
1132
- let a = se(e.group.definition.properties[t], i);
1132
+ let a = ce(e.group.definition.properties[t], i);
1133
1133
  if (a === void 0) return;
1134
1134
  n[t] = a;
1135
1135
  }
1136
1136
  let r = e.prefix;
1137
- for (let e = 0; e < n.length; e++) r += n[e] ?? P;
1137
+ for (let e = 0; e < n.length; e++) r += n[e] ?? N;
1138
1138
  return r;
1139
1139
  }
1140
1140
  };
1141
- function ce(e, t) {
1142
- return new G(t).compressAll(e);
1141
+ function ue(e, t) {
1142
+ return new U(t).compressAll(e);
1143
1143
  }
1144
- var K = /* @__PURE__ */ new WeakMap();
1145
- function le(e) {
1146
- let t = K.get(e);
1144
+ var W = /* @__PURE__ */ new WeakMap();
1145
+ function de(e) {
1146
+ let t = W.get(e);
1147
1147
  if (t) return t;
1148
1148
  t = /* @__PURE__ */ new Map();
1149
1149
  for (let [n, r] of Object.entries(e)) {
1150
- let e = W(r);
1150
+ let e = H(r);
1151
1151
  for (let i = 0; i < e.length; i++) {
1152
1152
  if (t.has(Number(n) + i)) throw Error(`Compressed entity definitions overlap at key ${Number(n) + i}`);
1153
1153
  t.set(Number(n) + i, {
@@ -1156,23 +1156,23 @@ function le(e) {
1156
1156
  });
1157
1157
  }
1158
1158
  }
1159
- return K.set(e, t), t;
1159
+ return W.set(e, t), t;
1160
1160
  }
1161
- function q(e, t) {
1161
+ function G(e, t) {
1162
1162
  let n = 0, r = () => {
1163
- if (e[n] === P) {
1163
+ if (e[n] === N) {
1164
1164
  n++;
1165
1165
  return;
1166
1166
  }
1167
1167
  let t = 0, r = 1;
1168
1168
  for (; n < e.length;) {
1169
- let i = e.charCodeAt(n++), a = i < 128 ? L[i] : -1;
1169
+ let i = e.charCodeAt(n++), a = i < 128 ? I[i] : -1;
1170
1170
  if (a < 0) throw Error(`Invalid compressed entity ${e}`);
1171
- if (a < N) return t + a * r;
1172
- t += (a - N) * r, r *= N;
1171
+ if (a < M) return t + a * r;
1172
+ t += (a - M) * r, r *= M;
1173
1173
  }
1174
1174
  throw Error(`Truncated compressed entity ${e}`);
1175
- }, i = r(), a = i === void 0 ? void 0 : le(t).get(i);
1175
+ }, i = r(), a = i === void 0 ? void 0 : de(t).get(i);
1176
1176
  if (!a) throw Error(`No compressed entity definition for ${e}`);
1177
1177
  let o = { type: a.type };
1178
1178
  for (let t = 0; n < e.length; t++) {
@@ -1180,7 +1180,7 @@ function q(e, t) {
1180
1180
  if (!i) throw Error(`Compressed entity ${e} has more values than ${a.type} defines`);
1181
1181
  let s = r();
1182
1182
  if (s !== void 0) {
1183
- if (i.type !== "string") o[i.name] = U(i, s);
1183
+ if (i.type !== "string") o[i.name] = V(i, s);
1184
1184
  else if (s === 0) o[i.name] = null;
1185
1185
  else {
1186
1186
  if (n + s > e.length) throw Error(`Truncated compressed entity ${e}`);
@@ -1190,23 +1190,24 @@ function q(e, t) {
1190
1190
  }
1191
1191
  return o;
1192
1192
  }
1193
- function J(e, t) {
1194
- return e.map((e) => {
1195
- if (typeof e != "string") return e;
1196
- if (!t) throw Error("Compressed entities need entityDefinitions to load");
1197
- return q(e, t);
1198
- });
1193
+ function fe(e, t) {
1194
+ return e.map((e) => K(e, t));
1195
+ }
1196
+ function K(e, t) {
1197
+ if (typeof e != "string") return e;
1198
+ if (!t) throw Error("Compressed entities need entityDefinitions to load");
1199
+ return G(e, t);
1199
1200
  }
1200
1201
  //#endregion
1201
1202
  //#region src/scheduling/default-scheduler.ts
1202
- var Y = class {
1203
+ var q = class {
1203
1204
  update(e, t) {
1204
1205
  let n = null;
1205
1206
  return e.systems.forEach((r) => {
1206
1207
  let i = !0, a = !1, o = !1;
1207
1208
  e.emit(`system-${r.name}-started`);
1208
1209
  try {
1209
- i = r.shouldRun(), i ? a = r.update(t) : e.systemSkipped?.(r);
1210
+ i = r.shouldRun(), i && (a = r.update(t));
1210
1211
  } catch (t) {
1211
1212
  let i = t;
1212
1213
  console.error(i.message, i), o = !0, n = i, e.emit("system-error", {
@@ -1222,7 +1223,24 @@ var Y = class {
1222
1223
  });
1223
1224
  }), { lastSystemError: n };
1224
1225
  }
1225
- }, ue = v, X = 1e4, de = class extends S.default {
1226
+ }, pe = ie, J = 1e4, me = {
1227
+ entities: {
1228
+ start: 0,
1229
+ weight: .6
1230
+ },
1231
+ linking: {
1232
+ start: .6,
1233
+ weight: .1
1234
+ },
1235
+ systems: {
1236
+ start: .7,
1237
+ weight: .1
1238
+ },
1239
+ workers: {
1240
+ start: .8,
1241
+ weight: .2
1242
+ }
1243
+ }, Y = 256, he = class extends x.default {
1226
1244
  heap;
1227
1245
  constantStrings;
1228
1246
  eidCounter;
@@ -1238,6 +1256,8 @@ var Y = class {
1238
1256
  destroyed = !1;
1239
1257
  pristine = !0;
1240
1258
  isBulkLoading = !1;
1259
+ isLoading = !1;
1260
+ activeLoad;
1241
1261
  activeFreeBuffer = {
1242
1262
  frees: [],
1243
1263
  waitingSystems: /* @__PURE__ */ new Set(),
@@ -1250,7 +1270,7 @@ var Y = class {
1250
1270
  };
1251
1271
  clearing = !1;
1252
1272
  constructor(e, t = {}) {
1253
- super(), this.scheduler = t.scheduler ?? new Y(), this.heap = new te({ bufferSize: t.heapSize ?? ue }), this.constantStrings = new g(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
1273
+ super(), this.scheduler = t.scheduler ?? new q(), this.heap = new re({ bufferSize: t.heapSize ?? pe }), this.constantStrings = new m(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
1254
1274
  let n = {
1255
1275
  ...e,
1256
1276
  entity: i
@@ -1259,10 +1279,10 @@ var Y = class {
1259
1279
  let t = n[e];
1260
1280
  r[e] = {
1261
1281
  ...t,
1262
- memoryComponent: new _(this.heap, t.type, t.size)
1282
+ memoryComponent: new h(this.heap, t.type, t.size)
1263
1283
  };
1264
1284
  }
1265
- this.registry = r, this.factory = t.factory ?? new M(), this.factory.setWorld(this);
1285
+ this.registry = r, this.factory = t.factory ?? new j(), this.factory.setWorld(this);
1266
1286
  }
1267
1287
  allocateEid() {
1268
1288
  return Atomics.add(this.eidCounter.data, 0, 1) + 1;
@@ -1279,14 +1299,11 @@ var Y = class {
1279
1299
  this.heap.buffers[e.bufferPosition] === void 0 && this.heap.addSharedBuffer(e), this.emit("grow-buffer", e);
1280
1300
  }
1281
1301
  get needsSpareBuffer() {
1282
- return this.systems.some((e) => e instanceof A && e.isWorkerThread && (!!e.options.createsEntities || !!e.options.addsComponents));
1302
+ return this.systems.some((e) => e instanceof k && e.isWorkerThread && (!!e.options.createsEntities || !!e.options.addsComponents));
1283
1303
  }
1284
1304
  async init() {
1285
1305
  await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
1286
1306
  }
1287
- async finishLoadingSystems() {
1288
- await Promise.all(this.systems.map((e) => e.finishLoading()).filter((e) => e instanceof Promise));
1289
- }
1290
1307
  addEntity(e, t = !0) {
1291
1308
  return this.entities.set(e.eid, e), e.world = this, this.pristine = !1, e.on("component-added", (t) => {
1292
1309
  this.addEntityToEntityWorkerSystem(e, t);
@@ -1319,27 +1336,81 @@ var Y = class {
1319
1336
  throw r;
1320
1337
  }
1321
1338
  }
1322
- load(e) {
1323
- if (!this.pristine || this.scheduler.prepareEffects && this.systems.some((e) => e.isCurrentlyRunning())) {
1339
+ load(e, t = {}) {
1340
+ if (this.abortActiveLoad(), !this.pristine || this.scheduler.prepareEffects && this.systems.some((e) => e.isCurrentlyRunning())) {
1341
+ let e = this.systems.filter((e) => e.isCurrentlyRunning());
1324
1342
  for (let e of Array.from(this.entities.values())) this.removeEntity(e);
1325
- this.systems.forEach((e) => e.clear()), this.scheduler.reset?.(this), this.consolidateFreeBuffersForReload();
1343
+ this.systems.forEach((e) => e.clear()), this.scheduler.reset?.(this), this.consolidateFreeBuffersForReload(e);
1326
1344
  }
1327
1345
  for (let e of Object.keys(this.factory.configs)) this.constantStrings.getOrCreate(e);
1328
1346
  for (let e of Object.keys(this.factory.classes)) this.constantStrings.getOrCreate(e);
1347
+ this.isLoading = !0;
1348
+ let n = this.loadSteps(e);
1349
+ if (t.sliceMs === void 0) {
1350
+ let e;
1351
+ try {
1352
+ for (; !(e = n.next()).done;) t.onProgress?.(e.value);
1353
+ } finally {
1354
+ this.isLoading = !1;
1355
+ }
1356
+ return t.onEntitiesReady?.(), this.awaitWorkers(e.value, t.onProgress, t.signal);
1357
+ }
1358
+ let r = new AbortController();
1359
+ this.activeLoad = r;
1360
+ let i = t.signal ? AbortSignal.any([r.signal, t.signal]) : r.signal;
1361
+ return this.loadSliced(n, t.sliceMs, r, i, t);
1362
+ }
1363
+ async loadSliced(e, t, n, r, i) {
1364
+ let a = i.yieldTo ?? Z;
1365
+ try {
1366
+ r.throwIfAborted();
1367
+ let n = performance.now(), o = e.next();
1368
+ for (; !o.done;) i.onProgress?.(o.value), performance.now() - n >= t && (await a(), r.throwIfAborted(), n = performance.now()), o = e.next();
1369
+ this.isLoading = !1, i.onEntitiesReady?.(), await this.awaitWorkers(o.value, i.onProgress, r);
1370
+ } catch (e) {
1371
+ throw this.activeLoad === n && (this.activeLoad = void 0, this.isBulkLoading = !1, this.isLoading = !1), e;
1372
+ }
1373
+ this.activeLoad === n && (this.activeLoad = void 0);
1374
+ }
1375
+ *loadSteps(e) {
1376
+ let t = "entityDefinitions" in e ? e.entityDefinitions : void 0, n = e.entities, r = [];
1329
1377
  this.isBulkLoading = !0;
1330
1378
  try {
1331
- let t = ("entityDefinitions" in e ? J(e.entities, e.entityDefinitions) : e.entities).map((e) => this.loadEntity(e, !1));
1332
- for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
1333
- this.onEntityDied(e);
1334
- });
1379
+ for (let e = 0; e < n.length; e++) r.push(this.loadEntity(K(n[e], t), !1)), (e + 1) % Y === 0 && (yield X("entities", e + 1, n.length));
1380
+ yield X("entities", n.length, n.length);
1381
+ for (let e = 0; e < r.length; e++) {
1382
+ let t = r[e];
1383
+ t.finishLoading(), this.emit("entity-added", t), t.on("death", () => {
1384
+ this.onEntityDied(t);
1385
+ }), (e + 1) % Y === 0 && (yield X("linking", e + 1, r.length));
1386
+ }
1387
+ yield X("linking", r.length, r.length);
1335
1388
  } finally {
1336
1389
  this.isBulkLoading = !1;
1337
1390
  }
1338
- let t = fe(this.entities.values());
1339
- return this.systems.forEach((e) => e.addEntities(t)), this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1, this.finishLoadingSystems();
1391
+ let i = _e(this.entities.values()), a = this.systems.slice(), o = a.length * 2;
1392
+ for (let e = 0; e < a.length; e++) a[e].addEntities(i), yield X("systems", e + 1, o);
1393
+ this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
1394
+ let s = [];
1395
+ for (let e = 0; e < a.length; e++) {
1396
+ let t = a[e].finishLoading();
1397
+ t instanceof Promise && s.push(t), yield X("systems", a.length + e + 1, o);
1398
+ }
1399
+ return s;
1400
+ }
1401
+ async awaitWorkers(e, t, n) {
1402
+ let r = 0;
1403
+ t?.(X("workers", 0, e.length));
1404
+ let i = Promise.all(e.map(async (n) => {
1405
+ await n, t?.(X("workers", ++r, e.length));
1406
+ }));
1407
+ n ? await Promise.race([i, ge(n)]) : await i;
1408
+ }
1409
+ abortActiveLoad() {
1410
+ this.activeLoad && (this.activeLoad.abort(), this.activeLoad = void 0, this.isBulkLoading = !1, this.isLoading = !1);
1340
1411
  }
1341
1412
  async clear() {
1342
- if (!this.pristine || this.scheduler.beforeClear) {
1413
+ if (this.abortActiveLoad(), !this.pristine || this.scheduler.beforeClear) {
1343
1414
  this.clearing = !0;
1344
1415
  try {
1345
1416
  this.scheduler.beforeClear?.(this), await Promise.all(this.systems.map((e) => e.waitForRunToComplete()).filter((e) => e instanceof Promise));
@@ -1385,7 +1456,7 @@ var Y = class {
1385
1456
  notifySystemRunCompleted(e) {
1386
1457
  this.activeFreeBuffer.waitingSystems.delete(e) && this.activeFreeBuffer.waitingSystems.size === 0 && this.processFreeBuffers(), this.scheduler.runCompleted?.(this, e);
1387
1458
  }
1388
- systemSkipped(e) {
1459
+ notifyAbandonedRunFinished(e) {
1389
1460
  e.isCurrentlyRunning() || this.activeFreeBuffer.waitingSystems.delete(e) && this.activeFreeBuffer.waitingSystems.size === 0 && this.processFreeBuffers();
1390
1461
  }
1391
1462
  processFreeBuffers() {
@@ -1396,10 +1467,11 @@ var Y = class {
1396
1467
  this.activeFreeBuffer = this.nextFreeBuffer, this.nextFreeBuffer = e;
1397
1468
  let t = this.activeFreeBuffer;
1398
1469
  t.waitElapsed = 0, t.waitingSystems.clear();
1399
- for (let e of this.systems) (e.shouldRun() || e.isCurrentlyRunning()) && t.waitingSystems.add(e);
1470
+ for (let e of this.systems) e.isCurrentlyRunning() && t.waitingSystems.add(e);
1400
1471
  }
1401
- consolidateFreeBuffersForReload() {
1402
- this.activeFreeBuffer.frees.length > 0 && (this.nextFreeBuffer.frees.push(...this.activeFreeBuffer.frees), this.activeFreeBuffer.frees.length = 0), this.activeFreeBuffer.waitingSystems.clear(), this.activeFreeBuffer.waitElapsed = 0;
1472
+ consolidateFreeBuffersForReload(e) {
1473
+ let t = this.activeFreeBuffer;
1474
+ t.frees.push(...this.nextFreeBuffer.frees), this.nextFreeBuffer.frees.length = 0, t.waitingSystems.clear(), e.forEach((e) => t.waitingSystems.add(e)), t.waitElapsed = 0;
1403
1475
  }
1404
1476
  freeAllPendingBuffers() {
1405
1477
  this.performFrees(this.activeFreeBuffer), this.performFrees(this.nextFreeBuffer), this.activeFreeBuffer.waitingSystems.clear(), this.activeFreeBuffer.waitElapsed = 0, this.nextFreeBuffer.waitingSystems.clear(), this.nextFreeBuffer.waitElapsed = 0;
@@ -1410,9 +1482,9 @@ var Y = class {
1410
1482
  }
1411
1483
  checkFreeBufferTimeout(e) {
1412
1484
  let t = this.activeFreeBuffer;
1413
- if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= X)) {
1485
+ if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= J)) {
1414
1486
  let e = Array.from(t.waitingSystems).map((e) => e.name).join(", ");
1415
- console.warn(`shared-memory-ecs: deferred component free waited over ${X}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
1487
+ console.warn(`shared-memory-ecs: deferred component free waited over ${J}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
1416
1488
  }
1417
1489
  }
1418
1490
  addSystem(e) {
@@ -1429,6 +1501,7 @@ var Y = class {
1429
1501
  }
1430
1502
  }
1431
1503
  update(e) {
1504
+ if (this.isLoading) return {};
1432
1505
  this.emit("update-started", e);
1433
1506
  let t = this.runUpdate(e);
1434
1507
  return this.emit("update-finished", e), t;
@@ -1460,12 +1533,12 @@ var Y = class {
1460
1533
  }
1461
1534
  addEntityToEntityWorkerSystem(e, t) {
1462
1535
  this.isBulkLoading || this.systems.forEach((n) => {
1463
- (n instanceof E && n.options.components?.includes(t) || n instanceof A && this.queriesReferenceComponent(n.options, t)) && n.checkAddEntity(e);
1536
+ (n instanceof T && n.options.components?.includes(t) || n instanceof k && this.queriesReferenceComponent(n.options, t)) && n.checkAddEntity(e);
1464
1537
  });
1465
1538
  }
1466
1539
  removeEntityFromEntityWorkerSystem(e, t) {
1467
1540
  this.isBulkLoading || this.systems.forEach((n) => {
1468
- n instanceof E && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof A && this.queriesReferenceComponent(n.options, t) && n.checkAddEntity(e);
1541
+ n instanceof T && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof k && this.queriesReferenceComponent(n.options, t) && n.checkAddEntity(e);
1469
1542
  });
1470
1543
  }
1471
1544
  queriesReferenceComponent(e, t) {
@@ -1473,12 +1546,30 @@ var Y = class {
1473
1546
  return n || r;
1474
1547
  }
1475
1548
  destroy() {
1476
- this.destroyed ||= (this.systems.forEach((e) => {
1549
+ this.destroyed ||= (this.abortActiveLoad(), this.systems.forEach((e) => {
1477
1550
  e.destroy();
1478
1551
  }), this.scheduler.destroy?.(this), !0);
1479
1552
  }
1480
1553
  };
1481
- function fe(e) {
1554
+ function X(e, t, n) {
1555
+ let { start: r, weight: i } = me[e];
1556
+ return {
1557
+ phase: e,
1558
+ done: t,
1559
+ total: n,
1560
+ fraction: r + i * (n === 0 ? 1 : t / n)
1561
+ };
1562
+ }
1563
+ function Z() {
1564
+ let e = globalThis.scheduler;
1565
+ return e?.yield ? e.yield() : new Promise((e) => setTimeout(e, 0));
1566
+ }
1567
+ function ge(e) {
1568
+ return new Promise((t, n) => {
1569
+ e.aborted ? n(e.reason) : e.addEventListener("abort", () => n(e.reason), { once: !0 });
1570
+ });
1571
+ }
1572
+ function _e(e) {
1482
1573
  let t = {
1483
1574
  entities: [],
1484
1575
  groupIndexes: [],
@@ -1492,7 +1583,7 @@ function fe(e) {
1492
1583
  }
1493
1584
  //#endregion
1494
1585
  //#region src/scheduling/conflict-scheduler.ts
1495
- var pe = class {
1586
+ var ve = class {
1496
1587
  compiled = Object.freeze({
1497
1588
  order: Object.freeze([]),
1498
1589
  batches: Object.freeze([])
@@ -1575,7 +1666,7 @@ var pe = class {
1575
1666
  }, 0);
1576
1667
  }
1577
1668
  isSliced(e) {
1578
- return e.system instanceof T && e.system.remainingInstancesToRun.length > 0;
1669
+ return e.system instanceof w && e.system.remainingInstancesToRun.length > 0;
1579
1670
  }
1580
1671
  reset() {
1581
1672
  this.continuationTimer !== void 0 && (clearTimeout(this.continuationTimer), this.continuationTimer = void 0);
@@ -1588,10 +1679,14 @@ var pe = class {
1588
1679
  prepareEffects(e, t) {
1589
1680
  let n = this.running.get(t), r = this.generation;
1590
1681
  return (e) => {
1591
- if (!n || r !== this.generation || !this.nodes.has(n)) {
1682
+ if (r !== this.generation || n && !this.nodes.has(n)) {
1592
1683
  e.discard();
1593
1684
  return;
1594
1685
  }
1686
+ if (!n) {
1687
+ e.apply();
1688
+ return;
1689
+ }
1595
1690
  n.effects = e, this.publications.add(n), n.publicationDependencies === 0 && this.publicationReady.push(n), this.publish();
1596
1691
  };
1597
1692
  }
@@ -1707,7 +1802,7 @@ var pe = class {
1707
1802
  let i = this.generation, a = !0, o = !1, s = !1, c = null;
1708
1803
  if (e.emit(`system-${r.name}-started`), this.nodes.has(t) && this.dispatchAllowed(e)) {
1709
1804
  try {
1710
- if (!r.withGameTime(t.gameTime, () => (a = n || r.shouldRun(), !this.nodes.has(t) || !this.dispatchAllowed(e) ? !1 : (a ? (t.state = "running", this.running.set(r, t), o = r.update(n ? 0 : t.elapsedTime), i === this.generation && !r.isCurrentlyRunning() && this.complete(t)) : (this.complete(t), e.systemSkipped?.(r)), !0)))) return;
1805
+ if (!r.withGameTime(t.gameTime, () => (a = n || r.shouldRun(), !this.nodes.has(t) || !this.dispatchAllowed(e) ? !1 : (a ? (t.state = "running", this.running.set(r, t), o = r.update(n ? 0 : t.elapsedTime), i === this.generation && !r.isCurrentlyRunning() && this.complete(t)) : this.complete(t), !0)))) return;
1711
1806
  } catch (n) {
1712
1807
  let a = n;
1713
1808
  console.error(a.message, a), s = !0, c = a, i === this.generation && (r.isCurrentlyRunning() ? (t.state = "running", this.running.set(r, t)) : this.complete(t)), e.emit("system-error", {
@@ -1743,20 +1838,20 @@ var pe = class {
1743
1838
  };
1744
1839
  //#endregion
1745
1840
  //#region src/entity-class.ts
1746
- function me(e, t) {
1841
+ function ye(e, t) {
1747
1842
  return t;
1748
1843
  }
1749
- function Z(e, t) {
1844
+ function be(e, t) {
1750
1845
  return t;
1751
1846
  }
1752
1847
  //#endregion
1753
1848
  //#region src/actions/kill-entity.ts
1754
- function he(e) {
1849
+ function xe(e) {
1755
1850
  e.components.entity.dead = !0, e.emit("death");
1756
1851
  }
1757
1852
  //#endregion
1758
1853
  //#region src/performance-timing.ts
1759
- var ge = 1e3, Q = {
1854
+ var Se = 1e3, Q = {
1760
1855
  avg: 0,
1761
1856
  min: 0,
1762
1857
  max: 0,
@@ -1773,7 +1868,7 @@ function $(e) {
1773
1868
  samples: e.length
1774
1869
  };
1775
1870
  }
1776
- var _e = class extends S.default {
1871
+ var Ce = class extends x.default {
1777
1872
  world;
1778
1873
  ticksBetweenUpdates;
1779
1874
  stats = {
@@ -1858,7 +1953,7 @@ var _e = class extends S.default {
1858
1953
  e.run = [], e.events = [];
1859
1954
  }), this.emit("stats-updated", this.stats), this.stats;
1860
1955
  }
1861
- }, ve = class extends A {
1956
+ }, we = class extends k {
1862
1957
  constructor(e, t) {
1863
1958
  super(e, {
1864
1959
  ...t,
@@ -1874,6 +1969,6 @@ var _e = class extends S.default {
1874
1969
  }
1875
1970
  };
1876
1971
  //#endregion
1877
- export { j as BaseEntity, de as BaseWorld, n as CLASS_INDEX, f as Component, pe as ConflictScheduler, g as ConstantStringCache, c as DEAD_INDEX, ge as DEFAULT_TICKS_BETWEEN_UPDATES, Y as DefaultScheduler, G as EntityCompressor, M as EntityFactory, E as EntitySystem, O as EntitySystemWebWorker, A as EntityWorkerSystem, T as IterableSystem, _ as MemoryComponent, _e as PerformanceTiming, ee as STATIC_INDEX, C as System, r as TYPE_INDEX, D as WebWorker, ve as WorkerSystem, t as addComponentWorker, ce as compressEntities, p as createEntitySystemWorker, d as createEntityWorker, h as createSystemWorker, J as decompressEntities, q as decompressEntity, me as defineEntityClasses, Z as defineEntityConfigs, i as entityDefinition, he as killEntity, m as killEntityWorker, s as removeComponentWorker };
1972
+ export { A as BaseEntity, he as BaseWorld, n as CLASS_INDEX, f as Component, ve as ConflictScheduler, m as ConstantStringCache, c as DEAD_INDEX, Se as DEFAULT_TICKS_BETWEEN_UPDATES, q as DefaultScheduler, U as EntityCompressor, j as EntityFactory, T as EntitySystem, D as EntitySystemWebWorker, k as EntityWorkerSystem, w as IterableSystem, h as MemoryComponent, Ce as PerformanceTiming, ne as STATIC_INDEX, S as System, r as TYPE_INDEX, E as WebWorker, we as WorkerSystem, t as addComponentWorker, ue as compressEntities, p as createEntitySystemWorker, d as createEntityWorker, te as createSystemWorker, fe as decompressEntities, G as decompressEntity, ye as defineEntityClasses, be as defineEntityConfigs, i as entityDefinition, xe as killEntity, ee as killEntityWorker, s as removeComponentWorker };
1878
1973
 
1879
1974
  //# sourceMappingURL=index.js.map