@dcl/ecs 7.27.1-34483684888.commit-8da580d → 7.27.1-34486417882.commit-c86ede9

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.
@@ -201,12 +201,16 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
201
201
  return data.has(entity);
202
202
  },
203
203
  deleteFrom(entity, markAsDirty = true) {
204
+ // Presence, not truthiness: a component defined from a primitive schema can
205
+ // legitimately hold 0, false or an empty string, and deleting one must still
206
+ // return the value that was there.
207
+ const had = data.has(entity);
204
208
  const component = data.get(entity);
205
209
  if (data.delete(entity) && markAsDirty) {
206
210
  dirtyIterator.add(entity);
207
211
  }
208
212
  lastSentData.delete(entity);
209
- return component || null;
213
+ return had ? component : null;
210
214
  },
211
215
  entityDeleted(entity, markAsDirty) {
212
216
  if (data.delete(entity) && markAsDirty) {
@@ -215,19 +219,20 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
215
219
  lastSentData.delete(entity);
216
220
  },
217
221
  getOrNull(entity) {
218
- const component = data.get(entity);
219
- return component ? deepReadonly(component) : null;
222
+ // Presence, not truthiness: a component defined from a primitive schema
223
+ // can legitimately hold 0, false or an empty string.
224
+ if (!data.has(entity))
225
+ return null;
226
+ return deepReadonly(data.get(entity));
220
227
  },
221
228
  get(entity) {
222
- const component = data.get(entity);
223
- if (!component) {
229
+ if (!data.has(entity)) {
224
230
  throw new Error(`[getFrom] Component ${componentName} for entity #${entity} not found`);
225
231
  }
226
- return deepReadonly(component);
232
+ return deepReadonly(data.get(entity));
227
233
  },
228
234
  create(entity, value) {
229
- const component = data.get(entity);
230
- if (component) {
235
+ if (data.has(entity)) {
231
236
  throw new Error(`[create] Component ${componentName} for ${entity} already exists`);
232
237
  }
233
238
  const usedValue = value === undefined ? schema.create() : schema.extend ? schema.extend(value) : value;
@@ -243,21 +248,22 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
243
248
  return usedValue;
244
249
  },
245
250
  getMutableOrNull(entity) {
246
- const component = data.get(entity);
247
- if (!component) {
251
+ // Presence, not truthiness, so a falsy primitive value is still mutable.
252
+ if (!data.has(entity)) {
248
253
  return null;
249
254
  }
250
255
  dirtyIterator.add(entity);
251
- return component;
256
+ return data.get(entity);
252
257
  },
253
258
  getOrCreateMutable(entity, value) {
254
- const component = data.get(entity);
255
- if (!component) {
259
+ // Presence, not truthiness: a stored 0/false/'' must not be treated as absent,
260
+ // which would send this through create() and throw "already exists".
261
+ if (!data.has(entity)) {
256
262
  return this.create(entity, value);
257
263
  }
258
264
  else {
259
265
  dirtyIterator.add(entity);
260
- return component;
266
+ return data.get(entity);
261
267
  }
262
268
  },
263
269
  getMutable(entity) {
@@ -16,7 +16,7 @@ export type DeepReadonlyObject<T> = {
16
16
  /**
17
17
  * @public
18
18
  */
19
- export type ReadonlyPrimitive = number | string | number[] | string[] | boolean | boolean[];
19
+ export type ReadonlyPrimitive = number | string | boolean;
20
20
  /**
21
21
  * @public
22
22
  */
@@ -2,5 +2,15 @@
2
2
  * @internal
3
3
  */
4
4
  export function deepReadonly(val) {
5
+ // A component can be defined straight from a primitive or array schema, not
6
+ // only from a Map, which is why this is no longer constrained to an object.
7
+ // Spreading those threw the value away: a number came back as {}, an array
8
+ // as an object keyed by index.
9
+ if (val === null || typeof val !== 'object') {
10
+ return val;
11
+ }
12
+ if (Array.isArray(val)) {
13
+ return Object.freeze([...val]);
14
+ }
5
15
  return Object.freeze({ ...val });
6
16
  }
@@ -208,12 +208,16 @@ function createComponentDefinitionFromSchema(componentName, componentId, schema)
208
208
  return data.has(entity);
209
209
  },
210
210
  deleteFrom(entity, markAsDirty = true) {
211
+ // Presence, not truthiness: a component defined from a primitive schema can
212
+ // legitimately hold 0, false or an empty string, and deleting one must still
213
+ // return the value that was there.
214
+ const had = data.has(entity);
211
215
  const component = data.get(entity);
212
216
  if (data.delete(entity) && markAsDirty) {
213
217
  dirtyIterator.add(entity);
214
218
  }
215
219
  lastSentData.delete(entity);
216
- return component || null;
220
+ return had ? component : null;
217
221
  },
218
222
  entityDeleted(entity, markAsDirty) {
219
223
  if (data.delete(entity) && markAsDirty) {
@@ -222,19 +226,20 @@ function createComponentDefinitionFromSchema(componentName, componentId, schema)
222
226
  lastSentData.delete(entity);
223
227
  },
224
228
  getOrNull(entity) {
225
- const component = data.get(entity);
226
- return component ? (0, readonly_1.deepReadonly)(component) : null;
229
+ // Presence, not truthiness: a component defined from a primitive schema
230
+ // can legitimately hold 0, false or an empty string.
231
+ if (!data.has(entity))
232
+ return null;
233
+ return (0, readonly_1.deepReadonly)(data.get(entity));
227
234
  },
228
235
  get(entity) {
229
- const component = data.get(entity);
230
- if (!component) {
236
+ if (!data.has(entity)) {
231
237
  throw new Error(`[getFrom] Component ${componentName} for entity #${entity} not found`);
232
238
  }
233
- return (0, readonly_1.deepReadonly)(component);
239
+ return (0, readonly_1.deepReadonly)(data.get(entity));
234
240
  },
235
241
  create(entity, value) {
236
- const component = data.get(entity);
237
- if (component) {
242
+ if (data.has(entity)) {
238
243
  throw new Error(`[create] Component ${componentName} for ${entity} already exists`);
239
244
  }
240
245
  const usedValue = value === undefined ? schema.create() : schema.extend ? schema.extend(value) : value;
@@ -250,21 +255,22 @@ function createComponentDefinitionFromSchema(componentName, componentId, schema)
250
255
  return usedValue;
251
256
  },
252
257
  getMutableOrNull(entity) {
253
- const component = data.get(entity);
254
- if (!component) {
258
+ // Presence, not truthiness, so a falsy primitive value is still mutable.
259
+ if (!data.has(entity)) {
255
260
  return null;
256
261
  }
257
262
  dirtyIterator.add(entity);
258
- return component;
263
+ return data.get(entity);
259
264
  },
260
265
  getOrCreateMutable(entity, value) {
261
- const component = data.get(entity);
262
- if (!component) {
266
+ // Presence, not truthiness: a stored 0/false/'' must not be treated as absent,
267
+ // which would send this through create() and throw "already exists".
268
+ if (!data.has(entity)) {
263
269
  return this.create(entity, value);
264
270
  }
265
271
  else {
266
272
  dirtyIterator.add(entity);
267
- return component;
273
+ return data.get(entity);
268
274
  }
269
275
  },
270
276
  getMutable(entity) {
@@ -16,7 +16,7 @@ export type DeepReadonlyObject<T> = {
16
16
  /**
17
17
  * @public
18
18
  */
19
- export type ReadonlyPrimitive = number | string | number[] | string[] | boolean | boolean[];
19
+ export type ReadonlyPrimitive = number | string | boolean;
20
20
  /**
21
21
  * @public
22
22
  */
@@ -5,6 +5,16 @@ exports.deepReadonly = void 0;
5
5
  * @internal
6
6
  */
7
7
  function deepReadonly(val) {
8
+ // A component can be defined straight from a primitive or array schema, not
9
+ // only from a Map, which is why this is no longer constrained to an object.
10
+ // Spreading those threw the value away: a number came back as {}, an array
11
+ // as an object keyed by index.
12
+ if (val === null || typeof val !== 'object') {
13
+ return val;
14
+ }
15
+ if (Array.isArray(val)) {
16
+ return Object.freeze([...val]);
17
+ }
8
18
  return Object.freeze({ ...val });
9
19
  }
10
20
  exports.deepReadonly = deepReadonly;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.27.1-34483684888.commit-8da580d",
4
+ "version": "7.27.1-34486417882.commit-c86ede9",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "files": [
@@ -34,5 +34,5 @@
34
34
  "dependencies": {},
35
35
  "types": "./dist/index.d.ts",
36
36
  "typings": "./dist/index.d.ts",
37
- "commit": "8da580d5df79697c08d248c377b805abe5cd3461"
37
+ "commit": "c86ede9e4aa95f86f3820b4d6862d865abbebc89"
38
38
  }