@inglorious/store 11.0.3 → 11.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "11.0.3",
3
+ "version": "11.0.5",
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",
@@ -62,7 +62,7 @@
62
62
  "@inglorious/utils": "5.0.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@inglorious/eslint-config": "1.1.2",
65
+ "@inglorious/eslint-config": "1.1.4",
66
66
  "@reduxjs/toolkit": "^2.12.0",
67
67
  "prettier": "^3.9.6",
68
68
  "vite": "^8.2.2",
package/src/store.js CHANGED
@@ -332,7 +332,10 @@ export function createStore({
332
332
  }
333
333
 
334
334
  /**
335
- * Sets an augmented type configuration given its name.
335
+ * Sets an augmented type configuration given its name. Replacing a type, as
336
+ * hot module reloading does, notifies subscribers, since what its entities
337
+ * render may have changed. Adding a type does not: the store is unchanged,
338
+ * and a type can be added while rendering.
336
339
  * @param {string} typeName - The name of the type to set.
337
340
  * @param {Type} type - The type configuration.
338
341
  * @returns {void}
@@ -358,6 +361,10 @@ export function createStore({
358
361
  type: typeName,
359
362
  })
360
363
  }
364
+
365
+ if (oldType) {
366
+ listeners.forEach((onUpdate) => onUpdate())
367
+ }
361
368
  }
362
369
 
363
370
  /**
package/types/api.d.ts CHANGED
@@ -21,7 +21,11 @@ export interface Api<
21
21
  (): TState
22
22
  (typeName: string): TEntity[]
23
23
  }
24
- getEntity: (id: string) => TEntity | undefined
24
+ /** A known id gives that entity's own type; any other id, the union. */
25
+ getEntity: {
26
+ <TId extends keyof TState & string>(id: TId): TState[TId] | undefined
27
+ (id: string): TEntity | undefined
28
+ }
25
29
  select: <TResult>(selector: (state: TState) => TResult) => TResult
26
30
  dispatch: (event: Event) => void
27
31
  notify: (type: string, payload?: any) => void
package/types/store.d.ts CHANGED
@@ -25,9 +25,18 @@ export interface Event<T = any> {
25
25
  * off so that it stops.
26
26
  */
27
27
  export type EntityType<TEntity extends BaseEntity = BaseEntity> = {
28
- [K: string]: (entity: TEntity, payload: any, api: Api) => void
28
+ [K: string]: EventHandler<TEntity>
29
29
  }
30
30
 
31
+ /**
32
+ * A handler, checked as a method is: a type for a specific entity, such as
33
+ * `Api<AppEntity>`'s, then fits where `BaseEntity`'s is expected, so a component
34
+ * typed with the plain `Api` accepts an app's.
35
+ */
36
+ type EventHandler<TEntity extends BaseEntity> = {
37
+ bivarianceHack(entity: TEntity, payload: any, api: Api): void
38
+ }["bivarianceHack"]
39
+
31
40
  /**
32
41
  * System definition with event handlers
33
42
  */
@@ -78,8 +87,11 @@ export interface StoreConfig<
78
87
  TEntity extends BaseEntity = BaseEntity,
79
88
  TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
80
89
  > {
81
- types?: TypesConfig<TEntity>
82
- entities?: TState
90
+ // Types do not pick the entity type: their handlers accept any entity, so
91
+ // it comes from `entities`, `middlewares` or the explicit type arguments.
92
+ types?: TypesConfig<NoInfer<TEntity>>
93
+ /** The initial entities. With `autoCreateEntities`, those the store creates itself can be left out. */
94
+ entities?: Partial<TState>
83
95
  systems?: System<TState>[]
84
96
  middlewares?: Middleware<TEntity, TState>[]
85
97
  autoCreateEntities?: boolean
@@ -112,7 +124,10 @@ export interface Store<
112
124
  getType: (typeName: string) => EntityType<TEntity>
113
125
  setType: (typeName: string, type: EntityType<TEntity>) => void
114
126
  getState: () => TState
115
- getEntity: (id: string) => TState[string] | undefined
127
+ getEntity: {
128
+ <TId extends keyof TState & string>(id: TId): TState[TId] | undefined
129
+ (id: string): TState[string] | undefined
130
+ }
116
131
  setState: (nextState: TState) => void
117
132
  reset: () => void
118
133
  _api?: Api<TEntity, TState>
package/types/test.d.ts CHANGED
@@ -11,7 +11,10 @@ export interface MockApi<
11
11
  (): TState
12
12
  (typeName: string): TEntity[]
13
13
  }
14
- getEntity: (id: string) => TEntity | undefined
14
+ getEntity: {
15
+ <TId extends keyof TState & string>(id: TId): TState[TId] | undefined
16
+ (id: string): TEntity | undefined
17
+ }
15
18
  select: <TResult>(selector: (state: TState) => TResult) => TResult
16
19
  dispatch: (event: Event) => void
17
20
  notify: (type: string, payload?: any) => void