@latticexyz/recs 2.2.18-9fa07c8489f1fbf167d0db01cd9aaa645a29c8e2 → 2.2.18-c44207f620a38653497b78db0b71f5de7bc1a940

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.
@@ -1,403 +0,0 @@
1
- // src/constants.ts
2
- var Type = /* @__PURE__ */ ((Type2) => {
3
- Type2[Type2["Boolean"] = 0] = "Boolean";
4
- Type2[Type2["Number"] = 1] = "Number";
5
- Type2[Type2["OptionalNumber"] = 2] = "OptionalNumber";
6
- Type2[Type2["BigInt"] = 3] = "BigInt";
7
- Type2[Type2["OptionalBigInt"] = 4] = "OptionalBigInt";
8
- Type2[Type2["String"] = 5] = "String";
9
- Type2[Type2["OptionalString"] = 6] = "OptionalString";
10
- Type2[Type2["NumberArray"] = 7] = "NumberArray";
11
- Type2[Type2["OptionalNumberArray"] = 8] = "OptionalNumberArray";
12
- Type2[Type2["BigIntArray"] = 9] = "BigIntArray";
13
- Type2[Type2["OptionalBigIntArray"] = 10] = "OptionalBigIntArray";
14
- Type2[Type2["StringArray"] = 11] = "StringArray";
15
- Type2[Type2["OptionalStringArray"] = 12] = "OptionalStringArray";
16
- Type2[Type2["Entity"] = 13] = "Entity";
17
- Type2[Type2["OptionalEntity"] = 14] = "OptionalEntity";
18
- Type2[Type2["EntityArray"] = 15] = "EntityArray";
19
- Type2[Type2["OptionalEntityArray"] = 16] = "OptionalEntityArray";
20
- Type2[Type2["T"] = 17] = "T";
21
- Type2[Type2["OptionalT"] = 18] = "OptionalT";
22
- return Type2;
23
- })(Type || {});
24
- var UpdateType = /* @__PURE__ */ ((UpdateType2) => {
25
- UpdateType2[UpdateType2["Enter"] = 0] = "Enter";
26
- UpdateType2[UpdateType2["Exit"] = 1] = "Exit";
27
- UpdateType2[UpdateType2["Update"] = 2] = "Update";
28
- UpdateType2[UpdateType2["Noop"] = 3] = "Noop";
29
- return UpdateType2;
30
- })(UpdateType || {});
31
- var OptionalTypes = [
32
- 14 /* OptionalEntity */,
33
- 16 /* OptionalEntityArray */,
34
- 2 /* OptionalNumber */,
35
- 8 /* OptionalNumberArray */,
36
- 4 /* OptionalBigInt */,
37
- 10 /* OptionalBigIntArray */,
38
- 6 /* OptionalString */,
39
- 12 /* OptionalStringArray */,
40
- 18 /* OptionalT */
41
- ];
42
-
43
- // src/Component.ts
44
- import { transformIterator, uuid } from "@latticexyz/utils";
45
- import { mapObject } from "@latticexyz/utils";
46
- import { filter, map as map2, Subject } from "rxjs";
47
-
48
- // src/Indexer.ts
49
- function createIndexer(component) {
50
- const valueToEntities = /* @__PURE__ */ new Map();
51
- function getEntitiesWithValue2(value) {
52
- const entities = valueToEntities.get(getValueKey(value));
53
- return entities ? new Set([...entities].map(getEntityString)) : /* @__PURE__ */ new Set();
54
- }
55
- function getValueKey(value) {
56
- return Object.values(value).join("/");
57
- }
58
- function add(entity, value) {
59
- if (!value) return;
60
- const valueKey = getValueKey(value);
61
- let entitiesWithValue = valueToEntities.get(valueKey);
62
- if (!entitiesWithValue) {
63
- entitiesWithValue = /* @__PURE__ */ new Set();
64
- valueToEntities.set(valueKey, entitiesWithValue);
65
- }
66
- entitiesWithValue.add(entity);
67
- }
68
- function remove(entity, value) {
69
- if (!value) return;
70
- const valueKey = getValueKey(value);
71
- const entitiesWithValue = valueToEntities.get(valueKey);
72
- if (!entitiesWithValue) return;
73
- entitiesWithValue.delete(entity);
74
- }
75
- for (const entity of getComponentEntities(component)) {
76
- const value = getComponentValue(component, entity);
77
- add(getEntitySymbol(entity), value);
78
- }
79
- const subscription = component.update$.subscribe(({ entity, value }) => {
80
- remove(getEntitySymbol(entity), value[1]);
81
- add(getEntitySymbol(entity), value[0]);
82
- });
83
- component.world.registerDisposer(() => subscription?.unsubscribe());
84
- return { ...component, getEntitiesWithValue: getEntitiesWithValue2 };
85
- }
86
-
87
- // src/utils.ts
88
- import { map, pipe } from "rxjs";
89
- function isComponentUpdate(update, component) {
90
- return update.component === component;
91
- }
92
- function toUpdate(entity, component) {
93
- const value = getComponentValue(component, entity);
94
- return {
95
- entity,
96
- component,
97
- value: [value, void 0],
98
- type: value == null ? 3 /* Noop */ : 0 /* Enter */
99
- };
100
- }
101
- function toUpdateStream(component) {
102
- return pipe(map((entity) => toUpdate(entity, component)));
103
- }
104
- function isIndexer(c) {
105
- return "getEntitiesWithValue" in c;
106
- }
107
- function isFullComponentValue(component, value) {
108
- return Object.keys(component.schema).every((key) => key in value);
109
- }
110
-
111
- // src/Component.ts
112
- function getComponentName(component) {
113
- return component.metadata?.componentName ?? component.metadata?.tableName ?? component.metadata?.tableId ?? component.metadata?.contractId ?? component.id;
114
- }
115
- function defineComponent(world, schema, options) {
116
- if (Object.keys(schema).length === 0) throw new Error("Component schema must have at least one key");
117
- const id = options?.id ?? uuid();
118
- const values = mapObject(schema, () => /* @__PURE__ */ new Map());
119
- const update$ = new Subject();
120
- const metadata = options?.metadata;
121
- const entities = () => transformIterator(Object.values(values)[0].keys(), getEntityString);
122
- let component = { values, schema, id, update$, metadata, entities, world };
123
- if (options?.indexed) component = createIndexer(component);
124
- world.registerComponent(component);
125
- return component;
126
- }
127
- function setComponent(component, entity, value, options = {}) {
128
- const entitySymbol = getEntitySymbol(entity);
129
- const prevValue = getComponentValue(component, entity);
130
- for (const [key, val] of Object.entries(value)) {
131
- if (component.values[key]) {
132
- component.values[key].set(entitySymbol, val);
133
- } else {
134
- const isTableFieldIndex = component.metadata?.tableId && /^\d+$/.test(key);
135
- if (!isTableFieldIndex) {
136
- console.warn(
137
- "Component definition for",
138
- getComponentName(component),
139
- "is missing key",
140
- key,
141
- ", ignoring value",
142
- val,
143
- "for entity",
144
- entity,
145
- ". Existing keys: ",
146
- Object.keys(component.values)
147
- );
148
- }
149
- }
150
- }
151
- if (!options.skipUpdateStream) {
152
- component.update$.next({ entity, value: [value, prevValue], component });
153
- }
154
- }
155
- function updateComponent(component, entity, value, initialValue, options = {}) {
156
- const currentValue = getComponentValue(component, entity);
157
- if (currentValue === void 0) {
158
- if (initialValue === void 0) {
159
- throw new Error(`Can't update component ${getComponentName(component)} without a current value or initial value`);
160
- }
161
- setComponent(component, entity, { ...initialValue, ...value }, options);
162
- } else {
163
- setComponent(component, entity, { ...currentValue, ...value }, options);
164
- }
165
- }
166
- function removeComponent(component, entity, options = {}) {
167
- const entitySymbol = getEntitySymbol(entity);
168
- const prevValue = getComponentValue(component, entity);
169
- for (const key of Object.keys(component.values)) {
170
- component.values[key].delete(entitySymbol);
171
- }
172
- if (!options.skipUpdateStream) {
173
- component.update$.next({ entity, value: [void 0, prevValue], component });
174
- }
175
- }
176
- function hasComponent(component, entity) {
177
- const entitySymbol = getEntitySymbol(entity);
178
- const map3 = Object.values(component.values)[0];
179
- return map3.has(entitySymbol);
180
- }
181
- function getComponentValue(component, entity) {
182
- const value = {};
183
- const entitySymbol = getEntitySymbol(entity);
184
- const schemaKeys = Object.keys(component.schema);
185
- for (const key of schemaKeys) {
186
- const val = component.values[key].get(entitySymbol);
187
- if (val === void 0 && !OptionalTypes.includes(component.schema[key])) return void 0;
188
- value[key] = val;
189
- }
190
- return value;
191
- }
192
- function getComponentValueStrict(component, entity) {
193
- const value = getComponentValue(component, entity);
194
- if (!value) throw new Error(`No value for component ${getComponentName(component)} on entity ${entity}`);
195
- return value;
196
- }
197
- function componentValueEquals(a, b) {
198
- if (!a && !b) return true;
199
- if (!a || !b) return false;
200
- let equals = true;
201
- for (const key of Object.keys(a)) {
202
- equals = a[key] === b[key];
203
- if (!equals) return false;
204
- }
205
- return equals;
206
- }
207
- function withValue(component, value) {
208
- return [component, value];
209
- }
210
- function getEntitiesWithValue(component, value) {
211
- if (isIndexer(component) && isFullComponentValue(component, value)) {
212
- return component.getEntitiesWithValue(value);
213
- }
214
- const entities = /* @__PURE__ */ new Set();
215
- for (const entity of getComponentEntities(component)) {
216
- const val = getComponentValue(component, entity);
217
- if (componentValueEquals(value, val)) {
218
- entities.add(entity);
219
- }
220
- }
221
- return entities;
222
- }
223
- function getComponentEntities(component) {
224
- return component.entities();
225
- }
226
- function overridableComponent(component) {
227
- let nonce = 0;
228
- const overrides = /* @__PURE__ */ new Map();
229
- const overriddenEntityValues = /* @__PURE__ */ new Map();
230
- const update$ = new Subject();
231
- function addOverride(id, update) {
232
- overrides.set(id, { update, nonce: nonce++ });
233
- setOverriddenComponentValue(update.entity, update.value);
234
- }
235
- function removeOverride(id) {
236
- const affectedEntity = overrides.get(id)?.update.entity;
237
- overrides.delete(id);
238
- if (affectedEntity == null) return;
239
- const relevantOverrides = [...overrides.values()].filter((o) => o.update.entity === affectedEntity).sort((a, b) => a.nonce < b.nonce ? -1 : 1);
240
- if (relevantOverrides.length > 0) {
241
- const lastOverride = relevantOverrides[relevantOverrides.length - 1];
242
- setOverriddenComponentValue(affectedEntity, lastOverride.update.value);
243
- } else {
244
- setOverriddenComponentValue(affectedEntity, void 0);
245
- }
246
- }
247
- function getOverriddenComponentValue(entity) {
248
- const originalValue = getComponentValue(component, entity);
249
- const entitySymbol = getEntitySymbol(entity);
250
- const overriddenValue = overriddenEntityValues.get(entitySymbol);
251
- return (originalValue || overriddenValue) && overriddenValue !== null ? { ...originalValue, ...overriddenValue } : void 0;
252
- }
253
- const valueProxyHandler = (key) => ({
254
- get(target, prop) {
255
- if (prop === "get") {
256
- return (entity) => {
257
- const originalValue = target.get(entity);
258
- const overriddenValue = overriddenEntityValues.get(entity);
259
- return overriddenValue && overriddenValue[key] != null ? overriddenValue[key] : originalValue;
260
- };
261
- }
262
- if (prop === "has") {
263
- return (entity) => {
264
- return target.has(entity) || overriddenEntityValues.has(entity);
265
- };
266
- }
267
- if (prop === "keys") {
268
- return () => (/* @__PURE__ */ new Set([...target.keys(), ...overriddenEntityValues.keys()])).values();
269
- }
270
- return Reflect.get(target, prop, target);
271
- }
272
- });
273
- const partialValues = {};
274
- for (const key of Object.keys(component.values)) {
275
- partialValues[key] = new Proxy(component.values[key], valueProxyHandler(key));
276
- }
277
- const valuesProxy = partialValues;
278
- const overriddenComponent = new Proxy(component, {
279
- get(target, prop) {
280
- if (prop === "addOverride") return addOverride;
281
- if (prop === "removeOverride") return removeOverride;
282
- if (prop === "values") return valuesProxy;
283
- if (prop === "update$") return update$;
284
- if (prop === "entities")
285
- return () => (/* @__PURE__ */ new Set([
286
- ...transformIterator(overriddenEntityValues.keys(), getEntityString),
287
- ...target.entities()
288
- ])).values();
289
- return Reflect.get(target, prop);
290
- },
291
- has(target, prop) {
292
- if (prop === "addOverride" || prop === "removeOverride") return true;
293
- return prop in target;
294
- }
295
- });
296
- function setOverriddenComponentValue(entity, value) {
297
- const entitySymbol = getEntitySymbol(entity);
298
- const prevValue = getOverriddenComponentValue(entity);
299
- if (value !== void 0) overriddenEntityValues.set(entitySymbol, value);
300
- else overriddenEntityValues.delete(entitySymbol);
301
- update$.next({ entity, value: [getOverriddenComponentValue(entity), prevValue], component: overriddenComponent });
302
- }
303
- component.update$.pipe(
304
- filter((e) => !overriddenEntityValues.get(getEntitySymbol(e.entity))),
305
- map2((update) => ({ ...update, component: overriddenComponent }))
306
- ).subscribe(update$);
307
- return overriddenComponent;
308
- }
309
- function getLocalCacheId(component, uniqueWorldIdentifier) {
310
- return `localcache-${uniqueWorldIdentifier}-${component.id}`;
311
- }
312
- function clearLocalCache(component, uniqueWorldIdentifier) {
313
- localStorage.removeItem(getLocalCacheId(component, uniqueWorldIdentifier));
314
- }
315
- function createLocalCache(component, uniqueWorldIdentifier) {
316
- const { world, update$, values } = component;
317
- const cacheId = getLocalCacheId(component, uniqueWorldIdentifier);
318
- let numUpdates = 0;
319
- const creation = Date.now();
320
- const encodedCache = localStorage.getItem(cacheId);
321
- if (encodedCache) {
322
- const cache = JSON.parse(encodedCache);
323
- const state = {};
324
- for (const [key, values2] of cache) {
325
- for (const [entity, value] of values2) {
326
- state[entity] = state[entity] || {};
327
- state[entity][key] = value;
328
- }
329
- }
330
- for (const [entityId, value] of Object.entries(state)) {
331
- const entity = world.registerEntity({ id: entityId });
332
- setComponent(component, entity, value);
333
- }
334
- console.info("Loading component", getComponentName(component), "from local cache.");
335
- }
336
- const updateSub = update$.subscribe(() => {
337
- numUpdates++;
338
- const encoded = JSON.stringify(
339
- Object.entries(mapObject(values, (m) => [...m.entries()].map((e) => [getEntityString(e[0]), e[1]])))
340
- );
341
- localStorage.setItem(cacheId, encoded);
342
- if (numUpdates > 200) {
343
- console.warn(
344
- "Component",
345
- getComponentName(component),
346
- "was locally cached",
347
- numUpdates,
348
- "times since",
349
- new Date(creation).toLocaleTimeString(),
350
- "- the local cache is in an alpha state and should not be used with components that update frequently yet"
351
- );
352
- }
353
- });
354
- component.world.registerDisposer(() => updateSub?.unsubscribe());
355
- return component;
356
- }
357
-
358
- // src/Entity.ts
359
- function createEntity(world, components, options) {
360
- const entity = world.registerEntity(options ?? {});
361
- if (components) {
362
- for (const [component, value] of components) {
363
- setComponent(component, entity, value);
364
- }
365
- }
366
- return entity;
367
- }
368
- function getEntitySymbol(entityString) {
369
- return Symbol.for(entityString);
370
- }
371
- function getEntityString(entity) {
372
- return Symbol.keyFor(entity);
373
- }
374
-
375
- export {
376
- Type,
377
- UpdateType,
378
- OptionalTypes,
379
- createEntity,
380
- getEntitySymbol,
381
- getEntityString,
382
- createIndexer,
383
- isComponentUpdate,
384
- toUpdate,
385
- toUpdateStream,
386
- isIndexer,
387
- isFullComponentValue,
388
- defineComponent,
389
- setComponent,
390
- updateComponent,
391
- removeComponent,
392
- hasComponent,
393
- getComponentValue,
394
- getComponentValueStrict,
395
- componentValueEquals,
396
- withValue,
397
- getEntitiesWithValue,
398
- getComponentEntities,
399
- overridableComponent,
400
- clearLocalCache,
401
- createLocalCache
402
- };
403
- //# sourceMappingURL=chunk-A6GOEGXN.js.map