@inglorious/store 12.8.0 → 12.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/store.js +12 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "12.8.0",
3
+ "version": "12.9.0",
4
4
  "description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
package/src/store.js CHANGED
@@ -56,7 +56,7 @@ const LIFECYCLE_EVENTS = ["create", "destroy"]
56
56
  * @param {boolean} [config.autoCreateEntities=false] - Creates an entity for each type that has none.
57
57
  * @param {"auto" | "manual"} [config.updateMode="auto"] - Whether each event triggers an update, or updates are batched until `update()` is called.
58
58
  * @param {UpdateStrategy} [config.updateStrategy="structural-sharing"] - How an event's changes are copied.
59
- * @param {boolean} [config.devMode=false] - Under structural sharing, freezes the state, so a write to an entity a handler was not given throws an error that says so.
59
+ * @param {boolean} [config.devMode=false] - Freezes the state, so a write to an entity a handler was not given, or one after an await, throws an error that says so.
60
60
  * @returns {Store} The store with methods to interact with state and events.
61
61
  * @throws {TypeError} If `updateStrategy` is not a supported strategy.
62
62
  */
@@ -146,6 +146,8 @@ export function createStore({
146
146
  const draft = copyOnDispatch(state)
147
147
  drain(draft)
148
148
  state = draft.entities
149
+ // Without a proxy, a late write would land on the state unseen.
150
+ if (devMode) draft.copied.forEach((id) => deepFreeze(state[id]))
149
151
  }
150
152
  } catch (error) {
151
153
  // The update is rolled back whole, and the events still queued with it.
@@ -426,7 +428,7 @@ export function createStore({
426
428
  }
427
429
  }
428
430
 
429
- if (devMode && updateStrategy === STRUCTURAL_SHARING) deepFreeze(state)
431
+ if (devMode) deepFreeze(state)
430
432
  }
431
433
 
432
434
  /**
@@ -681,6 +683,7 @@ function writeThrough(entities) {
681
683
  function copyOnDispatch(state) {
682
684
  const entities = { ...state }
683
685
  const before = new Map()
686
+ const copied = new Set()
684
687
  let all = null
685
688
 
686
689
  function remember(id) {
@@ -691,6 +694,7 @@ function copyOnDispatch(state) {
691
694
  if (!before.has(id) && Object.hasOwn(entities, id)) {
692
695
  before.set(id, entities[id])
693
696
  entities[id] = clone(entities[id])
697
+ copied.add(id)
694
698
  }
695
699
 
696
700
  return entities[id]
@@ -699,6 +703,7 @@ function copyOnDispatch(state) {
699
703
  function put(id, entity) {
700
704
  remember(id)
701
705
  entities[id] = entity
706
+ copied.add(id)
702
707
  }
703
708
 
704
709
  function remove(id) {
@@ -727,6 +732,7 @@ function copyOnDispatch(state) {
727
732
  return {
728
733
  entities,
729
734
  forSystems,
735
+ copied,
730
736
  take,
731
737
  put,
732
738
  remove,
@@ -794,9 +800,10 @@ function explainLateAccess(result, typeName, handlerName) {
794
800
  if (typeof result?.then !== "function") return
795
801
 
796
802
  result.then(undefined, (error) => {
797
- if (!(error instanceof TypeError) || !REVOKED.test(error.message)) {
798
- throw error
799
- }
803
+ const isLate =
804
+ error instanceof TypeError &&
805
+ (REVOKED.test(error.message) || FROZEN.test(error.message))
806
+ if (!isLate) throw error
800
807
 
801
808
  throw new TypeError(
802
809
  `${typeName}.${handlerName} accessed its entity after an async operation, when the update it belonged to had ended. Read what you need from the entity into a variable before the first await, or notify a new event after it and let that event's handler change the entity.`,