@assistant-ui/tap 0.0.2 → 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 +1 -1
- package/README.md +1 -2
- package/dist/__tests__/test-utils.d.ts +10 -10
- package/dist/__tests__/test-utils.d.ts.map +1 -1
- package/dist/__tests__/test-utils.js +3 -82
- package/dist/__tests__/test-utils.js.map +1 -1
- package/dist/core/ResourceFiber.d.ts +4 -4
- package/dist/core/ResourceFiber.d.ts.map +1 -1
- package/dist/core/ResourceFiber.js +19 -11
- package/dist/core/ResourceFiber.js.map +1 -1
- package/dist/core/ResourceHandle.d.ts +2 -1
- package/dist/core/ResourceHandle.d.ts.map +1 -1
- package/dist/core/ResourceHandle.js +23 -10
- package/dist/core/ResourceHandle.js.map +1 -1
- package/dist/core/commit.d.ts +2 -2
- package/dist/core/commit.d.ts.map +1 -1
- package/dist/core/commit.js +9 -5
- package/dist/core/commit.js.map +1 -1
- package/dist/core/execution-context.d.ts +2 -2
- package/dist/core/execution-context.d.ts.map +1 -1
- package/dist/core/execution-context.js +0 -4
- package/dist/core/execution-context.js.map +1 -1
- package/dist/core/resource.d.ts +1 -1
- package/dist/core/resource.d.ts.map +1 -1
- package/dist/core/resource.js.map +1 -1
- package/dist/core/scheduler.d.ts.map +1 -1
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +10 -7
- package/dist/core/types.d.ts.map +1 -1
- package/dist/hooks/depsShallowEqual.js.map +1 -1
- package/dist/hooks/tap-callback.js.map +1 -1
- package/dist/hooks/tap-effect.d.ts.map +1 -1
- package/dist/hooks/tap-effect.js +12 -8
- package/dist/hooks/tap-effect.js.map +1 -1
- package/dist/hooks/tap-ref.d.ts +3 -4
- package/dist/hooks/tap-ref.d.ts.map +1 -1
- package/dist/hooks/tap-ref.js.map +1 -1
- package/dist/hooks/tap-resource.js.map +1 -1
- package/dist/hooks/tap-resources.d.ts +1 -1
- package/dist/hooks/tap-resources.d.ts.map +1 -1
- package/dist/hooks/tap-resources.js +44 -48
- package/dist/hooks/tap-resources.js.map +1 -1
- package/dist/hooks/tap-state.d.ts +2 -1
- package/dist/hooks/tap-state.d.ts.map +1 -1
- package/dist/hooks/tap-state.js +19 -10
- package/dist/hooks/tap-state.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/use-resource.js.map +1 -1
- package/package.json +1 -1
- package/dist/hooks/tap-rerender.d.ts +0 -2
- package/dist/hooks/tap-rerender.d.ts.map +0 -1
- package/dist/hooks/tap-rerender.js +0 -10
- package/dist/hooks/tap-rerender.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/scheduler.ts"],"sourcesContent":["type Task = () => void;\n\nlet queue: Task[] = [];\nlet isFlushPending = false;\n\nfunction flushQueue() {\n isFlushPending = false;\n\n const tasksToRun = queue;\n queue = [];\n\n for (const task of tasksToRun) {\n try {\n task();\n } catch (error) {\n console.error(\"Error in scheduled task:\", error);\n }\n }\n}\n\nfunction scheduleUpdate(task: Task) {\n queue.push(task);\n\n if (!isFlushPending) {\n isFlushPending = true;\n queueMicrotask(flushQueue);\n }\n}\n\nexport class UpdateScheduler {\n private _isDirty = false;\n private _hasScheduledTask = false;\n private _isFlushing = false;\n private static readonly MAX_FLUSH_DEPTH = 50;\n\n constructor(private readonly _task: Task) {}\n\n get isDirty() {\n return this._isDirty;\n }\n\n markDirty() {\n this._isDirty = true;\n\n if (this._hasScheduledTask || this._isFlushing) return;\n this._hasScheduledTask = true;\n\n scheduleUpdate(() => {\n this._hasScheduledTask = false;\n\n this.flushSync();\n });\n }\n\n flushSync() {\n if (this._isFlushing) return;\n\n this._isFlushing = true;\n let flushDepth = 0;\n\n try {\n while (this._isDirty) {\n flushDepth++;\n\n if (flushDepth > UpdateScheduler.MAX_FLUSH_DEPTH) {\n throw new Error(\n `Maximum update depth exceeded. This can happen when a resource ` +\n `repeatedly calls setState inside tapEffect
|
|
1
|
+
{"version":3,"sources":["../../src/core/scheduler.ts"],"sourcesContent":["type Task = () => void;\n\nlet queue: Task[] = [];\nlet isFlushPending = false;\n\nfunction flushQueue() {\n isFlushPending = false;\n\n const tasksToRun = queue;\n queue = [];\n\n for (const task of tasksToRun) {\n try {\n task();\n } catch (error) {\n console.error(\"Error in scheduled task:\", error);\n }\n }\n}\n\nfunction scheduleUpdate(task: Task) {\n queue.push(task);\n\n if (!isFlushPending) {\n isFlushPending = true;\n queueMicrotask(flushQueue);\n }\n}\n\nexport class UpdateScheduler {\n private _isDirty = false;\n private _hasScheduledTask = false;\n private _isFlushing = false;\n private static readonly MAX_FLUSH_DEPTH = 50;\n\n constructor(private readonly _task: Task) {}\n\n get isDirty() {\n return this._isDirty;\n }\n\n markDirty() {\n this._isDirty = true;\n\n if (this._hasScheduledTask || this._isFlushing) return;\n this._hasScheduledTask = true;\n\n scheduleUpdate(() => {\n this._hasScheduledTask = false;\n\n this.flushSync();\n });\n }\n\n flushSync() {\n if (this._isFlushing) return;\n\n this._isFlushing = true;\n let flushDepth = 0;\n\n try {\n while (this._isDirty) {\n flushDepth++;\n\n if (flushDepth > UpdateScheduler.MAX_FLUSH_DEPTH) {\n throw new Error(\n `Maximum update depth exceeded. This can happen when a resource ` +\n `repeatedly calls setState inside tapEffect.`,\n );\n }\n\n this._isDirty = false;\n this._task();\n }\n } finally {\n this._isFlushing = false;\n }\n }\n}\n"],"mappings":";AAEA,IAAI,QAAgB,CAAC;AACrB,IAAI,iBAAiB;AAErB,SAAS,aAAa;AACpB,mBAAiB;AAEjB,QAAM,aAAa;AACnB,UAAQ,CAAC;AAET,aAAW,QAAQ,YAAY;AAC7B,QAAI;AACF,WAAK;AAAA,IACP,SAAS,OAAO;AACd,cAAQ,MAAM,4BAA4B,KAAK;AAAA,IACjD;AAAA,EACF;AACF;AAEA,SAAS,eAAe,MAAY;AAClC,QAAM,KAAK,IAAI;AAEf,MAAI,CAAC,gBAAgB;AACnB,qBAAiB;AACjB,mBAAe,UAAU;AAAA,EAC3B;AACF;AAEO,IAAM,kBAAN,MAAM,iBAAgB;AAAA,EAM3B,YAA6B,OAAa;AAAb;AAAA,EAAc;AAAA,EALnC,WAAW;AAAA,EACX,oBAAoB;AAAA,EACpB,cAAc;AAAA,EACtB,OAAwB,kBAAkB;AAAA,EAI1C,IAAI,UAAU;AACZ,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,YAAY;AACV,SAAK,WAAW;AAEhB,QAAI,KAAK,qBAAqB,KAAK,YAAa;AAChD,SAAK,oBAAoB;AAEzB,mBAAe,MAAM;AACnB,WAAK,oBAAoB;AAEzB,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA,EAEA,YAAY;AACV,QAAI,KAAK,YAAa;AAEtB,SAAK,cAAc;AACnB,QAAI,aAAa;AAEjB,QAAI;AACF,aAAO,KAAK,UAAU;AACpB;AAEA,YAAI,aAAa,iBAAgB,iBAAiB;AAChD,gBAAM,IAAI;AAAA,YACR;AAAA,UAEF;AAAA,QACF;AAEA,aAAK,WAAW;AAChB,aAAK,MAAM;AAAA,MACb;AAAA,IACF,UAAE;AACA,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AACF;","names":[]}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -5,9 +5,11 @@ export type ResourceElement<R, P = any> = {
|
|
|
5
5
|
props: P;
|
|
6
6
|
key?: string | number;
|
|
7
7
|
};
|
|
8
|
-
export type ResourceElementConstructor<R, P> = (
|
|
8
|
+
export type ResourceElementConstructor<R, P> = (...args: undefined extends P ? [props?: P, options?: {
|
|
9
9
|
key?: string | number;
|
|
10
|
-
}
|
|
10
|
+
}] : [props: P, options?: {
|
|
11
|
+
key?: string | number;
|
|
12
|
+
}]) => ResourceElement<R, P>;
|
|
11
13
|
export type StateUpdater<S> = S | ((prev: S) => S);
|
|
12
14
|
export type Destructor = () => void;
|
|
13
15
|
export type EffectCallback = () => void | Destructor;
|
|
@@ -31,14 +33,15 @@ export interface RenderResult {
|
|
|
31
33
|
props: any;
|
|
32
34
|
commitTasks: EffectTask[];
|
|
33
35
|
}
|
|
34
|
-
export interface ResourceFiber {
|
|
36
|
+
export interface ResourceFiber<R, P> {
|
|
35
37
|
readonly scheduleRerender: () => void;
|
|
36
|
-
readonly resourceFn: ResourceFn<
|
|
38
|
+
readonly resourceFn: ResourceFn<R, P>;
|
|
37
39
|
cells: Cell[];
|
|
38
40
|
currentIndex: number;
|
|
39
|
-
|
|
41
|
+
renderContext: RenderResult | undefined;
|
|
42
|
+
committedProps: P | undefined;
|
|
43
|
+
isMounted: boolean;
|
|
40
44
|
isFirstRender: boolean;
|
|
41
|
-
|
|
42
|
-
isRendering: boolean;
|
|
45
|
+
isNeverMounted: boolean;
|
|
43
46
|
}
|
|
44
47
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;AAE/C,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AAErC,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,IAAI;IACxC,IAAI,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACvB,KAAK,EAAE,CAAC,CAAC;IACT,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,0BAA0B,CAAC,CAAC,EAAE,CAAC,IAAI,CAC7C,KAAK,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;AAE/C,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AAErC,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,IAAI;IACxC,IAAI,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACvB,KAAK,EAAE,CAAC,CAAC;IACT,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,0BAA0B,CAAC,CAAC,EAAE,CAAC,IAAI,CAC7C,GAAG,IAAI,EAAE,SAAS,SAAS,CAAC,GACxB,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,GAChD,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,KAChD,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE3B,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAEnD,MAAM,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC;AACpC,MAAM,MAAM,cAAc,GAAG,MAAM,IAAI,GAAG,UAAU,CAAC;AAErD,MAAM,MAAM,IAAI,GACZ;IACE,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,GAAG,CAAC;IACX,GAAG,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC;CAC3C,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IACjC,IAAI,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,SAAS,CAAC;CACvC,CAAC;AAEN,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,cAAc,CAAC;IACvB,IAAI,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,SAAS,CAAC;IACtC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,GAAG,CAAC;IACX,KAAK,EAAE,GAAG,CAAC;IACX,WAAW,EAAE,UAAU,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,EAAE,CAAC;IACjC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,IAAI,CAAC;IACtC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAEtC,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IAErB,aAAa,EAAE,YAAY,GAAG,SAAS,CAAC;IACxC,cAAc,EAAE,CAAC,GAAG,SAAS,CAAC;IAE9B,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,OAAO,CAAC;IACvB,cAAc,EAAE,OAAO,CAAC;CACzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/depsShallowEqual.ts"],"sourcesContent":["export const depsShallowEqual = (\n a: readonly unknown[],\n b: readonly unknown[]
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/depsShallowEqual.ts"],"sourcesContent":["export const depsShallowEqual = (\n a: readonly unknown[],\n b: readonly unknown[],\n) => {\n if (a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) {\n if (!Object.is(a[i], b[i])) return false;\n }\n return true;\n};\n"],"mappings":";AAAO,IAAM,mBAAmB,CAC9B,GACA,MACG;AACH,MAAI,EAAE,WAAW,EAAE,OAAQ,QAAO;AAClC,WAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;AACjC,QAAI,CAAC,OAAO,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,EAAG,QAAO;AAAA,EACrC;AACA,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-callback.ts"],"sourcesContent":["import { tapMemo } from \"./tap-memo\";\n\nexport const tapCallback = <T extends (...args: any[]) => any>(\n fn: T,\n deps: readonly unknown[]
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-callback.ts"],"sourcesContent":["import { tapMemo } from \"./tap-memo\";\n\nexport const tapCallback = <T extends (...args: any[]) => any>(\n fn: T,\n deps: readonly unknown[],\n): T => {\n return tapMemo(() => fn, deps);\n};\n"],"mappings":";AAAA,SAAS,eAAe;AAEjB,IAAM,cAAc,CACzB,IACA,SACM;AACN,SAAO,QAAQ,MAAM,IAAI,IAAI;AAC/B;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-effect.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-effect.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAQ,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"tap-effect.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-effect.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAQ,MAAM,eAAe,CAAC;AAgCrD,wBAAgB,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAAC;AACxD,wBAAgB,SAAS,CACvB,MAAM,EAAE,cAAc,EACtB,IAAI,EAAE,SAAS,OAAO,EAAE,GACvB,IAAI,CAAC"}
|
package/dist/hooks/tap-effect.js
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
// src/hooks/tap-effect.ts
|
|
2
2
|
import { getCurrentResourceFiber } from "../core/execution-context.js";
|
|
3
3
|
function getEffectCell() {
|
|
4
|
-
const
|
|
5
|
-
const index =
|
|
6
|
-
if (!
|
|
4
|
+
const fiber = getCurrentResourceFiber();
|
|
5
|
+
const index = fiber.currentIndex++;
|
|
6
|
+
if (!fiber.isFirstRender && index >= fiber.cells.length) {
|
|
7
7
|
throw new Error(
|
|
8
8
|
"Rendered more hooks than during the previous render. Hooks must be called in the exact same order in every render."
|
|
9
9
|
);
|
|
10
10
|
}
|
|
11
|
-
if (!
|
|
11
|
+
if (!fiber.cells[index]) {
|
|
12
12
|
const cell2 = {
|
|
13
13
|
type: "effect",
|
|
14
14
|
mounted: false
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
fiber.cells[index] = cell2;
|
|
17
17
|
}
|
|
18
|
-
const cell =
|
|
18
|
+
const cell = fiber.cells[index];
|
|
19
19
|
if (cell.type !== "effect") {
|
|
20
20
|
throw new Error("Hook order changed between renders");
|
|
21
21
|
}
|
|
22
22
|
return index;
|
|
23
23
|
}
|
|
24
24
|
function tapEffect(effect, deps) {
|
|
25
|
-
const
|
|
25
|
+
const fiber = getCurrentResourceFiber();
|
|
26
26
|
const cellIndex = getEffectCell();
|
|
27
|
-
|
|
27
|
+
fiber.renderContext.commitTasks.push({
|
|
28
|
+
effect,
|
|
29
|
+
deps,
|
|
30
|
+
cellIndex
|
|
31
|
+
});
|
|
28
32
|
}
|
|
29
33
|
export {
|
|
30
34
|
tapEffect
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-effect.ts"],"sourcesContent":["import { getCurrentResourceFiber } from \"../core/execution-context\";\nimport { EffectCallback, Cell } from \"../core/types\";\n\nfunction getEffectCell(): number {\n const
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-effect.ts"],"sourcesContent":["import { getCurrentResourceFiber } from \"../core/execution-context\";\nimport { EffectCallback, Cell } from \"../core/types\";\n\nfunction getEffectCell(): number {\n const fiber = getCurrentResourceFiber();\n const index = fiber.currentIndex++;\n\n // Check if we're trying to use more hooks than in previous renders\n if (!fiber.isFirstRender && index >= fiber.cells.length) {\n throw new Error(\n \"Rendered more hooks than during the previous render. \" +\n \"Hooks must be called in the exact same order in every render.\",\n );\n }\n\n if (!fiber.cells[index]) {\n // Create the effect cell\n const cell: Cell & { type: \"effect\" } = {\n type: \"effect\",\n mounted: false,\n };\n\n fiber.cells[index] = cell;\n }\n\n const cell = fiber.cells[index];\n if (cell.type !== \"effect\") {\n throw new Error(\"Hook order changed between renders\");\n }\n\n return index;\n}\n\nexport function tapEffect(effect: EffectCallback): void;\nexport function tapEffect(\n effect: EffectCallback,\n deps: readonly unknown[],\n): void;\nexport function tapEffect(\n effect: EffectCallback,\n deps?: readonly unknown[],\n): void {\n const fiber = getCurrentResourceFiber();\n\n // Reserve a spot for the effect cell and get its index\n const cellIndex = getEffectCell();\n\n // Add task to render context for execution in commit phase\n fiber.renderContext!.commitTasks.push({\n effect,\n deps,\n cellIndex,\n });\n}\n"],"mappings":";AAAA,SAAS,+BAA+B;AAGxC,SAAS,gBAAwB;AAC/B,QAAM,QAAQ,wBAAwB;AACtC,QAAM,QAAQ,MAAM;AAGpB,MAAI,CAAC,MAAM,iBAAiB,SAAS,MAAM,MAAM,QAAQ;AACvD,UAAM,IAAI;AAAA,MACR;AAAA,IAEF;AAAA,EACF;AAEA,MAAI,CAAC,MAAM,MAAM,KAAK,GAAG;AAEvB,UAAMA,QAAkC;AAAA,MACtC,MAAM;AAAA,MACN,SAAS;AAAA,IACX;AAEA,UAAM,MAAM,KAAK,IAAIA;AAAA,EACvB;AAEA,QAAM,OAAO,MAAM,MAAM,KAAK;AAC9B,MAAI,KAAK,SAAS,UAAU;AAC1B,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AACT;AAOO,SAAS,UACd,QACA,MACM;AACN,QAAM,QAAQ,wBAAwB;AAGtC,QAAM,YAAY,cAAc;AAGhC,QAAM,cAAe,YAAY,KAAK;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;","names":["cell"]}
|
package/dist/hooks/tap-ref.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
interface
|
|
1
|
+
export interface RefObject<T> {
|
|
2
2
|
current: T;
|
|
3
3
|
}
|
|
4
|
-
export declare function tapRef<T>(initialValue: T | (() => T)):
|
|
5
|
-
export declare function tapRef<T = undefined>():
|
|
6
|
-
export {};
|
|
4
|
+
export declare function tapRef<T>(initialValue: T | (() => T)): RefObject<T>;
|
|
5
|
+
export declare function tapRef<T = undefined>(): RefObject<T | undefined>;
|
|
7
6
|
//# sourceMappingURL=tap-ref.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-ref.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-ref.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"tap-ref.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-ref.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,SAAS,CAAC,CAAC;IAC1B,OAAO,EAAE,CAAC,CAAC;CACZ;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;AACrE,wBAAgB,MAAM,CAAC,CAAC,GAAG,SAAS,KAAK,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-ref.ts"],"sourcesContent":["import { tapState } from \"./tap-state\";\n\
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-ref.ts"],"sourcesContent":["import { tapState } from \"./tap-state\";\n\nexport interface RefObject<T> {\n current: T;\n}\n\nexport function tapRef<T>(initialValue: T | (() => T)): RefObject<T>;\nexport function tapRef<T = undefined>(): RefObject<T | undefined>;\nexport function tapRef<T>(\n initialValue?: T | (() => T),\n): RefObject<T | undefined> {\n const [state] = tapState(() => ({\n current:\n initialValue !== undefined && typeof initialValue === \"function\"\n ? (initialValue as () => T)()\n : initialValue,\n }));\n return state;\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAQlB,SAAS,OACd,cAC0B;AAC1B,QAAM,CAAC,KAAK,IAAI,SAAS,OAAO;AAAA,IAC9B,SACE,iBAAiB,UAAa,OAAO,iBAAiB,aACjD,aAAyB,IAC1B;AAAA,EACR,EAAE;AACF,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-resource.ts"],"sourcesContent":["import { ResourceElement } from \"../core/types\";\nimport { tapEffect } from \"./tap-effect\";\nimport {\n createResourceFiber,\n unmountResource,\n renderResource,\n commitResource,\n} from \"../core/ResourceFiber\";\nimport { tapMemo } from \"./tap-memo\";\nimport { tapState } from \"./tap-state\";\n\nexport function tapResource<R, P>(element: ResourceElement<R, P>): R;\nexport function tapResource<R, P>(\n element: ResourceElement<R, P>,\n deps: readonly unknown[]
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-resource.ts"],"sourcesContent":["import { ResourceElement } from \"../core/types\";\nimport { tapEffect } from \"./tap-effect\";\nimport {\n createResourceFiber,\n unmountResource,\n renderResource,\n commitResource,\n} from \"../core/ResourceFiber\";\nimport { tapMemo } from \"./tap-memo\";\nimport { tapState } from \"./tap-state\";\n\nexport function tapResource<R, P>(element: ResourceElement<R, P>): R;\nexport function tapResource<R, P>(\n element: ResourceElement<R, P>,\n deps: readonly unknown[],\n): R;\nexport function tapResource<R, P>(\n element: ResourceElement<R, P>,\n deps?: readonly unknown[],\n): R {\n const [stateVersion, rerender] = tapState({});\n const fiber = tapMemo(\n () => createResourceFiber(element.type, () => rerender({})),\n [element.type],\n );\n\n const props = deps ? tapMemo(() => element.props, deps) : element.props;\n const result = tapMemo(\n () => renderResource(fiber, props),\n [fiber, props, stateVersion],\n );\n\n tapEffect(() => {\n return () => unmountResource(fiber);\n }, [fiber]);\n\n tapEffect(() => {\n commitResource(fiber, result);\n }, [fiber, result]);\n\n return result.state;\n}\n"],"mappings":";AACA,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,eAAe;AACxB,SAAS,gBAAgB;AAOlB,SAAS,YACd,SACA,MACG;AACH,QAAM,CAAC,cAAc,QAAQ,IAAI,SAAS,CAAC,CAAC;AAC5C,QAAM,QAAQ;AAAA,IACZ,MAAM,oBAAoB,QAAQ,MAAM,MAAM,SAAS,CAAC,CAAC,CAAC;AAAA,IAC1D,CAAC,QAAQ,IAAI;AAAA,EACf;AAEA,QAAM,QAAQ,OAAO,QAAQ,MAAM,QAAQ,OAAO,IAAI,IAAI,QAAQ;AAClE,QAAM,SAAS;AAAA,IACb,MAAM,eAAe,OAAO,KAAK;AAAA,IACjC,CAAC,OAAO,OAAO,YAAY;AAAA,EAC7B;AAEA,YAAU,MAAM;AACd,WAAO,MAAM,gBAAgB,KAAK;AAAA,EACpC,GAAG,CAAC,KAAK,CAAC;AAEV,YAAU,MAAM;AACd,mBAAe,OAAO,MAAM;AAAA,EAC9B,GAAG,CAAC,OAAO,MAAM,CAAC;AAElB,SAAO,OAAO;AAChB;","names":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ResourceElement } from "../core/types";
|
|
2
2
|
export declare function tapResources<T extends ReadonlyArray<ResourceElement<any, any>>>(elements: T): {
|
|
3
|
-
[K in keyof T]: T[K] extends ResourceElement<
|
|
3
|
+
[K in keyof T]: T[K] extends ResourceElement<infer R, any> ? R : never;
|
|
4
4
|
};
|
|
5
5
|
//# sourceMappingURL=tap-resources.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-resources.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-resources.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"tap-resources.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-resources.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAiB,MAAM,eAAe,CAAC;AAW/D,wBAAgB,YAAY,CAC1B,CAAC,SAAS,aAAa,CAAC,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAElD,QAAQ,EAAE,CAAC,GACV;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,eAAe,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK;CAAE,CAyF5E"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// src/hooks/tap-resources.ts
|
|
2
2
|
import { tapEffect } from "./tap-effect.js";
|
|
3
|
-
import {
|
|
3
|
+
import { tapMemo } from "./tap-memo.js";
|
|
4
|
+
import { tapState } from "./tap-state.js";
|
|
4
5
|
import {
|
|
5
6
|
createResourceFiber,
|
|
6
7
|
unmountResource,
|
|
7
8
|
renderResource,
|
|
8
9
|
commitResource
|
|
9
10
|
} from "../core/ResourceFiber.js";
|
|
10
|
-
import { tapRerender } from "./tap-rerender.js";
|
|
11
11
|
function tapResources(elements) {
|
|
12
12
|
const seenKeys = /* @__PURE__ */ new Set();
|
|
13
13
|
elements.forEach((element, index) => {
|
|
@@ -23,60 +23,56 @@ function tapResources(elements) {
|
|
|
23
23
|
}
|
|
24
24
|
seenKeys.add(element.key);
|
|
25
25
|
});
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
currentKeys
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
const [stateVersion, rerender] = tapState({});
|
|
27
|
+
const elementsByKey = tapMemo(
|
|
28
|
+
() => new Map(elements.map((element) => [element.key, element])),
|
|
29
|
+
[elements]
|
|
30
|
+
);
|
|
31
|
+
const [fibers] = tapState(
|
|
32
|
+
() => /* @__PURE__ */ new Map()
|
|
33
|
+
);
|
|
34
|
+
const results = tapMemo(() => {
|
|
35
|
+
const resultMap = /* @__PURE__ */ new Map();
|
|
36
|
+
const currentKeys = /* @__PURE__ */ new Set();
|
|
37
|
+
elementsByKey.forEach((element, key) => {
|
|
38
|
+
currentKeys.add(key);
|
|
39
|
+
let fiber = fibers.get(key);
|
|
40
|
+
if (!fiber || fiber.resourceFn !== element.type) {
|
|
41
|
+
if (fiber) unmountResource(fiber);
|
|
42
|
+
fiber = createResourceFiber(element.type, () => rerender({}));
|
|
43
|
+
fibers.set(key, fiber);
|
|
41
44
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
fiber,
|
|
45
|
-
type: element.type,
|
|
46
|
-
props: element.props,
|
|
47
|
-
state: void 0
|
|
48
|
-
};
|
|
49
|
-
entries.set(key, entry);
|
|
50
|
-
}
|
|
51
|
-
entry.props = element.props;
|
|
52
|
-
const result = renderResource(entry.fiber, element.props);
|
|
53
|
-
entry.state = result.state;
|
|
54
|
-
renderContexts.push({ entry, result });
|
|
55
|
-
});
|
|
56
|
-
tapEffect(() => {
|
|
57
|
-
renderContexts.forEach(({ entry, result: ctx }) => {
|
|
58
|
-
commitResource(entry.fiber, ctx);
|
|
45
|
+
const result = renderResource(fiber, element.props);
|
|
46
|
+
resultMap.set(key, result);
|
|
59
47
|
});
|
|
60
|
-
|
|
61
|
-
tapEffect(() => {
|
|
62
|
-
const keysToRemove = [];
|
|
63
|
-
for (const [key, entry] of entries) {
|
|
48
|
+
fibers.forEach((fiber, key) => {
|
|
64
49
|
if (!currentKeys.has(key)) {
|
|
65
|
-
unmountResource(
|
|
66
|
-
|
|
50
|
+
unmountResource(fiber);
|
|
51
|
+
fibers.delete(key);
|
|
67
52
|
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
});
|
|
53
|
+
});
|
|
54
|
+
return resultMap;
|
|
55
|
+
}, [elementsByKey, stateVersion]);
|
|
56
|
+
tapEffect(() => {
|
|
57
|
+
results.forEach((result, key) => {
|
|
58
|
+
const fiber = fibers.get(key);
|
|
59
|
+
if (fiber) {
|
|
60
|
+
commitResource(fiber, result);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}, [results, fibers]);
|
|
71
64
|
tapEffect(() => {
|
|
72
65
|
return () => {
|
|
73
|
-
|
|
74
|
-
unmountResource(
|
|
66
|
+
fibers.forEach((fiber) => {
|
|
67
|
+
unmountResource(fiber);
|
|
75
68
|
});
|
|
76
|
-
|
|
69
|
+
fibers.clear();
|
|
77
70
|
};
|
|
78
|
-
}, []);
|
|
79
|
-
return
|
|
71
|
+
}, [fibers]);
|
|
72
|
+
return tapMemo(
|
|
73
|
+
() => elements.map((element) => results.get(element.key)?.state),
|
|
74
|
+
[elements, results]
|
|
75
|
+
);
|
|
80
76
|
}
|
|
81
77
|
export {
|
|
82
78
|
tapResources
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-resources.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-resources.ts"],"sourcesContent":["import { ResourceElement, ResourceFiber } from \"../core/types\";\nimport { tapEffect } from \"./tap-effect\";\nimport { tapMemo } from \"./tap-memo\";\nimport { tapState } from \"./tap-state\";\nimport {\n createResourceFiber,\n unmountResource,\n renderResource,\n commitResource,\n} from \"../core/ResourceFiber\";\n\nexport function tapResources<\n T extends ReadonlyArray<ResourceElement<any, any>>,\n>(\n elements: T,\n): { [K in keyof T]: T[K] extends ResourceElement<infer R, any> ? R : never } {\n // Validate keys\n const seenKeys = new Set<string | number>();\n elements.forEach((element, index) => {\n if (element.key === undefined) {\n throw new Error(\n `tapResources: All resource elements must have a key. Element at index ${index} is missing a key.`,\n );\n }\n if (seenKeys.has(element.key)) {\n throw new Error(\n `tapResources: Duplicate key \"${element.key}\" found. All keys must be unique.`,\n );\n }\n seenKeys.add(element.key);\n });\n\n const [stateVersion, rerender] = tapState({});\n\n // Create a map of current elements by key for efficient lookup\n const elementsByKey = tapMemo(\n () => new Map(elements.map((element) => [element.key!, element])),\n [elements],\n );\n\n // Track fibers persistently across renders\n const [fibers] = tapState(\n () => new Map<string | number, ResourceFiber<any, any>>(),\n );\n\n // Process each element\n const results = tapMemo(() => {\n const resultMap = new Map<string | number, any>();\n const currentKeys = new Set<string | number>();\n\n // Create/update fibers and render\n elementsByKey.forEach((element, key) => {\n currentKeys.add(key);\n\n let fiber = fibers.get(key);\n\n // Create new fiber if needed or type changed\n if (!fiber || fiber.resourceFn !== element.type) {\n if (fiber) unmountResource(fiber);\n fiber = createResourceFiber(element.type, () => rerender({}));\n fibers.set(key, fiber);\n }\n\n // Render with current props\n const result = renderResource(fiber, element.props);\n resultMap.set(key, result);\n });\n\n // Clean up removed fibers\n fibers.forEach((fiber, key) => {\n if (!currentKeys.has(key)) {\n unmountResource(fiber);\n fibers.delete(key);\n }\n });\n\n return resultMap;\n }, [elementsByKey, stateVersion]);\n\n // Commit all renders\n tapEffect(() => {\n results.forEach((result, key) => {\n const fiber = fibers.get(key);\n if (fiber) {\n commitResource(fiber, result);\n }\n });\n }, [results, fibers]);\n\n // Cleanup on unmount\n tapEffect(() => {\n return () => {\n fibers.forEach((fiber) => {\n unmountResource(fiber);\n });\n fibers.clear();\n };\n }, [fibers]);\n\n // Return results in the same order as input elements\n return tapMemo(\n () => elements.map((element) => results.get(element.key!)?.state),\n [elements, results],\n ) as any;\n}\n"],"mappings":";AACA,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEA,SAAS,aAGd,UAC4E;AAE5E,QAAM,WAAW,oBAAI,IAAqB;AAC1C,WAAS,QAAQ,CAAC,SAAS,UAAU;AACnC,QAAI,QAAQ,QAAQ,QAAW;AAC7B,YAAM,IAAI;AAAA,QACR,yEAAyE,KAAK;AAAA,MAChF;AAAA,IACF;AACA,QAAI,SAAS,IAAI,QAAQ,GAAG,GAAG;AAC7B,YAAM,IAAI;AAAA,QACR,gCAAgC,QAAQ,GAAG;AAAA,MAC7C;AAAA,IACF;AACA,aAAS,IAAI,QAAQ,GAAG;AAAA,EAC1B,CAAC;AAED,QAAM,CAAC,cAAc,QAAQ,IAAI,SAAS,CAAC,CAAC;AAG5C,QAAM,gBAAgB;AAAA,IACpB,MAAM,IAAI,IAAI,SAAS,IAAI,CAAC,YAAY,CAAC,QAAQ,KAAM,OAAO,CAAC,CAAC;AAAA,IAChE,CAAC,QAAQ;AAAA,EACX;AAGA,QAAM,CAAC,MAAM,IAAI;AAAA,IACf,MAAM,oBAAI,IAA8C;AAAA,EAC1D;AAGA,QAAM,UAAU,QAAQ,MAAM;AAC5B,UAAM,YAAY,oBAAI,IAA0B;AAChD,UAAM,cAAc,oBAAI,IAAqB;AAG7C,kBAAc,QAAQ,CAAC,SAAS,QAAQ;AACtC,kBAAY,IAAI,GAAG;AAEnB,UAAI,QAAQ,OAAO,IAAI,GAAG;AAG1B,UAAI,CAAC,SAAS,MAAM,eAAe,QAAQ,MAAM;AAC/C,YAAI,MAAO,iBAAgB,KAAK;AAChC,gBAAQ,oBAAoB,QAAQ,MAAM,MAAM,SAAS,CAAC,CAAC,CAAC;AAC5D,eAAO,IAAI,KAAK,KAAK;AAAA,MACvB;AAGA,YAAM,SAAS,eAAe,OAAO,QAAQ,KAAK;AAClD,gBAAU,IAAI,KAAK,MAAM;AAAA,IAC3B,CAAC;AAGD,WAAO,QAAQ,CAAC,OAAO,QAAQ;AAC7B,UAAI,CAAC,YAAY,IAAI,GAAG,GAAG;AACzB,wBAAgB,KAAK;AACrB,eAAO,OAAO,GAAG;AAAA,MACnB;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT,GAAG,CAAC,eAAe,YAAY,CAAC;AAGhC,YAAU,MAAM;AACd,YAAQ,QAAQ,CAAC,QAAQ,QAAQ;AAC/B,YAAM,QAAQ,OAAO,IAAI,GAAG;AAC5B,UAAI,OAAO;AACT,uBAAe,OAAO,MAAM;AAAA,MAC9B;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,SAAS,MAAM,CAAC;AAGpB,YAAU,MAAM;AACd,WAAO,MAAM;AACX,aAAO,QAAQ,CAAC,UAAU;AACxB,wBAAgB,KAAK;AAAA,MACvB,CAAC;AACD,aAAO,MAAM;AAAA,IACf;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AAAA,IACL,MAAM,SAAS,IAAI,CAAC,YAAY,QAAQ,IAAI,QAAQ,GAAI,GAAG,KAAK;AAAA,IAChE,CAAC,UAAU,OAAO;AAAA,EACpB;AACF;","names":[]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { StateUpdater } from "../core/types";
|
|
1
|
+
import { StateUpdater, ResourceFiber } from "../core/types";
|
|
2
|
+
export declare const rerender: (fiber: ResourceFiber<any, any>) => void;
|
|
2
3
|
export declare function tapState<S = undefined>(): [
|
|
3
4
|
S | undefined,
|
|
4
5
|
(updater: StateUpdater<S>) => void
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tap-state.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAQ,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"tap-state.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAQ,aAAa,EAAE,MAAM,eAAe,CAAC;AAElE,eAAO,MAAM,QAAQ,GAAI,OAAO,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,SAWtD,CAAC;AAmDF,wBAAgB,QAAQ,CAAC,CAAC,GAAG,SAAS,KAAK;IACzC,CAAC,GAAG,SAAS;IACb,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI;CACnC,CAAC;AACF,wBAAgB,QAAQ,CAAC,CAAC,EACxB,OAAO,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GACrB,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC"}
|
package/dist/hooks/tap-state.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
// src/hooks/tap-state.ts
|
|
2
2
|
import { getCurrentResourceFiber } from "../core/execution-context.js";
|
|
3
|
+
var rerender = (fiber) => {
|
|
4
|
+
if (fiber.renderContext) {
|
|
5
|
+
throw new Error("Resource updated during render");
|
|
6
|
+
}
|
|
7
|
+
if (fiber.isNeverMounted) {
|
|
8
|
+
throw new Error("Resource updated before mount");
|
|
9
|
+
}
|
|
10
|
+
if (fiber.isMounted) {
|
|
11
|
+
fiber.scheduleRerender();
|
|
12
|
+
}
|
|
13
|
+
};
|
|
3
14
|
function getStateCell(initialValue) {
|
|
4
|
-
const
|
|
5
|
-
const index =
|
|
6
|
-
if (!
|
|
15
|
+
const fiber = getCurrentResourceFiber();
|
|
16
|
+
const index = fiber.currentIndex++;
|
|
17
|
+
if (!fiber.isFirstRender && index >= fiber.cells.length) {
|
|
7
18
|
throw new Error(
|
|
8
19
|
"Rendered more hooks than during the previous render. Hooks must be called in the exact same order in every render."
|
|
9
20
|
);
|
|
10
21
|
}
|
|
11
|
-
if (!
|
|
22
|
+
if (!fiber.cells[index]) {
|
|
12
23
|
const value = typeof initialValue === "function" ? initialValue() : initialValue;
|
|
13
24
|
const cell2 = {
|
|
14
25
|
type: "state",
|
|
@@ -18,16 +29,13 @@ function getStateCell(initialValue) {
|
|
|
18
29
|
const nextValue = typeof updater === "function" ? updater(currentValue) : updater;
|
|
19
30
|
if (!Object.is(currentValue, nextValue)) {
|
|
20
31
|
cell2.value = nextValue;
|
|
21
|
-
|
|
22
|
-
throw new Error("Resource updated during render");
|
|
23
|
-
}
|
|
24
|
-
executionContext.scheduleRerender();
|
|
32
|
+
rerender(fiber);
|
|
25
33
|
}
|
|
26
34
|
}
|
|
27
35
|
};
|
|
28
|
-
|
|
36
|
+
fiber.cells[index] = cell2;
|
|
29
37
|
}
|
|
30
|
-
const cell =
|
|
38
|
+
const cell = fiber.cells[index];
|
|
31
39
|
if (cell.type !== "state") {
|
|
32
40
|
throw new Error("Hook order changed between renders");
|
|
33
41
|
}
|
|
@@ -38,6 +46,7 @@ function tapState(initial) {
|
|
|
38
46
|
return [cell.value, cell.set];
|
|
39
47
|
}
|
|
40
48
|
export {
|
|
49
|
+
rerender,
|
|
41
50
|
tapState
|
|
42
51
|
};
|
|
43
52
|
//# sourceMappingURL=tap-state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-state.ts"],"sourcesContent":["import { getCurrentResourceFiber } from \"../core/execution-context\";\nimport { StateUpdater, Cell } from \"../core/types\";\n\nfunction getStateCell<T>(\n initialValue: T | (() => T)
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/tap-state.ts"],"sourcesContent":["import { getCurrentResourceFiber } from \"../core/execution-context\";\nimport { StateUpdater, Cell, ResourceFiber } from \"../core/types\";\n\nexport const rerender = (fiber: ResourceFiber<any, any>) => {\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 // Only schedule rerender if currently mounted\n if (fiber.isMounted) {\n fiber.scheduleRerender();\n }\n};\n\nfunction getStateCell<T>(\n initialValue: T | (() => T),\n): Cell & { type: \"state\" } {\n const fiber = getCurrentResourceFiber();\n const index = fiber.currentIndex++;\n\n // Check if we're trying to use more hooks than in previous renders\n if (!fiber.isFirstRender && index >= fiber.cells.length) {\n throw new Error(\n \"Rendered more hooks than during the previous render. \" +\n \"Hooks must be called in the exact same order in every render.\",\n );\n }\n\n if (!fiber.cells[index]) {\n // Initialize the value immediately\n const value =\n typeof initialValue === \"function\"\n ? (initialValue as () => T)()\n : initialValue;\n\n const cell: Cell & { type: \"state\" } = {\n type: \"state\",\n value,\n set: (updater: StateUpdater<T>) => {\n const currentValue = cell.value;\n const nextValue =\n typeof updater === \"function\"\n ? (updater as (prev: T) => T)(currentValue)\n : updater;\n\n if (!Object.is(currentValue, nextValue)) {\n cell.value = nextValue;\n rerender(fiber);\n }\n },\n };\n\n fiber.cells[index] = cell;\n }\n\n const cell = fiber.cells[index];\n if (cell.type !== \"state\") {\n throw new Error(\"Hook order changed between renders\");\n }\n\n return cell as Cell & { type: \"state\" };\n}\n\nexport function tapState<S = undefined>(): [\n S | undefined,\n (updater: StateUpdater<S>) => void,\n];\nexport function tapState<S>(\n initial: S | (() => S),\n): [S, (updater: StateUpdater<S>) => void];\nexport function tapState<S>(\n initial?: S | (() => S),\n): [S | undefined, (updater: StateUpdater<S>) => void] {\n const cell = getStateCell(initial as S | (() => S));\n\n return [cell.value, cell.set];\n}\n"],"mappings":";AAAA,SAAS,+BAA+B;AAGjC,IAAM,WAAW,CAAC,UAAmC;AAC1D,MAAI,MAAM,eAAe;AACvB,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACA,MAAI,MAAM,gBAAgB;AACxB,UAAM,IAAI,MAAM,+BAA+B;AAAA,EACjD;AAEA,MAAI,MAAM,WAAW;AACnB,UAAM,iBAAiB;AAAA,EACzB;AACF;AAEA,SAAS,aACP,cAC0B;AAC1B,QAAM,QAAQ,wBAAwB;AACtC,QAAM,QAAQ,MAAM;AAGpB,MAAI,CAAC,MAAM,iBAAiB,SAAS,MAAM,MAAM,QAAQ;AACvD,UAAM,IAAI;AAAA,MACR;AAAA,IAEF;AAAA,EACF;AAEA,MAAI,CAAC,MAAM,MAAM,KAAK,GAAG;AAEvB,UAAM,QACJ,OAAO,iBAAiB,aACnB,aAAyB,IAC1B;AAEN,UAAMA,QAAiC;AAAA,MACrC,MAAM;AAAA,MACN;AAAA,MACA,KAAK,CAAC,YAA6B;AACjC,cAAM,eAAeA,MAAK;AAC1B,cAAM,YACJ,OAAO,YAAY,aACd,QAA2B,YAAY,IACxC;AAEN,YAAI,CAAC,OAAO,GAAG,cAAc,SAAS,GAAG;AACvC,UAAAA,MAAK,QAAQ;AACb,mBAAS,KAAK;AAAA,QAChB;AAAA,MACF;AAAA,IACF;AAEA,UAAM,MAAM,KAAK,IAAIA;AAAA,EACvB;AAEA,QAAM,OAAO,MAAM,MAAM,KAAK;AAC9B,MAAI,KAAK,SAAS,SAAS;AACzB,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AACT;AASO,SAAS,SACd,SACqD;AACrD,QAAM,OAAO,aAAa,OAAwB;AAElD,SAAO,CAAC,KAAK,OAAO,KAAK,GAAG;AAC9B;","names":["cell"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { resource } from "./core/resource";
|
|
2
2
|
export { tapState } from "./hooks/tap-state";
|
|
3
3
|
export { tapEffect } from "./hooks/tap-effect";
|
|
4
|
-
export { tapRef } from "./hooks/tap-ref";
|
|
4
|
+
export { tapRef, type RefObject } from "./hooks/tap-ref";
|
|
5
5
|
export { tapMemo } from "./hooks/tap-memo";
|
|
6
6
|
export { tapCallback } from "./hooks/tap-callback";
|
|
7
7
|
export { tapResource } from "./hooks/tap-resource";
|
|
8
8
|
export { tapInlineResource } from "./hooks/tap-inline-resource";
|
|
9
|
+
export { tapResources } from "./hooks/tap-resources";
|
|
9
10
|
export { createResource, type ResourceHandle } from "./core/ResourceHandle";
|
|
10
11
|
export type { ResourceFn, ResourceElement, ResourceElementConstructor, Unsubscribe, StateUpdater, EffectCallback, Destructor, } from "./core/types";
|
|
11
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAG/C,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAG/C,OAAO,EAAE,MAAM,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGnD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5E,YAAY,EACV,UAAU,EACV,eAAe,EACf,0BAA0B,EAC1B,WAAW,EACX,YAAY,EACZ,cAAc,EACd,UAAU,GACX,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import { tapMemo } from "./hooks/tap-memo.js";
|
|
|
7
7
|
import { tapCallback } from "./hooks/tap-callback.js";
|
|
8
8
|
import { tapResource } from "./hooks/tap-resource.js";
|
|
9
9
|
import { tapInlineResource } from "./hooks/tap-inline-resource.js";
|
|
10
|
+
import { tapResources } from "./hooks/tap-resources.js";
|
|
10
11
|
import { createResource } from "./core/ResourceHandle.js";
|
|
11
12
|
export {
|
|
12
13
|
createResource,
|
|
@@ -17,6 +18,7 @@ export {
|
|
|
17
18
|
tapMemo,
|
|
18
19
|
tapRef,
|
|
19
20
|
tapResource,
|
|
21
|
+
tapResources,
|
|
20
22
|
tapState
|
|
21
23
|
};
|
|
22
24
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { resource } from \"./core/resource\";\n\n// primitive hooks\nexport { tapState } from \"./hooks/tap-state\";\nexport { tapEffect } from \"./hooks/tap-effect\";\n\n// utility hooks\nexport { tapRef } from \"./hooks/tap-ref\";\nexport { tapMemo } from \"./hooks/tap-memo\";\nexport { tapCallback } from \"./hooks/tap-callback\";\n\n// resources\nexport { tapResource } from \"./hooks/tap-resource\";\nexport { tapInlineResource } from \"./hooks/tap-inline-resource\";\
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { resource } from \"./core/resource\";\n\n// primitive hooks\nexport { tapState } from \"./hooks/tap-state\";\nexport { tapEffect } from \"./hooks/tap-effect\";\n\n// utility hooks\nexport { tapRef, type RefObject } from \"./hooks/tap-ref\";\nexport { tapMemo } from \"./hooks/tap-memo\";\nexport { tapCallback } from \"./hooks/tap-callback\";\n\n// resources\nexport { tapResource } from \"./hooks/tap-resource\";\nexport { tapInlineResource } from \"./hooks/tap-inline-resource\";\nexport { tapResources } from \"./hooks/tap-resources\";\n\n// imperative\nexport { createResource, type ResourceHandle } from \"./core/ResourceHandle\";\n\nexport type {\n ResourceFn,\n ResourceElement,\n ResourceElementConstructor,\n Unsubscribe,\n StateUpdater,\n EffectCallback,\n Destructor,\n} from \"./core/types\";\n"],"mappings":";AAAA,SAAS,gBAAgB;AAGzB,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAG1B,SAAS,cAA8B;AACvC,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAG5B,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAG7B,SAAS,sBAA2C;","names":[]}
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react/index.ts"],"sourcesContent":["export { useResource } from \"./use-resource\"
|
|
1
|
+
{"version":3,"sources":["../../src/react/index.ts"],"sourcesContent":["export { useResource } from \"./use-resource\";\n"],"mappings":";AAAA,SAAS,mBAAmB;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react/use-resource.ts"],"sourcesContent":["import { useEffect, useLayoutEffect, useMemo, useState } from \"react\";\nimport { ResourceElement } from \"../core/types\";\nimport {\n createResourceFiber,\n unmountResource,\n renderResource,\n commitResource,\n} from \"../core/ResourceFiber\";\n\nconst shouldAvoidLayoutEffect =\n (globalThis as any).__ASSISTANT_UI_DISABLE_LAYOUT_EFFECT__ === true;\n\nconst useIsomorphicLayoutEffect = shouldAvoidLayoutEffect\n ? useEffect\n : useLayoutEffect;\n\nexport function useResource<R, P>(element: ResourceElement<R, P>): R {\n const [, rerender] = useState({});\n const fiber = useMemo(\n () => createResourceFiber(element.type, () => rerender({})),\n [element.type]
|
|
1
|
+
{"version":3,"sources":["../../src/react/use-resource.ts"],"sourcesContent":["import { useEffect, useLayoutEffect, useMemo, useState } from \"react\";\nimport { ResourceElement } from \"../core/types\";\nimport {\n createResourceFiber,\n unmountResource,\n renderResource,\n commitResource,\n} from \"../core/ResourceFiber\";\n\nconst shouldAvoidLayoutEffect =\n (globalThis as any).__ASSISTANT_UI_DISABLE_LAYOUT_EFFECT__ === true;\n\nconst useIsomorphicLayoutEffect = shouldAvoidLayoutEffect\n ? useEffect\n : useLayoutEffect;\n\nexport function useResource<R, P>(element: ResourceElement<R, P>): R {\n const [, rerender] = useState({});\n const fiber = useMemo(\n () => createResourceFiber(element.type, () => rerender({})),\n [element.type],\n );\n\n const result = renderResource(fiber, element.props);\n useIsomorphicLayoutEffect(() => {\n return () => unmountResource(fiber);\n }, []);\n useIsomorphicLayoutEffect(() => {\n commitResource(fiber, result);\n });\n\n return result.state;\n}\n"],"mappings":";AAAA,SAAS,WAAW,iBAAiB,SAAS,gBAAgB;AAE9D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,IAAM,0BACH,WAAmB,2CAA2C;AAEjE,IAAM,4BAA4B,0BAC9B,YACA;AAEG,SAAS,YAAkB,SAAmC;AACnE,QAAM,CAAC,EAAE,QAAQ,IAAI,SAAS,CAAC,CAAC;AAChC,QAAM,QAAQ;AAAA,IACZ,MAAM,oBAAoB,QAAQ,MAAM,MAAM,SAAS,CAAC,CAAC,CAAC;AAAA,IAC1D,CAAC,QAAQ,IAAI;AAAA,EACf;AAEA,QAAM,SAAS,eAAe,OAAO,QAAQ,KAAK;AAClD,4BAA0B,MAAM;AAC9B,WAAO,MAAM,gBAAgB,KAAK;AAAA,EACpC,GAAG,CAAC,CAAC;AACL,4BAA0B,MAAM;AAC9B,mBAAe,OAAO,MAAM;AAAA,EAC9B,CAAC;AAED,SAAO,OAAO;AAChB;","names":[]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tap-rerender.d.ts","sourceRoot":"","sources":["../../src/hooks/tap-rerender.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW,kBAGvB,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// src/hooks/tap-rerender.ts
|
|
2
|
-
import { getCurrentResourceFiber } from "../core/execution-context.js";
|
|
3
|
-
var tapRerender = () => {
|
|
4
|
-
const executionContext = getCurrentResourceFiber();
|
|
5
|
-
return executionContext.scheduleRerender;
|
|
6
|
-
};
|
|
7
|
-
export {
|
|
8
|
-
tapRerender
|
|
9
|
-
};
|
|
10
|
-
//# sourceMappingURL=tap-rerender.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/tap-rerender.ts"],"sourcesContent":["import { getCurrentResourceFiber } from \"../core/execution-context\";\n\nexport const tapRerender = () => {\n const executionContext = getCurrentResourceFiber();\n return executionContext.scheduleRerender;\n};\n"],"mappings":";AAAA,SAAS,+BAA+B;AAEjC,IAAM,cAAc,MAAM;AAC/B,QAAM,mBAAmB,wBAAwB;AACjD,SAAO,iBAAiB;AAC1B;","names":[]}
|