@assistant-ui/store 0.2.10 → 0.2.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 +36 -65
- package/dist/AuiIf.d.ts +43 -8
- package/dist/AuiIf.d.ts.map +1 -1
- package/dist/AuiIf.js +28 -3
- package/dist/AuiIf.js.map +1 -1
- package/dist/Derived.d.ts +15 -13
- package/dist/Derived.d.ts.map +1 -1
- package/dist/Derived.js +24 -20
- package/dist/Derived.js.map +1 -1
- package/dist/RenderChildrenWithAccessor.d.ts +13 -6
- package/dist/RenderChildrenWithAccessor.d.ts.map +1 -1
- package/dist/RenderChildrenWithAccessor.js +42 -48
- package/dist/RenderChildrenWithAccessor.js.map +1 -1
- package/dist/attachTransformScopes.d.ts +11 -10
- package/dist/attachTransformScopes.d.ts.map +1 -1
- package/dist/attachTransformScopes.js +20 -23
- package/dist/attachTransformScopes.js.map +1 -1
- package/dist/index.d.ts +15 -16
- package/dist/index.js +14 -18
- package/dist/tapClientList.d.ts +26 -23
- package/dist/tapClientList.d.ts.map +1 -1
- package/dist/tapClientList.js +66 -63
- package/dist/tapClientList.js.map +1 -1
- package/dist/tapClientLookup.d.ts +14 -11
- package/dist/tapClientLookup.d.ts.map +1 -1
- package/dist/tapClientLookup.js +31 -37
- package/dist/tapClientLookup.js.map +1 -1
- package/dist/tapClientResource.d.ts +12 -9
- package/dist/tapClientResource.d.ts.map +1 -1
- package/dist/tapClientResource.js +92 -104
- package/dist/tapClientResource.js.map +1 -1
- package/dist/types/client.d.ts +50 -42
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/client.js +0 -2
- package/dist/types/events.d.ts +23 -24
- package/dist/types/events.d.ts.map +1 -1
- package/dist/types/events.js +13 -6
- package/dist/types/events.js.map +1 -1
- package/dist/useAui.d.ts +76 -15
- package/dist/useAui.d.ts.map +1 -1
- package/dist/useAui.js +178 -175
- package/dist/useAui.js.map +1 -1
- package/dist/useAuiEvent.d.ts +47 -2
- package/dist/useAuiEvent.d.ts.map +1 -1
- package/dist/useAuiEvent.js +60 -7
- package/dist/useAuiEvent.js.map +1 -1
- package/dist/useAuiState.d.ts +31 -10
- package/dist/useAuiState.d.ts.map +1 -1
- package/dist/useAuiState.js +44 -25
- package/dist/useAuiState.js.map +1 -1
- package/dist/utils/BaseProxyHandler.d.ts +19 -16
- package/dist/utils/BaseProxyHandler.d.ts.map +1 -1
- package/dist/utils/BaseProxyHandler.js +46 -43
- package/dist/utils/BaseProxyHandler.js.map +1 -1
- package/dist/utils/NotificationManager.d.ts +13 -9
- package/dist/utils/NotificationManager.d.ts.map +1 -1
- package/dist/utils/NotificationManager.js +69 -82
- package/dist/utils/NotificationManager.js.map +1 -1
- package/dist/utils/proxied-assistant-state.d.ts +8 -4
- package/dist/utils/proxied-assistant-state.d.ts.map +1 -1
- package/dist/utils/proxied-assistant-state.js +28 -26
- package/dist/utils/proxied-assistant-state.js.map +1 -1
- package/dist/utils/react-assistant-context.d.ts +31 -13
- package/dist/utils/react-assistant-context.d.ts.map +1 -1
- package/dist/utils/react-assistant-context.js +69 -56
- package/dist/utils/react-assistant-context.js.map +1 -1
- package/dist/utils/splitClients.d.ts +12 -8
- package/dist/utils/splitClients.d.ts.map +1 -1
- package/dist/utils/splitClients.js +39 -43
- package/dist/utils/splitClients.js.map +1 -1
- package/dist/utils/tap-assistant-context.d.ts +17 -14
- package/dist/utils/tap-assistant-context.d.ts.map +1 -1
- package/dist/utils/tap-assistant-context.js +18 -15
- package/dist/utils/tap-assistant-context.js.map +1 -1
- package/dist/utils/tap-client-stack-context.d.ts +10 -6
- package/dist/utils/tap-client-stack-context.d.ts.map +1 -1
- package/dist/utils/tap-client-stack-context.js +22 -19
- package/dist/utils/tap-client-stack-context.js.map +1 -1
- package/dist/wrapperResource.d.ts +6 -2
- package/dist/wrapperResource.d.ts.map +1 -1
- package/dist/wrapperResource.js +12 -9
- package/dist/wrapperResource.js.map +1 -1
- package/package.json +7 -7
- package/src/AuiIf.ts +35 -1
- package/src/Derived.ts +1 -1
- package/src/types/client.ts +1 -2
- package/src/useAui.ts +101 -32
- package/src/useAuiEvent.ts +41 -0
- package/src/useAuiState.ts +25 -8
- package/src/utils/react-assistant-context.tsx +18 -5
- package/src/utils/splitClients.ts +4 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/types/client.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export { tapClientResource
|
|
15
|
-
export { tapClientLookup } from "./tapClientLookup.js";
|
|
16
|
-
export { tapClientList } from "./tapClientList.js";
|
|
17
|
-
export { normalizeEventSelector, } from "./types/events.js";
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { tapClientResource } from "./tapClientResource.js";
|
|
2
|
+
import { AuiProvider } from "./utils/react-assistant-context.js";
|
|
3
|
+
import { Derived } from "./Derived.js";
|
|
4
|
+
import { attachTransformScopes, forwardTransformScopes } from "./attachTransformScopes.js";
|
|
5
|
+
import { normalizeEventSelector } from "./types/events.js";
|
|
6
|
+
import { tapAssistantClientRef, tapAssistantEmit } from "./utils/tap-assistant-context.js";
|
|
7
|
+
import { useAui } from "./useAui.js";
|
|
8
|
+
import { useAuiState } from "./useAuiState.js";
|
|
9
|
+
import { AuiIf } from "./AuiIf.js";
|
|
10
|
+
import { RenderChildrenWithAccessor } from "./RenderChildrenWithAccessor.js";
|
|
11
|
+
import { useAuiEvent } from "./useAuiEvent.js";
|
|
12
|
+
import { tapClientLookup } from "./tapClientLookup.js";
|
|
13
|
+
import { tapClientList } from "./tapClientList.js";
|
|
14
|
+
export { AuiIf, AuiProvider, Derived, RenderChildrenWithAccessor, attachTransformScopes, forwardTransformScopes, normalizeEventSelector, tapAssistantClientRef, tapAssistantEmit, tapClientList, tapClientLookup, tapClientResource, useAui, useAuiEvent, useAuiState };
|
package/dist/tapClientList.d.ts
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { ClientMethods } from "./types/client.js";
|
|
2
|
+
import { ContravariantResource } from "@assistant-ui/tap";
|
|
3
|
+
|
|
4
|
+
//#region src/tapClientList.d.ts
|
|
3
5
|
type InferClientState<TMethods> = TMethods extends {
|
|
4
|
-
|
|
6
|
+
getState: () => infer S;
|
|
5
7
|
} ? S : unknown;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
declare const tapClientList: <TData, TMethods extends ClientMethods>(props: tapClientList.Props<TData, TMethods>) => {
|
|
9
|
+
state: InferClientState<TMethods>[];
|
|
10
|
+
get: (lookup: {
|
|
11
|
+
index: number;
|
|
12
|
+
} | {
|
|
13
|
+
key: string;
|
|
14
|
+
}) => TMethods;
|
|
15
|
+
add: (initialData: TData) => void;
|
|
14
16
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
declare namespace tapClientList {
|
|
18
|
+
type ResourceProps<TData> = {
|
|
19
|
+
key: string;
|
|
20
|
+
getInitialData: () => TData;
|
|
21
|
+
remove: () => void;
|
|
22
|
+
};
|
|
23
|
+
type Props<TData, TMethods extends ClientMethods> = {
|
|
24
|
+
initialValues: TData[];
|
|
25
|
+
getKey: (data: TData) => string;
|
|
26
|
+
resource: ContravariantResource<TMethods, ResourceProps<TData>>;
|
|
27
|
+
};
|
|
26
28
|
}
|
|
27
|
-
|
|
29
|
+
//#endregion
|
|
30
|
+
export { tapClientList };
|
|
28
31
|
//# sourceMappingURL=tapClientList.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tapClientList.d.ts","
|
|
1
|
+
{"version":3,"file":"tapClientList.d.ts","names":[],"sources":["../src/tapClientList.ts"],"mappings":";;;;KAKK,gBAAA,aAA6B,QAAQ;EACxC,QAAA;AAAA,IAEE,CAAA;AAAA,cAwBS,aAAA,2BAAyC,aAAA,EACpD,KAAA,EAAO,aAAA,CAAc,KAAA,CAAM,KAAA,EAAO,QAAA;EAElC,KAAA,EAAO,gBAAA,CAAiB,QAAA;EACxB,GAAA,GAAM,MAAA;IAAU,KAAA;EAAA;IAAoB,GAAA;EAAA,MAAkB,QAAA;EACtD,GAAA,GAAM,WAAA,EAAa,KAAA;AAAA;AAAA,kBAuEJ,aAAA;EAAA,KACH,aAAA;IACV,GAAA;IACA,cAAA,QAAsB,KAAA;IACtB,MAAA;EAAA;EAAA,KAGU,KAAA,yBAA8B,aAAA;IACxC,aAAA,EAAe,KAAA;IACf,MAAA,GAAS,IAAA,EAAM,KAAA;IACf,QAAA,EAAU,qBAAA,CAAsB,QAAA,EAAU,aAAA,CAAc,KAAA;EAAA;AAAA"}
|
package/dist/tapClientList.js
CHANGED
|
@@ -1,68 +1,71 @@
|
|
|
1
|
-
import { tapConst, tapState, withKey } from "@assistant-ui/tap";
|
|
2
1
|
import { tapClientLookup } from "./tapClientLookup.js";
|
|
2
|
+
import { tapConst, tapState, withKey } from "@assistant-ui/tap";
|
|
3
|
+
//#region src/tapClientList.ts
|
|
3
4
|
const createProps = (key, data, remove) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
remove,
|
|
13
|
-
};
|
|
5
|
+
return {
|
|
6
|
+
key,
|
|
7
|
+
getInitialData: () => {
|
|
8
|
+
if (!data.hasData) throw new Error("getInitialData may only be called during initial render");
|
|
9
|
+
return data.data;
|
|
10
|
+
},
|
|
11
|
+
remove
|
|
12
|
+
};
|
|
14
13
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
14
|
+
const tapClientList = (props) => {
|
|
15
|
+
const { initialValues, getKey, resource: Resource } = props;
|
|
16
|
+
const initialDataHandles = tapConst(() => [], []);
|
|
17
|
+
const [items, setItems] = tapState(() => {
|
|
18
|
+
const entries = [];
|
|
19
|
+
for (const data of initialValues) {
|
|
20
|
+
const key = getKey(data);
|
|
21
|
+
const handle = {
|
|
22
|
+
data,
|
|
23
|
+
hasData: true
|
|
24
|
+
};
|
|
25
|
+
entries.push([key, createProps(key, handle, () => {
|
|
26
|
+
setItems((items) => {
|
|
27
|
+
const newItems = { ...items };
|
|
28
|
+
delete newItems[key];
|
|
29
|
+
return newItems;
|
|
30
|
+
});
|
|
31
|
+
})]);
|
|
32
|
+
initialDataHandles.push(handle);
|
|
33
|
+
}
|
|
34
|
+
return Object.fromEntries(entries);
|
|
35
|
+
});
|
|
36
|
+
const lookup = tapClientLookup(() => Object.values(items).map((props) => withKey(props.key, Resource(props))), [items, Resource]);
|
|
37
|
+
initialDataHandles.forEach((handle) => {
|
|
38
|
+
handle.data = void 0;
|
|
39
|
+
handle.hasData = false;
|
|
40
|
+
});
|
|
41
|
+
const add = (data) => {
|
|
42
|
+
const key = getKey(data);
|
|
43
|
+
setItems((items) => {
|
|
44
|
+
if (key in items) throw new Error(`Tried to add item with a key ${key} that already exists`);
|
|
45
|
+
const handle = {
|
|
46
|
+
data,
|
|
47
|
+
hasData: true
|
|
48
|
+
};
|
|
49
|
+
initialDataHandles.push(handle);
|
|
50
|
+
return {
|
|
51
|
+
...items,
|
|
52
|
+
[key]: createProps(key, handle, () => {
|
|
53
|
+
setItems((items) => {
|
|
54
|
+
const newItems = { ...items };
|
|
55
|
+
delete newItems[key];
|
|
56
|
+
return newItems;
|
|
57
|
+
});
|
|
58
|
+
})
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
state: lookup.state,
|
|
64
|
+
get: lookup.get,
|
|
65
|
+
add
|
|
66
|
+
};
|
|
67
67
|
};
|
|
68
|
+
//#endregion
|
|
69
|
+
export { tapClientList };
|
|
70
|
+
|
|
68
71
|
//# sourceMappingURL=tapClientList.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tapClientList.js","
|
|
1
|
+
{"version":3,"file":"tapClientList.js","names":[],"sources":["../src/tapClientList.ts"],"sourcesContent":["import { tapConst, tapState, withKey } from \"@assistant-ui/tap\";\nimport type { ContravariantResource } from \"@assistant-ui/tap\";\nimport { tapClientLookup } from \"./tapClientLookup\";\nimport type { ClientMethods } from \"./types/client\";\n\ntype InferClientState<TMethods> = TMethods extends {\n getState: () => infer S;\n}\n ? S\n : unknown;\n\ntype DataHandle<TData> = { data: TData | undefined; hasData: boolean };\n\nconst createProps = <TData>(\n key: string,\n data: DataHandle<TData>,\n remove: () => void,\n): tapClientList.ResourceProps<TData> => {\n return {\n key,\n getInitialData: () => {\n if (!data.hasData) {\n throw new Error(\n \"getInitialData may only be called during initial render\",\n );\n }\n return data.data!;\n },\n remove,\n };\n};\n\nexport const tapClientList = <TData, TMethods extends ClientMethods>(\n props: tapClientList.Props<TData, TMethods>,\n): {\n state: InferClientState<TMethods>[];\n get: (lookup: { index: number } | { key: string }) => TMethods;\n add: (initialData: TData) => void;\n} => {\n const { initialValues, getKey, resource: Resource } = props;\n\n type Props = tapClientList.ResourceProps<TData>;\n\n const initialDataHandles: DataHandle<TData>[] = tapConst(() => [], []);\n\n const [items, setItems] = tapState<Record<string, Props>>(() => {\n const entries: [string, Props][] = [];\n for (const data of initialValues) {\n const key = getKey(data);\n const handle = { data, hasData: true };\n entries.push([\n key,\n createProps(key, handle, () => {\n setItems((items) => {\n const newItems = { ...items };\n delete newItems[key];\n return newItems;\n });\n }),\n ]);\n initialDataHandles.push(handle);\n }\n return Object.fromEntries(entries);\n });\n\n const lookup = tapClientLookup<TMethods>(\n () =>\n Object.values(items).map((props) => withKey(props.key, Resource(props))),\n [items, Resource],\n );\n\n initialDataHandles.forEach((handle) => {\n handle.data = undefined;\n handle.hasData = false;\n });\n\n const add = (data: TData) => {\n const key = getKey(data);\n setItems((items) => {\n if (key in items) {\n throw new Error(\n `Tried to add item with a key ${key} that already exists`,\n );\n }\n\n const handle = { data, hasData: true };\n initialDataHandles.push(handle);\n\n return {\n ...items,\n [key]: createProps(key, handle, () => {\n setItems((items) => {\n const newItems = { ...items };\n delete newItems[key];\n return newItems;\n });\n }),\n };\n });\n };\n\n return {\n state: lookup.state,\n get: lookup.get,\n add,\n };\n};\n\nexport namespace tapClientList {\n export type ResourceProps<TData> = {\n key: string;\n getInitialData: () => TData;\n remove: () => void;\n };\n\n export type Props<TData, TMethods extends ClientMethods> = {\n initialValues: TData[];\n getKey: (data: TData) => string;\n resource: ContravariantResource<TMethods, ResourceProps<TData>>;\n };\n}\n"],"mappings":";;;AAaA,MAAM,eACJ,KACA,MACA,WACuC;CACvC,OAAO;EACL;EACA,sBAAsB;GACpB,IAAI,CAAC,KAAK,SACR,MAAM,IAAI,MACR,yDACF;GAEF,OAAO,KAAK;EACd;EACA;CACF;AACF;AAEA,MAAa,iBACX,UAKG;CACH,MAAM,EAAE,eAAe,QAAQ,UAAU,aAAa;CAItD,MAAM,qBAA0C,eAAe,CAAC,GAAG,CAAC,CAAC;CAErE,MAAM,CAAC,OAAO,YAAY,eAAsC;EAC9D,MAAM,UAA6B,CAAC;EACpC,KAAK,MAAM,QAAQ,eAAe;GAChC,MAAM,MAAM,OAAO,IAAI;GACvB,MAAM,SAAS;IAAE;IAAM,SAAS;GAAK;GACrC,QAAQ,KAAK,CACX,KACA,YAAY,KAAK,cAAc;IAC7B,UAAU,UAAU;KAClB,MAAM,WAAW,EAAE,GAAG,MAAM;KAC5B,OAAO,SAAS;KAChB,OAAO;IACT,CAAC;GACH,CAAC,CACH,CAAC;GACD,mBAAmB,KAAK,MAAM;EAChC;EACA,OAAO,OAAO,YAAY,OAAO;CACnC,CAAC;CAED,MAAM,SAAS,sBAEX,OAAO,OAAO,KAAK,EAAE,KAAK,UAAU,QAAQ,MAAM,KAAK,SAAS,KAAK,CAAC,CAAC,GACzE,CAAC,OAAO,QAAQ,CAClB;CAEA,mBAAmB,SAAS,WAAW;EACrC,OAAO,OAAO,KAAA;EACd,OAAO,UAAU;CACnB,CAAC;CAED,MAAM,OAAO,SAAgB;EAC3B,MAAM,MAAM,OAAO,IAAI;EACvB,UAAU,UAAU;GAClB,IAAI,OAAO,OACT,MAAM,IAAI,MACR,gCAAgC,IAAI,qBACtC;GAGF,MAAM,SAAS;IAAE;IAAM,SAAS;GAAK;GACrC,mBAAmB,KAAK,MAAM;GAE9B,OAAO;IACL,GAAG;KACF,MAAM,YAAY,KAAK,cAAc;KACpC,UAAU,UAAU;MAClB,MAAM,WAAW,EAAE,GAAG,MAAM;MAC5B,OAAO,SAAS;MAChB,OAAO;KACT,CAAC;IACH,CAAC;GACH;EACF,CAAC;CACH;CAEA,OAAO;EACL,OAAO,OAAO;EACd,KAAK,OAAO;EACZ;CACF;AACF"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { ClientMethods } from "./types/client.js";
|
|
2
|
+
import { ResourceElement } from "@assistant-ui/tap";
|
|
3
|
+
|
|
4
|
+
//#region src/tapClientLookup.d.ts
|
|
3
5
|
type InferClientState<TMethods> = TMethods extends {
|
|
4
|
-
|
|
6
|
+
getState: () => infer S;
|
|
5
7
|
} ? S : unknown;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
declare function tapClientLookup<TMethods extends ClientMethods>(getElements: () => readonly ResourceElement<TMethods>[], getElementsDeps: readonly unknown[]): {
|
|
9
|
+
state: InferClientState<TMethods>[];
|
|
10
|
+
get: (lookup: {
|
|
11
|
+
index: number;
|
|
12
|
+
} | {
|
|
13
|
+
key: string;
|
|
14
|
+
}) => TMethods;
|
|
13
15
|
};
|
|
14
|
-
|
|
16
|
+
//#endregion
|
|
17
|
+
export { tapClientLookup };
|
|
15
18
|
//# sourceMappingURL=tapClientLookup.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tapClientLookup.d.ts","
|
|
1
|
+
{"version":3,"file":"tapClientLookup.d.ts","names":[],"sources":["../src/tapClientLookup.ts"],"mappings":";;;;KAUK,gBAAA,aAA6B,QAAQ;EACxC,QAAA;AAAA,IAEE,CAAA;AAAA,iBAgBY,eAAA,kBAAiC,aAAA,CAAA,CAC/C,WAAA,iBAA4B,eAAA,CAAgB,QAAA,KAC5C,eAAA;EAEA,KAAA,EAAO,gBAAA,CAAiB,QAAA;EACxB,GAAA,GAAM,MAAA;IAAU,KAAA;EAAA;IAAoB,GAAA;EAAA,MAAkB,QAAA;AAAA"}
|
package/dist/tapClientLookup.js
CHANGED
|
@@ -1,42 +1,36 @@
|
|
|
1
|
-
import { tapMemo, tapResource, tapResources, } from "@assistant-ui/tap";
|
|
2
|
-
import { ClientResource } from "./tapClientResource.js";
|
|
3
1
|
import { wrapperResource } from "./wrapperResource.js";
|
|
2
|
+
import { ClientResource } from "./tapClientResource.js";
|
|
3
|
+
import { tapMemo, tapResource, tapResources } from "@assistant-ui/tap";
|
|
4
|
+
//#region src/tapClientLookup.ts
|
|
4
5
|
const ClientResourceWithKey = wrapperResource((el) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
return tapResource(ClientResource(el));
|
|
6
|
+
if (el.key === void 0) throw new Error("tapClientResource: Element has no key");
|
|
7
|
+
return tapResource(ClientResource(el));
|
|
9
8
|
});
|
|
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
|
-
const index = keyToIndex[lookup.key];
|
|
35
|
-
if (index === undefined) {
|
|
36
|
-
throw new Error(`tapClientLookup: Key "${lookup.key}" not found`);
|
|
37
|
-
}
|
|
38
|
-
return resources[index].methods;
|
|
39
|
-
},
|
|
40
|
-
};
|
|
9
|
+
function tapClientLookup(getElements, getElementsDeps) {
|
|
10
|
+
const resources = tapResources(() => getElements().map((el) => ClientResourceWithKey(el)), getElementsDeps);
|
|
11
|
+
const keys = tapMemo(() => Object.keys(resources), [resources]);
|
|
12
|
+
const keyToIndex = tapMemo(() => {
|
|
13
|
+
return resources.reduce((acc, resource, index) => {
|
|
14
|
+
acc[resource.key] = index;
|
|
15
|
+
return acc;
|
|
16
|
+
}, {});
|
|
17
|
+
}, [resources]);
|
|
18
|
+
return {
|
|
19
|
+
state: tapMemo(() => {
|
|
20
|
+
return resources.map((r) => r.state);
|
|
21
|
+
}, [resources]),
|
|
22
|
+
get: (lookup) => {
|
|
23
|
+
if ("index" in lookup) {
|
|
24
|
+
if (lookup.index < 0 || lookup.index >= keys.length) throw new Error(`tapClientLookup: Index ${lookup.index} out of bounds (length: ${keys.length})`);
|
|
25
|
+
return resources[lookup.index].methods;
|
|
26
|
+
}
|
|
27
|
+
const index = keyToIndex[lookup.key];
|
|
28
|
+
if (index === void 0) throw new Error(`tapClientLookup: Key "${lookup.key}" not found`);
|
|
29
|
+
return resources[index].methods;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
41
32
|
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { tapClientLookup };
|
|
35
|
+
|
|
42
36
|
//# sourceMappingURL=tapClientLookup.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tapClientLookup.js","
|
|
1
|
+
{"version":3,"file":"tapClientLookup.js","names":[],"sources":["../src/tapClientLookup.ts"],"sourcesContent":["import {\n tapMemo,\n tapResource,\n tapResources,\n type ResourceElement,\n} from \"@assistant-ui/tap\";\nimport type { ClientMethods } from \"./types/client\";\nimport { ClientResource } from \"./tapClientResource\";\nimport { wrapperResource } from \"./wrapperResource\";\n\ntype InferClientState<TMethods> = TMethods extends {\n getState: () => infer S;\n}\n ? S\n : unknown;\n\nconst ClientResourceWithKey = wrapperResource(\n <TMethods extends ClientMethods>(el: ResourceElement<TMethods>) => {\n if (el.key === undefined) {\n throw new Error(\"tapClientResource: Element has no key\");\n }\n return tapResource(ClientResource(el)) as {\n methods: TMethods;\n state: InferClientState<TMethods>;\n key: string | number;\n };\n },\n);\n\nexport function tapClientLookup<TMethods extends ClientMethods>(\n getElements: () => readonly ResourceElement<TMethods>[],\n getElementsDeps: readonly unknown[],\n): {\n state: InferClientState<TMethods>[];\n get: (lookup: { index: number } | { key: string }) => TMethods;\n} {\n const resources = tapResources(\n () => getElements().map((el) => ClientResourceWithKey(el)),\n // biome-ignore lint/correctness/useExhaustiveDependencies: getElementsDeps is passed through from caller\n getElementsDeps,\n );\n\n const keys = tapMemo(() => Object.keys(resources), [resources]);\n\n // For arrays, track element key -> index mapping\n const keyToIndex = tapMemo(() => {\n return resources.reduce(\n (acc, resource, index) => {\n acc[resource.key] = index;\n return acc;\n },\n {} as Record<string, number>,\n );\n }, [resources]);\n\n const state = tapMemo(() => {\n return resources.map((r) => r.state);\n }, [resources]);\n\n return {\n state,\n get: (lookup: { index: number } | { key: string }) => {\n if (\"index\" in lookup) {\n if (lookup.index < 0 || lookup.index >= keys.length) {\n throw new Error(\n `tapClientLookup: Index ${lookup.index} out of bounds (length: ${keys.length})`,\n );\n }\n return resources[lookup.index]!.methods;\n }\n\n const index = keyToIndex[lookup.key];\n if (index === undefined) {\n throw new Error(`tapClientLookup: Key \"${lookup.key}\" not found`);\n }\n return resources[index]!.methods;\n },\n };\n}\n"],"mappings":";;;;AAgBA,MAAM,wBAAwB,iBACK,OAAkC;CACjE,IAAI,GAAG,QAAQ,KAAA,GACb,MAAM,IAAI,MAAM,uCAAuC;CAEzD,OAAO,YAAY,eAAe,EAAE,CAAC;AAKvC,CACF;AAEA,SAAgB,gBACd,aACA,iBAIA;CACA,MAAM,YAAY,mBACV,YAAY,EAAE,KAAK,OAAO,sBAAsB,EAAE,CAAC,GAEzD,eACF;CAEA,MAAM,OAAO,cAAc,OAAO,KAAK,SAAS,GAAG,CAAC,SAAS,CAAC;CAG9D,MAAM,aAAa,cAAc;EAC/B,OAAO,UAAU,QACd,KAAK,UAAU,UAAU;GACxB,IAAI,SAAS,OAAO;GACpB,OAAO;EACT,GACA,CAAC,CACH;CACF,GAAG,CAAC,SAAS,CAAC;CAMd,OAAO;EACL,OALY,cAAc;GAC1B,OAAO,UAAU,KAAK,MAAM,EAAE,KAAK;EACrC,GAAG,CAAC,SAAS,CAGP;EACJ,MAAM,WAAgD;GACpD,IAAI,WAAW,QAAQ;IACrB,IAAI,OAAO,QAAQ,KAAK,OAAO,SAAS,KAAK,QAC3C,MAAM,IAAI,MACR,0BAA0B,OAAO,MAAM,0BAA0B,KAAK,OAAO,EAC/E;IAEF,OAAO,UAAU,OAAO,OAAQ;GAClC;GAEA,MAAM,QAAQ,WAAW,OAAO;GAChC,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,yBAAyB,OAAO,IAAI,YAAY;GAElE,OAAO,UAAU,OAAQ;EAC3B;CACF;AACF"}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { ClientMethods } from "./types/client.js";
|
|
2
|
+
import { ResourceElement } from "@assistant-ui/tap";
|
|
3
|
+
|
|
4
|
+
//#region src/tapClientResource.d.ts
|
|
5
|
+
declare const getClientState: (client: ClientMethods) => any;
|
|
4
6
|
type InferClientState<TMethods> = TMethods extends {
|
|
5
|
-
|
|
7
|
+
getState: () => infer S;
|
|
6
8
|
} ? S : undefined;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
declare const tapClientResource: <TMethods extends ClientMethods>(element: ResourceElement<TMethods>) => {
|
|
10
|
+
state: InferClientState<TMethods>;
|
|
11
|
+
methods: TMethods;
|
|
12
|
+
key: string | number | undefined;
|
|
11
13
|
};
|
|
12
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
export { getClientState, tapClientResource };
|
|
13
16
|
//# sourceMappingURL=tapClientResource.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tapClientResource.d.ts","
|
|
1
|
+
{"version":3,"file":"tapClientResource.d.ts","names":[],"sources":["../src/tapClientResource.ts"],"mappings":";;;;cA6Ba,cAAA,GAAkB,MAAqB,EAAb,aAAa;AAAA,KA6I/C,gBAAA,aAA6B,QAAQ;EACxC,QAAA;AAAA,IAEE,CAAA;AAAA,cAGS,iBAAA,oBAAsC,aAAA,EACjD,OAAA,EAAS,eAAA,CAAgB,QAAA;EAEzB,KAAA,EAAO,gBAAA,CAAiB,QAAA;EACxB,OAAA,EAAS,QAAA;EACT,GAAA;AAAA"}
|