@createstore/createstore 0.0.0-stage → 1.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.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/dist/cjs/client-root.cjs +39 -0
- package/dist/cjs/client-root.cjs.map +1 -0
- package/dist/cjs/createStore.cjs +2899 -0
- package/dist/cjs/createStore.cjs.map +1 -0
- package/dist/cjs/react-bindings.cjs +69 -0
- package/dist/cjs/react-bindings.cjs.map +1 -0
- package/dist/esm/client-root.js +15 -0
- package/dist/esm/client-root.js.map +1 -0
- package/dist/esm/createStore.js +2880 -0
- package/dist/esm/createStore.js.map +1 -0
- package/dist/esm/react-bindings.js +26 -0
- package/dist/esm/react-bindings.js.map +1 -0
- package/dist/types/client-root.d.cts +11 -0
- package/dist/types/client-root.d.ts +11 -0
- package/dist/types/createStore.d.cts +510 -0
- package/dist/types/createStore.d.ts +510 -0
- package/dist/types/react-bindings.d.cts +12 -0
- package/dist/types/react-bindings.d.ts +12 -0
- package/package.json +128 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gaurav Asawa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @createstore/createstore
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Global state that works like `useState`, shared by every component. No provider, no reducers, no action types.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @createstore/createstore
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Requires React 19 or newer. Works in the browser, in Node, and with Next.js Server Components.
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createStore } from '@createstore/createstore';
|
|
15
|
+
|
|
16
|
+
const store = createStore({ count: 0 });
|
|
17
|
+
|
|
18
|
+
function Counter() {
|
|
19
|
+
const [count, setCount] = store.useGlobalState('count');
|
|
20
|
+
return <button onClick={() => setCount((c) => c + 1)}>{count}</button>;
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Every component that reads `'count'` re-renders when any of them changes it. Outside React, use
|
|
25
|
+
`store.getState('count')` and `store.setState('count', 2)`.
|
|
26
|
+
|
|
27
|
+
### Share state between components
|
|
28
|
+
|
|
29
|
+
Take `useGlobalState` out of the store, and any component can read or change the same list. No provider, no props
|
|
30
|
+
passed down:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
const { useGlobalState } = createStore({ todos: [] as { text: string }[] });
|
|
34
|
+
|
|
35
|
+
function TodoCount() {
|
|
36
|
+
const [todos] = useGlobalState('todos');
|
|
37
|
+
return <p>{todos.length} todos</p>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function AddTodo() {
|
|
41
|
+
const [todos, setTodos] = useGlobalState('todos');
|
|
42
|
+
const add = (text: string) => setTodos([...todos, { text }]);
|
|
43
|
+
return <Form onAdd={add} />;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`<Form>` stands for your own form component. When `AddTodo` adds a todo, `TodoCount` updates by itself.
|
|
48
|
+
|
|
49
|
+
Everything else stays off until you use it: computed values, server data with `query()`, persistence, offline
|
|
50
|
+
support, undo/redo, tab sync, events and server rendering.
|
|
51
|
+
|
|
52
|
+
## Package
|
|
53
|
+
|
|
54
|
+
- ESM and CommonJS builds with TypeScript types, `"sideEffects": false`, so unused code is tree-shaken.
|
|
55
|
+
- Published with [npm provenance](https://docs.npmjs.com/generating-provenance-statements).
|
|
56
|
+
- Versioned with [semantic versioning](https://semver.org); see [CHANGELOG.md](./CHANGELOG.md) once a release exists.
|
|
57
|
+
|
|
58
|
+
## Links
|
|
59
|
+
|
|
60
|
+
- [GitHub repository](https://github.com/Gauravasawa/createstore): source code, issues and releases
|
|
61
|
+
- [npm package](https://www.npmjs.com/package/@createstore/createstore)
|
|
62
|
+
- [Documentation](https://createstore-docs.pages.dev)
|
|
63
|
+
- [Report a bug or request a feature](https://github.com/Gauravasawa/createstore/issues/new/choose)
|
|
64
|
+
- [Security policy](https://github.com/Gauravasawa/createstore/security/policy)
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var client_root_exports = {};
|
|
21
|
+
__export(client_root_exports, {
|
|
22
|
+
ClientRoot: () => ClientRoot
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(client_root_exports);
|
|
25
|
+
var import_react = require("react");
|
|
26
|
+
var import_createStore = require("./createStore.cjs");
|
|
27
|
+
function ClientRoot({ name, snapshot, children }) {
|
|
28
|
+
const parent = (0, import_react.useContext)(import_createStore.__SnapshotContext);
|
|
29
|
+
const value = (0, import_react.useMemo)(() => ({ ...parent ?? {}, [name]: { snapshot } }), [parent, name, snapshot]);
|
|
30
|
+
(0, import_react.useLayoutEffect)(() => {
|
|
31
|
+
(0, import_createStore.__mergeSnapshot)(name, snapshot);
|
|
32
|
+
}, [name, snapshot]);
|
|
33
|
+
return (0, import_react.createElement)(import_createStore.__SnapshotContext.Provider, { value }, children);
|
|
34
|
+
}
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
ClientRoot
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=client-root.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client-root.tsx"],"sourcesContent":["'use client';\n// What <Store.Root> in a Server Component renders: it hands the request's snapshot down by store name.\n// The store's first hook below picks it up; later snapshots (client-side navigation) are merged in a\n// layout effect, before components subscribe, so a new page's data isn't fetched again.\nimport { createElement, useContext, useLayoutEffect, useMemo, type ReactNode } from 'react';\nimport { __SnapshotContext, __mergeSnapshot } from './createStore';\n\nexport function ClientRoot({ name, snapshot, children }: { name: string; snapshot: unknown; children?: ReactNode }) {\n const parent = useContext(__SnapshotContext);\n const value = useMemo(() => ({ ...(parent ?? {}), [name]: { snapshot } }), [parent, name, snapshot]);\n useLayoutEffect(() => {\n __mergeSnapshot(name, snapshot);\n }, [name, snapshot]);\n return createElement(__SnapshotContext.Provider, { value }, children);\n}"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAoF;AACpF,yBAAmD;AAE5C,SAAS,WAAW,EAAE,MAAM,UAAU,SAAS,GAA8D;AAClH,QAAM,aAAS,yBAAW,oCAAiB;AAC3C,QAAM,YAAQ,sBAAQ,OAAO,EAAE,GAAI,UAAU,CAAC,GAAI,CAAC,IAAI,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,QAAQ,MAAM,QAAQ,CAAC;AACnG,oCAAgB,MAAM;AACpB,4CAAgB,MAAM,QAAQ;AAAA,EAChC,GAAG,CAAC,MAAM,QAAQ,CAAC;AACnB,aAAO,4BAAc,qCAAkB,UAAU,EAAE,MAAM,GAAG,QAAQ;AACtE;","names":[]}
|