@assistant-ui/tap 0.5.10 → 0.5.12
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 +21 -364
- package/dist/core/ResourceFiber.d.ts +9 -5
- package/dist/core/ResourceFiber.d.ts.map +1 -1
- package/dist/core/ResourceFiber.js +47 -45
- package/dist/core/ResourceFiber.js.map +1 -1
- package/dist/core/context.d.ts +7 -5
- package/dist/core/context.d.ts.map +1 -1
- package/dist/core/context.js +16 -15
- package/dist/core/context.js.map +1 -1
- package/dist/core/createResourceRoot.d.ts +8 -4
- package/dist/core/createResourceRoot.d.ts.map +1 -1
- package/dist/core/createResourceRoot.js +25 -26
- package/dist/core/createResourceRoot.js.map +1 -1
- package/dist/core/helpers/callResourceFn.d.ts +1 -2
- package/dist/core/helpers/callResourceFn.js +15 -13
- package/dist/core/helpers/callResourceFn.js.map +1 -1
- package/dist/core/helpers/commit.d.ts +7 -3
- package/dist/core/helpers/commit.d.ts.map +1 -1
- package/dist/core/helpers/commit.js +33 -50
- package/dist/core/helpers/commit.js.map +1 -1
- package/dist/core/helpers/env.d.ts +4 -1
- package/dist/core/helpers/env.d.ts.map +1 -1
- package/dist/core/helpers/env.js +5 -2
- package/dist/core/helpers/env.js.map +1 -1
- package/dist/core/helpers/execution-context.d.ts +8 -4
- package/dist/core/helpers/execution-context.d.ts.map +1 -1
- package/dist/core/helpers/execution-context.js +22 -27
- package/dist/core/helpers/execution-context.js.map +1 -1
- package/dist/core/helpers/root.d.ts +10 -6
- package/dist/core/helpers/root.d.ts.map +1 -1
- package/dist/core/helpers/root.js +45 -48
- package/dist/core/helpers/root.js.map +1 -1
- package/dist/core/resource.d.ts +8 -4
- package/dist/core/resource.d.ts.map +1 -1
- package/dist/core/resource.js +13 -9
- package/dist/core/resource.js.map +1 -1
- package/dist/core/scheduler.d.ts +11 -9
- package/dist/core/scheduler.d.ts.map +1 -1
- package/dist/core/scheduler.js +70 -83
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +56 -55
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +0 -2
- package/dist/core/withKey.d.ts +6 -2
- package/dist/core/withKey.d.ts.map +1 -1
- package/dist/core/withKey.js +9 -2
- package/dist/core/withKey.js.map +1 -1
- package/dist/hooks/tap-callback.d.ts +4 -1
- package/dist/hooks/tap-callback.d.ts.map +1 -1
- package/dist/hooks/tap-callback.js +6 -3
- package/dist/hooks/tap-callback.js.map +1 -1
- package/dist/hooks/tap-const.d.ts +4 -1
- package/dist/hooks/tap-const.d.ts.map +1 -1
- package/dist/hooks/tap-const.js +7 -3
- package/dist/hooks/tap-const.js.map +1 -1
- package/dist/hooks/tap-effect-event.d.ts +4 -1
- package/dist/hooks/tap-effect-event.d.ts.map +1 -1
- package/dist/hooks/tap-effect-event.js +31 -29
- package/dist/hooks/tap-effect-event.js.map +1 -1
- package/dist/hooks/tap-effect.d.ts +8 -5
- package/dist/hooks/tap-effect.d.ts.map +1 -1
- package/dist/hooks/tap-effect.js +35 -45
- package/dist/hooks/tap-effect.js.map +1 -1
- package/dist/hooks/tap-memo.d.ts +4 -1
- package/dist/hooks/tap-memo.d.ts.map +1 -1
- package/dist/hooks/tap-memo.js +17 -13
- package/dist/hooks/tap-memo.js.map +1 -1
- package/dist/hooks/tap-reducer.d.ts +7 -5
- package/dist/hooks/tap-reducer.d.ts.map +1 -1
- package/dist/hooks/tap-reducer.js +70 -76
- package/dist/hooks/tap-reducer.js.map +1 -1
- package/dist/hooks/tap-ref.d.ts +9 -6
- package/dist/hooks/tap-ref.d.ts.map +1 -1
- package/dist/hooks/tap-ref.js +7 -5
- package/dist/hooks/tap-ref.js.map +1 -1
- package/dist/hooks/tap-resource.d.ts +7 -3
- package/dist/hooks/tap-resource.d.ts.map +1 -1
- package/dist/hooks/tap-resource.js +31 -22
- package/dist/hooks/tap-resource.js.map +1 -1
- package/dist/hooks/tap-resources.d.ts +6 -2
- package/dist/hooks/tap-resources.d.ts.map +1 -1
- package/dist/hooks/tap-resources.js +74 -96
- package/dist/hooks/tap-resources.js.map +1 -1
- package/dist/hooks/tap-state.d.ts +7 -7
- package/dist/hooks/tap-state.d.ts.map +1 -1
- package/dist/hooks/tap-state.js +7 -5
- package/dist/hooks/tap-state.js.map +1 -1
- package/dist/hooks/utils/depsShallowEqual.d.ts +4 -1
- package/dist/hooks/utils/depsShallowEqual.d.ts.map +1 -1
- package/dist/hooks/utils/depsShallowEqual.js +8 -8
- package/dist/hooks/utils/depsShallowEqual.js.map +1 -1
- package/dist/hooks/utils/tapHook.d.ts +8 -4
- package/dist/hooks/utils/tapHook.d.ts.map +1 -1
- package/dist/hooks/utils/tapHook.js +17 -20
- package/dist/hooks/utils/tapHook.js.map +1 -1
- package/dist/index.d.ts +18 -18
- package/dist/index.js +17 -23
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +2 -2
- package/dist/react/use-resource.d.ts +6 -2
- package/dist/react/use-resource.d.ts.map +1 -1
- package/dist/react/use-resource.js +41 -36
- package/dist/react/use-resource.js.map +1 -1
- package/dist/tapResourceRoot.d.ts +18 -14
- package/dist/tapResourceRoot.d.ts.map +1 -1
- package/dist/tapResourceRoot.js +68 -77
- package/dist/tapResourceRoot.js.map +1 -1
- package/package.json +9 -9
- package/src/core/types.ts +1 -2
- package/dist/core/helpers/callResourceFn.d.ts.map +0 -1
- package/dist/core/types.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js.map +0 -1
package/dist/hooks/tap-effect.js
CHANGED
|
@@ -1,50 +1,40 @@
|
|
|
1
|
+
import { registerRenderMountTask, tapHook } from "./utils/tapHook.js";
|
|
1
2
|
import { depsShallowEqual } from "./utils/depsShallowEqual.js";
|
|
2
|
-
|
|
3
|
+
//#region src/hooks/tap-effect.ts
|
|
3
4
|
const newEffect = () => ({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
type: "effect",
|
|
6
|
+
cleanup: void 0,
|
|
7
|
+
deps: null
|
|
7
8
|
});
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
cell.deps = deps;
|
|
37
|
-
if (errors.length > 0) {
|
|
38
|
-
if (errors.length === 1) {
|
|
39
|
-
throw errors[0];
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
for (const error of errors) {
|
|
43
|
-
console.error(error);
|
|
44
|
-
}
|
|
45
|
-
throw new AggregateError(errors, "Errors during commit");
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
9
|
+
function tapEffect(effect, deps) {
|
|
10
|
+
const cell = tapHook("effect", newEffect);
|
|
11
|
+
if (deps && cell.deps && depsShallowEqual(cell.deps, deps)) return;
|
|
12
|
+
if (cell.deps !== null && !!deps !== !!cell.deps) throw new Error("tapEffect called with and without dependencies across re-renders");
|
|
13
|
+
registerRenderMountTask(() => {
|
|
14
|
+
const errors = [];
|
|
15
|
+
try {
|
|
16
|
+
cell.cleanup?.();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
errors.push(error);
|
|
19
|
+
} finally {
|
|
20
|
+
cell.cleanup = void 0;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const cleanup = effect();
|
|
24
|
+
if (cleanup !== void 0 && typeof cleanup !== "function") throw new Error(`An effect function must either return a cleanup function or nothing. Received: ${typeof cleanup}`);
|
|
25
|
+
cell.cleanup = cleanup;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
errors.push(error);
|
|
28
|
+
}
|
|
29
|
+
cell.deps = deps;
|
|
30
|
+
if (errors.length > 0) if (errors.length === 1) throw errors[0];
|
|
31
|
+
else {
|
|
32
|
+
for (const error of errors) console.error(error);
|
|
33
|
+
throw new AggregateError(errors, "Errors during commit");
|
|
34
|
+
}
|
|
35
|
+
});
|
|
49
36
|
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { tapEffect };
|
|
39
|
+
|
|
50
40
|
//# sourceMappingURL=tap-effect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-effect.js","
|
|
1
|
+
{"version":3,"file":"tap-effect.js","names":[],"sources":["../../src/hooks/tap-effect.ts"],"sourcesContent":["import type { Cell } from \"../core/types\";\nimport { depsShallowEqual } from \"./utils/depsShallowEqual\";\nimport { tapHook, registerRenderMountTask } from \"./utils/tapHook\";\n\nconst newEffect = (): Cell & { type: \"effect\" } => ({\n type: \"effect\",\n cleanup: undefined,\n deps: null, // null means the effect has never been run\n});\n\nexport namespace tapEffect {\n export type Destructor = () => void;\n export type EffectCallback = () => Destructor | undefined;\n}\n\nexport function tapEffect(effect: tapEffect.EffectCallback): void;\nexport function tapEffect(\n effect: tapEffect.EffectCallback,\n deps: readonly unknown[],\n): void;\nexport function tapEffect(\n effect: tapEffect.EffectCallback,\n deps?: readonly unknown[],\n): void {\n const cell = tapHook(\"effect\", newEffect);\n\n if (deps && cell.deps && depsShallowEqual(cell.deps, deps)) return;\n if (cell.deps !== null && !!deps !== !!cell.deps)\n throw new Error(\n \"tapEffect called with and without dependencies across re-renders\",\n );\n\n registerRenderMountTask(() => {\n const errors: unknown[] = [];\n\n try {\n cell.cleanup?.();\n } catch (error) {\n errors.push(error);\n } finally {\n cell.cleanup = undefined;\n }\n\n try {\n const cleanup = effect();\n\n if (cleanup !== undefined && typeof cleanup !== \"function\") {\n throw new Error(\n \"An effect function must either return a cleanup function or nothing. \" +\n `Received: ${typeof cleanup}`,\n );\n }\n\n cell.cleanup = cleanup;\n } catch (error) {\n errors.push(error);\n }\n\n cell.deps = deps;\n\n if (errors.length > 0) {\n if (errors.length === 1) {\n throw errors[0];\n } else {\n for (const error of errors) {\n console.error(error);\n }\n throw new AggregateError(errors, \"Errors during commit\");\n }\n }\n });\n}\n"],"mappings":";;;AAIA,MAAM,mBAA8C;CAClD,MAAM;CACN,SAAS,KAAA;CACT,MAAM;AACR;AAYA,SAAgB,UACd,QACA,MACM;CACN,MAAM,OAAO,QAAQ,UAAU,SAAS;CAExC,IAAI,QAAQ,KAAK,QAAQ,iBAAiB,KAAK,MAAM,IAAI,GAAG;CAC5D,IAAI,KAAK,SAAS,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,MAC1C,MAAM,IAAI,MACR,kEACF;CAEF,8BAA8B;EAC5B,MAAM,SAAoB,CAAC;EAE3B,IAAI;GACF,KAAK,UAAU;EACjB,SAAS,OAAO;GACd,OAAO,KAAK,KAAK;EACnB,UAAU;GACR,KAAK,UAAU,KAAA;EACjB;EAEA,IAAI;GACF,MAAM,UAAU,OAAO;GAEvB,IAAI,YAAY,KAAA,KAAa,OAAO,YAAY,YAC9C,MAAM,IAAI,MACR,kFACe,OAAO,SACxB;GAGF,KAAK,UAAU;EACjB,SAAS,OAAO;GACd,OAAO,KAAK,KAAK;EACnB;EAEA,KAAK,OAAO;EAEZ,IAAI,OAAO,SAAS,GAClB,IAAI,OAAO,WAAW,GACpB,MAAM,OAAO;OACR;GACL,KAAK,MAAM,SAAS,QAClB,QAAQ,MAAM,KAAK;GAErB,MAAM,IAAI,eAAe,QAAQ,sBAAsB;EACzD;CAEJ,CAAC;AACH"}
|
package/dist/hooks/tap-memo.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-memo.d.ts","
|
|
1
|
+
{"version":3,"file":"tap-memo.d.ts","names":[],"sources":["../../src/hooks/tap-memo.ts"],"mappings":";cAWa,OAAA,MAAc,EAAA,QAAU,CAAA,EAAG,IAAA,yBAA2B,CAkBlE"}
|
package/dist/hooks/tap-memo.js
CHANGED
|
@@ -2,20 +2,24 @@ import { isDevelopment } from "../core/helpers/env.js";
|
|
|
2
2
|
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
3
3
|
import { tapReducerWithDerivedState } from "./tap-reducer.js";
|
|
4
4
|
import { depsShallowEqual } from "./utils/depsShallowEqual.js";
|
|
5
|
+
//#region src/hooks/tap-memo.ts
|
|
5
6
|
const memoReducer = () => {
|
|
6
|
-
|
|
7
|
+
throw new Error("Memo reducer should not be called");
|
|
7
8
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
const tapMemo = (fn, deps) => {
|
|
10
|
+
const fiber = getCurrentResourceFiber();
|
|
11
|
+
const [state] = tapReducerWithDerivedState(memoReducer, (state) => {
|
|
12
|
+
if (state && depsShallowEqual(state.deps, deps)) return state;
|
|
13
|
+
const value = fn();
|
|
14
|
+
if (isDevelopment && fiber.devStrictMode) fn();
|
|
15
|
+
return {
|
|
16
|
+
value,
|
|
17
|
+
deps
|
|
18
|
+
};
|
|
19
|
+
}, null);
|
|
20
|
+
return state.value;
|
|
20
21
|
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { tapMemo };
|
|
24
|
+
|
|
21
25
|
//# sourceMappingURL=tap-memo.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-memo.js","
|
|
1
|
+
{"version":3,"file":"tap-memo.js","names":[],"sources":["../../src/hooks/tap-memo.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\nimport { tapReducerWithDerivedState } from \"./tap-reducer\";\nimport { depsShallowEqual } from \"./utils/depsShallowEqual\";\n\nconst memoReducer = () => {\n throw new Error(\"Memo reducer should not be called\");\n};\n\ntype MemoState<T> = { value: T; deps: readonly unknown[] };\n\nexport const tapMemo = <T>(fn: () => T, deps: readonly unknown[]): T => {\n const fiber = getCurrentResourceFiber();\n const [state] = tapReducerWithDerivedState(\n memoReducer,\n (state: MemoState<T> | null): MemoState<T> => {\n if (state && depsShallowEqual(state.deps, deps)) return state;\n\n const value = fn();\n\n if (isDevelopment && fiber.devStrictMode) {\n void fn();\n }\n\n return { value, deps };\n },\n null,\n );\n return state.value;\n};\n"],"mappings":";;;;;AAKA,MAAM,oBAAoB;CACxB,MAAM,IAAI,MAAM,mCAAmC;AACrD;AAIA,MAAa,WAAc,IAAa,SAAgC;CACtE,MAAM,QAAQ,wBAAwB;CACtC,MAAM,CAAC,SAAS,2BACd,cACC,UAA6C;EAC5C,IAAI,SAAS,iBAAiB,MAAM,MAAM,IAAI,GAAG,OAAO;EAExD,MAAM,QAAQ,GAAG;EAEjB,IAAI,iBAAiB,MAAM,eACzB,GAAQ;EAGV,OAAO;GAAE;GAAO;EAAK;CACvB,GACA,IACF;CACA,OAAO,MAAM;AACf"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
//#region src/hooks/tap-reducer.d.ts
|
|
1
2
|
type Dispatch<A> = (action: A) => void;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
declare function tapReducer<S, A>(reducer: (state: S, action: A) => S, initialState: S): [S, Dispatch<A>];
|
|
4
|
+
declare function tapReducer<S, A, I>(reducer: (state: S, action: A) => S, initialArg: I, init: (arg: I) => S): [S, Dispatch<A>];
|
|
5
|
+
declare function tapReducerWithDerivedState<S, A, R extends S>(reducer: (state: S, action: A) => S, getDerivedState: (state: S) => R, initialState: S): [R, Dispatch<A>];
|
|
6
|
+
declare function tapReducerWithDerivedState<S, A, I, R extends S>(reducer: (state: S, action: A) => S, getDerivedState: (state: S) => R, initialArg: I, init: (arg: I) => S): [R, Dispatch<A>];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { tapReducer, tapReducerWithDerivedState };
|
|
7
9
|
//# sourceMappingURL=tap-reducer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-reducer.d.ts","
|
|
1
|
+
{"version":3,"file":"tap-reducer.d.ts","names":[],"sources":["../../src/hooks/tap-reducer.ts"],"mappings":";KAMK,QAAA,OAAe,MAAA,EAAQ,CAAC;AAAA,iBAqGb,UAAA,MAAA,CACd,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,YAAA,EAAc,CAAA,IACZ,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBACA,UAAA,SAAA,CACd,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,UAAA,EAAY,CAAA,EACZ,IAAA,GAAO,GAAA,EAAK,CAAA,KAAM,CAAA,IAChB,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBAcA,0BAAA,iBAA2C,CAAA,CAAA,CACzD,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,eAAA,GAAkB,KAAA,EAAO,CAAA,KAAM,CAAA,EAC/B,YAAA,EAAc,CAAA,IACZ,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBACA,0BAAA,oBAA8C,CAAA,CAAA,CAC5D,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,eAAA,GAAkB,KAAA,EAAO,CAAA,KAAM,CAAA,EAC/B,UAAA,EAAY,CAAA,EACZ,IAAA,GAAO,GAAA,EAAK,CAAA,KAAM,CAAA,IAChB,CAAA,EAAG,QAAA,CAAS,CAAA"}
|
|
@@ -2,86 +2,80 @@ import { isDevelopment } from "../core/helpers/env.js";
|
|
|
2
2
|
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
3
3
|
import { markCellDirty } from "../core/helpers/root.js";
|
|
4
4
|
import { tapHook } from "./utils/tapHook.js";
|
|
5
|
+
//#region src/hooks/tap-reducer.ts
|
|
5
6
|
const dispatchOnFiber = (fiber, callback) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
return false;
|
|
20
|
-
});
|
|
7
|
+
if (fiber.renderContext) throw new Error("Resource updated during render");
|
|
8
|
+
if (fiber.isNeverMounted) throw new Error("Resource updated before mount");
|
|
9
|
+
fiber.root.dispatchUpdate(() => {
|
|
10
|
+
const result = callback();
|
|
11
|
+
if (result) {
|
|
12
|
+
result();
|
|
13
|
+
fiber.root.changelog.push(result);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
});
|
|
21
18
|
};
|
|
22
19
|
function tapReducerImpl(reducer, getDerivedState, initialArg, initFn) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (!Object.is(derived, cell.workInProgress)) {
|
|
75
|
-
markCellDirty(fiber, cell);
|
|
76
|
-
cell.workInProgress = derived;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return [cell.workInProgress, cell.dispatch];
|
|
20
|
+
const cell = tapHook("reducer", () => {
|
|
21
|
+
const fiber = getCurrentResourceFiber();
|
|
22
|
+
const initialState = initFn ? initFn(initialArg) : initialArg;
|
|
23
|
+
if (isDevelopment && fiber.devStrictMode && initFn) initFn(initialArg);
|
|
24
|
+
return {
|
|
25
|
+
type: "reducer",
|
|
26
|
+
queue: /* @__PURE__ */ new Set(),
|
|
27
|
+
dirty: false,
|
|
28
|
+
workInProgress: initialState,
|
|
29
|
+
current: initialState,
|
|
30
|
+
reducer,
|
|
31
|
+
dispatch: (action) => {
|
|
32
|
+
const entry = {
|
|
33
|
+
action,
|
|
34
|
+
hasEagerState: false,
|
|
35
|
+
eagerState: void 0
|
|
36
|
+
};
|
|
37
|
+
dispatchOnFiber(fiber, () => {
|
|
38
|
+
if (fiber.root.dirtyCells.length === 0 && !entry.hasEagerState) {
|
|
39
|
+
entry.eagerState = reducer(cell.workInProgress, action);
|
|
40
|
+
entry.hasEagerState = true;
|
|
41
|
+
if (Object.is(cell.current, entry.eagerState)) return null;
|
|
42
|
+
}
|
|
43
|
+
return () => {
|
|
44
|
+
markCellDirty(fiber, cell);
|
|
45
|
+
cell.queue.add(entry);
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
const fiber = getCurrentResourceFiber();
|
|
52
|
+
const sameReducer = reducer === cell.reducer;
|
|
53
|
+
cell.reducer = reducer;
|
|
54
|
+
for (const item of cell.queue) {
|
|
55
|
+
if (!item.hasEagerState || !sameReducer) {
|
|
56
|
+
item.eagerState = reducer(cell.workInProgress, item.action);
|
|
57
|
+
item.hasEagerState = true;
|
|
58
|
+
}
|
|
59
|
+
if (isDevelopment && fiber.devStrictMode) reducer(cell.workInProgress, item.action);
|
|
60
|
+
cell.workInProgress = item.eagerState;
|
|
61
|
+
}
|
|
62
|
+
cell.queue.clear();
|
|
63
|
+
if (getDerivedState) {
|
|
64
|
+
const derived = getDerivedState(cell.workInProgress);
|
|
65
|
+
if (!Object.is(derived, cell.workInProgress)) {
|
|
66
|
+
markCellDirty(fiber, cell);
|
|
67
|
+
cell.workInProgress = derived;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return [cell.workInProgress, cell.dispatch];
|
|
80
71
|
}
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
function tapReducer(reducer, initialArg, init) {
|
|
73
|
+
return tapReducerImpl(reducer, void 0, initialArg, init);
|
|
83
74
|
}
|
|
84
|
-
|
|
85
|
-
|
|
75
|
+
function tapReducerWithDerivedState(reducer, getDerivedState, initialArg, init) {
|
|
76
|
+
return tapReducerImpl(reducer, getDerivedState, initialArg, init);
|
|
86
77
|
}
|
|
78
|
+
//#endregion
|
|
79
|
+
export { tapReducer, tapReducerWithDerivedState };
|
|
80
|
+
|
|
87
81
|
//# sourceMappingURL=tap-reducer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-reducer.js","
|
|
1
|
+
{"version":3,"file":"tap-reducer.js","names":[],"sources":["../../src/hooks/tap-reducer.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\nimport type { ReducerQueueEntry, ResourceFiber } from \"../core/types\";\nimport { markCellDirty } from \"../core/helpers/root\";\nimport { tapHook } from \"./utils/tapHook\";\n\ntype Dispatch<A> = (action: A) => void;\n\nconst dispatchOnFiber = (\n fiber: ResourceFiber<any, any>,\n callback: () => (() => void) | null,\n): void => {\n if (fiber.renderContext) {\n throw new Error(\"Resource updated during render\");\n }\n if (fiber.isNeverMounted) {\n throw new Error(\"Resource updated before mount\");\n }\n\n fiber.root.dispatchUpdate(() => {\n const result = callback();\n if (result) {\n result();\n fiber.root.changelog.push(result);\n return true;\n }\n return false;\n });\n};\n\nfunction tapReducerImpl<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: ((state: S) => R) | undefined,\n initialArg: S | I,\n initFn: ((arg: I) => S) | undefined,\n): [R, Dispatch<A>] {\n const cell = tapHook(\"reducer\", () => {\n const fiber = getCurrentResourceFiber();\n\n // First render: compute initial state\n const initialState = initFn ? initFn(initialArg as I) : initialArg;\n\n if (isDevelopment && fiber.devStrictMode && initFn) {\n void initFn(initialArg as I);\n }\n\n return {\n type: \"reducer\",\n queue: new Set(),\n dirty: false,\n workInProgress: initialState,\n current: initialState,\n reducer,\n dispatch: (action: A) => {\n const entry: ReducerQueueEntry = {\n action,\n hasEagerState: false,\n eagerState: undefined,\n };\n\n dispatchOnFiber(fiber, () => {\n if (fiber.root.dirtyCells.length === 0 && !entry.hasEagerState) {\n entry.eagerState = reducer(cell.workInProgress, action);\n entry.hasEagerState = true;\n\n if (Object.is(cell.current, entry.eagerState)) return null;\n }\n\n return () => {\n markCellDirty(fiber, cell);\n cell.queue.add(entry);\n };\n });\n },\n };\n });\n\n const fiber = getCurrentResourceFiber();\n const sameReducer = reducer === cell.reducer;\n cell.reducer = reducer;\n\n for (const item of cell.queue) {\n if (!item.hasEagerState || !sameReducer) {\n item.eagerState = reducer(cell.workInProgress, item.action);\n item.hasEagerState = true;\n }\n\n if (isDevelopment && fiber.devStrictMode) {\n void reducer(cell.workInProgress, item.action);\n }\n\n cell.workInProgress = item.eagerState;\n }\n cell.queue.clear();\n\n if (getDerivedState) {\n const derived = getDerivedState(cell.workInProgress);\n\n if (!Object.is(derived, cell.workInProgress)) {\n markCellDirty(fiber, cell);\n cell.workInProgress = derived;\n }\n }\n\n return [cell.workInProgress, cell.dispatch];\n}\n\nexport function tapReducer<S, A>(\n reducer: (state: S, action: A) => S,\n initialState: S,\n): [S, Dispatch<A>];\nexport function tapReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: I,\n init: (arg: I) => S,\n): [S, Dispatch<A>];\nexport function tapReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: S | I,\n init?: (arg: I) => S,\n): [S, Dispatch<A>] {\n return tapReducerImpl(\n reducer,\n undefined,\n initialArg as S,\n init as ((arg: S) => S) | undefined,\n );\n}\n\nexport function tapReducerWithDerivedState<S, A, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialState: S,\n): [R, Dispatch<A>];\nexport function tapReducerWithDerivedState<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialArg: I,\n init: (arg: I) => S,\n): [R, Dispatch<A>];\nexport function tapReducerWithDerivedState<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialArg: I,\n init?: (arg: I) => S,\n): [R, Dispatch<A>] {\n return tapReducerImpl(reducer, getDerivedState, initialArg, init);\n}\n"],"mappings":";;;;;AAQA,MAAM,mBACJ,OACA,aACS;CACT,IAAI,MAAM,eACR,MAAM,IAAI,MAAM,gCAAgC;CAElD,IAAI,MAAM,gBACR,MAAM,IAAI,MAAM,+BAA+B;CAGjD,MAAM,KAAK,qBAAqB;EAC9B,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;GACV,OAAO;GACP,MAAM,KAAK,UAAU,KAAK,MAAM;GAChC,OAAO;EACT;EACA,OAAO;CACT,CAAC;AACH;AAEA,SAAS,eACP,SACA,iBACA,YACA,QACkB;CAClB,MAAM,OAAO,QAAQ,iBAAiB;EACpC,MAAM,QAAQ,wBAAwB;EAGtC,MAAM,eAAe,SAAS,OAAO,UAAe,IAAI;EAExD,IAAI,iBAAiB,MAAM,iBAAiB,QAC1C,OAAY,UAAe;EAG7B,OAAO;GACL,MAAM;GACN,uBAAO,IAAI,IAAI;GACf,OAAO;GACP,gBAAgB;GAChB,SAAS;GACT;GACA,WAAW,WAAc;IACvB,MAAM,QAA2B;KAC/B;KACA,eAAe;KACf,YAAY,KAAA;IACd;IAEA,gBAAgB,aAAa;KAC3B,IAAI,MAAM,KAAK,WAAW,WAAW,KAAK,CAAC,MAAM,eAAe;MAC9D,MAAM,aAAa,QAAQ,KAAK,gBAAgB,MAAM;MACtD,MAAM,gBAAgB;MAEtB,IAAI,OAAO,GAAG,KAAK,SAAS,MAAM,UAAU,GAAG,OAAO;KACxD;KAEA,aAAa;MACX,cAAc,OAAO,IAAI;MACzB,KAAK,MAAM,IAAI,KAAK;KACtB;IACF,CAAC;GACH;EACF;CACF,CAAC;CAED,MAAM,QAAQ,wBAAwB;CACtC,MAAM,cAAc,YAAY,KAAK;CACrC,KAAK,UAAU;CAEf,KAAK,MAAM,QAAQ,KAAK,OAAO;EAC7B,IAAI,CAAC,KAAK,iBAAiB,CAAC,aAAa;GACvC,KAAK,aAAa,QAAQ,KAAK,gBAAgB,KAAK,MAAM;GAC1D,KAAK,gBAAgB;EACvB;EAEA,IAAI,iBAAiB,MAAM,eACzB,QAAa,KAAK,gBAAgB,KAAK,MAAM;EAG/C,KAAK,iBAAiB,KAAK;CAC7B;CACA,KAAK,MAAM,MAAM;CAEjB,IAAI,iBAAiB;EACnB,MAAM,UAAU,gBAAgB,KAAK,cAAc;EAEnD,IAAI,CAAC,OAAO,GAAG,SAAS,KAAK,cAAc,GAAG;GAC5C,cAAc,OAAO,IAAI;GACzB,KAAK,iBAAiB;EACxB;CACF;CAEA,OAAO,CAAC,KAAK,gBAAgB,KAAK,QAAQ;AAC5C;AAWA,SAAgB,WACd,SACA,YACA,MACkB;CAClB,OAAO,eACL,SACA,KAAA,GACA,YACA,IACF;AACF;AAaA,SAAgB,2BACd,SACA,iBACA,YACA,MACkB;CAClB,OAAO,eAAe,SAAS,iBAAiB,YAAY,IAAI;AAClE"}
|
package/dist/hooks/tap-ref.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region src/hooks/tap-ref.d.ts
|
|
2
|
+
declare namespace tapRef {
|
|
3
|
+
interface RefObject<T> {
|
|
4
|
+
current: T;
|
|
5
|
+
}
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
declare function tapRef<T>(initialValue: T): tapRef.RefObject<T>;
|
|
8
|
+
declare function tapRef<T = undefined>(): tapRef.RefObject<T | undefined>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { tapRef };
|
|
8
11
|
//# sourceMappingURL=tap-ref.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-ref.d.ts","
|
|
1
|
+
{"version":3,"file":"tap-ref.d.ts","names":[],"sources":["../../src/hooks/tap-ref.ts"],"mappings":";kBAEiB,MAAA;EAAA,UACE,SAAA;IACf,OAAA,EAAS,CAAC;EAAA;AAAA;AAAA,iBAIE,MAAA,GAAA,CAAU,YAAA,EAAc,CAAA,GAAI,MAAA,CAAO,SAAA,CAAU,CAAA;AAAA,iBAC7C,MAAA,eAAA,CAAA,GAAyB,MAAA,CAAO,SAAS,CAAC,CAAA"}
|
package/dist/hooks/tap-ref.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { tapState } from "./tap-state.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return state;
|
|
2
|
+
//#region src/hooks/tap-ref.ts
|
|
3
|
+
function tapRef(initialValue) {
|
|
4
|
+
const [state] = tapState(() => ({ current: initialValue }));
|
|
5
|
+
return state;
|
|
7
6
|
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { tapRef };
|
|
9
|
+
|
|
8
10
|
//# sourceMappingURL=tap-ref.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-ref.js","
|
|
1
|
+
{"version":3,"file":"tap-ref.js","names":[],"sources":["../../src/hooks/tap-ref.ts"],"sourcesContent":["import { tapState } from \"./tap-state\";\n\nexport namespace tapRef {\n export interface RefObject<T> {\n current: T;\n }\n}\n\nexport function tapRef<T>(initialValue: T): tapRef.RefObject<T>;\nexport function tapRef<T = undefined>(): tapRef.RefObject<T | undefined>;\nexport function tapRef<T>(initialValue?: T): tapRef.RefObject<T | undefined> {\n const [state] = tapState(() => ({\n current: initialValue,\n }));\n return state;\n}\n"],"mappings":";;AAUA,SAAgB,OAAU,cAAmD;CAC3E,MAAM,CAAC,SAAS,gBAAgB,EAC9B,SAAS,aACX,EAAE;CACF,OAAO;AACT"}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { ExtractResourceReturnType, ResourceElement } from "../core/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/tap-resource.d.ts
|
|
4
|
+
declare function tapResource<E extends ResourceElement<any, any>>(element: E): ExtractResourceReturnType<E>;
|
|
5
|
+
declare function tapResource<E extends ResourceElement<any, any>>(element: E, propsDeps: readonly unknown[]): ExtractResourceReturnType<E>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { tapResource };
|
|
4
8
|
//# sourceMappingURL=tap-resource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-resource.d.ts","
|
|
1
|
+
{"version":3,"file":"tap-resource.d.ts","names":[],"sources":["../../src/hooks/tap-resource.ts"],"mappings":";;;iBAYgB,WAAA,WAAsB,eAAA,WAAA,CACpC,OAAA,EAAS,CAAA,GACR,yBAAA,CAA0B,CAAA;AAAA,iBACb,WAAA,WAAsB,eAAA,WAAA,CACpC,OAAA,EAAS,CAAA,EACT,SAAA,uBACC,yBAAA,CAA0B,CAAA"}
|
|
@@ -1,26 +1,35 @@
|
|
|
1
|
+
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
1
2
|
import { tapEffect } from "./tap-effect.js";
|
|
2
|
-
import { createResourceFiber, unmountResourceFiber, renderResourceFiber, commitResourceFiber, } from "../core/ResourceFiber.js";
|
|
3
|
-
import { tapMemo } from "./tap-memo.js";
|
|
4
3
|
import { tapRef } from "./tap-ref.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
import { tapMemo } from "./tap-memo.js";
|
|
5
|
+
import { commitResourceFiber, createResourceFiber, renderResourceFiber, unmountResourceFiber } from "../core/ResourceFiber.js";
|
|
6
|
+
//#region src/hooks/tap-resource.ts
|
|
7
|
+
function tapResource(element, propsDeps) {
|
|
8
|
+
const parentFiber = getCurrentResourceFiber();
|
|
9
|
+
const versionRef = tapRef(0);
|
|
10
|
+
const fiber = tapMemo(() => {
|
|
11
|
+
element.key;
|
|
12
|
+
return createResourceFiber(element.type, parentFiber.root, () => {
|
|
13
|
+
versionRef.current++;
|
|
14
|
+
parentFiber.markDirty?.();
|
|
15
|
+
});
|
|
16
|
+
}, [
|
|
17
|
+
element.type,
|
|
18
|
+
element.key,
|
|
19
|
+
parentFiber
|
|
20
|
+
]);
|
|
21
|
+
const result = propsDeps ? tapMemo(() => renderResourceFiber(fiber, element.props), [
|
|
22
|
+
fiber,
|
|
23
|
+
...propsDeps,
|
|
24
|
+
versionRef.current
|
|
25
|
+
]) : renderResourceFiber(fiber, element.props);
|
|
26
|
+
tapEffect(() => () => unmountResourceFiber(fiber), [fiber]);
|
|
27
|
+
tapEffect(() => {
|
|
28
|
+
commitResourceFiber(fiber, result);
|
|
29
|
+
}, [fiber, result]);
|
|
30
|
+
return result.output;
|
|
25
31
|
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { tapResource };
|
|
34
|
+
|
|
26
35
|
//# sourceMappingURL=tap-resource.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-resource.js","
|
|
1
|
+
{"version":3,"file":"tap-resource.js","names":[],"sources":["../../src/hooks/tap-resource.ts"],"sourcesContent":["import type { ExtractResourceReturnType, ResourceElement } from \"../core/types\";\nimport { tapEffect } from \"./tap-effect\";\nimport {\n createResourceFiber,\n unmountResourceFiber,\n renderResourceFiber,\n commitResourceFiber,\n} from \"../core/ResourceFiber\";\nimport { tapMemo } from \"./tap-memo\";\nimport { tapRef } from \"./tap-ref\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\n\nexport function tapResource<E extends ResourceElement<any, any>>(\n element: E,\n): ExtractResourceReturnType<E>;\nexport function tapResource<E extends ResourceElement<any, any>>(\n element: E,\n propsDeps: readonly unknown[],\n): ExtractResourceReturnType<E>;\nexport function tapResource<E extends ResourceElement<any, any>>(\n element: E,\n propsDeps?: readonly unknown[],\n): ExtractResourceReturnType<E> {\n const parentFiber = getCurrentResourceFiber();\n const versionRef = tapRef(0);\n const fiber = tapMemo(() => {\n void element.key;\n return createResourceFiber(element.type, parentFiber.root, () => {\n versionRef.current++;\n parentFiber.markDirty?.();\n });\n }, [element.type, element.key, parentFiber]);\n\n const result = propsDeps\n ? // biome-ignore lint/correctness/useExhaustiveDependencies: user provided deps instead of prop identity\n tapMemo(\n () => renderResourceFiber(fiber, element.props),\n [fiber, ...propsDeps, versionRef.current],\n )\n : renderResourceFiber(fiber, element.props);\n\n tapEffect(() => () => unmountResourceFiber(fiber), [fiber]);\n tapEffect(() => {\n commitResourceFiber(fiber, result);\n }, [fiber, result]);\n\n return result.output;\n}\n"],"mappings":";;;;;;AAmBA,SAAgB,YACd,SACA,WAC8B;CAC9B,MAAM,cAAc,wBAAwB;CAC5C,MAAM,aAAa,OAAO,CAAC;CAC3B,MAAM,QAAQ,cAAc;EAC1B,QAAa;EACb,OAAO,oBAAoB,QAAQ,MAAM,YAAY,YAAY;GAC/D,WAAW;GACX,YAAY,YAAY;EAC1B,CAAC;CACH,GAAG;EAAC,QAAQ;EAAM,QAAQ;EAAK;CAAW,CAAC;CAE3C,MAAM,SAAS,YAEX,cACQ,oBAAoB,OAAO,QAAQ,KAAK,GAC9C;EAAC;EAAO,GAAG;EAAW,WAAW;CAAO,CAC1C,IACA,oBAAoB,OAAO,QAAQ,KAAK;CAE5C,sBAAsB,qBAAqB,KAAK,GAAG,CAAC,KAAK,CAAC;CAC1D,gBAAgB;EACd,oBAAoB,OAAO,MAAM;CACnC,GAAG,CAAC,OAAO,MAAM,CAAC;CAElB,OAAO,OAAO;AAChB"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { ExtractResourceReturnType, ResourceElement } from "../core/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/tap-resources.d.ts
|
|
4
|
+
declare function tapResources<E extends ResourceElement<any, any>>(getElements: () => readonly E[], getElementsDeps?: readonly unknown[]): ExtractResourceReturnType<E>[];
|
|
5
|
+
//#endregion
|
|
6
|
+
export { tapResources };
|
|
3
7
|
//# sourceMappingURL=tap-resources.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-resources.d.ts","
|
|
1
|
+
{"version":3,"file":"tap-resources.d.ts","names":[],"sources":["../../src/hooks/tap-resources.ts"],"mappings":";;;iBA2BgB,YAAA,WAAuB,eAAA,WAAA,CACrC,WAAA,iBAA4B,CAAA,IAC5B,eAAA,wBACC,yBAAA,CAA0B,CAAA"}
|