@latticexyz/recs 2.2.18-9fa07c8489f1fbf167d0db01cd9aaa645a29c8e2 → 2.2.18-c2ad22c7feb566e1731ff16e8be291746bdffb3e
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/chunk-2YR7WENB.js +2 -0
- package/dist/{chunk-A6GOEGXN.js.map → chunk-2YR7WENB.js.map} +1 -1
- package/dist/deprecated/index.js +1 -162
- package/dist/deprecated/index.js.map +1 -1
- package/dist/index.js +1 -411
- package/dist/index.js.map +1 -1
- package/package.json +5 -23
- package/dist/chunk-A6GOEGXN.js +0 -403
- package/dist/deprecated/index.cjs +0 -412
- package/dist/deprecated/index.cjs.map +0 -1
- package/dist/deprecated/index.d.cts +0 -51
- package/dist/index.cjs +0 -836
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -606
- package/dist/types-CAQycjNT.d.cts +0 -224
package/dist/index.cjs
DELETED
@@ -1,836 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __export = (target, all) => {
|
7
|
-
for (var name in all)
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
-
};
|
10
|
-
var __copyProps = (to, from3, except, desc) => {
|
11
|
-
if (from3 && typeof from3 === "object" || typeof from3 === "function") {
|
12
|
-
for (let key of __getOwnPropNames(from3))
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
-
__defProp(to, key, { get: () => from3[key], enumerable: !(desc = __getOwnPropDesc(from3, key)) || desc.enumerable });
|
15
|
-
}
|
16
|
-
return to;
|
17
|
-
};
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
-
|
20
|
-
// src/index.ts
|
21
|
-
var src_exports = {};
|
22
|
-
__export(src_exports, {
|
23
|
-
Has: () => Has,
|
24
|
-
HasValue: () => HasValue,
|
25
|
-
Not: () => Not,
|
26
|
-
NotValue: () => NotValue,
|
27
|
-
OptionalTypes: () => OptionalTypes,
|
28
|
-
ProxyExpand: () => ProxyExpand,
|
29
|
-
ProxyRead: () => ProxyRead,
|
30
|
-
QueryFragmentType: () => QueryFragmentType,
|
31
|
-
Type: () => Type,
|
32
|
-
UpdateType: () => UpdateType,
|
33
|
-
clearLocalCache: () => clearLocalCache,
|
34
|
-
componentValueEquals: () => componentValueEquals,
|
35
|
-
createEntity: () => createEntity,
|
36
|
-
createIndexer: () => createIndexer,
|
37
|
-
createLocalCache: () => createLocalCache,
|
38
|
-
createWorld: () => createWorld,
|
39
|
-
defineComponent: () => defineComponent,
|
40
|
-
defineComponentSystem: () => defineComponentSystem,
|
41
|
-
defineEnterQuery: () => defineEnterQuery,
|
42
|
-
defineEnterSystem: () => defineEnterSystem,
|
43
|
-
defineExitQuery: () => defineExitQuery,
|
44
|
-
defineExitSystem: () => defineExitSystem,
|
45
|
-
defineQuery: () => defineQuery,
|
46
|
-
defineRxSystem: () => defineRxSystem,
|
47
|
-
defineSyncSystem: () => defineSyncSystem,
|
48
|
-
defineSystem: () => defineSystem,
|
49
|
-
defineUpdateQuery: () => defineUpdateQuery,
|
50
|
-
defineUpdateSystem: () => defineUpdateSystem,
|
51
|
-
getChildEntities: () => getChildEntities,
|
52
|
-
getComponentEntities: () => getComponentEntities,
|
53
|
-
getComponentValue: () => getComponentValue,
|
54
|
-
getComponentValueStrict: () => getComponentValueStrict,
|
55
|
-
getEntitiesWithValue: () => getEntitiesWithValue,
|
56
|
-
getEntityComponents: () => getEntityComponents,
|
57
|
-
getEntityString: () => getEntityString,
|
58
|
-
getEntitySymbol: () => getEntitySymbol,
|
59
|
-
hasComponent: () => hasComponent,
|
60
|
-
isArrayType: () => isArrayType,
|
61
|
-
isComponentUpdate: () => isComponentUpdate,
|
62
|
-
isEntityType: () => isEntityType,
|
63
|
-
isFullComponentValue: () => isFullComponentValue,
|
64
|
-
isIndexer: () => isIndexer,
|
65
|
-
isNumberType: () => isNumberType,
|
66
|
-
isOptionalType: () => isOptionalType,
|
67
|
-
namespaceWorld: () => namespaceWorld,
|
68
|
-
overridableComponent: () => overridableComponent,
|
69
|
-
removeComponent: () => removeComponent,
|
70
|
-
runQuery: () => runQuery,
|
71
|
-
setComponent: () => setComponent,
|
72
|
-
toUpdate: () => toUpdate,
|
73
|
-
toUpdateStream: () => toUpdateStream,
|
74
|
-
updateComponent: () => updateComponent,
|
75
|
-
withValue: () => withValue
|
76
|
-
});
|
77
|
-
module.exports = __toCommonJS(src_exports);
|
78
|
-
|
79
|
-
// src/Component.ts
|
80
|
-
var import_utils = require("@latticexyz/utils");
|
81
|
-
var import_utils2 = require("@latticexyz/utils");
|
82
|
-
var import_rxjs2 = require("rxjs");
|
83
|
-
|
84
|
-
// src/constants.ts
|
85
|
-
var Type = /* @__PURE__ */ ((Type3) => {
|
86
|
-
Type3[Type3["Boolean"] = 0] = "Boolean";
|
87
|
-
Type3[Type3["Number"] = 1] = "Number";
|
88
|
-
Type3[Type3["OptionalNumber"] = 2] = "OptionalNumber";
|
89
|
-
Type3[Type3["BigInt"] = 3] = "BigInt";
|
90
|
-
Type3[Type3["OptionalBigInt"] = 4] = "OptionalBigInt";
|
91
|
-
Type3[Type3["String"] = 5] = "String";
|
92
|
-
Type3[Type3["OptionalString"] = 6] = "OptionalString";
|
93
|
-
Type3[Type3["NumberArray"] = 7] = "NumberArray";
|
94
|
-
Type3[Type3["OptionalNumberArray"] = 8] = "OptionalNumberArray";
|
95
|
-
Type3[Type3["BigIntArray"] = 9] = "BigIntArray";
|
96
|
-
Type3[Type3["OptionalBigIntArray"] = 10] = "OptionalBigIntArray";
|
97
|
-
Type3[Type3["StringArray"] = 11] = "StringArray";
|
98
|
-
Type3[Type3["OptionalStringArray"] = 12] = "OptionalStringArray";
|
99
|
-
Type3[Type3["Entity"] = 13] = "Entity";
|
100
|
-
Type3[Type3["OptionalEntity"] = 14] = "OptionalEntity";
|
101
|
-
Type3[Type3["EntityArray"] = 15] = "EntityArray";
|
102
|
-
Type3[Type3["OptionalEntityArray"] = 16] = "OptionalEntityArray";
|
103
|
-
Type3[Type3["T"] = 17] = "T";
|
104
|
-
Type3[Type3["OptionalT"] = 18] = "OptionalT";
|
105
|
-
return Type3;
|
106
|
-
})(Type || {});
|
107
|
-
var UpdateType = /* @__PURE__ */ ((UpdateType2) => {
|
108
|
-
UpdateType2[UpdateType2["Enter"] = 0] = "Enter";
|
109
|
-
UpdateType2[UpdateType2["Exit"] = 1] = "Exit";
|
110
|
-
UpdateType2[UpdateType2["Update"] = 2] = "Update";
|
111
|
-
UpdateType2[UpdateType2["Noop"] = 3] = "Noop";
|
112
|
-
return UpdateType2;
|
113
|
-
})(UpdateType || {});
|
114
|
-
var OptionalTypes = [
|
115
|
-
14 /* OptionalEntity */,
|
116
|
-
16 /* OptionalEntityArray */,
|
117
|
-
2 /* OptionalNumber */,
|
118
|
-
8 /* OptionalNumberArray */,
|
119
|
-
4 /* OptionalBigInt */,
|
120
|
-
10 /* OptionalBigIntArray */,
|
121
|
-
6 /* OptionalString */,
|
122
|
-
12 /* OptionalStringArray */,
|
123
|
-
18 /* OptionalT */
|
124
|
-
];
|
125
|
-
|
126
|
-
// src/Entity.ts
|
127
|
-
function createEntity(world, components, options) {
|
128
|
-
const entity = world.registerEntity(options ?? {});
|
129
|
-
if (components) {
|
130
|
-
for (const [component, value] of components) {
|
131
|
-
setComponent(component, entity, value);
|
132
|
-
}
|
133
|
-
}
|
134
|
-
return entity;
|
135
|
-
}
|
136
|
-
function getEntitySymbol(entityString) {
|
137
|
-
return Symbol.for(entityString);
|
138
|
-
}
|
139
|
-
function getEntityString(entity) {
|
140
|
-
return Symbol.keyFor(entity);
|
141
|
-
}
|
142
|
-
|
143
|
-
// src/Indexer.ts
|
144
|
-
function createIndexer(component) {
|
145
|
-
const valueToEntities = /* @__PURE__ */ new Map();
|
146
|
-
function getEntitiesWithValue2(value) {
|
147
|
-
const entities = valueToEntities.get(getValueKey(value));
|
148
|
-
return entities ? new Set([...entities].map(getEntityString)) : /* @__PURE__ */ new Set();
|
149
|
-
}
|
150
|
-
function getValueKey(value) {
|
151
|
-
return Object.values(value).join("/");
|
152
|
-
}
|
153
|
-
function add(entity, value) {
|
154
|
-
if (!value) return;
|
155
|
-
const valueKey = getValueKey(value);
|
156
|
-
let entitiesWithValue = valueToEntities.get(valueKey);
|
157
|
-
if (!entitiesWithValue) {
|
158
|
-
entitiesWithValue = /* @__PURE__ */ new Set();
|
159
|
-
valueToEntities.set(valueKey, entitiesWithValue);
|
160
|
-
}
|
161
|
-
entitiesWithValue.add(entity);
|
162
|
-
}
|
163
|
-
function remove(entity, value) {
|
164
|
-
if (!value) return;
|
165
|
-
const valueKey = getValueKey(value);
|
166
|
-
const entitiesWithValue = valueToEntities.get(valueKey);
|
167
|
-
if (!entitiesWithValue) return;
|
168
|
-
entitiesWithValue.delete(entity);
|
169
|
-
}
|
170
|
-
for (const entity of getComponentEntities(component)) {
|
171
|
-
const value = getComponentValue(component, entity);
|
172
|
-
add(getEntitySymbol(entity), value);
|
173
|
-
}
|
174
|
-
const subscription = component.update$.subscribe(({ entity, value }) => {
|
175
|
-
remove(getEntitySymbol(entity), value[1]);
|
176
|
-
add(getEntitySymbol(entity), value[0]);
|
177
|
-
});
|
178
|
-
component.world.registerDisposer(() => subscription?.unsubscribe());
|
179
|
-
return { ...component, getEntitiesWithValue: getEntitiesWithValue2 };
|
180
|
-
}
|
181
|
-
|
182
|
-
// src/utils.ts
|
183
|
-
var import_rxjs = require("rxjs");
|
184
|
-
function isComponentUpdate(update, component) {
|
185
|
-
return update.component === component;
|
186
|
-
}
|
187
|
-
function toUpdate(entity, component) {
|
188
|
-
const value = getComponentValue(component, entity);
|
189
|
-
return {
|
190
|
-
entity,
|
191
|
-
component,
|
192
|
-
value: [value, void 0],
|
193
|
-
type: value == null ? 3 /* Noop */ : 0 /* Enter */
|
194
|
-
};
|
195
|
-
}
|
196
|
-
function toUpdateStream(component) {
|
197
|
-
return (0, import_rxjs.pipe)((0, import_rxjs.map)((entity) => toUpdate(entity, component)));
|
198
|
-
}
|
199
|
-
function isIndexer(c) {
|
200
|
-
return "getEntitiesWithValue" in c;
|
201
|
-
}
|
202
|
-
function isFullComponentValue(component, value) {
|
203
|
-
return Object.keys(component.schema).every((key) => key in value);
|
204
|
-
}
|
205
|
-
|
206
|
-
// src/Component.ts
|
207
|
-
function getComponentName(component) {
|
208
|
-
return component.metadata?.componentName ?? component.metadata?.tableName ?? component.metadata?.tableId ?? component.metadata?.contractId ?? component.id;
|
209
|
-
}
|
210
|
-
function defineComponent(world, schema, options) {
|
211
|
-
if (Object.keys(schema).length === 0) throw new Error("Component schema must have at least one key");
|
212
|
-
const id = options?.id ?? (0, import_utils.uuid)();
|
213
|
-
const values = (0, import_utils2.mapObject)(schema, () => /* @__PURE__ */ new Map());
|
214
|
-
const update$ = new import_rxjs2.Subject();
|
215
|
-
const metadata = options?.metadata;
|
216
|
-
const entities = () => (0, import_utils.transformIterator)(Object.values(values)[0].keys(), getEntityString);
|
217
|
-
let component = { values, schema, id, update$, metadata, entities, world };
|
218
|
-
if (options?.indexed) component = createIndexer(component);
|
219
|
-
world.registerComponent(component);
|
220
|
-
return component;
|
221
|
-
}
|
222
|
-
function setComponent(component, entity, value, options = {}) {
|
223
|
-
const entitySymbol = getEntitySymbol(entity);
|
224
|
-
const prevValue = getComponentValue(component, entity);
|
225
|
-
for (const [key, val] of Object.entries(value)) {
|
226
|
-
if (component.values[key]) {
|
227
|
-
component.values[key].set(entitySymbol, val);
|
228
|
-
} else {
|
229
|
-
const isTableFieldIndex = component.metadata?.tableId && /^\d+$/.test(key);
|
230
|
-
if (!isTableFieldIndex) {
|
231
|
-
console.warn(
|
232
|
-
"Component definition for",
|
233
|
-
getComponentName(component),
|
234
|
-
"is missing key",
|
235
|
-
key,
|
236
|
-
", ignoring value",
|
237
|
-
val,
|
238
|
-
"for entity",
|
239
|
-
entity,
|
240
|
-
". Existing keys: ",
|
241
|
-
Object.keys(component.values)
|
242
|
-
);
|
243
|
-
}
|
244
|
-
}
|
245
|
-
}
|
246
|
-
if (!options.skipUpdateStream) {
|
247
|
-
component.update$.next({ entity, value: [value, prevValue], component });
|
248
|
-
}
|
249
|
-
}
|
250
|
-
function updateComponent(component, entity, value, initialValue, options = {}) {
|
251
|
-
const currentValue = getComponentValue(component, entity);
|
252
|
-
if (currentValue === void 0) {
|
253
|
-
if (initialValue === void 0) {
|
254
|
-
throw new Error(`Can't update component ${getComponentName(component)} without a current value or initial value`);
|
255
|
-
}
|
256
|
-
setComponent(component, entity, { ...initialValue, ...value }, options);
|
257
|
-
} else {
|
258
|
-
setComponent(component, entity, { ...currentValue, ...value }, options);
|
259
|
-
}
|
260
|
-
}
|
261
|
-
function removeComponent(component, entity, options = {}) {
|
262
|
-
const entitySymbol = getEntitySymbol(entity);
|
263
|
-
const prevValue = getComponentValue(component, entity);
|
264
|
-
for (const key of Object.keys(component.values)) {
|
265
|
-
component.values[key].delete(entitySymbol);
|
266
|
-
}
|
267
|
-
if (!options.skipUpdateStream) {
|
268
|
-
component.update$.next({ entity, value: [void 0, prevValue], component });
|
269
|
-
}
|
270
|
-
}
|
271
|
-
function hasComponent(component, entity) {
|
272
|
-
const entitySymbol = getEntitySymbol(entity);
|
273
|
-
const map4 = Object.values(component.values)[0];
|
274
|
-
return map4.has(entitySymbol);
|
275
|
-
}
|
276
|
-
function getComponentValue(component, entity) {
|
277
|
-
const value = {};
|
278
|
-
const entitySymbol = getEntitySymbol(entity);
|
279
|
-
const schemaKeys = Object.keys(component.schema);
|
280
|
-
for (const key of schemaKeys) {
|
281
|
-
const val = component.values[key].get(entitySymbol);
|
282
|
-
if (val === void 0 && !OptionalTypes.includes(component.schema[key])) return void 0;
|
283
|
-
value[key] = val;
|
284
|
-
}
|
285
|
-
return value;
|
286
|
-
}
|
287
|
-
function getComponentValueStrict(component, entity) {
|
288
|
-
const value = getComponentValue(component, entity);
|
289
|
-
if (!value) throw new Error(`No value for component ${getComponentName(component)} on entity ${entity}`);
|
290
|
-
return value;
|
291
|
-
}
|
292
|
-
function componentValueEquals(a, b) {
|
293
|
-
if (!a && !b) return true;
|
294
|
-
if (!a || !b) return false;
|
295
|
-
let equals = true;
|
296
|
-
for (const key of Object.keys(a)) {
|
297
|
-
equals = a[key] === b[key];
|
298
|
-
if (!equals) return false;
|
299
|
-
}
|
300
|
-
return equals;
|
301
|
-
}
|
302
|
-
function withValue(component, value) {
|
303
|
-
return [component, value];
|
304
|
-
}
|
305
|
-
function getEntitiesWithValue(component, value) {
|
306
|
-
if (isIndexer(component) && isFullComponentValue(component, value)) {
|
307
|
-
return component.getEntitiesWithValue(value);
|
308
|
-
}
|
309
|
-
const entities = /* @__PURE__ */ new Set();
|
310
|
-
for (const entity of getComponentEntities(component)) {
|
311
|
-
const val = getComponentValue(component, entity);
|
312
|
-
if (componentValueEquals(value, val)) {
|
313
|
-
entities.add(entity);
|
314
|
-
}
|
315
|
-
}
|
316
|
-
return entities;
|
317
|
-
}
|
318
|
-
function getComponentEntities(component) {
|
319
|
-
return component.entities();
|
320
|
-
}
|
321
|
-
function overridableComponent(component) {
|
322
|
-
let nonce = 0;
|
323
|
-
const overrides = /* @__PURE__ */ new Map();
|
324
|
-
const overriddenEntityValues = /* @__PURE__ */ new Map();
|
325
|
-
const update$ = new import_rxjs2.Subject();
|
326
|
-
function addOverride(id, update) {
|
327
|
-
overrides.set(id, { update, nonce: nonce++ });
|
328
|
-
setOverriddenComponentValue(update.entity, update.value);
|
329
|
-
}
|
330
|
-
function removeOverride(id) {
|
331
|
-
const affectedEntity = overrides.get(id)?.update.entity;
|
332
|
-
overrides.delete(id);
|
333
|
-
if (affectedEntity == null) return;
|
334
|
-
const relevantOverrides = [...overrides.values()].filter((o) => o.update.entity === affectedEntity).sort((a, b) => a.nonce < b.nonce ? -1 : 1);
|
335
|
-
if (relevantOverrides.length > 0) {
|
336
|
-
const lastOverride = relevantOverrides[relevantOverrides.length - 1];
|
337
|
-
setOverriddenComponentValue(affectedEntity, lastOverride.update.value);
|
338
|
-
} else {
|
339
|
-
setOverriddenComponentValue(affectedEntity, void 0);
|
340
|
-
}
|
341
|
-
}
|
342
|
-
function getOverriddenComponentValue(entity) {
|
343
|
-
const originalValue = getComponentValue(component, entity);
|
344
|
-
const entitySymbol = getEntitySymbol(entity);
|
345
|
-
const overriddenValue = overriddenEntityValues.get(entitySymbol);
|
346
|
-
return (originalValue || overriddenValue) && overriddenValue !== null ? { ...originalValue, ...overriddenValue } : void 0;
|
347
|
-
}
|
348
|
-
const valueProxyHandler = (key) => ({
|
349
|
-
get(target, prop) {
|
350
|
-
if (prop === "get") {
|
351
|
-
return (entity) => {
|
352
|
-
const originalValue = target.get(entity);
|
353
|
-
const overriddenValue = overriddenEntityValues.get(entity);
|
354
|
-
return overriddenValue && overriddenValue[key] != null ? overriddenValue[key] : originalValue;
|
355
|
-
};
|
356
|
-
}
|
357
|
-
if (prop === "has") {
|
358
|
-
return (entity) => {
|
359
|
-
return target.has(entity) || overriddenEntityValues.has(entity);
|
360
|
-
};
|
361
|
-
}
|
362
|
-
if (prop === "keys") {
|
363
|
-
return () => (/* @__PURE__ */ new Set([...target.keys(), ...overriddenEntityValues.keys()])).values();
|
364
|
-
}
|
365
|
-
return Reflect.get(target, prop, target);
|
366
|
-
}
|
367
|
-
});
|
368
|
-
const partialValues = {};
|
369
|
-
for (const key of Object.keys(component.values)) {
|
370
|
-
partialValues[key] = new Proxy(component.values[key], valueProxyHandler(key));
|
371
|
-
}
|
372
|
-
const valuesProxy = partialValues;
|
373
|
-
const overriddenComponent = new Proxy(component, {
|
374
|
-
get(target, prop) {
|
375
|
-
if (prop === "addOverride") return addOverride;
|
376
|
-
if (prop === "removeOverride") return removeOverride;
|
377
|
-
if (prop === "values") return valuesProxy;
|
378
|
-
if (prop === "update$") return update$;
|
379
|
-
if (prop === "entities")
|
380
|
-
return () => (/* @__PURE__ */ new Set([
|
381
|
-
...(0, import_utils.transformIterator)(overriddenEntityValues.keys(), getEntityString),
|
382
|
-
...target.entities()
|
383
|
-
])).values();
|
384
|
-
return Reflect.get(target, prop);
|
385
|
-
},
|
386
|
-
has(target, prop) {
|
387
|
-
if (prop === "addOverride" || prop === "removeOverride") return true;
|
388
|
-
return prop in target;
|
389
|
-
}
|
390
|
-
});
|
391
|
-
function setOverriddenComponentValue(entity, value) {
|
392
|
-
const entitySymbol = getEntitySymbol(entity);
|
393
|
-
const prevValue = getOverriddenComponentValue(entity);
|
394
|
-
if (value !== void 0) overriddenEntityValues.set(entitySymbol, value);
|
395
|
-
else overriddenEntityValues.delete(entitySymbol);
|
396
|
-
update$.next({ entity, value: [getOverriddenComponentValue(entity), prevValue], component: overriddenComponent });
|
397
|
-
}
|
398
|
-
component.update$.pipe(
|
399
|
-
(0, import_rxjs2.filter)((e) => !overriddenEntityValues.get(getEntitySymbol(e.entity))),
|
400
|
-
(0, import_rxjs2.map)((update) => ({ ...update, component: overriddenComponent }))
|
401
|
-
).subscribe(update$);
|
402
|
-
return overriddenComponent;
|
403
|
-
}
|
404
|
-
function getLocalCacheId(component, uniqueWorldIdentifier) {
|
405
|
-
return `localcache-${uniqueWorldIdentifier}-${component.id}`;
|
406
|
-
}
|
407
|
-
function clearLocalCache(component, uniqueWorldIdentifier) {
|
408
|
-
localStorage.removeItem(getLocalCacheId(component, uniqueWorldIdentifier));
|
409
|
-
}
|
410
|
-
function createLocalCache(component, uniqueWorldIdentifier) {
|
411
|
-
const { world, update$, values } = component;
|
412
|
-
const cacheId = getLocalCacheId(component, uniqueWorldIdentifier);
|
413
|
-
let numUpdates = 0;
|
414
|
-
const creation = Date.now();
|
415
|
-
const encodedCache = localStorage.getItem(cacheId);
|
416
|
-
if (encodedCache) {
|
417
|
-
const cache = JSON.parse(encodedCache);
|
418
|
-
const state = {};
|
419
|
-
for (const [key, values2] of cache) {
|
420
|
-
for (const [entity, value] of values2) {
|
421
|
-
state[entity] = state[entity] || {};
|
422
|
-
state[entity][key] = value;
|
423
|
-
}
|
424
|
-
}
|
425
|
-
for (const [entityId, value] of Object.entries(state)) {
|
426
|
-
const entity = world.registerEntity({ id: entityId });
|
427
|
-
setComponent(component, entity, value);
|
428
|
-
}
|
429
|
-
console.info("Loading component", getComponentName(component), "from local cache.");
|
430
|
-
}
|
431
|
-
const updateSub = update$.subscribe(() => {
|
432
|
-
numUpdates++;
|
433
|
-
const encoded = JSON.stringify(
|
434
|
-
Object.entries((0, import_utils2.mapObject)(values, (m) => [...m.entries()].map((e) => [getEntityString(e[0]), e[1]])))
|
435
|
-
);
|
436
|
-
localStorage.setItem(cacheId, encoded);
|
437
|
-
if (numUpdates > 200) {
|
438
|
-
console.warn(
|
439
|
-
"Component",
|
440
|
-
getComponentName(component),
|
441
|
-
"was locally cached",
|
442
|
-
numUpdates,
|
443
|
-
"times since",
|
444
|
-
new Date(creation).toLocaleTimeString(),
|
445
|
-
"- the local cache is in an alpha state and should not be used with components that update frequently yet"
|
446
|
-
);
|
447
|
-
}
|
448
|
-
});
|
449
|
-
component.world.registerDisposer(() => updateSub?.unsubscribe());
|
450
|
-
return component;
|
451
|
-
}
|
452
|
-
|
453
|
-
// src/System.ts
|
454
|
-
var import_rxjs4 = require("rxjs");
|
455
|
-
|
456
|
-
// src/Query.ts
|
457
|
-
var import_utils4 = require("@latticexyz/utils");
|
458
|
-
var import_mobx = require("mobx");
|
459
|
-
var import_rxjs3 = require("rxjs");
|
460
|
-
|
461
|
-
// src/types.ts
|
462
|
-
var QueryFragmentType = /* @__PURE__ */ ((QueryFragmentType2) => {
|
463
|
-
QueryFragmentType2[QueryFragmentType2["Has"] = 0] = "Has";
|
464
|
-
QueryFragmentType2[QueryFragmentType2["HasValue"] = 1] = "HasValue";
|
465
|
-
QueryFragmentType2[QueryFragmentType2["Not"] = 2] = "Not";
|
466
|
-
QueryFragmentType2[QueryFragmentType2["NotValue"] = 3] = "NotValue";
|
467
|
-
QueryFragmentType2[QueryFragmentType2["ProxyRead"] = 4] = "ProxyRead";
|
468
|
-
QueryFragmentType2[QueryFragmentType2["ProxyExpand"] = 5] = "ProxyExpand";
|
469
|
-
return QueryFragmentType2;
|
470
|
-
})(QueryFragmentType || {});
|
471
|
-
function isOptionalType(t) {
|
472
|
-
return [
|
473
|
-
2 /* OptionalNumber */,
|
474
|
-
4 /* OptionalBigInt */,
|
475
|
-
6 /* OptionalString */,
|
476
|
-
14 /* OptionalEntity */,
|
477
|
-
16 /* OptionalEntityArray */,
|
478
|
-
8 /* OptionalNumberArray */,
|
479
|
-
10 /* OptionalBigIntArray */,
|
480
|
-
12 /* OptionalStringArray */
|
481
|
-
].includes(t);
|
482
|
-
}
|
483
|
-
function isArrayType(t) {
|
484
|
-
return [
|
485
|
-
7 /* NumberArray */,
|
486
|
-
8 /* OptionalNumberArray */,
|
487
|
-
9 /* BigIntArray */,
|
488
|
-
10 /* OptionalBigIntArray */,
|
489
|
-
11 /* StringArray */,
|
490
|
-
12 /* OptionalStringArray */,
|
491
|
-
15 /* EntityArray */,
|
492
|
-
16 /* OptionalEntityArray */
|
493
|
-
].includes(t);
|
494
|
-
}
|
495
|
-
function isNumberType(t) {
|
496
|
-
return [1 /* Number */, 2 /* OptionalNumber */].includes(t);
|
497
|
-
}
|
498
|
-
function isEntityType(t) {
|
499
|
-
return [13 /* Entity */, 14 /* OptionalEntity */].includes(t);
|
500
|
-
}
|
501
|
-
|
502
|
-
// src/Query.ts
|
503
|
-
function Has(component) {
|
504
|
-
return { type: 0 /* Has */, component };
|
505
|
-
}
|
506
|
-
function Not(component) {
|
507
|
-
return { type: 2 /* Not */, component };
|
508
|
-
}
|
509
|
-
function HasValue(component, value) {
|
510
|
-
return { type: 1 /* HasValue */, component, value };
|
511
|
-
}
|
512
|
-
function NotValue(component, value) {
|
513
|
-
return { type: 3 /* NotValue */, component, value };
|
514
|
-
}
|
515
|
-
function ProxyRead(component, depth) {
|
516
|
-
return { type: 4 /* ProxyRead */, component, depth };
|
517
|
-
}
|
518
|
-
function ProxyExpand(component, depth) {
|
519
|
-
return { type: 5 /* ProxyExpand */, component, depth };
|
520
|
-
}
|
521
|
-
function passesQueryFragment(entity, fragment) {
|
522
|
-
if (fragment.type === 0 /* Has */) {
|
523
|
-
return hasComponent(fragment.component, entity);
|
524
|
-
}
|
525
|
-
if (fragment.type === 1 /* HasValue */) {
|
526
|
-
return componentValueEquals(fragment.value, getComponentValue(fragment.component, entity));
|
527
|
-
}
|
528
|
-
if (fragment.type === 2 /* Not */) {
|
529
|
-
return !hasComponent(fragment.component, entity);
|
530
|
-
}
|
531
|
-
if (fragment.type === 3 /* NotValue */) {
|
532
|
-
return !componentValueEquals(fragment.value, getComponentValue(fragment.component, entity));
|
533
|
-
}
|
534
|
-
throw new Error("Unknown query fragment");
|
535
|
-
}
|
536
|
-
function isPositiveFragment(fragment) {
|
537
|
-
return fragment.type === 0 /* Has */ || fragment.type == 1 /* HasValue */;
|
538
|
-
}
|
539
|
-
function isNegativeFragment(fragment) {
|
540
|
-
return fragment.type === 2 /* Not */ || fragment.type == 3 /* NotValue */;
|
541
|
-
}
|
542
|
-
function isSettingFragment(fragment) {
|
543
|
-
return fragment.type === 5 /* ProxyExpand */ || fragment.type == 4 /* ProxyRead */;
|
544
|
-
}
|
545
|
-
function isBreakingPassState(passes, fragment) {
|
546
|
-
return passes && isPositiveFragment(fragment) || !passes && isNegativeFragment(fragment);
|
547
|
-
}
|
548
|
-
function passesQueryFragmentProxy(entity, fragment, proxyRead) {
|
549
|
-
let proxyEntity = entity;
|
550
|
-
let passes = false;
|
551
|
-
for (let i = 0; i < proxyRead.depth; i++) {
|
552
|
-
const value = getComponentValue(proxyRead.component, proxyEntity);
|
553
|
-
if (!value) return null;
|
554
|
-
const entity2 = value.value;
|
555
|
-
if (!entity2) return null;
|
556
|
-
proxyEntity = entity2;
|
557
|
-
passes = passesQueryFragment(proxyEntity, fragment);
|
558
|
-
if (isBreakingPassState(passes, fragment)) {
|
559
|
-
return passes;
|
560
|
-
}
|
561
|
-
}
|
562
|
-
return passes;
|
563
|
-
}
|
564
|
-
function getChildEntities(entity, component, depth) {
|
565
|
-
if (depth === 0) return /* @__PURE__ */ new Set();
|
566
|
-
const directChildEntities = getEntitiesWithValue(component, { value: entity });
|
567
|
-
if (depth === 1) return directChildEntities;
|
568
|
-
const indirectChildEntities = [...directChildEntities].map((childEntity) => [...getChildEntities(childEntity, component, depth - 1)]).flat();
|
569
|
-
return /* @__PURE__ */ new Set([...directChildEntities, ...indirectChildEntities]);
|
570
|
-
}
|
571
|
-
function runQuery(fragments, initialSet) {
|
572
|
-
let entities = initialSet ? /* @__PURE__ */ new Set([...initialSet]) : void 0;
|
573
|
-
let proxyRead = void 0;
|
574
|
-
let proxyExpand = void 0;
|
575
|
-
for (let i = 0; i < fragments.length; i++) {
|
576
|
-
const fragment = fragments[i];
|
577
|
-
if (isSettingFragment(fragment)) {
|
578
|
-
if (fragment.type === 4 /* ProxyRead */) proxyRead = fragment;
|
579
|
-
if (fragment.type === 5 /* ProxyExpand */) proxyExpand = fragment;
|
580
|
-
} else if (!entities) {
|
581
|
-
if (isNegativeFragment(fragment)) {
|
582
|
-
throw new Error("First EntityQueryFragment must be Has or HasValue");
|
583
|
-
}
|
584
|
-
entities = fragment.type === 0 /* Has */ ? /* @__PURE__ */ new Set([...getComponentEntities(fragment.component)]) : getEntitiesWithValue(fragment.component, fragment.value);
|
585
|
-
if (proxyExpand && proxyExpand.depth > 0) {
|
586
|
-
for (const entity of [...entities]) {
|
587
|
-
for (const childEntity of getChildEntities(entity, proxyExpand.component, proxyExpand.depth)) {
|
588
|
-
entities.add(childEntity);
|
589
|
-
}
|
590
|
-
}
|
591
|
-
}
|
592
|
-
} else {
|
593
|
-
for (const entity of [...entities]) {
|
594
|
-
let passes = passesQueryFragment(entity, fragment);
|
595
|
-
if (proxyRead && proxyRead.depth > 0 && !isBreakingPassState(passes, fragment)) {
|
596
|
-
passes = passesQueryFragmentProxy(entity, fragment, proxyRead) ?? passes;
|
597
|
-
}
|
598
|
-
if (!passes) entities.delete(entity);
|
599
|
-
if (proxyExpand && proxyExpand.depth > 0) {
|
600
|
-
const childEntities = getChildEntities(entity, proxyExpand.component, proxyExpand.depth);
|
601
|
-
for (const childEntity of childEntities) {
|
602
|
-
if (passesQueryFragment(childEntity, fragment) || proxyRead && proxyRead.depth > 0 && passesQueryFragmentProxy(childEntity, fragment, proxyRead))
|
603
|
-
entities.add(childEntity);
|
604
|
-
}
|
605
|
-
}
|
606
|
-
}
|
607
|
-
}
|
608
|
-
}
|
609
|
-
return entities ?? /* @__PURE__ */ new Set();
|
610
|
-
}
|
611
|
-
function defineQuery(fragments, options) {
|
612
|
-
const initialSet = options?.runOnInit || options?.initialSet ? runQuery(fragments, options.initialSet) : /* @__PURE__ */ new Set();
|
613
|
-
const matching = (0, import_mobx.observable)(initialSet);
|
614
|
-
const initial$ = (0, import_rxjs3.from)(matching).pipe(toUpdateStream(fragments[0].component));
|
615
|
-
const containsProxy = fragments.findIndex((v) => [5 /* ProxyExpand */, 4 /* ProxyRead */].includes(v.type)) !== -1;
|
616
|
-
const internal$ = (0, import_rxjs3.merge)(...fragments.map((f) => f.component.update$)).pipe(
|
617
|
-
containsProxy ? (0, import_rxjs3.concatMap)((update) => {
|
618
|
-
const newMatchingSet = runQuery(fragments, options?.initialSet);
|
619
|
-
const updates = [];
|
620
|
-
for (const previouslyMatchingEntity of matching) {
|
621
|
-
if (!newMatchingSet.has(previouslyMatchingEntity)) {
|
622
|
-
matching.delete(previouslyMatchingEntity);
|
623
|
-
updates.push({
|
624
|
-
entity: previouslyMatchingEntity,
|
625
|
-
type: 1 /* Exit */,
|
626
|
-
component: update.component,
|
627
|
-
value: [void 0, void 0]
|
628
|
-
});
|
629
|
-
}
|
630
|
-
}
|
631
|
-
for (const matchingEntity of newMatchingSet) {
|
632
|
-
if (matching.has(matchingEntity)) {
|
633
|
-
updates.push({
|
634
|
-
entity: matchingEntity,
|
635
|
-
type: 2 /* Update */,
|
636
|
-
component: update.component,
|
637
|
-
value: [getComponentValue(update.component, matchingEntity), void 0]
|
638
|
-
});
|
639
|
-
} else {
|
640
|
-
matching.add(matchingEntity);
|
641
|
-
updates.push({
|
642
|
-
entity: matchingEntity,
|
643
|
-
type: 0 /* Enter */,
|
644
|
-
component: update.component,
|
645
|
-
value: [getComponentValue(update.component, matchingEntity), void 0]
|
646
|
-
});
|
647
|
-
}
|
648
|
-
}
|
649
|
-
return (0, import_rxjs3.of)(...updates);
|
650
|
-
}) : (
|
651
|
-
// Query does not contain proxies
|
652
|
-
(0, import_rxjs3.map)((update) => {
|
653
|
-
if (matching.has(update.entity)) {
|
654
|
-
const relevantFragments = fragments.filter((f) => f.component.id === update.component.id);
|
655
|
-
const pass2 = relevantFragments.every((f) => passesQueryFragment(update.entity, f));
|
656
|
-
if (pass2) {
|
657
|
-
return { ...update, type: 2 /* Update */ };
|
658
|
-
} else {
|
659
|
-
matching.delete(update.entity);
|
660
|
-
return { ...update, type: 1 /* Exit */ };
|
661
|
-
}
|
662
|
-
}
|
663
|
-
const pass = fragments.every((f) => passesQueryFragment(update.entity, f));
|
664
|
-
if (pass) {
|
665
|
-
matching.add(update.entity);
|
666
|
-
return { ...update, type: 0 /* Enter */ };
|
667
|
-
}
|
668
|
-
})
|
669
|
-
),
|
670
|
-
(0, import_utils4.filterNullish)()
|
671
|
-
);
|
672
|
-
return {
|
673
|
-
matching,
|
674
|
-
update$: (0, import_rxjs3.concat)(initial$, internal$).pipe((0, import_rxjs3.share)())
|
675
|
-
};
|
676
|
-
}
|
677
|
-
function defineUpdateQuery(fragments, options) {
|
678
|
-
return defineQuery(fragments, options).update$.pipe((0, import_rxjs3.filter)((e) => e.type === 2 /* Update */));
|
679
|
-
}
|
680
|
-
function defineEnterQuery(fragments, options) {
|
681
|
-
return defineQuery(fragments, options).update$.pipe((0, import_rxjs3.filter)((e) => e.type === 0 /* Enter */));
|
682
|
-
}
|
683
|
-
function defineExitQuery(fragments, options) {
|
684
|
-
return defineQuery(fragments, options).update$.pipe((0, import_rxjs3.filter)((e) => e.type === 1 /* Exit */));
|
685
|
-
}
|
686
|
-
|
687
|
-
// src/System.ts
|
688
|
-
function defineRxSystem(world, observable$, system) {
|
689
|
-
const subscription = observable$.subscribe(system);
|
690
|
-
world.registerDisposer(() => subscription?.unsubscribe());
|
691
|
-
}
|
692
|
-
function defineUpdateSystem(world, query, system, options = { runOnInit: true }) {
|
693
|
-
defineRxSystem(world, defineUpdateQuery(query, options), system);
|
694
|
-
}
|
695
|
-
function defineEnterSystem(world, query, system, options = { runOnInit: true }) {
|
696
|
-
defineRxSystem(world, defineEnterQuery(query, options), system);
|
697
|
-
}
|
698
|
-
function defineExitSystem(world, query, system, options = { runOnInit: true }) {
|
699
|
-
defineRxSystem(world, defineExitQuery(query, options), system);
|
700
|
-
}
|
701
|
-
function defineSystem(world, query, system, options = { runOnInit: true }) {
|
702
|
-
defineRxSystem(world, defineQuery(query, options).update$, system);
|
703
|
-
}
|
704
|
-
function defineComponentSystem(world, component, system, options = { runOnInit: true }) {
|
705
|
-
const initial$ = options?.runOnInit ? (0, import_rxjs4.from)(getComponentEntities(component)).pipe(toUpdateStream(component)) : import_rxjs4.EMPTY;
|
706
|
-
defineRxSystem(world, (0, import_rxjs4.concat)(initial$, component.update$), system);
|
707
|
-
}
|
708
|
-
function defineSyncSystem(world, query, component, value, options = { update: false, runOnInit: true }) {
|
709
|
-
defineSystem(
|
710
|
-
world,
|
711
|
-
query,
|
712
|
-
({ entity, type }) => {
|
713
|
-
if (type === 0 /* Enter */) setComponent(component(entity), entity, value(entity));
|
714
|
-
if (type === 1 /* Exit */) removeComponent(component(entity), entity);
|
715
|
-
if (options?.update && type === 2 /* Update */) setComponent(component(entity), entity, value(entity));
|
716
|
-
},
|
717
|
-
options
|
718
|
-
);
|
719
|
-
}
|
720
|
-
|
721
|
-
// src/World.ts
|
722
|
-
var import_utils7 = require("@latticexyz/utils");
|
723
|
-
function createWorld() {
|
724
|
-
const entitySymbols = /* @__PURE__ */ new Set();
|
725
|
-
const components = [];
|
726
|
-
let disposers = [];
|
727
|
-
function registerEntity({ id, idSuffix } = {}) {
|
728
|
-
const entity = id || entitySymbols.size + (idSuffix ? "-" + idSuffix : "");
|
729
|
-
const entitySymbol = getEntitySymbol(entity);
|
730
|
-
entitySymbols.add(entitySymbol);
|
731
|
-
return entity;
|
732
|
-
}
|
733
|
-
function getEntities() {
|
734
|
-
return (0, import_utils7.transformIterator)(entitySymbols.values(), getEntityString);
|
735
|
-
}
|
736
|
-
function registerComponent(component) {
|
737
|
-
components.push(component);
|
738
|
-
}
|
739
|
-
function dispose(namespace) {
|
740
|
-
for (const [, disposer] of disposers.filter((d) => !namespace || d[0] === namespace)) {
|
741
|
-
disposer();
|
742
|
-
}
|
743
|
-
disposers = disposers.filter((d) => namespace && d[0] !== namespace);
|
744
|
-
}
|
745
|
-
function registerDisposer(disposer, namespace = "") {
|
746
|
-
disposers.push([namespace, disposer]);
|
747
|
-
}
|
748
|
-
function hasEntity(entity) {
|
749
|
-
const entitySymbol = getEntitySymbol(entity);
|
750
|
-
return entitySymbols.has(entitySymbol);
|
751
|
-
}
|
752
|
-
function deleteEntity(entity) {
|
753
|
-
for (const component of components) {
|
754
|
-
if (hasComponent(component, entity)) removeComponent(component, entity);
|
755
|
-
}
|
756
|
-
entitySymbols.delete(getEntitySymbol(entity));
|
757
|
-
}
|
758
|
-
return {
|
759
|
-
registerEntity,
|
760
|
-
components,
|
761
|
-
registerComponent,
|
762
|
-
dispose,
|
763
|
-
registerDisposer,
|
764
|
-
hasEntity,
|
765
|
-
getEntities,
|
766
|
-
entitySymbols,
|
767
|
-
deleteEntity
|
768
|
-
};
|
769
|
-
}
|
770
|
-
function namespaceWorld(world, namespace) {
|
771
|
-
return {
|
772
|
-
...world,
|
773
|
-
registerDisposer: (disposer) => world.registerDisposer(disposer, namespace),
|
774
|
-
dispose: () => world.dispose(namespace)
|
775
|
-
};
|
776
|
-
}
|
777
|
-
function getEntityComponents(world, entity) {
|
778
|
-
return world.components.filter((component) => hasComponent(component, entity));
|
779
|
-
}
|
780
|
-
// Annotate the CommonJS export names for ESM import in node:
|
781
|
-
0 && (module.exports = {
|
782
|
-
Has,
|
783
|
-
HasValue,
|
784
|
-
Not,
|
785
|
-
NotValue,
|
786
|
-
OptionalTypes,
|
787
|
-
ProxyExpand,
|
788
|
-
ProxyRead,
|
789
|
-
QueryFragmentType,
|
790
|
-
Type,
|
791
|
-
UpdateType,
|
792
|
-
clearLocalCache,
|
793
|
-
componentValueEquals,
|
794
|
-
createEntity,
|
795
|
-
createIndexer,
|
796
|
-
createLocalCache,
|
797
|
-
createWorld,
|
798
|
-
defineComponent,
|
799
|
-
defineComponentSystem,
|
800
|
-
defineEnterQuery,
|
801
|
-
defineEnterSystem,
|
802
|
-
defineExitQuery,
|
803
|
-
defineExitSystem,
|
804
|
-
defineQuery,
|
805
|
-
defineRxSystem,
|
806
|
-
defineSyncSystem,
|
807
|
-
defineSystem,
|
808
|
-
defineUpdateQuery,
|
809
|
-
defineUpdateSystem,
|
810
|
-
getChildEntities,
|
811
|
-
getComponentEntities,
|
812
|
-
getComponentValue,
|
813
|
-
getComponentValueStrict,
|
814
|
-
getEntitiesWithValue,
|
815
|
-
getEntityComponents,
|
816
|
-
getEntityString,
|
817
|
-
getEntitySymbol,
|
818
|
-
hasComponent,
|
819
|
-
isArrayType,
|
820
|
-
isComponentUpdate,
|
821
|
-
isEntityType,
|
822
|
-
isFullComponentValue,
|
823
|
-
isIndexer,
|
824
|
-
isNumberType,
|
825
|
-
isOptionalType,
|
826
|
-
namespaceWorld,
|
827
|
-
overridableComponent,
|
828
|
-
removeComponent,
|
829
|
-
runQuery,
|
830
|
-
setComponent,
|
831
|
-
toUpdate,
|
832
|
-
toUpdateStream,
|
833
|
-
updateComponent,
|
834
|
-
withValue
|
835
|
-
});
|
836
|
-
//# sourceMappingURL=index.cjs.map
|