@inglorious/store 9.5.3 → 9.6.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.
- package/package.json +1 -1
- package/src/api.js +15 -3
- package/src/test.js +9 -2
- package/types/api.d.ts +4 -1
- package/types/test.d.ts +4 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.6.0",
|
|
4
4
|
"description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
package/src/api.js
CHANGED
|
@@ -19,10 +19,22 @@ export function createApi(store, extras) {
|
|
|
19
19
|
*/
|
|
20
20
|
setType: store.setType,
|
|
21
21
|
/**
|
|
22
|
-
* Retrieves
|
|
23
|
-
*
|
|
22
|
+
* Retrieves entities.
|
|
23
|
+
* If `typeName` is omitted, returns the full entities state object.
|
|
24
|
+
* If `typeName` is provided, returns an array of entities of that type.
|
|
25
|
+
* @param {string} [typeName]
|
|
26
|
+
* @returns {Object|Object[]}
|
|
24
27
|
*/
|
|
25
|
-
getEntities:
|
|
28
|
+
getEntities: (typeName) => {
|
|
29
|
+
const entities = store.getState()
|
|
30
|
+
if (typeName == null) {
|
|
31
|
+
return entities
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return Object.values(entities).filter(
|
|
35
|
+
(entity) => entity.type === typeName,
|
|
36
|
+
)
|
|
37
|
+
},
|
|
26
38
|
/**
|
|
27
39
|
* Retrieves a single entity by ID.
|
|
28
40
|
* @param {string} id
|
package/src/test.js
CHANGED
|
@@ -12,6 +12,7 @@ import { create } from "mutative"
|
|
|
12
12
|
*
|
|
13
13
|
* @returns {Object} A mock API object with methods:
|
|
14
14
|
* - `getEntities()`: Returns all entities (frozen)
|
|
15
|
+
* - `getEntities(typeName)`: Returns entities matching that type (frozen array)
|
|
15
16
|
* - `getEntity(id)`: Returns a specific entity by ID (frozen)
|
|
16
17
|
* - `select(selector)`: Runs a selector against the entities
|
|
17
18
|
* - `dispatch(event)`: Records an event (for assertions)
|
|
@@ -37,8 +38,14 @@ export function createMockApi(entities) {
|
|
|
37
38
|
const events = []
|
|
38
39
|
|
|
39
40
|
return {
|
|
40
|
-
getEntities() {
|
|
41
|
-
|
|
41
|
+
getEntities(typeName) {
|
|
42
|
+
if (typeName == null) {
|
|
43
|
+
return frozenEntities
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return Object.values(frozenEntities).filter(
|
|
47
|
+
(entity) => entity.type === typeName,
|
|
48
|
+
)
|
|
42
49
|
},
|
|
43
50
|
getEntity(id) {
|
|
44
51
|
return frozenEntities[id]
|
package/types/api.d.ts
CHANGED
|
@@ -17,7 +17,10 @@ export interface Api<
|
|
|
17
17
|
getTypes: () => TypesConfig<TEntity>
|
|
18
18
|
getType: (typeName: string) => EntityType<TEntity>
|
|
19
19
|
setType: (typeName: string, type: EntityType<TEntity>) => void
|
|
20
|
-
getEntities:
|
|
20
|
+
getEntities: {
|
|
21
|
+
(): TState
|
|
22
|
+
(typeName: string): TEntity[]
|
|
23
|
+
}
|
|
21
24
|
getEntity: (id: string) => TEntity | undefined
|
|
22
25
|
select: <TResult>(selector: (state: TState) => TResult) => TResult
|
|
23
26
|
dispatch: (event: Event) => void
|
package/types/test.d.ts
CHANGED
|
@@ -7,7 +7,10 @@ export interface MockApi<
|
|
|
7
7
|
TEntity extends BaseEntity = BaseEntity,
|
|
8
8
|
TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
|
|
9
9
|
> {
|
|
10
|
-
getEntities:
|
|
10
|
+
getEntities: {
|
|
11
|
+
(): TState
|
|
12
|
+
(typeName: string): TEntity[]
|
|
13
|
+
}
|
|
11
14
|
getEntity: (id: string) => TEntity | undefined
|
|
12
15
|
select: <TResult>(selector: (state: TState) => TResult) => TResult
|
|
13
16
|
dispatch: (event: Event) => void
|