use-entity 0.0.1-alpha → 0.1.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/README.md +95 -6
- package/dist/chunk-3DHT5C4J.cjs +38 -0
- package/dist/chunk-3DHT5C4J.cjs.map +1 -0
- package/dist/chunk-UANHY5CW.js +35 -0
- package/dist/chunk-UANHY5CW.js.map +1 -0
- package/dist/index.cjs +28 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/selectors-vvbtAjSu.d.cts +45 -0
- package/dist/selectors-vvbtAjSu.d.ts +45 -0
- package/dist/tanstack.cjs +28 -0
- package/dist/tanstack.cjs.map +1 -0
- package/dist/tanstack.d.cts +23 -0
- package/dist/tanstack.d.ts +23 -0
- package/dist/tanstack.js +25 -0
- package/dist/tanstack.js.map +1 -0
- package/package.json +48 -6
- package/.github/workflows/release-please.yml +0 -52
- package/CLAUDE.md +0 -111
- package/biome.json +0 -34
- package/bun.lock +0 -180
- package/bunfig.toml +0 -2
- package/happydom.ts +0 -3
- package/index.ts +0 -1
- package/lefthook.yml +0 -5
- package/matchers.d.ts +0 -7
- package/src/__tests__/useEntity.test.ts +0 -271
- package/src/types.ts +0 -48
- package/src/uncheckedindexed.ts +0 -16
- package/src/useEntity.ts +0 -128
- package/testing-library.ts +0 -10
- package/tsconfig.json +0 -29
package/README.md
CHANGED
|
@@ -1,15 +1,104 @@
|
|
|
1
1
|
# use-entity
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fast, typed entity state for React with minimal boilerplate. It gives you
|
|
4
|
+
consistent CRUD operations for normalized collections, plus a ready-to-use
|
|
5
|
+
hook and selector helpers that stay portable across state managers. Powered by Redux Toolkit's
|
|
6
|
+
[`createEntityAdapter`](https://redux-toolkit.js.org/api/createEntityAdapter).
|
|
7
|
+
|
|
8
|
+
Why it’s useful:
|
|
9
|
+
- `useState`-like API for collections with CRUD actions.
|
|
10
|
+
- CRUD-first API for collections (add, update, remove, upsert) with strong typing.
|
|
11
|
+
- TanStack Store integration (see [docs](./tanstack-store-readme.md)).
|
|
12
|
+
- Normalized data with built-in selectors (`all`, `ids`, `entities`, `byId`, `total`).
|
|
13
|
+
|
|
14
|
+
## Install
|
|
4
15
|
|
|
5
16
|
```bash
|
|
6
|
-
|
|
17
|
+
npm install use-entity
|
|
7
18
|
```
|
|
8
19
|
|
|
9
|
-
|
|
20
|
+
## Quick Start (React useState)
|
|
10
21
|
|
|
11
|
-
```
|
|
12
|
-
|
|
22
|
+
```tsx
|
|
23
|
+
import { useStateEntity } from "use-entity";
|
|
24
|
+
|
|
25
|
+
type User = { id: string; name: string; age: number };
|
|
26
|
+
|
|
27
|
+
export function Users() {
|
|
28
|
+
const [users, actions] = useStateEntity<User>();
|
|
29
|
+
|
|
30
|
+
const addUser = () =>
|
|
31
|
+
actions.addOne({ id: String(Date.now()), name: `User ${users.length + 1}`, age: 20 });
|
|
32
|
+
|
|
33
|
+
const birthday = (user: User) =>
|
|
34
|
+
actions.updateOne({ id: user.id, changes: { age: user.age + 1 } });
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<button onClick={addUser}>Add user</button>
|
|
39
|
+
<ul>
|
|
40
|
+
{users.map((user) => (
|
|
41
|
+
<li key={user.id}>
|
|
42
|
+
<span>
|
|
43
|
+
{user.name} ({user.age})
|
|
44
|
+
</span>
|
|
45
|
+
<button onClick={() => birthday(user)}>+1 age</button>
|
|
46
|
+
<button onClick={() => actions.removeOne(user.id)}>Remove</button>
|
|
47
|
+
</li>
|
|
48
|
+
))}
|
|
49
|
+
</ul>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Actions
|
|
57
|
+
|
|
58
|
+
All actions mirror Redux Toolkit's entity adapter API
|
|
59
|
+
([docs](https://redux-toolkit.js.org/api/createEntityAdapter#crud-functions)):
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
addOne, addMany,
|
|
63
|
+
setOne, setMany, setAll,
|
|
64
|
+
removeOne, removeMany, removeAll,
|
|
65
|
+
updateOne, updateMany,
|
|
66
|
+
upsertOne, upsertMany
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Selectors
|
|
70
|
+
|
|
71
|
+
`useEntity()` and `useStateEntity()` default to the `"all"` selector, which returns the array of items. You can
|
|
72
|
+
opt into other selectors (including the full selector object) and access `byId`. (Based on [rtk docs](https://redux-toolkit.js.org/api/createEntityAdapter#selector-functions))
|
|
73
|
+
|
|
74
|
+
#### Selector options:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
all: T[];
|
|
78
|
+
ids: string[];
|
|
79
|
+
entities: Record<string, T>;
|
|
80
|
+
total: number;
|
|
81
|
+
full: {
|
|
82
|
+
all: T[];
|
|
83
|
+
ids: string[];
|
|
84
|
+
entities: Record<string, T>;
|
|
85
|
+
total: number;
|
|
86
|
+
byId: (id: string) => T | undefined;
|
|
87
|
+
};
|
|
13
88
|
```
|
|
14
89
|
|
|
15
|
-
|
|
90
|
+
#### Usage:
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
const [all] = useStateEntity<User>([], "all");
|
|
94
|
+
const [ids] = useStateEntity<User>([], "ids");
|
|
95
|
+
const [entities] = useStateEntity<User>([], "entities");
|
|
96
|
+
const [total] = useStateEntity<User>([], "total");
|
|
97
|
+
const [full] = useStateEntity<User>([], "full");
|
|
98
|
+
|
|
99
|
+
const user2 = full.byId("2");
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Notes
|
|
103
|
+
|
|
104
|
+
- `T` must include `id: string` (number as id is not yet supported).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/actions.ts
|
|
4
|
+
var getEntityActions = (adapter, setState) => ({
|
|
5
|
+
setOne: (entity) => setState((prev) => adapter.setOne(prev, entity)),
|
|
6
|
+
setMany: (entities) => setState((prev) => adapter.setMany(prev, entities)),
|
|
7
|
+
setAll: (entities) => setState((prev) => adapter.setAll(prev, entities)),
|
|
8
|
+
addOne: (entity) => setState((prev) => adapter.addOne(prev, entity)),
|
|
9
|
+
addMany: (entities) => setState((prev) => adapter.addMany(prev, entities)),
|
|
10
|
+
removeOne: (entityId) => setState((prev) => adapter.removeOne(prev, entityId)),
|
|
11
|
+
removeMany: (entityIds) => setState((prev) => adapter.removeMany(prev, entityIds)),
|
|
12
|
+
removeAll: () => setState((prev) => adapter.removeAll(prev)),
|
|
13
|
+
updateOne: (update) => setState((prev) => adapter.updateOne(prev, update)),
|
|
14
|
+
updateMany: (updates) => setState((prev) => adapter.updateMany(prev, updates)),
|
|
15
|
+
upsertOne: (entity) => setState((prev) => adapter.upsertOne(prev, entity)),
|
|
16
|
+
upsertMany: (entities) => setState((prev) => adapter.upsertMany(prev, entities))
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// src/selectors.ts
|
|
20
|
+
var getEntitySelectorsFull = (entityState, selectors) => ({
|
|
21
|
+
all: selectors.selectAll(entityState),
|
|
22
|
+
byId: (id) => selectors.selectById(entityState, id),
|
|
23
|
+
ids: selectors.selectIds(entityState),
|
|
24
|
+
entities: selectors.selectEntities(entityState),
|
|
25
|
+
total: selectors.selectTotal(entityState)
|
|
26
|
+
});
|
|
27
|
+
var getSelectors = (baseSelectors) => ({
|
|
28
|
+
all: baseSelectors.selectAll,
|
|
29
|
+
entities: baseSelectors.selectEntities,
|
|
30
|
+
ids: baseSelectors.selectIds,
|
|
31
|
+
total: baseSelectors.selectTotal,
|
|
32
|
+
full: (state) => getEntitySelectorsFull(state, baseSelectors)
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
exports.getEntityActions = getEntityActions;
|
|
36
|
+
exports.getSelectors = getSelectors;
|
|
37
|
+
//# sourceMappingURL=chunk-3DHT5C4J.cjs.map
|
|
38
|
+
//# sourceMappingURL=chunk-3DHT5C4J.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/actions.ts","../src/selectors.ts"],"names":[],"mappings":";;;AAGO,IAAM,gBAAA,GAAmB,CAC/B,OAAA,EACA,QAAA,MACqC;AAAA,EACrC,MAAA,EAAQ,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACnE,OAAA,EAAS,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EACzE,MAAA,EAAQ,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAEvE,MAAA,EAAQ,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACnE,OAAA,EAAS,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAEzE,SAAA,EAAW,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAC7E,UAAA,EAAY,CAAC,SAAA,KAAc,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,SAAS,CAAC,CAAA;AAAA,EACjF,SAAA,EAAW,MAAM,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAC,CAAA;AAAA,EAE3D,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACzE,UAAA,EAAY,CAAC,OAAA,KAAY,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,OAAO,CAAC,CAAA;AAAA,EAE7E,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACzE,UAAA,EAAY,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,QAAQ,CAAC;AAChF,CAAA;;;ACpBA,IAAM,sBAAA,GAAyB,CAC9B,WAAA,EACA,SAAA,MACsC;AAAA,EACtC,GAAA,EAAK,SAAA,CAAU,SAAA,CAAU,WAAW,CAAA;AAAA,EACpC,MAAM,CAAC,EAAA,KAAO,SAAA,CAAU,UAAA,CAAW,aAAa,EAAE,CAAA;AAAA,EAClD,GAAA,EAAK,SAAA,CAAU,SAAA,CAAU,WAAW,CAAA;AAAA,EACpC,QAAA,EAAU,SAAA,CAAU,cAAA,CAAe,WAAW,CAAA;AAAA,EAC9C,KAAA,EAAO,SAAA,CAAU,WAAA,CAAY,WAAW;AACzC,CAAA,CAAA;AAUO,IAAM,YAAA,GAAe,CAC3B,aAAA,MACuB;AAAA,EACvB,KAAK,aAAA,CAAc,SAAA;AAAA,EACnB,UAAU,aAAA,CAAc,cAAA;AAAA,EACxB,KAAK,aAAA,CAAc,SAAA;AAAA,EACnB,OAAO,aAAA,CAAc,WAAA;AAAA,EACrB,IAAA,EAAM,CAAC,KAAA,KAAU,sBAAA,CAAuB,OAAO,aAAa;AAC7D,CAAA","file":"chunk-3DHT5C4J.cjs","sourcesContent":["import type { EntityAdapter, EntityState } from \"@reduxjs/toolkit\";\nimport type { EntityStateAdapter, IdItem } from \"./types.ts\";\n\nexport const getEntityActions = <T extends IdItem>(\n\tadapter: EntityAdapter<T, T[\"id\"]>,\n\tsetState: (updater: (prev: EntityState<T, T[\"id\"]>) => EntityState<T, T[\"id\"]>) => void,\n): EntityStateAdapter<T, T[\"id\"]> => ({\n\tsetOne: (entity) => setState((prev) => adapter.setOne(prev, entity)),\n\tsetMany: (entities) => setState((prev) => adapter.setMany(prev, entities)),\n\tsetAll: (entities) => setState((prev) => adapter.setAll(prev, entities)),\n\n\taddOne: (entity) => setState((prev) => adapter.addOne(prev, entity)),\n\taddMany: (entities) => setState((prev) => adapter.addMany(prev, entities)),\n\n\tremoveOne: (entityId) => setState((prev) => adapter.removeOne(prev, entityId)),\n\tremoveMany: (entityIds) => setState((prev) => adapter.removeMany(prev, entityIds)),\n\tremoveAll: () => setState((prev) => adapter.removeAll(prev)),\n\n\tupdateOne: (update) => setState((prev) => adapter.updateOne(prev, update)),\n\tupdateMany: (updates) => setState((prev) => adapter.updateMany(prev, updates)),\n\n\tupsertOne: (entity) => setState((prev) => adapter.upsertOne(prev, entity)),\n\tupsertMany: (entities) => setState((prev) => adapter.upsertMany(prev, entities)),\n});\n","import type { EntitySelectors, EntityState } from \"@reduxjs/toolkit\";\nimport type { EntitySelectorsData, IdItem } from \"./types.ts\";\n\nconst getEntitySelectorsFull = <T extends IdItem>(\n\tentityState: EntityState<T, T[\"id\"]>,\n\tselectors: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>,\n): EntitySelectorsData<T, T[\"id\"]> => ({\n\tall: selectors.selectAll(entityState),\n\tbyId: (id) => selectors.selectById(entityState, id),\n\tids: selectors.selectIds(entityState) as T[\"id\"][],\n\tentities: selectors.selectEntities(entityState),\n\ttotal: selectors.selectTotal(entityState),\n});\n\nexport type DataSelectors<T extends IdItem> = {\n\tall: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectAll\"];\n\tentities: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectEntities\"];\n\tids: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectIds\"];\n\ttotal: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectTotal\"];\n\tfull: (state: EntityState<T, T[\"id\"]>) => EntitySelectorsData<T, T[\"id\"]>;\n};\n\nexport const getSelectors = <T extends IdItem>(\n\tbaseSelectors: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>,\n): DataSelectors<T> => ({\n\tall: baseSelectors.selectAll,\n\tentities: baseSelectors.selectEntities,\n\tids: baseSelectors.selectIds,\n\ttotal: baseSelectors.selectTotal,\n\tfull: (state) => getEntitySelectorsFull(state, baseSelectors),\n});\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/actions.ts
|
|
2
|
+
var getEntityActions = (adapter, setState) => ({
|
|
3
|
+
setOne: (entity) => setState((prev) => adapter.setOne(prev, entity)),
|
|
4
|
+
setMany: (entities) => setState((prev) => adapter.setMany(prev, entities)),
|
|
5
|
+
setAll: (entities) => setState((prev) => adapter.setAll(prev, entities)),
|
|
6
|
+
addOne: (entity) => setState((prev) => adapter.addOne(prev, entity)),
|
|
7
|
+
addMany: (entities) => setState((prev) => adapter.addMany(prev, entities)),
|
|
8
|
+
removeOne: (entityId) => setState((prev) => adapter.removeOne(prev, entityId)),
|
|
9
|
+
removeMany: (entityIds) => setState((prev) => adapter.removeMany(prev, entityIds)),
|
|
10
|
+
removeAll: () => setState((prev) => adapter.removeAll(prev)),
|
|
11
|
+
updateOne: (update) => setState((prev) => adapter.updateOne(prev, update)),
|
|
12
|
+
updateMany: (updates) => setState((prev) => adapter.updateMany(prev, updates)),
|
|
13
|
+
upsertOne: (entity) => setState((prev) => adapter.upsertOne(prev, entity)),
|
|
14
|
+
upsertMany: (entities) => setState((prev) => adapter.upsertMany(prev, entities))
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// src/selectors.ts
|
|
18
|
+
var getEntitySelectorsFull = (entityState, selectors) => ({
|
|
19
|
+
all: selectors.selectAll(entityState),
|
|
20
|
+
byId: (id) => selectors.selectById(entityState, id),
|
|
21
|
+
ids: selectors.selectIds(entityState),
|
|
22
|
+
entities: selectors.selectEntities(entityState),
|
|
23
|
+
total: selectors.selectTotal(entityState)
|
|
24
|
+
});
|
|
25
|
+
var getSelectors = (baseSelectors) => ({
|
|
26
|
+
all: baseSelectors.selectAll,
|
|
27
|
+
entities: baseSelectors.selectEntities,
|
|
28
|
+
ids: baseSelectors.selectIds,
|
|
29
|
+
total: baseSelectors.selectTotal,
|
|
30
|
+
full: (state) => getEntitySelectorsFull(state, baseSelectors)
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export { getEntityActions, getSelectors };
|
|
34
|
+
//# sourceMappingURL=chunk-UANHY5CW.js.map
|
|
35
|
+
//# sourceMappingURL=chunk-UANHY5CW.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/actions.ts","../src/selectors.ts"],"names":[],"mappings":";AAGO,IAAM,gBAAA,GAAmB,CAC/B,OAAA,EACA,QAAA,MACqC;AAAA,EACrC,MAAA,EAAQ,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACnE,OAAA,EAAS,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EACzE,MAAA,EAAQ,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAEvE,MAAA,EAAQ,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,MAAA,CAAO,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACnE,OAAA,EAAS,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAEzE,SAAA,EAAW,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,EAC7E,UAAA,EAAY,CAAC,SAAA,KAAc,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,SAAS,CAAC,CAAA;AAAA,EACjF,SAAA,EAAW,MAAM,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAC,CAAA;AAAA,EAE3D,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACzE,UAAA,EAAY,CAAC,OAAA,KAAY,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,OAAO,CAAC,CAAA;AAAA,EAE7E,SAAA,EAAW,CAAC,MAAA,KAAW,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,SAAA,CAAU,IAAA,EAAM,MAAM,CAAC,CAAA;AAAA,EACzE,UAAA,EAAY,CAAC,QAAA,KAAa,QAAA,CAAS,CAAC,SAAS,OAAA,CAAQ,UAAA,CAAW,IAAA,EAAM,QAAQ,CAAC;AAChF,CAAA;;;ACpBA,IAAM,sBAAA,GAAyB,CAC9B,WAAA,EACA,SAAA,MACsC;AAAA,EACtC,GAAA,EAAK,SAAA,CAAU,SAAA,CAAU,WAAW,CAAA;AAAA,EACpC,MAAM,CAAC,EAAA,KAAO,SAAA,CAAU,UAAA,CAAW,aAAa,EAAE,CAAA;AAAA,EAClD,GAAA,EAAK,SAAA,CAAU,SAAA,CAAU,WAAW,CAAA;AAAA,EACpC,QAAA,EAAU,SAAA,CAAU,cAAA,CAAe,WAAW,CAAA;AAAA,EAC9C,KAAA,EAAO,SAAA,CAAU,WAAA,CAAY,WAAW;AACzC,CAAA,CAAA;AAUO,IAAM,YAAA,GAAe,CAC3B,aAAA,MACuB;AAAA,EACvB,KAAK,aAAA,CAAc,SAAA;AAAA,EACnB,UAAU,aAAA,CAAc,cAAA;AAAA,EACxB,KAAK,aAAA,CAAc,SAAA;AAAA,EACnB,OAAO,aAAA,CAAc,WAAA;AAAA,EACrB,IAAA,EAAM,CAAC,KAAA,KAAU,sBAAA,CAAuB,OAAO,aAAa;AAC7D,CAAA","file":"chunk-UANHY5CW.js","sourcesContent":["import type { EntityAdapter, EntityState } from \"@reduxjs/toolkit\";\nimport type { EntityStateAdapter, IdItem } from \"./types.ts\";\n\nexport const getEntityActions = <T extends IdItem>(\n\tadapter: EntityAdapter<T, T[\"id\"]>,\n\tsetState: (updater: (prev: EntityState<T, T[\"id\"]>) => EntityState<T, T[\"id\"]>) => void,\n): EntityStateAdapter<T, T[\"id\"]> => ({\n\tsetOne: (entity) => setState((prev) => adapter.setOne(prev, entity)),\n\tsetMany: (entities) => setState((prev) => adapter.setMany(prev, entities)),\n\tsetAll: (entities) => setState((prev) => adapter.setAll(prev, entities)),\n\n\taddOne: (entity) => setState((prev) => adapter.addOne(prev, entity)),\n\taddMany: (entities) => setState((prev) => adapter.addMany(prev, entities)),\n\n\tremoveOne: (entityId) => setState((prev) => adapter.removeOne(prev, entityId)),\n\tremoveMany: (entityIds) => setState((prev) => adapter.removeMany(prev, entityIds)),\n\tremoveAll: () => setState((prev) => adapter.removeAll(prev)),\n\n\tupdateOne: (update) => setState((prev) => adapter.updateOne(prev, update)),\n\tupdateMany: (updates) => setState((prev) => adapter.updateMany(prev, updates)),\n\n\tupsertOne: (entity) => setState((prev) => adapter.upsertOne(prev, entity)),\n\tupsertMany: (entities) => setState((prev) => adapter.upsertMany(prev, entities)),\n});\n","import type { EntitySelectors, EntityState } from \"@reduxjs/toolkit\";\nimport type { EntitySelectorsData, IdItem } from \"./types.ts\";\n\nconst getEntitySelectorsFull = <T extends IdItem>(\n\tentityState: EntityState<T, T[\"id\"]>,\n\tselectors: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>,\n): EntitySelectorsData<T, T[\"id\"]> => ({\n\tall: selectors.selectAll(entityState),\n\tbyId: (id) => selectors.selectById(entityState, id),\n\tids: selectors.selectIds(entityState) as T[\"id\"][],\n\tentities: selectors.selectEntities(entityState),\n\ttotal: selectors.selectTotal(entityState),\n});\n\nexport type DataSelectors<T extends IdItem> = {\n\tall: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectAll\"];\n\tentities: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectEntities\"];\n\tids: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectIds\"];\n\ttotal: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>[\"selectTotal\"];\n\tfull: (state: EntityState<T, T[\"id\"]>) => EntitySelectorsData<T, T[\"id\"]>;\n};\n\nexport const getSelectors = <T extends IdItem>(\n\tbaseSelectors: EntitySelectors<T, EntityState<T, T[\"id\"]>, T[\"id\"]>,\n): DataSelectors<T> => ({\n\tall: baseSelectors.selectAll,\n\tentities: baseSelectors.selectEntities,\n\tids: baseSelectors.selectIds,\n\ttotal: baseSelectors.selectTotal,\n\tfull: (state) => getEntitySelectorsFull(state, baseSelectors),\n});\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk3DHT5C4J_cjs = require('./chunk-3DHT5C4J.cjs');
|
|
4
|
+
var toolkit = require('@reduxjs/toolkit');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
var adapterInstance = toolkit.createEntityAdapter();
|
|
8
|
+
function useStateEntity(initialState, selector) {
|
|
9
|
+
const adapter = adapterInstance;
|
|
10
|
+
const selectorKey = selector ?? "all";
|
|
11
|
+
const [state, setState] = react.useState(
|
|
12
|
+
() => initialState ? adapter.setAll(adapter.getInitialState(), initialState instanceof Function ? initialState() : initialState) : adapter.getInitialState()
|
|
13
|
+
);
|
|
14
|
+
const actions = react.useMemo(() => chunk3DHT5C4J_cjs.getEntityActions(adapter, setState), [adapter]);
|
|
15
|
+
const data = react.useMemo(
|
|
16
|
+
() => chunk3DHT5C4J_cjs.getSelectors(adapter.getSelectors())[selectorKey](state),
|
|
17
|
+
[state, adapter.getSelectors, selectorKey]
|
|
18
|
+
);
|
|
19
|
+
return [data, actions];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
Object.defineProperty(exports, "getEntityActions", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () { return chunk3DHT5C4J_cjs.getEntityActions; }
|
|
25
|
+
});
|
|
26
|
+
exports.useStateEntity = useStateEntity;
|
|
27
|
+
//# sourceMappingURL=index.cjs.map
|
|
28
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter/useState.ts"],"names":["createEntityAdapter","useState","useMemo","getEntityActions","getSelectors"],"mappings":";;;;;;AAYA,IAAM,kBAAkBA,2BAAA,EAAyB;AA0C1C,SAAS,cAAA,CACf,cACA,QAAA,EAC4E;AAE5E,EAAA,MAAM,OAAA,GAAU,eAAA;AAChB,EAAA,MAAM,cAAe,QAAA,IAAY,KAAA;AAEjC,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIC,cAAA;AAAA,IAAS,MAClC,YAAA,GACG,OAAA,CAAQ,MAAA,CAAO,QAAQ,eAAA,EAAgB,EAAG,YAAA,YAAwB,QAAA,GAAW,YAAA,EAAa,GAAI,YAAY,CAAA,GAC1G,QAAQ,eAAA;AAAgB,GAC5B;AAEA,EAAA,MAAM,OAAA,GAAUC,cAAQ,MAAMC,kCAAA,CAAiB,SAAS,QAAQ,CAAA,EAAG,CAAC,OAAO,CAAC,CAAA;AAE5E,EAAA,MAAM,IAAA,GAAOD,aAAA;AAAA,IACZ,MAAME,+BAAa,OAAA,CAAQ,YAAA,EAAc,CAAA,CAAE,WAAW,EAAE,KAAK,CAAA;AAAA,IAC7D,CAAC,KAAA,EAAO,OAAA,CAAQ,YAAA,EAAc,WAAW;AAAA,GAC1C;AAEA,EAAA,OAAO,CAAC,MAAM,OAAO,CAAA;AACtB","file":"index.cjs","sourcesContent":["import { createEntityAdapter } from \"@reduxjs/toolkit\";\nimport { useMemo, useState } from \"react\";\nimport { getEntityActions } from \"../actions.ts\";\nimport { type DataSelectors, getSelectors } from \"../selectors.ts\";\nimport type { EntityStateAdapter, IdItem, InitialEntityState } from \"../types.ts\";\n\n/**\n * Creates a stable adapter instance outside the hook.\n * Since adapters are stateless configuration objects, we don't need\n * to recreate them or store them in React state.\n */\n// biome-ignore lint/suspicious/noExplicitAny: fine here\nconst adapterInstance = createEntityAdapter<any>();\n\n/**\n * react useState hook integrated with an entity adapter.\n * Provides entity state and actions to manipulate that state.\n *\n * @example\n * const [] = useStateWithEntity<{ id: string; name: string }>();\n * const [] = useStateWithEntity<{ id: string; name: string }>([]);\n * const [] = useStateWithEntity<{ id: string; name: string }>([], \"total\");\n * const [] = useStateWithEntity<{ id: string; name: string }>([], \"full\");\n * const [] = useStateWithEntity([] as { id: string; name: string }[], \"full\");\n * action.addOne({ id: \"1\", name: \"Demo\" });\n */\ntype SelectorKey<T extends IdItem> = keyof DataSelectors<T>;\ntype SelectorReturn<T extends IdItem, K extends SelectorKey<T>> = ReturnType<DataSelectors<T>[K]>;\ntype SelectorOrFull<T extends IdItem, K> = K extends SelectorKey<T> ? K : \"full\";\n\ntype Return<T extends IdItem, S extends SelectorKey<T>> = [SelectorReturn<T, S>, EntityStateAdapter<T, T[\"id\"]>];\n\n// biome-ignore format: no selector => full\nexport function useStateEntity<T extends IdItem>(): Return<T, \"all\">;\n\n// biome-ignore format: initial state => full selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>): Return<T, \"all\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"all\"): Return<T, \"all\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"entities\"): Return<T, \"entities\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"ids\"): Return<T, \"ids\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"total\"): Return<T, \"total\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"full\"): Return<T, \"full\">;\n\n// Keep it short\nexport function useStateEntity<T extends IdItem, K extends SelectorKey<T>>(\n\tinitialState?: InitialEntityState<T>,\n\tselector?: K,\n): [SelectorReturn<T, SelectorOrFull<T, K>>, EntityStateAdapter<T, T[\"id\"]>] {\n\t// Cast the generic adapter for type safety within the hook\n\tconst adapter = adapterInstance as ReturnType<typeof createEntityAdapter<T>>;\n\tconst selectorKey = (selector ?? \"all\") as SelectorKey<T>;\n\n\tconst [state, setState] = useState(() =>\n\t\tinitialState\n\t\t\t? adapter.setAll(adapter.getInitialState(), initialState instanceof Function ? initialState() : initialState)\n\t\t\t: adapter.getInitialState(),\n\t);\n\n\tconst actions = useMemo(() => getEntityActions(adapter, setState), [adapter]);\n\n\tconst data = useMemo(\n\t\t() => getSelectors(adapter.getSelectors())[selectorKey](state) as SelectorReturn<T, SelectorOrFull<T, K>>,\n\t\t[state, adapter.getSelectors, selectorKey],\n\t);\n\n\treturn [data, actions];\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EntityAdapter, EntityState } from '@reduxjs/toolkit';
|
|
2
|
+
import { I as IdItem, E as EntityStateAdapter, D as DataSelectors, a as InitialEntityState } from './selectors-vvbtAjSu.cjs';
|
|
3
|
+
|
|
4
|
+
declare const getEntityActions: <T extends IdItem>(adapter: EntityAdapter<T, T["id"]>, setState: (updater: (prev: EntityState<T, T["id"]>) => EntityState<T, T["id"]>) => void) => EntityStateAdapter<T, T["id"]>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* react useState hook integrated with an entity adapter.
|
|
8
|
+
* Provides entity state and actions to manipulate that state.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>();
|
|
12
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([]);
|
|
13
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([], "total");
|
|
14
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([], "full");
|
|
15
|
+
* const [] = useStateWithEntity([] as { id: string; name: string }[], "full");
|
|
16
|
+
* action.addOne({ id: "1", name: "Demo" });
|
|
17
|
+
*/
|
|
18
|
+
type SelectorKey<T extends IdItem> = keyof DataSelectors<T>;
|
|
19
|
+
type SelectorReturn<T extends IdItem, K extends SelectorKey<T>> = ReturnType<DataSelectors<T>[K]>;
|
|
20
|
+
type Return<T extends IdItem, S extends SelectorKey<T>> = [SelectorReturn<T, S>, EntityStateAdapter<T, T["id"]>];
|
|
21
|
+
declare function useStateEntity<T extends IdItem>(): Return<T, "all">;
|
|
22
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>): Return<T, "all">;
|
|
23
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "all"): Return<T, "all">;
|
|
24
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "entities"): Return<T, "entities">;
|
|
25
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "ids"): Return<T, "ids">;
|
|
26
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "total"): Return<T, "total">;
|
|
27
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "full"): Return<T, "full">;
|
|
28
|
+
|
|
29
|
+
export { getEntityActions, useStateEntity };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EntityAdapter, EntityState } from '@reduxjs/toolkit';
|
|
2
|
+
import { I as IdItem, E as EntityStateAdapter, D as DataSelectors, a as InitialEntityState } from './selectors-vvbtAjSu.js';
|
|
3
|
+
|
|
4
|
+
declare const getEntityActions: <T extends IdItem>(adapter: EntityAdapter<T, T["id"]>, setState: (updater: (prev: EntityState<T, T["id"]>) => EntityState<T, T["id"]>) => void) => EntityStateAdapter<T, T["id"]>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* react useState hook integrated with an entity adapter.
|
|
8
|
+
* Provides entity state and actions to manipulate that state.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>();
|
|
12
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([]);
|
|
13
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([], "total");
|
|
14
|
+
* const [] = useStateWithEntity<{ id: string; name: string }>([], "full");
|
|
15
|
+
* const [] = useStateWithEntity([] as { id: string; name: string }[], "full");
|
|
16
|
+
* action.addOne({ id: "1", name: "Demo" });
|
|
17
|
+
*/
|
|
18
|
+
type SelectorKey<T extends IdItem> = keyof DataSelectors<T>;
|
|
19
|
+
type SelectorReturn<T extends IdItem, K extends SelectorKey<T>> = ReturnType<DataSelectors<T>[K]>;
|
|
20
|
+
type Return<T extends IdItem, S extends SelectorKey<T>> = [SelectorReturn<T, S>, EntityStateAdapter<T, T["id"]>];
|
|
21
|
+
declare function useStateEntity<T extends IdItem>(): Return<T, "all">;
|
|
22
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>): Return<T, "all">;
|
|
23
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "all"): Return<T, "all">;
|
|
24
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "entities"): Return<T, "entities">;
|
|
25
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "ids"): Return<T, "ids">;
|
|
26
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "total"): Return<T, "total">;
|
|
27
|
+
declare function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: "full"): Return<T, "full">;
|
|
28
|
+
|
|
29
|
+
export { getEntityActions, useStateEntity };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getEntityActions, getSelectors } from './chunk-UANHY5CW.js';
|
|
2
|
+
export { getEntityActions } from './chunk-UANHY5CW.js';
|
|
3
|
+
import { createEntityAdapter } from '@reduxjs/toolkit';
|
|
4
|
+
import { useState, useMemo } from 'react';
|
|
5
|
+
|
|
6
|
+
var adapterInstance = createEntityAdapter();
|
|
7
|
+
function useStateEntity(initialState, selector) {
|
|
8
|
+
const adapter = adapterInstance;
|
|
9
|
+
const selectorKey = selector ?? "all";
|
|
10
|
+
const [state, setState] = useState(
|
|
11
|
+
() => initialState ? adapter.setAll(adapter.getInitialState(), initialState instanceof Function ? initialState() : initialState) : adapter.getInitialState()
|
|
12
|
+
);
|
|
13
|
+
const actions = useMemo(() => getEntityActions(adapter, setState), [adapter]);
|
|
14
|
+
const data = useMemo(
|
|
15
|
+
() => getSelectors(adapter.getSelectors())[selectorKey](state),
|
|
16
|
+
[state, adapter.getSelectors, selectorKey]
|
|
17
|
+
);
|
|
18
|
+
return [data, actions];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { useStateEntity };
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter/useState.ts"],"names":[],"mappings":";;;;;AAYA,IAAM,kBAAkB,mBAAA,EAAyB;AA0C1C,SAAS,cAAA,CACf,cACA,QAAA,EAC4E;AAE5E,EAAA,MAAM,OAAA,GAAU,eAAA;AAChB,EAAA,MAAM,cAAe,QAAA,IAAY,KAAA;AAEjC,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,QAAA;AAAA,IAAS,MAClC,YAAA,GACG,OAAA,CAAQ,MAAA,CAAO,QAAQ,eAAA,EAAgB,EAAG,YAAA,YAAwB,QAAA,GAAW,YAAA,EAAa,GAAI,YAAY,CAAA,GAC1G,QAAQ,eAAA;AAAgB,GAC5B;AAEA,EAAA,MAAM,OAAA,GAAU,QAAQ,MAAM,gBAAA,CAAiB,SAAS,QAAQ,CAAA,EAAG,CAAC,OAAO,CAAC,CAAA;AAE5E,EAAA,MAAM,IAAA,GAAO,OAAA;AAAA,IACZ,MAAM,aAAa,OAAA,CAAQ,YAAA,EAAc,CAAA,CAAE,WAAW,EAAE,KAAK,CAAA;AAAA,IAC7D,CAAC,KAAA,EAAO,OAAA,CAAQ,YAAA,EAAc,WAAW;AAAA,GAC1C;AAEA,EAAA,OAAO,CAAC,MAAM,OAAO,CAAA;AACtB","file":"index.js","sourcesContent":["import { createEntityAdapter } from \"@reduxjs/toolkit\";\nimport { useMemo, useState } from \"react\";\nimport { getEntityActions } from \"../actions.ts\";\nimport { type DataSelectors, getSelectors } from \"../selectors.ts\";\nimport type { EntityStateAdapter, IdItem, InitialEntityState } from \"../types.ts\";\n\n/**\n * Creates a stable adapter instance outside the hook.\n * Since adapters are stateless configuration objects, we don't need\n * to recreate them or store them in React state.\n */\n// biome-ignore lint/suspicious/noExplicitAny: fine here\nconst adapterInstance = createEntityAdapter<any>();\n\n/**\n * react useState hook integrated with an entity adapter.\n * Provides entity state and actions to manipulate that state.\n *\n * @example\n * const [] = useStateWithEntity<{ id: string; name: string }>();\n * const [] = useStateWithEntity<{ id: string; name: string }>([]);\n * const [] = useStateWithEntity<{ id: string; name: string }>([], \"total\");\n * const [] = useStateWithEntity<{ id: string; name: string }>([], \"full\");\n * const [] = useStateWithEntity([] as { id: string; name: string }[], \"full\");\n * action.addOne({ id: \"1\", name: \"Demo\" });\n */\ntype SelectorKey<T extends IdItem> = keyof DataSelectors<T>;\ntype SelectorReturn<T extends IdItem, K extends SelectorKey<T>> = ReturnType<DataSelectors<T>[K]>;\ntype SelectorOrFull<T extends IdItem, K> = K extends SelectorKey<T> ? K : \"full\";\n\ntype Return<T extends IdItem, S extends SelectorKey<T>> = [SelectorReturn<T, S>, EntityStateAdapter<T, T[\"id\"]>];\n\n// biome-ignore format: no selector => full\nexport function useStateEntity<T extends IdItem>(): Return<T, \"all\">;\n\n// biome-ignore format: initial state => full selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>): Return<T, \"all\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"all\"): Return<T, \"all\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"entities\"): Return<T, \"entities\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"ids\"): Return<T, \"ids\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"total\"): Return<T, \"total\">;\n\n// biome-ignore format: initial state + selector\nexport function useStateEntity<T extends IdItem>(initialState: InitialEntityState<T>, selector: \"full\"): Return<T, \"full\">;\n\n// Keep it short\nexport function useStateEntity<T extends IdItem, K extends SelectorKey<T>>(\n\tinitialState?: InitialEntityState<T>,\n\tselector?: K,\n): [SelectorReturn<T, SelectorOrFull<T, K>>, EntityStateAdapter<T, T[\"id\"]>] {\n\t// Cast the generic adapter for type safety within the hook\n\tconst adapter = adapterInstance as ReturnType<typeof createEntityAdapter<T>>;\n\tconst selectorKey = (selector ?? \"all\") as SelectorKey<T>;\n\n\tconst [state, setState] = useState(() =>\n\t\tinitialState\n\t\t\t? adapter.setAll(adapter.getInitialState(), initialState instanceof Function ? initialState() : initialState)\n\t\t\t: adapter.getInitialState(),\n\t);\n\n\tconst actions = useMemo(() => getEntityActions(adapter, setState), [adapter]);\n\n\tconst data = useMemo(\n\t\t() => getSelectors(adapter.getSelectors())[selectorKey](state) as SelectorReturn<T, SelectorOrFull<T, K>>,\n\t\t[state, adapter.getSelectors, selectorKey],\n\t);\n\n\treturn [data, actions];\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EntityId, Update, EntitySelectors, EntityState } from '@reduxjs/toolkit';
|
|
2
|
+
|
|
3
|
+
type IfMaybeUndefined<T, True, False> = [undefined] extends [T] ? True : False;
|
|
4
|
+
declare const testAccess: 0 | undefined;
|
|
5
|
+
type IfUncheckedIndexedAccess<True, False> = IfMaybeUndefined<typeof testAccess, True, False>;
|
|
6
|
+
type UncheckedIndexedAccess<T> = IfUncheckedIndexedAccess<T | undefined, T>;
|
|
7
|
+
|
|
8
|
+
interface EntityStateAdapter<T, Id extends EntityId> {
|
|
9
|
+
addOne(entity: T): void;
|
|
10
|
+
addMany(entities: readonly T[] | Record<Id, T>): void;
|
|
11
|
+
setOne(entity: T): void;
|
|
12
|
+
setMany(entities: readonly T[] | Record<Id, T>): void;
|
|
13
|
+
setAll(entities: readonly T[] | Record<Id, T>): void;
|
|
14
|
+
removeOne(key: Id): void;
|
|
15
|
+
removeMany(keys: readonly Id[]): void;
|
|
16
|
+
removeAll(): void;
|
|
17
|
+
updateOne(update: Update<T, Id>): void;
|
|
18
|
+
updateMany(updates: ReadonlyArray<Update<T, Id>>): void;
|
|
19
|
+
upsertOne(entity: T): void;
|
|
20
|
+
upsertMany(entities: readonly T[] | Record<Id, T>): void;
|
|
21
|
+
}
|
|
22
|
+
type IdItem = {
|
|
23
|
+
id: string;
|
|
24
|
+
};
|
|
25
|
+
type InitialEntityState<T extends IdItem> = T[] | Record<T["id"], T> | (() => T[] | Record<T["id"], T>) | undefined;
|
|
26
|
+
type Id<T> = {
|
|
27
|
+
[K in keyof T]: T[K];
|
|
28
|
+
} & {};
|
|
29
|
+
interface EntitySelectorsData<T, IdType extends EntityId> {
|
|
30
|
+
ids: IdType[];
|
|
31
|
+
entities: Record<IdType, T>;
|
|
32
|
+
all: T[];
|
|
33
|
+
total: number;
|
|
34
|
+
byId: (id: IdType) => Id<UncheckedIndexedAccess<T>>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type DataSelectors<T extends IdItem> = {
|
|
38
|
+
all: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectAll"];
|
|
39
|
+
entities: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectEntities"];
|
|
40
|
+
ids: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectIds"];
|
|
41
|
+
total: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectTotal"];
|
|
42
|
+
full: (state: EntityState<T, T["id"]>) => EntitySelectorsData<T, T["id"]>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type { DataSelectors as D, EntityStateAdapter as E, IdItem as I, InitialEntityState as a };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EntityId, Update, EntitySelectors, EntityState } from '@reduxjs/toolkit';
|
|
2
|
+
|
|
3
|
+
type IfMaybeUndefined<T, True, False> = [undefined] extends [T] ? True : False;
|
|
4
|
+
declare const testAccess: 0 | undefined;
|
|
5
|
+
type IfUncheckedIndexedAccess<True, False> = IfMaybeUndefined<typeof testAccess, True, False>;
|
|
6
|
+
type UncheckedIndexedAccess<T> = IfUncheckedIndexedAccess<T | undefined, T>;
|
|
7
|
+
|
|
8
|
+
interface EntityStateAdapter<T, Id extends EntityId> {
|
|
9
|
+
addOne(entity: T): void;
|
|
10
|
+
addMany(entities: readonly T[] | Record<Id, T>): void;
|
|
11
|
+
setOne(entity: T): void;
|
|
12
|
+
setMany(entities: readonly T[] | Record<Id, T>): void;
|
|
13
|
+
setAll(entities: readonly T[] | Record<Id, T>): void;
|
|
14
|
+
removeOne(key: Id): void;
|
|
15
|
+
removeMany(keys: readonly Id[]): void;
|
|
16
|
+
removeAll(): void;
|
|
17
|
+
updateOne(update: Update<T, Id>): void;
|
|
18
|
+
updateMany(updates: ReadonlyArray<Update<T, Id>>): void;
|
|
19
|
+
upsertOne(entity: T): void;
|
|
20
|
+
upsertMany(entities: readonly T[] | Record<Id, T>): void;
|
|
21
|
+
}
|
|
22
|
+
type IdItem = {
|
|
23
|
+
id: string;
|
|
24
|
+
};
|
|
25
|
+
type InitialEntityState<T extends IdItem> = T[] | Record<T["id"], T> | (() => T[] | Record<T["id"], T>) | undefined;
|
|
26
|
+
type Id<T> = {
|
|
27
|
+
[K in keyof T]: T[K];
|
|
28
|
+
} & {};
|
|
29
|
+
interface EntitySelectorsData<T, IdType extends EntityId> {
|
|
30
|
+
ids: IdType[];
|
|
31
|
+
entities: Record<IdType, T>;
|
|
32
|
+
all: T[];
|
|
33
|
+
total: number;
|
|
34
|
+
byId: (id: IdType) => Id<UncheckedIndexedAccess<T>>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type DataSelectors<T extends IdItem> = {
|
|
38
|
+
all: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectAll"];
|
|
39
|
+
entities: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectEntities"];
|
|
40
|
+
ids: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectIds"];
|
|
41
|
+
total: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>["selectTotal"];
|
|
42
|
+
full: (state: EntityState<T, T["id"]>) => EntitySelectorsData<T, T["id"]>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type { DataSelectors as D, EntityStateAdapter as E, IdItem as I, InitialEntityState as a };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk3DHT5C4J_cjs = require('./chunk-3DHT5C4J.cjs');
|
|
4
|
+
var toolkit = require('@reduxjs/toolkit');
|
|
5
|
+
var reactStore = require('@tanstack/react-store');
|
|
6
|
+
|
|
7
|
+
var entityStoreFactory = (initialState) => {
|
|
8
|
+
const adapter = toolkit.createEntityAdapter();
|
|
9
|
+
const store = new reactStore.Store(
|
|
10
|
+
initialState ? adapter.setAll(adapter.getInitialState(), initialState) : adapter.getInitialState()
|
|
11
|
+
);
|
|
12
|
+
const actions = chunk3DHT5C4J_cjs.getEntityActions(adapter, (updater) => store.setState(updater));
|
|
13
|
+
const selectors = chunk3DHT5C4J_cjs.getSelectors(adapter.getSelectors());
|
|
14
|
+
return { store, adapter, selectors, actions };
|
|
15
|
+
};
|
|
16
|
+
var createEntityStoreTanstack = (initialState) => {
|
|
17
|
+
const { adapter, store, actions, selectors } = entityStoreFactory(initialState);
|
|
18
|
+
function useEntity(selector = "all") {
|
|
19
|
+
const entityState = reactStore.useStore(store, selectors[selector]);
|
|
20
|
+
return [entityState, actions];
|
|
21
|
+
}
|
|
22
|
+
return { useEntity, store, actions, adapter, selectors };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
exports.createEntityStoreTanstack = createEntityStoreTanstack;
|
|
26
|
+
exports.entityStoreFactory = entityStoreFactory;
|
|
27
|
+
//# sourceMappingURL=tanstack.cjs.map
|
|
28
|
+
//# sourceMappingURL=tanstack.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter/tanstack.ts"],"names":["createEntityAdapter","Store","getEntityActions","getSelectors","useStore"],"mappings":";;;;;;AAMO,IAAM,kBAAA,GAAqB,CAAmB,YAAA,KAAuB;AAC3E,EAAA,MAAM,UAAUA,2BAAA,EAAuB;AACvC,EAAA,MAAM,QAAQ,IAAIC,gBAAA;AAAA,IACjB,YAAA,GAAe,QAAQ,MAAA,CAAO,OAAA,CAAQ,iBAAgB,EAAG,YAAY,CAAA,GAAI,OAAA,CAAQ,eAAA;AAAgB,GAClG;AACA,EAAA,MAAM,OAAA,GAAUC,mCAAiB,OAAA,EAAS,CAAC,YAAY,KAAA,CAAM,QAAA,CAAS,OAAO,CAAC,CAAA;AAC9E,EAAA,MAAM,SAAA,GAAYC,8BAAA,CAAa,OAAA,CAAQ,YAAA,EAAc,CAAA;AACrD,EAAA,OAAO,EAAE,KAAA,EAAO,OAAA,EAAS,SAAA,EAAW,OAAA,EAAQ;AAC7C;AAEO,IAAM,yBAAA,GAA4B,CAAmB,YAAA,KAAuB;AAClF,EAAA,MAAM,EAAE,OAAA,EAAS,KAAA,EAAO,SAAS,SAAA,EAAU,GAAI,mBAAmB,YAAY,CAAA;AAW9E,EAAA,SAAS,SAAA,CAAiC,WAAc,KAAA,EAAY;AACnE,IAAA,MAAM,WAAA,GAAcC,mBAAA,CAAS,KAAA,EAAO,SAAA,CAAU,QAAQ,CAA4B,CAAA;AAElF,IAAA,OAAO,CAAC,aAAa,OAAO,CAAA;AAAA,EAC7B;AAEA,EAAA,OAAO,EAAE,SAAA,EAAW,KAAA,EAAO,OAAA,EAAS,SAAS,SAAA,EAAU;AACxD","file":"tanstack.cjs","sourcesContent":["import { createEntityAdapter } from \"@reduxjs/toolkit\";\nimport { Store, useStore } from \"@tanstack/react-store\";\nimport { getEntityActions } from \"../actions.ts\";\nimport { getSelectors } from \"../selectors.ts\";\nimport type { EntityStateAdapter, IdItem } from \"../types.ts\";\n\nexport const entityStoreFactory = <T extends IdItem>(initialState?: T[]) => {\n\tconst adapter = createEntityAdapter<T>();\n\tconst store = new Store(\n\t\tinitialState ? adapter.setAll(adapter.getInitialState(), initialState) : adapter.getInitialState(),\n\t);\n\tconst actions = getEntityActions(adapter, (updater) => store.setState(updater));\n\tconst selectors = getSelectors(adapter.getSelectors());\n\treturn { store, adapter, selectors, actions };\n};\n\nexport const createEntityStoreTanstack = <T extends IdItem>(initialState?: T[]) => {\n\tconst { adapter, store, actions, selectors } = entityStoreFactory(initialState);\n\n\ttype SelectorKey = keyof typeof selectors;\n\ttype SelectorReturn<K extends SelectorKey> = ReturnType<(typeof selectors)[K]>;\n\n\tfunction useEntity(): [SelectorReturn<\"all\">, EntityStateAdapter<T, T[\"id\"]>];\n\tfunction useEntity(selector: \"all\"): [SelectorReturn<\"all\">, EntityStateAdapter<T, T[\"id\"]>];\n\tfunction useEntity<K extends Exclude<SelectorKey, \"all\">>(\n\t\tselector: K,\n\t): [SelectorReturn<K>, EntityStateAdapter<T, T[\"id\"]>];\n\n\tfunction useEntity<K extends SelectorKey>(selector: K = \"all\" as K) {\n\t\tconst entityState = useStore(store, selectors[selector] as () => SelectorReturn<K>);\n\n\t\treturn [entityState, actions] satisfies [SelectorReturn<K>, EntityStateAdapter<T, T[\"id\"]>];\n\t}\n\n\treturn { useEntity, store, actions, adapter, selectors };\n};\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { I as IdItem, E as EntityStateAdapter, D as DataSelectors } from './selectors-vvbtAjSu.cjs';
|
|
2
|
+
import * as _reduxjs_toolkit from '@reduxjs/toolkit';
|
|
3
|
+
import { Store } from '@tanstack/react-store';
|
|
4
|
+
|
|
5
|
+
declare const entityStoreFactory: <T extends IdItem>(initialState?: T[]) => {
|
|
6
|
+
store: Store<_reduxjs_toolkit.EntityState<T, T["id"]>, (cb: _reduxjs_toolkit.EntityState<T, T["id"]>) => _reduxjs_toolkit.EntityState<T, T["id"]>>;
|
|
7
|
+
adapter: _reduxjs_toolkit.EntityAdapter<T, T["id"]>;
|
|
8
|
+
selectors: DataSelectors<T>;
|
|
9
|
+
actions: EntityStateAdapter<T, T["id"]>;
|
|
10
|
+
};
|
|
11
|
+
declare const createEntityStoreTanstack: <T extends IdItem>(initialState?: T[]) => {
|
|
12
|
+
useEntity: {
|
|
13
|
+
(): [T[], EntityStateAdapter<T, T["id"]>];
|
|
14
|
+
(selector: "all"): [T[], EntityStateAdapter<T, T["id"]>];
|
|
15
|
+
<K extends Exclude<keyof DataSelectors<T>, "all">>(selector: K): [ReturnType<DataSelectors<T>[K]>, EntityStateAdapter<T, T["id"]>];
|
|
16
|
+
};
|
|
17
|
+
store: Store<_reduxjs_toolkit.EntityState<T, T["id"]>, (cb: _reduxjs_toolkit.EntityState<T, T["id"]>) => _reduxjs_toolkit.EntityState<T, T["id"]>>;
|
|
18
|
+
actions: EntityStateAdapter<T, T["id"]>;
|
|
19
|
+
adapter: _reduxjs_toolkit.EntityAdapter<T, T["id"]>;
|
|
20
|
+
selectors: DataSelectors<T>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { createEntityStoreTanstack, entityStoreFactory };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { I as IdItem, E as EntityStateAdapter, D as DataSelectors } from './selectors-vvbtAjSu.js';
|
|
2
|
+
import * as _reduxjs_toolkit from '@reduxjs/toolkit';
|
|
3
|
+
import { Store } from '@tanstack/react-store';
|
|
4
|
+
|
|
5
|
+
declare const entityStoreFactory: <T extends IdItem>(initialState?: T[]) => {
|
|
6
|
+
store: Store<_reduxjs_toolkit.EntityState<T, T["id"]>, (cb: _reduxjs_toolkit.EntityState<T, T["id"]>) => _reduxjs_toolkit.EntityState<T, T["id"]>>;
|
|
7
|
+
adapter: _reduxjs_toolkit.EntityAdapter<T, T["id"]>;
|
|
8
|
+
selectors: DataSelectors<T>;
|
|
9
|
+
actions: EntityStateAdapter<T, T["id"]>;
|
|
10
|
+
};
|
|
11
|
+
declare const createEntityStoreTanstack: <T extends IdItem>(initialState?: T[]) => {
|
|
12
|
+
useEntity: {
|
|
13
|
+
(): [T[], EntityStateAdapter<T, T["id"]>];
|
|
14
|
+
(selector: "all"): [T[], EntityStateAdapter<T, T["id"]>];
|
|
15
|
+
<K extends Exclude<keyof DataSelectors<T>, "all">>(selector: K): [ReturnType<DataSelectors<T>[K]>, EntityStateAdapter<T, T["id"]>];
|
|
16
|
+
};
|
|
17
|
+
store: Store<_reduxjs_toolkit.EntityState<T, T["id"]>, (cb: _reduxjs_toolkit.EntityState<T, T["id"]>) => _reduxjs_toolkit.EntityState<T, T["id"]>>;
|
|
18
|
+
actions: EntityStateAdapter<T, T["id"]>;
|
|
19
|
+
adapter: _reduxjs_toolkit.EntityAdapter<T, T["id"]>;
|
|
20
|
+
selectors: DataSelectors<T>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { createEntityStoreTanstack, entityStoreFactory };
|
package/dist/tanstack.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getEntityActions, getSelectors } from './chunk-UANHY5CW.js';
|
|
2
|
+
import { createEntityAdapter } from '@reduxjs/toolkit';
|
|
3
|
+
import { Store, useStore } from '@tanstack/react-store';
|
|
4
|
+
|
|
5
|
+
var entityStoreFactory = (initialState) => {
|
|
6
|
+
const adapter = createEntityAdapter();
|
|
7
|
+
const store = new Store(
|
|
8
|
+
initialState ? adapter.setAll(adapter.getInitialState(), initialState) : adapter.getInitialState()
|
|
9
|
+
);
|
|
10
|
+
const actions = getEntityActions(adapter, (updater) => store.setState(updater));
|
|
11
|
+
const selectors = getSelectors(adapter.getSelectors());
|
|
12
|
+
return { store, adapter, selectors, actions };
|
|
13
|
+
};
|
|
14
|
+
var createEntityStoreTanstack = (initialState) => {
|
|
15
|
+
const { adapter, store, actions, selectors } = entityStoreFactory(initialState);
|
|
16
|
+
function useEntity(selector = "all") {
|
|
17
|
+
const entityState = useStore(store, selectors[selector]);
|
|
18
|
+
return [entityState, actions];
|
|
19
|
+
}
|
|
20
|
+
return { useEntity, store, actions, adapter, selectors };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { createEntityStoreTanstack, entityStoreFactory };
|
|
24
|
+
//# sourceMappingURL=tanstack.js.map
|
|
25
|
+
//# sourceMappingURL=tanstack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter/tanstack.ts"],"names":[],"mappings":";;;;AAMO,IAAM,kBAAA,GAAqB,CAAmB,YAAA,KAAuB;AAC3E,EAAA,MAAM,UAAU,mBAAA,EAAuB;AACvC,EAAA,MAAM,QAAQ,IAAI,KAAA;AAAA,IACjB,YAAA,GAAe,QAAQ,MAAA,CAAO,OAAA,CAAQ,iBAAgB,EAAG,YAAY,CAAA,GAAI,OAAA,CAAQ,eAAA;AAAgB,GAClG;AACA,EAAA,MAAM,OAAA,GAAU,iBAAiB,OAAA,EAAS,CAAC,YAAY,KAAA,CAAM,QAAA,CAAS,OAAO,CAAC,CAAA;AAC9E,EAAA,MAAM,SAAA,GAAY,YAAA,CAAa,OAAA,CAAQ,YAAA,EAAc,CAAA;AACrD,EAAA,OAAO,EAAE,KAAA,EAAO,OAAA,EAAS,SAAA,EAAW,OAAA,EAAQ;AAC7C;AAEO,IAAM,yBAAA,GAA4B,CAAmB,YAAA,KAAuB;AAClF,EAAA,MAAM,EAAE,OAAA,EAAS,KAAA,EAAO,SAAS,SAAA,EAAU,GAAI,mBAAmB,YAAY,CAAA;AAW9E,EAAA,SAAS,SAAA,CAAiC,WAAc,KAAA,EAAY;AACnE,IAAA,MAAM,WAAA,GAAc,QAAA,CAAS,KAAA,EAAO,SAAA,CAAU,QAAQ,CAA4B,CAAA;AAElF,IAAA,OAAO,CAAC,aAAa,OAAO,CAAA;AAAA,EAC7B;AAEA,EAAA,OAAO,EAAE,SAAA,EAAW,KAAA,EAAO,OAAA,EAAS,SAAS,SAAA,EAAU;AACxD","file":"tanstack.js","sourcesContent":["import { createEntityAdapter } from \"@reduxjs/toolkit\";\nimport { Store, useStore } from \"@tanstack/react-store\";\nimport { getEntityActions } from \"../actions.ts\";\nimport { getSelectors } from \"../selectors.ts\";\nimport type { EntityStateAdapter, IdItem } from \"../types.ts\";\n\nexport const entityStoreFactory = <T extends IdItem>(initialState?: T[]) => {\n\tconst adapter = createEntityAdapter<T>();\n\tconst store = new Store(\n\t\tinitialState ? adapter.setAll(adapter.getInitialState(), initialState) : adapter.getInitialState(),\n\t);\n\tconst actions = getEntityActions(adapter, (updater) => store.setState(updater));\n\tconst selectors = getSelectors(adapter.getSelectors());\n\treturn { store, adapter, selectors, actions };\n};\n\nexport const createEntityStoreTanstack = <T extends IdItem>(initialState?: T[]) => {\n\tconst { adapter, store, actions, selectors } = entityStoreFactory(initialState);\n\n\ttype SelectorKey = keyof typeof selectors;\n\ttype SelectorReturn<K extends SelectorKey> = ReturnType<(typeof selectors)[K]>;\n\n\tfunction useEntity(): [SelectorReturn<\"all\">, EntityStateAdapter<T, T[\"id\"]>];\n\tfunction useEntity(selector: \"all\"): [SelectorReturn<\"all\">, EntityStateAdapter<T, T[\"id\"]>];\n\tfunction useEntity<K extends Exclude<SelectorKey, \"all\">>(\n\t\tselector: K,\n\t): [SelectorReturn<K>, EntityStateAdapter<T, T[\"id\"]>];\n\n\tfunction useEntity<K extends SelectorKey>(selector: K = \"all\" as K) {\n\t\tconst entityState = useStore(store, selectors[selector] as () => SelectorReturn<K>);\n\n\t\treturn [entityState, actions] satisfies [SelectorReturn<K>, EntityStateAdapter<T, T[\"id\"]>];\n\t}\n\n\treturn { useEntity, store, actions, adapter, selectors };\n};\n"]}
|