@violetflux/kerros 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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.cjs +59 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +58 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Violetflux
|
|
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
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Kerros
|
|
2
|
+
|
|
3
|
+
Tiny, selector-first React stores with stable Providers and official external-store semantics.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun add @violetflux/kerros
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { createStore } from '@violetflux/kerros'
|
|
11
|
+
import { useState } from 'react'
|
|
12
|
+
|
|
13
|
+
export const [useCounter, CounterProvider] = createStore(() => {
|
|
14
|
+
const [count, setCount] = useState(0)
|
|
15
|
+
|
|
16
|
+
return { count, setCount }
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function Counter() {
|
|
20
|
+
const { count, setCount } = useCounter(s => ({
|
|
21
|
+
count: s.count,
|
|
22
|
+
setCount: s.setCount,
|
|
23
|
+
}))
|
|
24
|
+
|
|
25
|
+
return <button onClick={() => setCount(count + 1)}>{count}</button>
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Kerros uses `use-sync-external-store/shim/with-selector`, so the same package works with React 17, 18, and 19. Selectors may be declared inline. Their returned objects are compared with top-level shallow equality.
|
|
30
|
+
|
|
31
|
+
Stores compose through normal Provider nesting: an inner store hook can select values from an outer store. This keeps dependencies explicit and prevents a global store from becoming a single rerender domain.
|
|
32
|
+
|
|
33
|
+
- [Documentation](https://violetflux.github.io/kerros/)
|
|
34
|
+
- [API reference](https://violetflux.github.io/kerros/api/)
|
|
35
|
+
- [Migration from hox](https://violetflux.github.io/kerros/guide/migration)
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
let use_sync_external_store_shim_with_selector = require("use-sync-external-store/shim/with-selector");
|
|
4
|
+
//#region src/index.tsx
|
|
5
|
+
const useStoreLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
6
|
+
/**
|
|
7
|
+
* Create a selector-first React Store and its matching Provider
|
|
8
|
+
*/
|
|
9
|
+
function createStore(useStoreValue) {
|
|
10
|
+
const StoreContext = (0, react.createContext)(void 0);
|
|
11
|
+
const storeName = useStoreValue.name || "KerrosStore";
|
|
12
|
+
/** Run the Store hook and publish its committed snapshot */
|
|
13
|
+
const StoreProvider = (props) => {
|
|
14
|
+
const { children, ...storeProps } = props;
|
|
15
|
+
const value = useStoreValue(storeProps);
|
|
16
|
+
const [container] = (0, react.useState)(() => createStoreContainer(value));
|
|
17
|
+
useStoreLayoutEffect(() => container.publish(value), [container, value]);
|
|
18
|
+
return (0, react.createElement)(StoreContext.Provider, { value: container }, children);
|
|
19
|
+
};
|
|
20
|
+
StoreProvider.displayName = `${storeName}Provider`;
|
|
21
|
+
StoreContext.displayName = `${storeName}Context`;
|
|
22
|
+
/** Select Store fields through the stable Provider container */
|
|
23
|
+
const useStore = (selector) => {
|
|
24
|
+
const container = (0, react.useContext)(StoreContext);
|
|
25
|
+
if (!container) throw new Error("Kerros store hook must be used within its matching Provider");
|
|
26
|
+
return (0, use_sync_external_store_shim_with_selector.useSyncExternalStoreWithSelector)(container.subscribe, container.getSnapshot, container.getSnapshot, selector, shallowEqual);
|
|
27
|
+
};
|
|
28
|
+
return [useStore, StoreProvider];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create the stable external-store container for one Provider instance
|
|
32
|
+
*/
|
|
33
|
+
function createStoreContainer(initialSnapshot) {
|
|
34
|
+
let snapshot = initialSnapshot;
|
|
35
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
36
|
+
return {
|
|
37
|
+
getSnapshot: () => snapshot,
|
|
38
|
+
publish: (nextSnapshot) => {
|
|
39
|
+
if (Object.is(snapshot, nextSnapshot)) return;
|
|
40
|
+
snapshot = nextSnapshot;
|
|
41
|
+
for (const listener of listeners) listener();
|
|
42
|
+
},
|
|
43
|
+
subscribe: (listener) => {
|
|
44
|
+
listeners.add(listener);
|
|
45
|
+
return () => listeners.delete(listener);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Compare selector objects by their enumerable top-level fields
|
|
51
|
+
*/
|
|
52
|
+
function shallowEqual(left, right) {
|
|
53
|
+
if (Object.is(left, right)) return true;
|
|
54
|
+
const leftKeys = Object.keys(left);
|
|
55
|
+
if (leftKeys.length !== Object.keys(right).length) return false;
|
|
56
|
+
return leftKeys.every((key) => Object.prototype.hasOwnProperty.call(right, key) && Object.is(left[key], right[key]));
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
exports.createStore = createStore;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FC, PropsWithChildren } from "react";
|
|
2
|
+
//#region src/index.d.ts
|
|
3
|
+
/** Store selector returning an object compared with shallow equality */
|
|
4
|
+
type StoreSelector<TStore, TSelection extends object> = (store: TStore) => TSelection;
|
|
5
|
+
/** Hook used by consumers to select Store fields */
|
|
6
|
+
interface StoreHook<TStore> {
|
|
7
|
+
<TSelection extends object>(selector: StoreSelector<TStore, TSelection>): TSelection;
|
|
8
|
+
}
|
|
9
|
+
/** Provider created for a Store hook */
|
|
10
|
+
type StoreProvider<TProps> = FC<PropsWithChildren<TProps>>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a selector-first React Store and its matching Provider
|
|
13
|
+
*/
|
|
14
|
+
declare function createStore<TStore, TProps = Record<never, never>>(useStoreValue: (props: TProps) => TStore): readonly [StoreHook<TStore>, StoreProvider<TProps>];
|
|
15
|
+
//#endregion
|
|
16
|
+
export { StoreHook, StoreProvider, StoreSelector, createStore };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FC, PropsWithChildren } from "react";
|
|
2
|
+
//#region src/index.d.ts
|
|
3
|
+
/** Store selector returning an object compared with shallow equality */
|
|
4
|
+
type StoreSelector<TStore, TSelection extends object> = (store: TStore) => TSelection;
|
|
5
|
+
/** Hook used by consumers to select Store fields */
|
|
6
|
+
interface StoreHook<TStore> {
|
|
7
|
+
<TSelection extends object>(selector: StoreSelector<TStore, TSelection>): TSelection;
|
|
8
|
+
}
|
|
9
|
+
/** Provider created for a Store hook */
|
|
10
|
+
type StoreProvider<TProps> = FC<PropsWithChildren<TProps>>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a selector-first React Store and its matching Provider
|
|
13
|
+
*/
|
|
14
|
+
declare function createStore<TStore, TProps = Record<never, never>>(useStoreValue: (props: TProps) => TStore): readonly [StoreHook<TStore>, StoreProvider<TProps>];
|
|
15
|
+
//#endregion
|
|
16
|
+
export { StoreHook, StoreProvider, StoreSelector, createStore };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createContext, createElement, useContext, useEffect, useLayoutEffect, useState } from "react";
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
|
|
3
|
+
//#region src/index.tsx
|
|
4
|
+
const useStoreLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
5
|
+
/**
|
|
6
|
+
* Create a selector-first React Store and its matching Provider
|
|
7
|
+
*/
|
|
8
|
+
function createStore(useStoreValue) {
|
|
9
|
+
const StoreContext = createContext(void 0);
|
|
10
|
+
const storeName = useStoreValue.name || "KerrosStore";
|
|
11
|
+
/** Run the Store hook and publish its committed snapshot */
|
|
12
|
+
const StoreProvider = (props) => {
|
|
13
|
+
const { children, ...storeProps } = props;
|
|
14
|
+
const value = useStoreValue(storeProps);
|
|
15
|
+
const [container] = useState(() => createStoreContainer(value));
|
|
16
|
+
useStoreLayoutEffect(() => container.publish(value), [container, value]);
|
|
17
|
+
return createElement(StoreContext.Provider, { value: container }, children);
|
|
18
|
+
};
|
|
19
|
+
StoreProvider.displayName = `${storeName}Provider`;
|
|
20
|
+
StoreContext.displayName = `${storeName}Context`;
|
|
21
|
+
/** Select Store fields through the stable Provider container */
|
|
22
|
+
const useStore = (selector) => {
|
|
23
|
+
const container = useContext(StoreContext);
|
|
24
|
+
if (!container) throw new Error("Kerros store hook must be used within its matching Provider");
|
|
25
|
+
return useSyncExternalStoreWithSelector(container.subscribe, container.getSnapshot, container.getSnapshot, selector, shallowEqual);
|
|
26
|
+
};
|
|
27
|
+
return [useStore, StoreProvider];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create the stable external-store container for one Provider instance
|
|
31
|
+
*/
|
|
32
|
+
function createStoreContainer(initialSnapshot) {
|
|
33
|
+
let snapshot = initialSnapshot;
|
|
34
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
35
|
+
return {
|
|
36
|
+
getSnapshot: () => snapshot,
|
|
37
|
+
publish: (nextSnapshot) => {
|
|
38
|
+
if (Object.is(snapshot, nextSnapshot)) return;
|
|
39
|
+
snapshot = nextSnapshot;
|
|
40
|
+
for (const listener of listeners) listener();
|
|
41
|
+
},
|
|
42
|
+
subscribe: (listener) => {
|
|
43
|
+
listeners.add(listener);
|
|
44
|
+
return () => listeners.delete(listener);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Compare selector objects by their enumerable top-level fields
|
|
50
|
+
*/
|
|
51
|
+
function shallowEqual(left, right) {
|
|
52
|
+
if (Object.is(left, right)) return true;
|
|
53
|
+
const leftKeys = Object.keys(left);
|
|
54
|
+
if (leftKeys.length !== Object.keys(right).length) return false;
|
|
55
|
+
return leftKeys.every((key) => Object.prototype.hasOwnProperty.call(right, key) && Object.is(left[key], right[key]));
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { createStore };
|
package/package.json
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@violetflux/kerros",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A tiny selector-first React store built on useSyncExternalStore.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"state-management",
|
|
8
|
+
"store",
|
|
9
|
+
"selector",
|
|
10
|
+
"use-sync-external-store"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://violetflux.github.io/kerros/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/violetflux/kerros/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/violetflux/kerros.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Violetflux",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.mjs",
|
|
29
|
+
"types": "./dist/index.d.mts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/index.d.mts",
|
|
34
|
+
"default": "./dist/index.mjs"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/index.d.cts",
|
|
38
|
+
"default": "./dist/index.cjs"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown",
|
|
44
|
+
"dev": "rspress dev",
|
|
45
|
+
"docs:build": "rspress build",
|
|
46
|
+
"docs:preview": "rspress preview",
|
|
47
|
+
"docs:check": "bun scripts/check-docs.ts",
|
|
48
|
+
"lint": "eslint .",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"check": "bun run lint && bun run typecheck && bun run test && bun run build && bun run docs:check && bun run docs:build",
|
|
53
|
+
"prepublishOnly": "bun run lint && bun run typecheck && bun run test && bun run build"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"use-sync-external-store": "1.6.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@eslint/js": "10.0.1",
|
|
66
|
+
"@rspress/core": "2.0.17",
|
|
67
|
+
"@types/node": "26.1.1",
|
|
68
|
+
"@types/react": "19.2.17",
|
|
69
|
+
"@types/react-dom": "19.2.3",
|
|
70
|
+
"@types/use-sync-external-store": "1.5.0",
|
|
71
|
+
"eslint": "10.7.0",
|
|
72
|
+
"eslint-plugin-react-hooks": "7.1.1",
|
|
73
|
+
"globals": "17.7.0",
|
|
74
|
+
"jsdom": "29.1.1",
|
|
75
|
+
"react": "19.2.7",
|
|
76
|
+
"react-dom": "19.2.7",
|
|
77
|
+
"tsdown": "0.22.9",
|
|
78
|
+
"typescript": "5.9.3",
|
|
79
|
+
"typescript-eslint": "8.64.0",
|
|
80
|
+
"vitest": "4.1.10"
|
|
81
|
+
}
|
|
82
|
+
}
|