@jlnstack/store 0.0.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 (55) hide show
  1. package/README.md +7 -0
  2. package/dist/core/core.cjs +41 -0
  3. package/dist/core/core.d.cts +13 -0
  4. package/dist/core/core.d.cts.map +1 -0
  5. package/dist/core/core.d.mts +13 -0
  6. package/dist/core/core.d.mts.map +1 -0
  7. package/dist/core/core.mjs +42 -0
  8. package/dist/core/core.mjs.map +1 -0
  9. package/dist/core/plugins/history/index.cjs +43 -0
  10. package/dist/core/plugins/history/index.d.cts +22 -0
  11. package/dist/core/plugins/history/index.d.cts.map +1 -0
  12. package/dist/core/plugins/history/index.d.mts +22 -0
  13. package/dist/core/plugins/history/index.d.mts.map +1 -0
  14. package/dist/core/plugins/history/index.mjs +44 -0
  15. package/dist/core/plugins/history/index.mjs.map +1 -0
  16. package/dist/core/plugins/immer/index.cjs +16 -0
  17. package/dist/core/plugins/immer/index.d.cts +10 -0
  18. package/dist/core/plugins/immer/index.d.cts.map +1 -0
  19. package/dist/core/plugins/immer/index.d.mts +10 -0
  20. package/dist/core/plugins/immer/index.d.mts.map +1 -0
  21. package/dist/core/plugins/immer/index.mjs +17 -0
  22. package/dist/core/plugins/immer/index.mjs.map +1 -0
  23. package/dist/core/plugins/logger/index.cjs +23 -0
  24. package/dist/core/plugins/logger/index.d.cts +14 -0
  25. package/dist/core/plugins/logger/index.d.cts.map +1 -0
  26. package/dist/core/plugins/logger/index.d.mts +14 -0
  27. package/dist/core/plugins/logger/index.d.mts.map +1 -0
  28. package/dist/core/plugins/logger/index.mjs +24 -0
  29. package/dist/core/plugins/logger/index.mjs.map +1 -0
  30. package/dist/core/plugins/plugin.cjs +8 -0
  31. package/dist/core/plugins/plugin.d.cts +18 -0
  32. package/dist/core/plugins/plugin.d.cts.map +1 -0
  33. package/dist/core/plugins/plugin.d.mts +18 -0
  34. package/dist/core/plugins/plugin.d.mts.map +1 -0
  35. package/dist/core/plugins/plugin.mjs +8 -0
  36. package/dist/core/plugins/plugin.mjs.map +1 -0
  37. package/dist/core/plugins/reset/index.cjs +28 -0
  38. package/dist/core/plugins/reset/index.d.cts +14 -0
  39. package/dist/core/plugins/reset/index.d.cts.map +1 -0
  40. package/dist/core/plugins/reset/index.d.mts +14 -0
  41. package/dist/core/plugins/reset/index.d.mts.map +1 -0
  42. package/dist/core/plugins/reset/index.mjs +29 -0
  43. package/dist/core/plugins/reset/index.mjs.map +1 -0
  44. package/dist/core/types.d.cts +20 -0
  45. package/dist/core/types.d.cts.map +1 -0
  46. package/dist/core/types.d.mts +20 -0
  47. package/dist/core/types.d.mts.map +1 -0
  48. package/dist/react/react.cjs +53 -0
  49. package/dist/react/react.d.cts +27 -0
  50. package/dist/react/react.d.cts.map +1 -0
  51. package/dist/react/react.d.mts +27 -0
  52. package/dist/react/react.d.mts.map +1 -0
  53. package/dist/react/react.mjs +54 -0
  54. package/dist/react/react.mjs.map +1 -0
  55. package/package.json +113 -0
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @jlnstack/store
2
+
3
+ Type-safe store for React.
4
+
5
+ Inspired by [TkDodo's Zustand best practices](https://tkdodo.eu/blog/working-with-zustand).
6
+
7
+ [Documentation](https://jlnstack.com/docs/store)
@@ -0,0 +1,41 @@
1
+ let zustand_vanilla = require("zustand/vanilla");
2
+
3
+ //#region src/core/core.ts
4
+ function createStore(options) {
5
+ var _options$plugins$map, _options$plugins;
6
+ const zustandStore = (0, zustand_vanilla.createStore)(() => options.state);
7
+ let pluginResults;
8
+ const baseSetState = (updater) => {
9
+ const prevState = zustandStore.getState();
10
+ const nextState = typeof updater === "function" ? updater(prevState) : updater;
11
+ zustandStore.setState(nextState, true);
12
+ for (const plugin of pluginResults) {
13
+ var _plugin$onStateChange;
14
+ (_plugin$onStateChange = plugin.onStateChange) === null || _plugin$onStateChange === void 0 || _plugin$onStateChange.call(plugin, nextState, prevState);
15
+ }
16
+ };
17
+ let currentSetState = baseSetState;
18
+ const store = {
19
+ getState: zustandStore.getState,
20
+ setStateSilent: (newState) => {
21
+ zustandStore.setState(newState, true);
22
+ },
23
+ setState: (updater) => currentSetState(updater),
24
+ subscribe: zustandStore.subscribe
25
+ };
26
+ pluginResults = (_options$plugins$map = (_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.map((factory) => factory(store))) !== null && _options$plugins$map !== void 0 ? _options$plugins$map : [];
27
+ currentSetState = pluginResults.map((p) => p.middleware).filter((m) => m != null).reduce((setState, middleware) => middleware(setState, store.getState), baseSetState);
28
+ const plugins = {};
29
+ for (const plugin of pluginResults) if ("extend" in plugin && plugin.extend) plugins[plugin.id] = plugin.extend;
30
+ const actions = options.actions(store);
31
+ return {
32
+ state: options.state,
33
+ actions,
34
+ store,
35
+ plugins,
36
+ pluginResults
37
+ };
38
+ }
39
+
40
+ //#endregion
41
+ exports.createStore = createStore;
@@ -0,0 +1,13 @@
1
+ import { Store, StoreApi } from "./types.cjs";
2
+ import { PluginResult } from "./plugins/plugin.cjs";
3
+
4
+ //#region src/core/core.d.ts
5
+ type StoreOptions<TState, TActions, TResults extends PluginResult[]> = {
6
+ state: TState;
7
+ actions: (store: StoreApi<TState>) => TActions;
8
+ plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };
9
+ };
10
+ declare function createStore<TState, TActions, TResults extends PluginResult[]>(options: StoreOptions<TState, TActions, TResults>): Store<TState, TActions, TResults>;
11
+ //#endregion
12
+ export { StoreOptions, createStore };
13
+ //# sourceMappingURL=core.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.cts","names":[],"sources":["../../src/core/core.ts"],"sourcesContent":[],"mappings":";;;;KAIY,gDAAgD;SACnD;EADG,OAAA,EAAA,CAAA,KAAY,EAEL,QAFK,CAEI,MAFJ,CAAA,EAAA,GAEgB,QAFhB;EAAA,OAAA,CAAA,EAAA,QAAoC,MAGlC,QAHkC,GAAA,CAAA,KAAA,EAGf,QAHe,CAGN,MAHM,CAAA,EAAA,GAGM,QAHN,CAGe,CAHf,CAAA;;AAEzC,iBAIH,WAJG,CAAA,MAAA,EAAA,QAAA,EAAA,iBAI4C,YAJ5C,EAAA,CAAA,CAAA,OAAA,EAKR,YALQ,CAKK,MALL,EAKa,QALb,EAKuB,QALvB,CAAA,CAAA,EAMhB,KANgB,CAMV,MANU,EAMF,QANE,EAMQ,QANR,CAAA"}
@@ -0,0 +1,13 @@
1
+ import { Store, StoreApi } from "./types.mjs";
2
+ import { PluginResult } from "./plugins/plugin.mjs";
3
+
4
+ //#region src/core/core.d.ts
5
+ type StoreOptions<TState, TActions, TResults extends PluginResult[]> = {
6
+ state: TState;
7
+ actions: (store: StoreApi<TState>) => TActions;
8
+ plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };
9
+ };
10
+ declare function createStore<TState, TActions, TResults extends PluginResult[]>(options: StoreOptions<TState, TActions, TResults>): Store<TState, TActions, TResults>;
11
+ //#endregion
12
+ export { StoreOptions, createStore };
13
+ //# sourceMappingURL=core.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.mts","names":[],"sources":["../../src/core/core.ts"],"sourcesContent":[],"mappings":";;;;KAIY,gDAAgD;SACnD;EADG,OAAA,EAAA,CAAA,KAAY,EAEL,QAFK,CAEI,MAFJ,CAAA,EAAA,GAEgB,QAFhB;EAAA,OAAA,CAAA,EAAA,QAAoC,MAGlC,QAHkC,GAAA,CAAA,KAAA,EAGf,QAHe,CAGN,MAHM,CAAA,EAAA,GAGM,QAHN,CAGe,CAHf,CAAA;;AAEzC,iBAIH,WAJG,CAAA,MAAA,EAAA,QAAA,EAAA,iBAI4C,YAJ5C,EAAA,CAAA,CAAA,OAAA,EAKR,YALQ,CAKK,MALL,EAKa,QALb,EAKuB,QALvB,CAAA,CAAA,EAMhB,KANgB,CAMV,MANU,EAMF,QANE,EAMQ,QANR,CAAA"}
@@ -0,0 +1,42 @@
1
+ import { createStore as createStore$1 } from "zustand/vanilla";
2
+
3
+ //#region src/core/core.ts
4
+ function createStore(options) {
5
+ var _options$plugins$map, _options$plugins;
6
+ const zustandStore = createStore$1(() => options.state);
7
+ let pluginResults;
8
+ const baseSetState = (updater) => {
9
+ const prevState = zustandStore.getState();
10
+ const nextState = typeof updater === "function" ? updater(prevState) : updater;
11
+ zustandStore.setState(nextState, true);
12
+ for (const plugin of pluginResults) {
13
+ var _plugin$onStateChange;
14
+ (_plugin$onStateChange = plugin.onStateChange) === null || _plugin$onStateChange === void 0 || _plugin$onStateChange.call(plugin, nextState, prevState);
15
+ }
16
+ };
17
+ let currentSetState = baseSetState;
18
+ const store = {
19
+ getState: zustandStore.getState,
20
+ setStateSilent: (newState) => {
21
+ zustandStore.setState(newState, true);
22
+ },
23
+ setState: (updater) => currentSetState(updater),
24
+ subscribe: zustandStore.subscribe
25
+ };
26
+ pluginResults = (_options$plugins$map = (_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.map((factory) => factory(store))) !== null && _options$plugins$map !== void 0 ? _options$plugins$map : [];
27
+ currentSetState = pluginResults.map((p) => p.middleware).filter((m) => m != null).reduce((setState, middleware) => middleware(setState, store.getState), baseSetState);
28
+ const plugins = {};
29
+ for (const plugin of pluginResults) if ("extend" in plugin && plugin.extend) plugins[plugin.id] = plugin.extend;
30
+ const actions = options.actions(store);
31
+ return {
32
+ state: options.state,
33
+ actions,
34
+ store,
35
+ plugins,
36
+ pluginResults
37
+ };
38
+ }
39
+
40
+ //#endregion
41
+ export { createStore };
42
+ //# sourceMappingURL=core.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.mjs","names":["zustandCreateStore"],"sources":["../../src/core/core.ts"],"sourcesContent":["import { createStore as zustandCreateStore } from \"zustand/vanilla\";\nimport type { ExtractPlugins, PluginResult } from \"./plugins/plugin\";\nimport type { SetState, Store, StoreApi } from \"./types\";\n\nexport type StoreOptions<TState, TActions, TResults extends PluginResult[]> = {\n state: TState;\n actions: (store: StoreApi<TState>) => TActions;\n plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };\n};\n\nexport function createStore<TState, TActions, TResults extends PluginResult[]>(\n options: StoreOptions<TState, TActions, TResults>,\n): Store<TState, TActions, TResults> {\n const zustandStore = zustandCreateStore<TState>(() => options.state);\n let pluginResults: TResults;\n\n const baseSetState: SetState<TState> = (updater) => {\n const prevState = zustandStore.getState();\n const nextState =\n typeof updater === \"function\"\n ? (updater as (s: TState) => TState)(prevState)\n : updater;\n\n zustandStore.setState(nextState, true);\n\n for (const plugin of pluginResults) {\n plugin.onStateChange?.(nextState, prevState);\n }\n };\n\n let currentSetState = baseSetState;\n\n const store: StoreApi<TState> = {\n getState: zustandStore.getState,\n setStateSilent: (newState) => {\n zustandStore.setState(newState, true);\n },\n setState: (updater) => currentSetState(updater),\n subscribe: zustandStore.subscribe,\n };\n\n pluginResults = (options.plugins?.map((factory) => factory(store)) ??\n []) as TResults;\n\n const middlewares = pluginResults\n .map((p) => p.middleware)\n .filter((m): m is NonNullable<typeof m> => m != null);\n\n currentSetState = middlewares.reduce<SetState<TState>>(\n (setState, middleware) => middleware(setState, store.getState),\n baseSetState,\n );\n\n const plugins = {} as ExtractPlugins<TResults>;\n for (const plugin of pluginResults) {\n if (\"extend\" in plugin && plugin.extend) {\n (plugins as Record<string, unknown>)[plugin.id] = plugin.extend;\n }\n }\n\n const actions = options.actions(store);\n\n return {\n state: options.state,\n actions,\n store,\n plugins,\n pluginResults,\n };\n}\n"],"mappings":";;;AAUA,SAAgB,YACd,SACmC;;CACnC,MAAM,eAAeA,oBAAiC,QAAQ,MAAM;CACpE,IAAI;CAEJ,MAAM,gBAAkC,YAAY;EAClD,MAAM,YAAY,aAAa,UAAU;EACzC,MAAM,YACJ,OAAO,YAAY,aACd,QAAkC,UAAU,GAC7C;AAEN,eAAa,SAAS,WAAW,KAAK;AAEtC,OAAK,MAAM,UAAU,eAAe;;AAClC,mCAAO,kGAAgB,WAAW,UAAU;;;CAIhD,IAAI,kBAAkB;CAEtB,MAAM,QAA0B;EAC9B,UAAU,aAAa;EACvB,iBAAiB,aAAa;AAC5B,gBAAa,SAAS,UAAU,KAAK;;EAEvC,WAAW,YAAY,gBAAgB,QAAQ;EAC/C,WAAW,aAAa;EACzB;AAED,6DAAiB,QAAQ,6EAAS,KAAK,YAAY,QAAQ,MAAM,CAAC,uEAChE,EAAE;AAMJ,mBAJoB,cACjB,KAAK,MAAM,EAAE,WAAW,CACxB,QAAQ,MAAkC,KAAK,KAAK,CAEzB,QAC3B,UAAU,eAAe,WAAW,UAAU,MAAM,SAAS,EAC9D,aACD;CAED,MAAM,UAAU,EAAE;AAClB,MAAK,MAAM,UAAU,cACnB,KAAI,YAAY,UAAU,OAAO,OAC/B,CAAC,QAAoC,OAAO,MAAM,OAAO;CAI7D,MAAM,UAAU,QAAQ,QAAQ,MAAM;AAEtC,QAAO;EACL,OAAO,QAAQ;EACf;EACA;EACA;EACA;EACD"}
@@ -0,0 +1,43 @@
1
+ const require_plugin = require('../plugin.cjs');
2
+
3
+ //#region src/core/plugins/history/index.ts
4
+ function history(options = {}) {
5
+ const { limit = 100 } = options;
6
+ return require_plugin.definePlugin((store) => {
7
+ const past = [];
8
+ const future = [];
9
+ return {
10
+ id: "history",
11
+ onStateChange: (_state, prevState) => {
12
+ past.push(prevState);
13
+ if (past.length > limit) past.shift();
14
+ future.length = 0;
15
+ },
16
+ extend: {
17
+ undo: () => {
18
+ const prev = past.pop();
19
+ if (prev === void 0) return;
20
+ future.push(store.getState());
21
+ store.setStateSilent(prev);
22
+ },
23
+ redo: () => {
24
+ const next = future.pop();
25
+ if (next === void 0) return;
26
+ past.push(store.getState());
27
+ store.setStateSilent(next);
28
+ },
29
+ clear: () => {
30
+ past.length = 0;
31
+ future.length = 0;
32
+ },
33
+ canUndo: () => past.length > 0,
34
+ canRedo: () => future.length > 0,
35
+ pastStates: () => [...past],
36
+ futureStates: () => [...future]
37
+ }
38
+ };
39
+ });
40
+ }
41
+
42
+ //#endregion
43
+ exports.history = history;
@@ -0,0 +1,22 @@
1
+ import { StoreApi } from "../../types.cjs";
2
+
3
+ //#region src/core/plugins/history/index.d.ts
4
+ interface HistoryOptions {
5
+ limit?: number;
6
+ }
7
+ declare function history(options?: HistoryOptions): <TState>(store: StoreApi<TState>) => {
8
+ id: "history";
9
+ onStateChange: (_state: any, prevState: any) => void;
10
+ extend: {
11
+ undo: () => void;
12
+ redo: () => void;
13
+ clear: () => void;
14
+ canUndo: () => boolean;
15
+ canRedo: () => boolean;
16
+ pastStates: () => TState[];
17
+ futureStates: () => TState[];
18
+ };
19
+ };
20
+ //#endregion
21
+ export { HistoryOptions, history };
22
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../../src/core/plugins/history/index.ts"],"sourcesContent":[],"mappings":";;;UAGiB,cAAA;;AAAjB;AAIgB,iBAAA,OAAA,CAAO,OAAA,CAAA,EAAU,cAAV,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,KAAA,EAGe,QAHf,CAGwB,MAHxB,CAAA,EAAA,GAAA;EAAA,EAAA,EAAA,SAAA;eAAU,EAAA,CAAA,MAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA;QAGc,EAAA;IAAT,IAAA,EAAA,GAAA,GAAA,IAAA"}
@@ -0,0 +1,22 @@
1
+ import { StoreApi } from "../../types.mjs";
2
+
3
+ //#region src/core/plugins/history/index.d.ts
4
+ interface HistoryOptions {
5
+ limit?: number;
6
+ }
7
+ declare function history(options?: HistoryOptions): <TState>(store: StoreApi<TState>) => {
8
+ id: "history";
9
+ onStateChange: (_state: any, prevState: any) => void;
10
+ extend: {
11
+ undo: () => void;
12
+ redo: () => void;
13
+ clear: () => void;
14
+ canUndo: () => boolean;
15
+ canRedo: () => boolean;
16
+ pastStates: () => TState[];
17
+ futureStates: () => TState[];
18
+ };
19
+ };
20
+ //#endregion
21
+ export { HistoryOptions, history };
22
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../../src/core/plugins/history/index.ts"],"sourcesContent":[],"mappings":";;;UAGiB,cAAA;;AAAjB;AAIgB,iBAAA,OAAA,CAAO,OAAA,CAAA,EAAU,cAAV,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,KAAA,EAGe,QAHf,CAGwB,MAHxB,CAAA,EAAA,GAAA;EAAA,EAAA,EAAA,SAAA;eAAU,EAAA,CAAA,MAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA;QAGc,EAAA;IAAT,IAAA,EAAA,GAAA,GAAA,IAAA"}
@@ -0,0 +1,44 @@
1
+ import { definePlugin } from "../plugin.mjs";
2
+
3
+ //#region src/core/plugins/history/index.ts
4
+ function history(options = {}) {
5
+ const { limit = 100 } = options;
6
+ return definePlugin((store) => {
7
+ const past = [];
8
+ const future = [];
9
+ return {
10
+ id: "history",
11
+ onStateChange: (_state, prevState) => {
12
+ past.push(prevState);
13
+ if (past.length > limit) past.shift();
14
+ future.length = 0;
15
+ },
16
+ extend: {
17
+ undo: () => {
18
+ const prev = past.pop();
19
+ if (prev === void 0) return;
20
+ future.push(store.getState());
21
+ store.setStateSilent(prev);
22
+ },
23
+ redo: () => {
24
+ const next = future.pop();
25
+ if (next === void 0) return;
26
+ past.push(store.getState());
27
+ store.setStateSilent(next);
28
+ },
29
+ clear: () => {
30
+ past.length = 0;
31
+ future.length = 0;
32
+ },
33
+ canUndo: () => past.length > 0,
34
+ canRedo: () => future.length > 0,
35
+ pastStates: () => [...past],
36
+ futureStates: () => [...future]
37
+ }
38
+ };
39
+ });
40
+ }
41
+
42
+ //#endregion
43
+ export { history };
44
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/core/plugins/history/index.ts"],"sourcesContent":["import type { StoreApi } from \"../../types\";\nimport { definePlugin } from \"../plugin\";\n\nexport interface HistoryOptions {\n limit?: number;\n}\n\nexport function history(options: HistoryOptions = {}) {\n const { limit = 100 } = options;\n\n return definePlugin(<TState>(store: StoreApi<TState>) => {\n const past: TState[] = [];\n const future: TState[] = [];\n\n return {\n id: \"history\",\n onStateChange: (_state, prevState) => {\n past.push(prevState);\n if (past.length > limit) past.shift();\n future.length = 0;\n },\n extend: {\n undo: () => {\n const prev = past.pop();\n if (prev === undefined) return;\n\n future.push(store.getState());\n store.setStateSilent(prev);\n },\n redo: () => {\n const next = future.pop();\n if (next === undefined) return;\n\n past.push(store.getState());\n store.setStateSilent(next);\n },\n clear: () => {\n past.length = 0;\n future.length = 0;\n },\n canUndo: () => past.length > 0,\n canRedo: () => future.length > 0,\n pastStates: () => [...past],\n futureStates: () => [...future],\n },\n };\n });\n}\n"],"mappings":";;;AAOA,SAAgB,QAAQ,UAA0B,EAAE,EAAE;CACpD,MAAM,EAAE,QAAQ,QAAQ;AAExB,QAAO,cAAsB,UAA4B;EACvD,MAAM,OAAiB,EAAE;EACzB,MAAM,SAAmB,EAAE;AAE3B,SAAO;GACL,IAAI;GACJ,gBAAgB,QAAQ,cAAc;AACpC,SAAK,KAAK,UAAU;AACpB,QAAI,KAAK,SAAS,MAAO,MAAK,OAAO;AACrC,WAAO,SAAS;;GAElB,QAAQ;IACN,YAAY;KACV,MAAM,OAAO,KAAK,KAAK;AACvB,SAAI,SAAS,OAAW;AAExB,YAAO,KAAK,MAAM,UAAU,CAAC;AAC7B,WAAM,eAAe,KAAK;;IAE5B,YAAY;KACV,MAAM,OAAO,OAAO,KAAK;AACzB,SAAI,SAAS,OAAW;AAExB,UAAK,KAAK,MAAM,UAAU,CAAC;AAC3B,WAAM,eAAe,KAAK;;IAE5B,aAAa;AACX,UAAK,SAAS;AACd,YAAO,SAAS;;IAElB,eAAe,KAAK,SAAS;IAC7B,eAAe,OAAO,SAAS;IAC/B,kBAAkB,CAAC,GAAG,KAAK;IAC3B,oBAAoB,CAAC,GAAG,OAAO;IAChC;GACF;GACD"}
@@ -0,0 +1,16 @@
1
+ const require_plugin = require('../plugin.cjs');
2
+ let immer = require("immer");
3
+
4
+ //#region src/core/plugins/immer/index.ts
5
+ function immer$1() {
6
+ return require_plugin.definePlugin((_store) => ({
7
+ id: "immer",
8
+ middleware: (setState, getState) => (updater) => {
9
+ if (typeof updater === "function") setState((0, immer.produce)(getState(), updater));
10
+ else setState(updater);
11
+ }
12
+ }));
13
+ }
14
+
15
+ //#endregion
16
+ exports.immer = immer$1;
@@ -0,0 +1,10 @@
1
+ import { SetState, StoreApi } from "../../types.cjs";
2
+
3
+ //#region src/core/plugins/immer/index.d.ts
4
+ declare function immer(): <TState>(_store: StoreApi<TState>) => {
5
+ id: "immer";
6
+ middleware: (setState: SetState<any>, getState: () => any) => (updater: unknown) => void;
7
+ };
8
+ //#endregion
9
+ export { immer };
10
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../../src/core/plugins/immer/index.ts"],"sourcesContent":[],"mappings":";;;iBAIgB,KAAA,CAAA,oBACuB,SAAS;;EADhC,UAAK,EAAA,CAAA,QAAA,EAC0B,QAD1B,CAAA,GAAA,CAAA,EAAA,QAAA,EAAA,GAAA,GAAA,GAAA,EAAA,GAAA,CAAA,OAAA,EAAA,OAAA,EAAA,GAAA,IAAA;CAAA"}
@@ -0,0 +1,10 @@
1
+ import { SetState, StoreApi } from "../../types.mjs";
2
+
3
+ //#region src/core/plugins/immer/index.d.ts
4
+ declare function immer(): <TState>(_store: StoreApi<TState>) => {
5
+ id: "immer";
6
+ middleware: (setState: SetState<any>, getState: () => any) => (updater: unknown) => void;
7
+ };
8
+ //#endregion
9
+ export { immer };
10
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../../src/core/plugins/immer/index.ts"],"sourcesContent":[],"mappings":";;;iBAIgB,KAAA,CAAA,oBACuB,SAAS;;EADhC,UAAK,EAAA,CAAA,QAAA,EAC0B,QAD1B,CAAA,GAAA,CAAA,EAAA,QAAA,EAAA,GAAA,GAAA,GAAA,EAAA,GAAA,CAAA,OAAA,EAAA,OAAA,EAAA,GAAA,IAAA;CAAA"}
@@ -0,0 +1,17 @@
1
+ import { definePlugin } from "../plugin.mjs";
2
+ import { produce } from "immer";
3
+
4
+ //#region src/core/plugins/immer/index.ts
5
+ function immer() {
6
+ return definePlugin((_store) => ({
7
+ id: "immer",
8
+ middleware: (setState, getState) => (updater) => {
9
+ if (typeof updater === "function") setState(produce(getState(), updater));
10
+ else setState(updater);
11
+ }
12
+ }));
13
+ }
14
+
15
+ //#endregion
16
+ export { immer };
17
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/core/plugins/immer/index.ts"],"sourcesContent":["import { produce } from \"immer\";\nimport type { StoreApi } from \"../../types\";\nimport { definePlugin } from \"../plugin\";\n\nexport function immer() {\n return definePlugin(<TState>(_store: StoreApi<TState>) => ({\n id: \"immer\",\n middleware: (setState, getState) => (updater: unknown) => {\n if (typeof updater === \"function\") {\n setState(produce(getState(), updater as (draft: any) => void));\n } else {\n setState(updater);\n }\n },\n }));\n}\n"],"mappings":";;;;AAIA,SAAgB,QAAQ;AACtB,QAAO,cAAsB,YAA8B;EACzD,IAAI;EACJ,aAAa,UAAU,cAAc,YAAqB;AACxD,OAAI,OAAO,YAAY,WACrB,UAAS,QAAQ,UAAU,EAAE,QAAgC,CAAC;OAE9D,UAAS,QAAQ;;EAGtB,EAAE"}
@@ -0,0 +1,23 @@
1
+ const require_plugin = require('../plugin.cjs');
2
+
3
+ //#region src/core/plugins/logger/index.ts
4
+ function logger(options) {
5
+ const { enabled = true, name } = options;
6
+ return require_plugin.definePlugin((_store) => ({
7
+ id: "logger",
8
+ onStateChange: (state, prevState) => {
9
+ if (!enabled) return;
10
+ const changes = getChanges(prevState, state);
11
+ if (Object.keys(changes).length === 0) return;
12
+ console.log(`%c ${name} %c setState `, "background: #a3e635; color: black; padding: 2px;", "background: #65a30d; color: white; padding: 2px; font-weight: bold;", changes);
13
+ }
14
+ }));
15
+ }
16
+ function getChanges(prev, next) {
17
+ const changes = {};
18
+ for (const key of Object.keys(next)) if (prev[key] !== next[key]) changes[key] = next[key];
19
+ return changes;
20
+ }
21
+
22
+ //#endregion
23
+ exports.logger = logger;
@@ -0,0 +1,14 @@
1
+ import { StoreApi } from "../../types.cjs";
2
+
3
+ //#region src/core/plugins/logger/index.d.ts
4
+ interface LoggerOptions {
5
+ name: string;
6
+ enabled?: boolean;
7
+ }
8
+ declare function logger(options: LoggerOptions): <TState>(_store: StoreApi<TState>) => {
9
+ id: "logger";
10
+ onStateChange: (state: any, prevState: any) => void;
11
+ };
12
+ //#endregion
13
+ export { logger };
14
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../../src/core/plugins/logger/index.ts"],"sourcesContent":[],"mappings":";;;UAGU,aAAA;;EAAA,OAAA,CAAA,EAAA,OAAa;AAKvB;AAAsB,iBAAN,MAAA,CAAM,OAAA,EAAU,aAAV,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,MAAA,EAGiB,QAHjB,CAG0B,MAH1B,CAAA,EAAA,GAAA;MAAU,QAAA;eAGgB,EAAA,CAAA,KAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA"}
@@ -0,0 +1,14 @@
1
+ import { StoreApi } from "../../types.mjs";
2
+
3
+ //#region src/core/plugins/logger/index.d.ts
4
+ interface LoggerOptions {
5
+ name: string;
6
+ enabled?: boolean;
7
+ }
8
+ declare function logger(options: LoggerOptions): <TState>(_store: StoreApi<TState>) => {
9
+ id: "logger";
10
+ onStateChange: (state: any, prevState: any) => void;
11
+ };
12
+ //#endregion
13
+ export { logger };
14
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../../src/core/plugins/logger/index.ts"],"sourcesContent":[],"mappings":";;;UAGU,aAAA;;EAAA,OAAA,CAAA,EAAA,OAAa;AAKvB;AAAsB,iBAAN,MAAA,CAAM,OAAA,EAAU,aAAV,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,MAAA,EAGiB,QAHjB,CAG0B,MAH1B,CAAA,EAAA,GAAA;MAAU,QAAA;eAGgB,EAAA,CAAA,KAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA"}
@@ -0,0 +1,24 @@
1
+ import { definePlugin } from "../plugin.mjs";
2
+
3
+ //#region src/core/plugins/logger/index.ts
4
+ function logger(options) {
5
+ const { enabled = true, name } = options;
6
+ return definePlugin((_store) => ({
7
+ id: "logger",
8
+ onStateChange: (state, prevState) => {
9
+ if (!enabled) return;
10
+ const changes = getChanges(prevState, state);
11
+ if (Object.keys(changes).length === 0) return;
12
+ console.log(`%c ${name} %c setState `, "background: #a3e635; color: black; padding: 2px;", "background: #65a30d; color: white; padding: 2px; font-weight: bold;", changes);
13
+ }
14
+ }));
15
+ }
16
+ function getChanges(prev, next) {
17
+ const changes = {};
18
+ for (const key of Object.keys(next)) if (prev[key] !== next[key]) changes[key] = next[key];
19
+ return changes;
20
+ }
21
+
22
+ //#endregion
23
+ export { logger };
24
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/core/plugins/logger/index.ts"],"sourcesContent":["import type { StoreApi } from \"../../types\";\nimport { definePlugin } from \"../plugin\";\n\ninterface LoggerOptions {\n name: string;\n enabled?: boolean;\n}\n\nexport function logger(options: LoggerOptions) {\n const { enabled = true, name } = options;\n\n return definePlugin(<TState>(_store: StoreApi<TState>) => ({\n id: \"logger\",\n onStateChange: (state, prevState) => {\n if (!enabled) return;\n\n const changes = getChanges(prevState as object, state as object);\n if (Object.keys(changes).length === 0) return;\n\n console.log(\n `%c ${name} %c setState `,\n \"background: #a3e635; color: black; padding: 2px;\",\n \"background: #65a30d; color: white; padding: 2px; font-weight: bold;\",\n changes,\n );\n },\n }));\n}\n\nfunction getChanges(prev: object, next: object): Record<string, unknown> {\n const changes: Record<string, unknown> = {};\n for (const key of Object.keys(next)) {\n if (prev[key as keyof typeof prev] !== next[key as keyof typeof next]) {\n changes[key] = next[key as keyof typeof next];\n }\n }\n return changes;\n}\n"],"mappings":";;;AAQA,SAAgB,OAAO,SAAwB;CAC7C,MAAM,EAAE,UAAU,MAAM,SAAS;AAEjC,QAAO,cAAsB,YAA8B;EACzD,IAAI;EACJ,gBAAgB,OAAO,cAAc;AACnC,OAAI,CAAC,QAAS;GAEd,MAAM,UAAU,WAAW,WAAqB,MAAgB;AAChE,OAAI,OAAO,KAAK,QAAQ,CAAC,WAAW,EAAG;AAEvC,WAAQ,IACN,MAAM,KAAK,gBACX,oDACA,uEACA,QACD;;EAEJ,EAAE;;AAGL,SAAS,WAAW,MAAc,MAAuC;CACvE,MAAM,UAAmC,EAAE;AAC3C,MAAK,MAAM,OAAO,OAAO,KAAK,KAAK,CACjC,KAAI,KAAK,SAA8B,KAAK,KAC1C,SAAQ,OAAO,KAAK;AAGxB,QAAO"}
@@ -0,0 +1,8 @@
1
+
2
+ //#region src/core/plugins/plugin.ts
3
+ function definePlugin(factory) {
4
+ return factory;
5
+ }
6
+
7
+ //#endregion
8
+ exports.definePlugin = definePlugin;
@@ -0,0 +1,18 @@
1
+ import { SetState } from "../types.cjs";
2
+
3
+ //#region src/core/plugins/plugin.d.ts
4
+ type LiteralString = "" | (string & Record<never, never>);
5
+ type Middleware = (setState: SetState<any>, getState: () => any) => SetState<any>;
6
+ type PluginShape = {
7
+ id: LiteralString;
8
+ extend?: unknown;
9
+ onStateChange?: (state: any, prevState: any) => void;
10
+ middleware?: Middleware;
11
+ };
12
+ type PluginResult = PluginShape;
13
+ type ExtractPlugins<TResults extends PluginResult[]> = { [K in TResults[number] as K["id"]]: K extends {
14
+ extend: infer E;
15
+ } ? E : never };
16
+ //#endregion
17
+ export { ExtractPlugins, Middleware, PluginResult };
18
+ //# sourceMappingURL=plugin.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.cts","names":[],"sources":["../../../src/core/plugins/plugin.ts"],"sourcesContent":[],"mappings":";;;KAEK,aAAA,kBAA+B;KAExB,UAAA,cACA,uCAEP;AALA,KAOO,WAAA,GAPM;EAEN,EAAA,EAMN,aANgB;EAAA,MAAA,CAAA,EAAA,OAAA;eACV,CAAA,EAAA,CAAA,KAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA;YAEP,CAAA,EAMU,UANV;CAAQ;AAED,KAOA,YAAA,GAAe,WAPJ;AACjB,KAeM,cAfN,CAAA,iBAesC,YAftC,EAAA,CAAA,GAAA,QAgBE,QAbO,CAAA,MAAA,CAAA,IAaa,CAbb,CAAA,IAAA,CAAA,GAauB,CAbvB,SAAA;EAAU,MAAA,EAAA,KAAA,EAAA;AAGb,CAAA,GAU0D,CAV1D,GAAA,KAAA,EASZ"}
@@ -0,0 +1,18 @@
1
+ import { SetState } from "../types.mjs";
2
+
3
+ //#region src/core/plugins/plugin.d.ts
4
+ type LiteralString = "" | (string & Record<never, never>);
5
+ type Middleware = (setState: SetState<any>, getState: () => any) => SetState<any>;
6
+ type PluginShape = {
7
+ id: LiteralString;
8
+ extend?: unknown;
9
+ onStateChange?: (state: any, prevState: any) => void;
10
+ middleware?: Middleware;
11
+ };
12
+ type PluginResult = PluginShape;
13
+ type ExtractPlugins<TResults extends PluginResult[]> = { [K in TResults[number] as K["id"]]: K extends {
14
+ extend: infer E;
15
+ } ? E : never };
16
+ //#endregion
17
+ export { ExtractPlugins, Middleware, PluginResult };
18
+ //# sourceMappingURL=plugin.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.mts","names":[],"sources":["../../../src/core/plugins/plugin.ts"],"sourcesContent":[],"mappings":";;;KAEK,aAAA,kBAA+B;KAExB,UAAA,cACA,uCAEP;AALA,KAOO,WAAA,GAPM;EAEN,EAAA,EAMN,aANgB;EAAA,MAAA,CAAA,EAAA,OAAA;eACV,CAAA,EAAA,CAAA,KAAA,EAAA,GAAA,EAAA,SAAA,EAAA,GAAA,EAAA,GAAA,IAAA;YAEP,CAAA,EAMU,UANV;CAAQ;AAED,KAOA,YAAA,GAAe,WAPJ;AACjB,KAeM,cAfN,CAAA,iBAesC,YAftC,EAAA,CAAA,GAAA,QAgBE,QAbO,CAAA,MAAA,CAAA,IAaa,CAbb,CAAA,IAAA,CAAA,GAauB,CAbvB,SAAA;EAAU,MAAA,EAAA,KAAA,EAAA;AAGb,CAAA,GAU0D,CAV1D,GAAA,KAAA,EASZ"}
@@ -0,0 +1,8 @@
1
+ //#region src/core/plugins/plugin.ts
2
+ function definePlugin(factory) {
3
+ return factory;
4
+ }
5
+
6
+ //#endregion
7
+ export { definePlugin };
8
+ //# sourceMappingURL=plugin.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.mjs","names":[],"sources":["../../../src/core/plugins/plugin.ts"],"sourcesContent":["import type { SetState, StoreApi } from \"../types\";\n\ntype LiteralString = \"\" | (string & Record<never, never>);\n\nexport type Middleware = (\n setState: SetState<any>,\n getState: () => any,\n) => SetState<any>;\n\nexport type PluginShape = {\n id: LiteralString;\n extend?: unknown;\n onStateChange?: (state: any, prevState: any) => void;\n middleware?: Middleware;\n};\n\nexport type PluginResult = PluginShape;\n\nexport function definePlugin<\n TResult extends PluginShape,\n TFactory extends <TState>(store: StoreApi<TState>) => TResult,\n>(factory: TFactory): TFactory {\n return factory;\n}\n\nexport type ExtractPlugins<TResults extends PluginResult[]> = {\n [K in TResults[number] as K[\"id\"]]: K extends { extend: infer E } ? E : never;\n};\n"],"mappings":";AAkBA,SAAgB,aAGd,SAA6B;AAC7B,QAAO"}
@@ -0,0 +1,28 @@
1
+ const require_plugin = require('../plugin.cjs');
2
+
3
+ //#region src/core/plugins/reset/index.ts
4
+ function reset() {
5
+ return require_plugin.definePlugin((store) => {
6
+ let changed = false;
7
+ const initialState = store.getState();
8
+ return {
9
+ id: "reset",
10
+ onStateChange: () => {
11
+ changed = true;
12
+ },
13
+ extend: {
14
+ reset: () => {
15
+ store.setState(initialState);
16
+ changed = false;
17
+ return initialState;
18
+ },
19
+ canReset: () => {
20
+ return changed;
21
+ }
22
+ }
23
+ };
24
+ });
25
+ }
26
+
27
+ //#endregion
28
+ exports.reset = reset;
@@ -0,0 +1,14 @@
1
+ import { StoreApi } from "../../types.cjs";
2
+
3
+ //#region src/core/plugins/reset/index.d.ts
4
+ declare function reset(): <TState>(store: StoreApi<TState>) => {
5
+ id: "reset";
6
+ onStateChange: () => void;
7
+ extend: {
8
+ reset: () => TState;
9
+ canReset: () => boolean;
10
+ };
11
+ };
12
+ //#endregion
13
+ export { reset };
14
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../../src/core/plugins/reset/index.ts"],"sourcesContent":[],"mappings":";;;iBAGgB,KAAA,CAAA,mBACsB,SAAS;;EAD/B,aAAK,EAAA,GAAA,GAAA,IAAA;EAAA,MAAA,EAAA;IAC0B,KAAA,EAAA,GAAA,SAAA;IAAT,QAAA,EAAA,GAAA,GAAA,OAAA"}
@@ -0,0 +1,14 @@
1
+ import { StoreApi } from "../../types.mjs";
2
+
3
+ //#region src/core/plugins/reset/index.d.ts
4
+ declare function reset(): <TState>(store: StoreApi<TState>) => {
5
+ id: "reset";
6
+ onStateChange: () => void;
7
+ extend: {
8
+ reset: () => TState;
9
+ canReset: () => boolean;
10
+ };
11
+ };
12
+ //#endregion
13
+ export { reset };
14
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../../src/core/plugins/reset/index.ts"],"sourcesContent":[],"mappings":";;;iBAGgB,KAAA,CAAA,mBACsB,SAAS;;EAD/B,aAAK,EAAA,GAAA,GAAA,IAAA;EAAA,MAAA,EAAA;IAC0B,KAAA,EAAA,GAAA,SAAA;IAAT,QAAA,EAAA,GAAA,GAAA,OAAA"}
@@ -0,0 +1,29 @@
1
+ import { definePlugin } from "../plugin.mjs";
2
+
3
+ //#region src/core/plugins/reset/index.ts
4
+ function reset() {
5
+ return definePlugin((store) => {
6
+ let changed = false;
7
+ const initialState = store.getState();
8
+ return {
9
+ id: "reset",
10
+ onStateChange: () => {
11
+ changed = true;
12
+ },
13
+ extend: {
14
+ reset: () => {
15
+ store.setState(initialState);
16
+ changed = false;
17
+ return initialState;
18
+ },
19
+ canReset: () => {
20
+ return changed;
21
+ }
22
+ }
23
+ };
24
+ });
25
+ }
26
+
27
+ //#endregion
28
+ export { reset };
29
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/core/plugins/reset/index.ts"],"sourcesContent":["import type { StoreApi } from \"../../types\";\nimport { definePlugin } from \"../plugin\";\n\nexport function reset() {\n return definePlugin(<TState>(store: StoreApi<TState>) => {\n let changed = false;\n const initialState = store.getState();\n return {\n id: \"reset\",\n onStateChange: () => {\n changed = true;\n },\n extend: {\n reset: () => {\n store.setState(initialState);\n changed = false;\n return initialState;\n },\n canReset: () => {\n return changed;\n },\n },\n };\n });\n}\n"],"mappings":";;;AAGA,SAAgB,QAAQ;AACtB,QAAO,cAAsB,UAA4B;EACvD,IAAI,UAAU;EACd,MAAM,eAAe,MAAM,UAAU;AACrC,SAAO;GACL,IAAI;GACJ,qBAAqB;AACnB,cAAU;;GAEZ,QAAQ;IACN,aAAa;AACX,WAAM,SAAS,aAAa;AAC5B,eAAU;AACV,YAAO;;IAET,gBAAgB;AACd,YAAO;;IAEV;GACF;GACD"}
@@ -0,0 +1,20 @@
1
+ import { ExtractPlugins, Middleware, PluginResult } from "./plugins/plugin.cjs";
2
+
3
+ //#region src/core/types.d.ts
4
+ type SetState<TState> = (updater: TState | ((state: TState) => TState)) => void;
5
+ type StoreApi<TState> = {
6
+ setState: SetState<TState>;
7
+ setStateSilent: (state: TState) => void;
8
+ getState: () => TState;
9
+ subscribe: (listener: () => void) => () => void;
10
+ };
11
+ type Store<TState, TActions, TResults extends PluginResult[]> = {
12
+ state: TState;
13
+ actions: TActions;
14
+ store: StoreApi<TState>;
15
+ plugins: ExtractPlugins<TResults>;
16
+ pluginResults: TResults;
17
+ };
18
+ //#endregion
19
+ export { SetState, Store, StoreApi };
20
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/core/types.ts"],"sourcesContent":[],"mappings":";;;KAEY,6BACD,kBAAkB,WAAW;KAG5B;EAJA,QAAA,EAKA,QALQ,CAKC,MALD,CAAA;EAAA,cAAA,EAAA,CAAA,KAAA,EAMM,MANN,EAAA,GAAA,IAAA;UACT,EAAA,GAAA,GAMO,MANP;WAAkB,EAAA,CAAA,QAAA,EAAA,GAAA,GAAA,IAAA,EAAA,GAAA,GAAA,GAAA,IAAA;;AAIR,KAgBT,KAhBS,CAAA,MAAA,EAAA,QAAA,EAAA,iBAgBgC,YAhBhC,EAAA,CAAA,GAAA;OAAT,EAiBH,MAjBG;SACc,EAiBf,QAjBe;OACR,EAiBT,QAjBS,CAiBA,MAjBA,CAAA;EAAM,OAAA,EAkBb,cAlBa,CAkBE,QAlBF,CAAA;EAcZ,aAAK,EAKA,QALA;CAAA"}
@@ -0,0 +1,20 @@
1
+ import { ExtractPlugins, Middleware, PluginResult } from "./plugins/plugin.mjs";
2
+
3
+ //#region src/core/types.d.ts
4
+ type SetState<TState> = (updater: TState | ((state: TState) => TState)) => void;
5
+ type StoreApi<TState> = {
6
+ setState: SetState<TState>;
7
+ setStateSilent: (state: TState) => void;
8
+ getState: () => TState;
9
+ subscribe: (listener: () => void) => () => void;
10
+ };
11
+ type Store<TState, TActions, TResults extends PluginResult[]> = {
12
+ state: TState;
13
+ actions: TActions;
14
+ store: StoreApi<TState>;
15
+ plugins: ExtractPlugins<TResults>;
16
+ pluginResults: TResults;
17
+ };
18
+ //#endregion
19
+ export { SetState, Store, StoreApi };
20
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","names":[],"sources":["../../src/core/types.ts"],"sourcesContent":[],"mappings":";;;KAEY,6BACD,kBAAkB,WAAW;KAG5B;EAJA,QAAA,EAKA,QALQ,CAKC,MALD,CAAA;EAAA,cAAA,EAAA,CAAA,KAAA,EAMM,MANN,EAAA,GAAA,IAAA;UACT,EAAA,GAAA,GAMO,MANP;WAAkB,EAAA,CAAA,QAAA,EAAA,GAAA,GAAA,IAAA,EAAA,GAAA,GAAA,GAAA,IAAA;;AAIR,KAgBT,KAhBS,CAAA,MAAA,EAAA,QAAA,EAAA,iBAgBgC,YAhBhC,EAAA,CAAA,GAAA;OAAT,EAiBH,MAjBG;SACc,EAiBf,QAjBe;OACR,EAiBT,QAjBS,CAiBA,MAjBA,CAAA;EAAM,OAAA,EAkBb,cAlBa,CAkBE,QAlBF,CAAA;EAcZ,aAAK,EAKA,QALA;CAAA"}
@@ -0,0 +1,53 @@
1
+ const require_core_core = require('../core/core.cjs');
2
+ let react = require("react");
3
+ let react_jsx_runtime = require("react/jsx-runtime");
4
+
5
+ //#region src/react/react.tsx
6
+ const PluginHookRunner = ({ hook }) => {
7
+ hook();
8
+ return null;
9
+ };
10
+ function createReactStore(config) {
11
+ const Context = (0, react.createContext)(null);
12
+ const Provider = ({ initialState, children }) => {
13
+ const [value] = (0, react.useState)(() => {
14
+ return require_core_core.createStore({
15
+ state: config.state(initialState),
16
+ actions: config.actions,
17
+ plugins: config.plugins
18
+ });
19
+ });
20
+ const { pluginResults } = value;
21
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Context.Provider, {
22
+ value,
23
+ children: [pluginResults.map((plugin, index) => plugin.useHook ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PluginHookRunner, { hook: plugin.useHook }, `${plugin.id}-${index}`) : null), children]
24
+ });
25
+ };
26
+ const useCtx = () => {
27
+ const ctx = (0, react.useContext)(Context);
28
+ if (!ctx) throw new Error(`Missing Store Provider`);
29
+ return ctx;
30
+ };
31
+ const useStoreHook = (selector) => {
32
+ const { store } = useCtx();
33
+ const selectorRef = (0, react.useRef)(selector);
34
+ selectorRef.current = selector;
35
+ const getSnapshot = () => selectorRef.current(store.getState());
36
+ return (0, react.useSyncExternalStore)(store.subscribe, getSnapshot, getSnapshot);
37
+ };
38
+ const useActionsHook = () => useCtx().actions;
39
+ const usePluginsHook = () => {
40
+ const { store, plugins } = useCtx();
41
+ (0, react.useSyncExternalStore)(store.subscribe, () => store.getState(), () => store.getState());
42
+ return plugins;
43
+ };
44
+ return {
45
+ Provider,
46
+ useStore: useStoreHook,
47
+ useActions: useActionsHook,
48
+ usePlugins: usePluginsHook
49
+ };
50
+ }
51
+
52
+ //#endregion
53
+ exports.createReactStore = createReactStore;
@@ -0,0 +1,27 @@
1
+ import { StoreApi } from "../core/types.cjs";
2
+ import { ExtractPlugins, PluginResult } from "../core/plugins/plugin.cjs";
3
+ import { ReactNode } from "react";
4
+
5
+ //#region src/react/react.d.ts
6
+ type ReactPluginResult = PluginResult & {
7
+ useHook?: () => void;
8
+ };
9
+ type ReactStoreOptions<TInitialState, TState, TActions, TResults extends ReactPluginResult[]> = {
10
+ state: (initialState: TInitialState) => TState;
11
+ actions: (store: StoreApi<TState>) => TActions;
12
+ plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };
13
+ };
14
+ interface ProviderProps<TInitialState> {
15
+ initialState: TInitialState;
16
+ children: ReactNode;
17
+ }
18
+ interface ReactStore<TState extends object, TActions extends object, TInitialState, TResults extends ReactPluginResult[]> {
19
+ Provider: (props: ProviderProps<TInitialState>) => ReactNode;
20
+ useStore: <TSelected>(selector: (state: TState) => TSelected) => TSelected;
21
+ useActions: () => TActions;
22
+ usePlugins: () => ExtractPlugins<TResults>;
23
+ }
24
+ declare function createReactStore<TInitialState, TState extends object, TActions extends object, const TResults extends ReactPluginResult[]>(config: ReactStoreOptions<TInitialState, TState, TActions, TResults>): ReactStore<TState, TActions, TInitialState, TResults>;
25
+ //#endregion
26
+ export { ReactPluginResult, createReactStore };
27
+ //# sourceMappingURL=react.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.cts","names":[],"sources":["../../src/react/react.tsx"],"sourcesContent":[],"mappings":";;;;;KAgBY,iBAAA,GAAoB;;;AAAhC,KASK,iBATO,CAAA,aAAoB,EAAA,MAAA,EAAA,QAAY,EAAA,iBAazB,iBAbyB,EAAA,CAAA,GAAA;EASvC,KAAA,EAAA,CAAA,YAAiB,EAME,aANF,EAAA,GAMoB,MANpB;EAAA,OAAA,EAAA,CAAA,KAAA,EAOH,QAPG,CAOM,MAPN,CAAA,EAAA,GAOkB,QAPlB;SAIH,CAAA,EAAA,QAEK,MAEE,QAFF,GAAA,CAAA,KAAA,EAEqB,QAFrB,CAE8B,MAF9B,CAAA,EAAA,GAE0C,QAF1C,CAEmD,CAFnD,CAAA;;UAKd,aAJS,CAAA,aAAA,CAAA,CAAA;cAAqB,EAKxB,aALwB;UACd,EAKd,SALc;;UAQhB,UARmC,CAAA,eAAA,MAAA,EAAA,iBAAA,MAAA,EAAA,aAAA,EAAA,iBAY1B,iBAZ0B,EAAA,CAAA,CAAA;UAAqB,EAAA,CAAA,KAAA,EAc9C,aAd8C,CAchC,aAdgC,CAAA,EAAA,GAcb,SAda;UAAS,EAAA,CAAA,SAAA,CAAA,CAAA,QAAA,EAAA,CAAA,KAAA,EAejC,MAfiC,EAAA,GAetB,SAfsB,EAAA,GAeR,SAfQ;EAAC,UAAA,EAAA,GAAA,GAgBxD,QAhBwD;EAGlE,UAAA,EAAA,GAAA,GAcU,cAdG,CAcY,QAdZ,CAAA;;AACP,iBAsBA,gBAtBA,CAAA,aAAA,EAAA,eAAA,MAAA,EAAA,iBAAA,MAAA,EAAA,uBA0BS,iBA1BT,EAAA,CAAA,CAAA,MAAA,EA4BN,iBA5BM,CA4BY,aA5BZ,EA4B2B,MA5B3B,EA4BmC,QA5BnC,EA4B6C,QA5B7C,CAAA,CAAA,EA6Bb,UA7Ba,CA6BF,MA7BE,EA6BM,QA7BN,EA6BgB,aA7BhB,EA6B+B,QA7B/B,CAAA"}
@@ -0,0 +1,27 @@
1
+ import { StoreApi } from "../core/types.mjs";
2
+ import { ExtractPlugins, PluginResult } from "../core/plugins/plugin.mjs";
3
+ import { ReactNode } from "react";
4
+
5
+ //#region src/react/react.d.ts
6
+ type ReactPluginResult = PluginResult & {
7
+ useHook?: () => void;
8
+ };
9
+ type ReactStoreOptions<TInitialState, TState, TActions, TResults extends ReactPluginResult[]> = {
10
+ state: (initialState: TInitialState) => TState;
11
+ actions: (store: StoreApi<TState>) => TActions;
12
+ plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };
13
+ };
14
+ interface ProviderProps<TInitialState> {
15
+ initialState: TInitialState;
16
+ children: ReactNode;
17
+ }
18
+ interface ReactStore<TState extends object, TActions extends object, TInitialState, TResults extends ReactPluginResult[]> {
19
+ Provider: (props: ProviderProps<TInitialState>) => ReactNode;
20
+ useStore: <TSelected>(selector: (state: TState) => TSelected) => TSelected;
21
+ useActions: () => TActions;
22
+ usePlugins: () => ExtractPlugins<TResults>;
23
+ }
24
+ declare function createReactStore<TInitialState, TState extends object, TActions extends object, const TResults extends ReactPluginResult[]>(config: ReactStoreOptions<TInitialState, TState, TActions, TResults>): ReactStore<TState, TActions, TInitialState, TResults>;
25
+ //#endregion
26
+ export { ReactPluginResult, createReactStore };
27
+ //# sourceMappingURL=react.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.mts","names":[],"sources":["../../src/react/react.tsx"],"sourcesContent":[],"mappings":";;;;;KAgBY,iBAAA,GAAoB;;;AAAhC,KASK,iBATO,CAAA,aAAoB,EAAA,MAAA,EAAA,QAAY,EAAA,iBAazB,iBAbyB,EAAA,CAAA,GAAA;EASvC,KAAA,EAAA,CAAA,YAAiB,EAME,aANF,EAAA,GAMoB,MANpB;EAAA,OAAA,EAAA,CAAA,KAAA,EAOH,QAPG,CAOM,MAPN,CAAA,EAAA,GAOkB,QAPlB;SAIH,CAAA,EAAA,QAEK,MAEE,QAFF,GAAA,CAAA,KAAA,EAEqB,QAFrB,CAE8B,MAF9B,CAAA,EAAA,GAE0C,QAF1C,CAEmD,CAFnD,CAAA;;UAKd,aAJS,CAAA,aAAA,CAAA,CAAA;cAAqB,EAKxB,aALwB;UACd,EAKd,SALc;;UAQhB,UARmC,CAAA,eAAA,MAAA,EAAA,iBAAA,MAAA,EAAA,aAAA,EAAA,iBAY1B,iBAZ0B,EAAA,CAAA,CAAA;UAAqB,EAAA,CAAA,KAAA,EAc9C,aAd8C,CAchC,aAdgC,CAAA,EAAA,GAcb,SAda;UAAS,EAAA,CAAA,SAAA,CAAA,CAAA,QAAA,EAAA,CAAA,KAAA,EAejC,MAfiC,EAAA,GAetB,SAfsB,EAAA,GAeR,SAfQ;EAAC,UAAA,EAAA,GAAA,GAgBxD,QAhBwD;EAGlE,UAAA,EAAA,GAAA,GAcU,cAdG,CAcY,QAdZ,CAAA;;AACP,iBAsBA,gBAtBA,CAAA,aAAA,EAAA,eAAA,MAAA,EAAA,iBAAA,MAAA,EAAA,uBA0BS,iBA1BT,EAAA,CAAA,CAAA,MAAA,EA4BN,iBA5BM,CA4BY,aA5BZ,EA4B2B,MA5B3B,EA4BmC,QA5BnC,EA4B6C,QA5B7C,CAAA,CAAA,EA6Bb,UA7Ba,CA6BF,MA7BE,EA6BM,QA7BN,EA6BgB,aA7BhB,EA6B+B,QA7B/B,CAAA"}
@@ -0,0 +1,54 @@
1
+ import { createStore } from "../core/core.mjs";
2
+ import { createContext, useContext, useRef, useState, useSyncExternalStore } from "react";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+
5
+ //#region src/react/react.tsx
6
+ const PluginHookRunner = ({ hook }) => {
7
+ hook();
8
+ return null;
9
+ };
10
+ function createReactStore(config) {
11
+ const Context = createContext(null);
12
+ const Provider = ({ initialState, children }) => {
13
+ const [value] = useState(() => {
14
+ return createStore({
15
+ state: config.state(initialState),
16
+ actions: config.actions,
17
+ plugins: config.plugins
18
+ });
19
+ });
20
+ const { pluginResults } = value;
21
+ return /* @__PURE__ */ jsxs(Context.Provider, {
22
+ value,
23
+ children: [pluginResults.map((plugin, index) => plugin.useHook ? /* @__PURE__ */ jsx(PluginHookRunner, { hook: plugin.useHook }, `${plugin.id}-${index}`) : null), children]
24
+ });
25
+ };
26
+ const useCtx = () => {
27
+ const ctx = useContext(Context);
28
+ if (!ctx) throw new Error(`Missing Store Provider`);
29
+ return ctx;
30
+ };
31
+ const useStoreHook = (selector) => {
32
+ const { store } = useCtx();
33
+ const selectorRef = useRef(selector);
34
+ selectorRef.current = selector;
35
+ const getSnapshot = () => selectorRef.current(store.getState());
36
+ return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
37
+ };
38
+ const useActionsHook = () => useCtx().actions;
39
+ const usePluginsHook = () => {
40
+ const { store, plugins } = useCtx();
41
+ useSyncExternalStore(store.subscribe, () => store.getState(), () => store.getState());
42
+ return plugins;
43
+ };
44
+ return {
45
+ Provider,
46
+ useStore: useStoreHook,
47
+ useActions: useActionsHook,
48
+ usePlugins: usePluginsHook
49
+ };
50
+ }
51
+
52
+ //#endregion
53
+ export { createReactStore };
54
+ //# sourceMappingURL=react.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.mjs","names":[],"sources":["../../src/react/react.tsx"],"sourcesContent":["import {\n createContext,\n type ReactNode,\n useContext,\n useRef,\n useState,\n useSyncExternalStore,\n} from \"react\";\nimport { createStore } from \"../core/core\";\nimport type {\n Store as CoreStore,\n ExtractPlugins,\n PluginResult,\n StoreApi,\n} from \"../core/types\";\n\nexport type ReactPluginResult = PluginResult & {\n useHook?: () => void;\n};\n\nconst PluginHookRunner = ({ hook }: { hook: () => void }) => {\n hook();\n return null;\n};\n\ntype ReactStoreOptions<\n TInitialState,\n TState,\n TActions,\n TResults extends ReactPluginResult[],\n> = {\n state: (initialState: TInitialState) => TState;\n actions: (store: StoreApi<TState>) => TActions;\n plugins?: { [K in keyof TResults]: (store: StoreApi<TState>) => TResults[K] };\n};\n\ninterface ProviderProps<TInitialState> {\n initialState: TInitialState;\n children: ReactNode;\n}\n\ninterface ReactStore<\n TState extends object,\n TActions extends object,\n TInitialState,\n TResults extends ReactPluginResult[],\n> {\n Provider: (props: ProviderProps<TInitialState>) => ReactNode;\n useStore: <TSelected>(selector: (state: TState) => TSelected) => TSelected;\n useActions: () => TActions;\n usePlugins: () => ExtractPlugins<TResults>;\n}\n\ntype ContextValue<\n TState,\n TActions,\n TResults extends ReactPluginResult[],\n> = CoreStore<TState, TActions, TResults>;\n\nexport function createReactStore<\n TInitialState,\n TState extends object,\n TActions extends object,\n const TResults extends ReactPluginResult[],\n>(\n config: ReactStoreOptions<TInitialState, TState, TActions, TResults>,\n): ReactStore<TState, TActions, TInitialState, TResults> {\n const Context = createContext<ContextValue<\n TState,\n TActions,\n TResults\n > | null>(null);\n\n const Provider = ({\n initialState,\n children,\n }: ProviderProps<TInitialState>) => {\n const [value] = useState(() => {\n const store = createStore({\n state: config.state(initialState),\n actions: config.actions,\n plugins: config.plugins,\n });\n\n return store as ContextValue<TState, TActions, TResults>;\n });\n const { pluginResults } = value;\n\n return (\n <Context.Provider value={value}>\n {pluginResults.map((plugin, index) =>\n plugin.useHook ? (\n <PluginHookRunner\n key={`${plugin.id}-${index}`}\n hook={plugin.useHook}\n />\n ) : null,\n )}\n {children}\n </Context.Provider>\n );\n };\n\n const useCtx = () => {\n const ctx = useContext(Context);\n if (!ctx) throw new Error(`Missing Store Provider`);\n return ctx;\n };\n\n const useStoreHook = <TSelected,>(\n selector: (state: TState) => TSelected,\n ): TSelected => {\n const { store } = useCtx();\n const selectorRef = useRef(selector);\n selectorRef.current = selector;\n\n const getSnapshot = () => selectorRef.current(store.getState());\n\n return useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);\n };\n\n const useActionsHook = (): TActions => useCtx().actions;\n\n const usePluginsHook = (): ExtractPlugins<TResults> => {\n const { store, plugins } = useCtx();\n\n useSyncExternalStore(\n store.subscribe,\n () => store.getState(),\n () => store.getState(),\n );\n\n return plugins;\n };\n\n return {\n Provider,\n useStore: useStoreHook,\n useActions: useActionsHook,\n usePlugins: usePluginsHook,\n };\n}\n"],"mappings":";;;;;AAoBA,MAAM,oBAAoB,EAAE,WAAiC;AAC3D,OAAM;AACN,QAAO;;AAqCT,SAAgB,iBAMd,QACuD;CACvD,MAAM,UAAU,cAIN,KAAK;CAEf,MAAM,YAAY,EAChB,cACA,eACkC;EAClC,MAAM,CAAC,SAAS,eAAe;AAO7B,UANc,YAAY;IACxB,OAAO,OAAO,MAAM,aAAa;IACjC,SAAS,OAAO;IAChB,SAAS,OAAO;IACjB,CAAC;IAGF;EACF,MAAM,EAAE,kBAAkB;AAE1B,SACE,qBAAC,QAAQ;GAAgB;cACtB,cAAc,KAAK,QAAQ,UAC1B,OAAO,UACL,oBAAC,oBAEC,MAAM,OAAO,WADR,GAAG,OAAO,GAAG,GAAG,QAErB,GACA,KACL,EACA;IACgB;;CAIvB,MAAM,eAAe;EACnB,MAAM,MAAM,WAAW,QAAQ;AAC/B,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,yBAAyB;AACnD,SAAO;;CAGT,MAAM,gBACJ,aACc;EACd,MAAM,EAAE,UAAU,QAAQ;EAC1B,MAAM,cAAc,OAAO,SAAS;AACpC,cAAY,UAAU;EAEtB,MAAM,oBAAoB,YAAY,QAAQ,MAAM,UAAU,CAAC;AAE/D,SAAO,qBAAqB,MAAM,WAAW,aAAa,YAAY;;CAGxE,MAAM,uBAAiC,QAAQ,CAAC;CAEhD,MAAM,uBAAiD;EACrD,MAAM,EAAE,OAAO,YAAY,QAAQ;AAEnC,uBACE,MAAM,iBACA,MAAM,UAAU,QAChB,MAAM,UAAU,CACvB;AAED,SAAO;;AAGT,QAAO;EACL;EACA,UAAU;EACV,YAAY;EACZ,YAAY;EACb"}
package/package.json ADDED
@@ -0,0 +1,113 @@
1
+ {
2
+ "name": "@jlnstack/store",
3
+ "description": "Type-safe store",
4
+ "version": "0.0.0",
5
+ "license": "MIT",
6
+ "author": "jln13x",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/jln13x/jlnstack"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "package.json"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsdown",
20
+ "dev": "tsdown --watch",
21
+ "lint": "tsc --noEmit",
22
+ "test": "vitest",
23
+ "test:run": "vitest run",
24
+ "test:ui": "vitest --ui"
25
+ },
26
+ "peerDependencies": {
27
+ "immer": ">=10",
28
+ "react": ">=18",
29
+ "zustand": ">=5"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "immer": {
33
+ "optional": true
34
+ },
35
+ "react": {
36
+ "optional": true
37
+ }
38
+ },
39
+ "devDependencies": {
40
+ "@testing-library/jest-dom": "^6.6.3",
41
+ "@testing-library/react": "^16.1.0",
42
+ "@types/react": "^19.1.16",
43
+ "@types/react-dom": "^19.1.9",
44
+ "@vitest/ui": "^2.1.8",
45
+ "tsdown": "^0.15.4",
46
+ "typescript": "catalog:",
47
+ "vitest": "^2.1.8",
48
+ "zustand": "^5.0.0"
49
+ },
50
+ "exports": {
51
+ "./package.json": "./package.json",
52
+ ".": {
53
+ "import": {
54
+ "types": "./dist/core/core.d.mts",
55
+ "default": "./dist/core/core.mjs"
56
+ },
57
+ "require": {
58
+ "types": "./dist/core/core.d.cts",
59
+ "default": "./dist/core/core.cjs"
60
+ }
61
+ },
62
+ "./plugins/history": {
63
+ "import": {
64
+ "types": "./dist/core/plugins/history/index.d.mts",
65
+ "default": "./dist/core/plugins/history/index.mjs"
66
+ },
67
+ "require": {
68
+ "types": "./dist/core/plugins/history/index.d.cts",
69
+ "default": "./dist/core/plugins/history/index.cjs"
70
+ }
71
+ },
72
+ "./plugins/immer": {
73
+ "import": {
74
+ "types": "./dist/core/plugins/immer/index.d.mts",
75
+ "default": "./dist/core/plugins/immer/index.mjs"
76
+ },
77
+ "require": {
78
+ "types": "./dist/core/plugins/immer/index.d.cts",
79
+ "default": "./dist/core/plugins/immer/index.cjs"
80
+ }
81
+ },
82
+ "./plugins/logger": {
83
+ "import": {
84
+ "types": "./dist/core/plugins/logger/index.d.mts",
85
+ "default": "./dist/core/plugins/logger/index.mjs"
86
+ },
87
+ "require": {
88
+ "types": "./dist/core/plugins/logger/index.d.cts",
89
+ "default": "./dist/core/plugins/logger/index.cjs"
90
+ }
91
+ },
92
+ "./plugins/reset": {
93
+ "import": {
94
+ "types": "./dist/core/plugins/reset/index.d.mts",
95
+ "default": "./dist/core/plugins/reset/index.mjs"
96
+ },
97
+ "require": {
98
+ "types": "./dist/core/plugins/reset/index.d.cts",
99
+ "default": "./dist/core/plugins/reset/index.cjs"
100
+ }
101
+ },
102
+ "./react": {
103
+ "import": {
104
+ "types": "./dist/react/react.d.mts",
105
+ "default": "./dist/react/react.mjs"
106
+ },
107
+ "require": {
108
+ "types": "./dist/react/react.d.cts",
109
+ "default": "./dist/react/react.cjs"
110
+ }
111
+ }
112
+ }
113
+ }