@violetflux/kerros 0.2.1 → 0.2.2
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/dist/index.cjs +3 -2
- package/dist/index.mjs +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
Kerros is a lightweight way to share state between React components.
|
|
26
26
|
|
|
27
|
-
Write a Store the same way you write a custom Hook. When local state needs to be shared, pass it to `createStore`, mount its Provider, and let each component
|
|
27
|
+
Write a Store the same way you write a custom Hook. When local state needs to be shared, pass it to `createStore`, mount its Provider, and let automatic tracking focus each component on the properties it reads.
|
|
28
28
|
|
|
29
29
|
> [!TIP]
|
|
30
30
|
> **Install with your coding agent** — paste this sentence into your coding agent to install both the dependency and the project Skill:
|
|
@@ -254,7 +254,7 @@ function createStore<TStore, TProps = Record<never, never>>(
|
|
|
254
254
|
|
|
255
255
|
Most applications only need `createStore`. Use `bindStore` when a library or SDK already owns authoritative state outside React and exposes stable `getSnapshot` and `subscribe` functions.
|
|
256
256
|
|
|
257
|
-
Without this API, an integration must either repeat the Context and
|
|
257
|
+
Without this API, an integration must either repeat the Context and focused-subscription code or copy the external snapshot through a second Store. `bindStore` provides Provider scoping, selector-free automatic tracking, and explicit selectors while keeping the original Store as the only state owner.
|
|
258
258
|
|
|
259
259
|
```tsx
|
|
260
260
|
const [useStream, StreamBindingProvider] = bindStore<Stream>('Stream')
|
package/README.zh-CN.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
Kerros 是一个在 React 组件间共享状态的轻量方案。
|
|
18
18
|
|
|
19
|
-
你怎么写 custom Hook,就可以怎么写 Store。只有当局部状态需要被多个组件使用时,再交给 `createStore`,用 Provider
|
|
19
|
+
你怎么写 custom Hook,就可以怎么写 Store。只有当局部状态需要被多个组件使用时,再交给 `createStore`,用 Provider 决定共享范围;组件直接解构需要的数据,Kerros 默认自动追踪实际访问的属性。
|
|
20
20
|
|
|
21
21
|
> [!TIP]
|
|
22
22
|
> **使用 Coding Agent 安装**:复制下面这句话并粘贴给你的 Coding Agent,它会同时安装依赖和当前项目的 Skill:
|
|
@@ -246,7 +246,7 @@ function createStore<TStore, TProps = Record<never, never>>(
|
|
|
246
246
|
|
|
247
247
|
绝大多数应用只需要 `createStore`。只有当某个库或 SDK 已经在 React 外持有权威状态,并提供稳定的 `getSnapshot` 和 `subscribe` 函数时,才使用 `bindStore`。
|
|
248
248
|
|
|
249
|
-
没有这个 API 时,集成层只能重复实现 Context
|
|
249
|
+
没有这个 API 时,集成层只能重复实现 Context 和细粒度订阅,或者把 External Store 快照复制进第二个 Store。`bindStore` 提供 Provider 作用域、无 selector 自动追踪和显式 selector,原 Store 仍是唯一状态所有者。
|
|
250
250
|
|
|
251
251
|
```tsx
|
|
252
252
|
const [useStream, StreamBindingProvider] = bindStore<Stream>('Stream')
|
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ let proxy_compare = require("proxy-compare");
|
|
|
4
4
|
let use_sync_external_store_shim_with_selector = require("use-sync-external-store/shim/with-selector");
|
|
5
5
|
//#region src/tracking.ts
|
|
6
6
|
const useStoreLayoutEffect$1 = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
7
|
+
const proxyTargetCache = /* @__PURE__ */ new WeakMap();
|
|
7
8
|
/**
|
|
8
9
|
* Subscribe through either an explicit selector, shallow snapshots, or render access tracking
|
|
9
10
|
*/
|
|
@@ -27,7 +28,7 @@ function useStoreValue(store, selector, tracking) {
|
|
|
27
28
|
const snapshot = (0, use_sync_external_store_shim_with_selector.useSyncExternalStoreWithSelector)(store.subscribe, store.getSnapshot, store.getSnapshot, selectSnapshot, compareSelections);
|
|
28
29
|
const affected = /* @__PURE__ */ new WeakMap();
|
|
29
30
|
const shouldTrack = !selector && tracking;
|
|
30
|
-
const value = shouldTrack ? (0, proxy_compare.createProxy)(snapshot, affected, proxyCache) : snapshot;
|
|
31
|
+
const value = shouldTrack ? (0, proxy_compare.createProxy)(snapshot, affected, proxyCache, proxyTargetCache) : snapshot;
|
|
31
32
|
useStoreLayoutEffect$1(() => {
|
|
32
33
|
if (shouldTrack) {
|
|
33
34
|
const renderedSnapshot = snapshot;
|
|
@@ -36,7 +37,7 @@ function useStoreValue(store, selector, tracking) {
|
|
|
36
37
|
snapshot: renderedSnapshot
|
|
37
38
|
};
|
|
38
39
|
const currentSnapshot = store.getSnapshot();
|
|
39
|
-
if ((0, proxy_compare.isChanged)(renderedSnapshot, currentSnapshot, affected, /* @__PURE__ */ new WeakMap())) calibrate(currentSnapshot);
|
|
40
|
+
if (!Object.is(renderedSnapshot, currentSnapshot) && (0, proxy_compare.isChanged)(renderedSnapshot, currentSnapshot, affected, /* @__PURE__ */ new WeakMap())) calibrate(currentSnapshot);
|
|
40
41
|
}
|
|
41
42
|
}, [
|
|
42
43
|
affected,
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { createProxy, isChanged } from "proxy-compare";
|
|
|
3
3
|
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
|
|
4
4
|
//#region src/tracking.ts
|
|
5
5
|
const useStoreLayoutEffect$1 = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
6
|
+
const proxyTargetCache = /* @__PURE__ */ new WeakMap();
|
|
6
7
|
/**
|
|
7
8
|
* Subscribe through either an explicit selector, shallow snapshots, or render access tracking
|
|
8
9
|
*/
|
|
@@ -26,7 +27,7 @@ function useStoreValue(store, selector, tracking) {
|
|
|
26
27
|
const snapshot = useSyncExternalStoreWithSelector(store.subscribe, store.getSnapshot, store.getSnapshot, selectSnapshot, compareSelections);
|
|
27
28
|
const affected = /* @__PURE__ */ new WeakMap();
|
|
28
29
|
const shouldTrack = !selector && tracking;
|
|
29
|
-
const value = shouldTrack ? createProxy(snapshot, affected, proxyCache) : snapshot;
|
|
30
|
+
const value = shouldTrack ? createProxy(snapshot, affected, proxyCache, proxyTargetCache) : snapshot;
|
|
30
31
|
useStoreLayoutEffect$1(() => {
|
|
31
32
|
if (shouldTrack) {
|
|
32
33
|
const renderedSnapshot = snapshot;
|
|
@@ -35,7 +36,7 @@ function useStoreValue(store, selector, tracking) {
|
|
|
35
36
|
snapshot: renderedSnapshot
|
|
36
37
|
};
|
|
37
38
|
const currentSnapshot = store.getSnapshot();
|
|
38
|
-
if (isChanged(renderedSnapshot, currentSnapshot, affected, /* @__PURE__ */ new WeakMap())) calibrate(currentSnapshot);
|
|
39
|
+
if (!Object.is(renderedSnapshot, currentSnapshot) && isChanged(renderedSnapshot, currentSnapshot, affected, /* @__PURE__ */ new WeakMap())) calibrate(currentSnapshot);
|
|
39
40
|
}
|
|
40
41
|
}, [
|
|
41
42
|
affected,
|