@makeswift/runtime 0.28.8-canary.19 → 0.28.8-canary.20
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/dist/cjs/api/graphql-api-client.js +1 -1
- package/dist/cjs/api/rest-api-client.js +1 -1
- package/dist/cjs/api-handler/handlers/manifest.js +1 -1
- package/dist/cjs/client/index.js +1 -1
- package/dist/cjs/runtimes/react/components/control-instances-context.js.map +1 -1
- package/dist/cjs/runtimes/react/hooks/use-control-instance.js +88 -0
- package/dist/cjs/runtimes/react/hooks/use-control-instance.js.map +1 -0
- package/dist/esm/api/graphql-api-client.js +1 -1
- package/dist/esm/api/rest-api-client.js +1 -1
- package/dist/esm/api-handler/handlers/manifest.js +1 -1
- package/dist/esm/client/index.js +1 -1
- package/dist/esm/runtimes/react/components/control-instances-context.js.map +1 -1
- package/dist/esm/runtimes/react/hooks/use-control-instance.js +62 -0
- package/dist/esm/runtimes/react/hooks/use-control-instance.js.map +1 -0
- package/dist/types/runtimes/react/components/control-instances-context.d.ts +3 -3
- package/dist/types/runtimes/react/components/control-instances-context.d.ts.map +1 -1
- package/dist/types/runtimes/react/hooks/__tests__/use-control-instance.test.d.ts +3 -0
- package/dist/types/runtimes/react/hooks/__tests__/use-control-instance.test.d.ts.map +1 -0
- package/dist/types/runtimes/react/hooks/use-control-instance.d.ts +7 -0
- package/dist/types/runtimes/react/hooks/use-control-instance.d.ts.map +1 -0
- package/package.json +3 -3
|
@@ -27,7 +27,7 @@ class MakeswiftGraphQLApiClient {
|
|
|
27
27
|
graphqlClient;
|
|
28
28
|
constructor({ endpoint }) {
|
|
29
29
|
this.graphqlClient = new import_client.GraphQLClient(endpoint, {
|
|
30
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
30
|
+
"makeswift-runtime-version": "0.28.8-canary.20"
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
async createTableRecord(tableId, columns) {
|
|
@@ -147,7 +147,7 @@ class MakeswiftRestAPIClient {
|
|
|
147
147
|
const requestHeaders = new Headers({
|
|
148
148
|
"x-api-key": this.apiKey,
|
|
149
149
|
"makeswift-site-api-key": this.apiKey,
|
|
150
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
150
|
+
"makeswift-runtime-version": "0.28.8-canary.20"
|
|
151
151
|
});
|
|
152
152
|
if (siteVersion?.token) {
|
|
153
153
|
requestUrl.searchParams.set("version", siteVersion.version);
|
|
@@ -28,7 +28,7 @@ async function manifestHandler(req, { apiKey, manifest }) {
|
|
|
28
28
|
return import_request_response.ApiResponse.json({ message: "Unauthorized" }, { status: 401 });
|
|
29
29
|
}
|
|
30
30
|
return import_request_response.ApiResponse.json({
|
|
31
|
-
version: "0.28.8-canary.
|
|
31
|
+
version: "0.28.8-canary.20",
|
|
32
32
|
interactionMode: true,
|
|
33
33
|
clientSideNavigation: false,
|
|
34
34
|
elementFromPoint: false,
|
package/dist/cjs/client/index.js
CHANGED
|
@@ -689,7 +689,7 @@ Received "${apiKey}" instead.`
|
|
|
689
689
|
headers: {
|
|
690
690
|
"x-api-key": this.apiKey,
|
|
691
691
|
"makeswift-site-api-key": this.apiKey,
|
|
692
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
692
|
+
"makeswift-runtime-version": "0.28.8-canary.20",
|
|
693
693
|
"content-type": "application/json"
|
|
694
694
|
},
|
|
695
695
|
body: JSON.stringify({ token }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useContext } from 'react'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useContext } from 'react'\n\nimport { type AnyControlInstance } from '@makeswift/controls'\n\ntype Context = {\n elementKey: string\n instances: Record<string, AnyControlInstance> | null\n}\n\nconst ControlInstancesContext = createContext<Context | null>(null)\n\nexport const ControlInstancesProvider = ControlInstancesContext.Provider\n\nexport function useControlInstances(elementKey: string | undefined): Context['instances'] | null {\n const cx = useContext(ControlInstancesContext)\n return cx && cx.elementKey === elementKey ? cx.instances : null\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0C;AAS1C,MAAM,8BAA0B,4BAA8B,IAAI;AAE3D,MAAM,2BAA2B,wBAAwB;AAEzD,SAAS,oBAAoB,YAA6D;AAC/F,QAAM,SAAK,yBAAW,uBAAuB;AAC7C,SAAO,MAAM,GAAG,eAAe,aAAa,GAAG,YAAY;AAC7D;","names":[]}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var use_control_instance_exports = {};
|
|
20
|
+
__export(use_control_instance_exports, {
|
|
21
|
+
getInstanceByPropPath: () => getInstanceByPropPath,
|
|
22
|
+
getInstanceChainByPropPath: () => getInstanceChainByPropPath,
|
|
23
|
+
useControlInstance: () => useControlInstance
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_control_instance_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_controls = require("@makeswift/controls");
|
|
28
|
+
var import_use_control_instances = require("./use-control-instances");
|
|
29
|
+
function useControlInstance(instanceKey, instanceClass) {
|
|
30
|
+
const { elementKey, propPath } = instanceKey ?? {};
|
|
31
|
+
const instances = (0, import_use_control_instances.useControlInstances)(elementKey);
|
|
32
|
+
const getInstance = (0, import_react.useCallback)(
|
|
33
|
+
() => instances != null && propPath != null ? getInstanceByPropPath(instances, propPath) : null,
|
|
34
|
+
[instances, propPath]
|
|
35
|
+
);
|
|
36
|
+
const subscribe = (0, import_react.useCallback)(
|
|
37
|
+
(onStateChange) => {
|
|
38
|
+
if (instances == null || propPath == null)
|
|
39
|
+
return () => {
|
|
40
|
+
};
|
|
41
|
+
let unsubscribes = [];
|
|
42
|
+
const handleStateChange = () => {
|
|
43
|
+
resubscribe();
|
|
44
|
+
onStateChange();
|
|
45
|
+
};
|
|
46
|
+
const resubscribe = () => {
|
|
47
|
+
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
48
|
+
const chain = getInstanceChainByPropPath(instances, propPath).filter(import_controls.isNotNil);
|
|
49
|
+
unsubscribes = chain.map((instance) => instance.subscribe(handleStateChange));
|
|
50
|
+
};
|
|
51
|
+
resubscribe();
|
|
52
|
+
return () => unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
53
|
+
},
|
|
54
|
+
[instances, propPath]
|
|
55
|
+
);
|
|
56
|
+
const result = (0, import_react.useSyncExternalStore)(subscribe, getInstance, getInstance);
|
|
57
|
+
if (result) {
|
|
58
|
+
const foundKey = result.instanceKey;
|
|
59
|
+
if (foundKey.elementKey !== elementKey || foundKey.propPath != propPath) {
|
|
60
|
+
console.error("Mismatching control instance key: expected", instanceKey, " got", foundKey);
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
if (!(result instanceof instanceClass)) {
|
|
64
|
+
console.error(
|
|
65
|
+
`Unexpected control instance class: requested '${instanceClass.name}', found '${result.constructor.name}'`,
|
|
66
|
+
{ instanceKey }
|
|
67
|
+
);
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
function getInstanceChainByPropPath(instances, propPath) {
|
|
74
|
+
const [parentProp, ...childPath] = propPath.split(".");
|
|
75
|
+
const parentInstance = instances[parentProp];
|
|
76
|
+
const getInstanceChain = (parent, childPath2) => parent == null || childPath2.length === 0 ? [parent ?? null] : [parent, ...getInstanceChain(parent.child(childPath2[0]), childPath2.slice(1))];
|
|
77
|
+
return getInstanceChain(parentInstance, childPath);
|
|
78
|
+
}
|
|
79
|
+
function getInstanceByPropPath(instances, propPath) {
|
|
80
|
+
return getInstanceChainByPropPath(instances, propPath).at(-1) ?? null;
|
|
81
|
+
}
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
getInstanceByPropPath,
|
|
85
|
+
getInstanceChainByPropPath,
|
|
86
|
+
useControlInstance
|
|
87
|
+
});
|
|
88
|
+
//# sourceMappingURL=use-control-instance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-control-instance.ts"],"sourcesContent":["import { useCallback, useSyncExternalStore } from 'react'\nimport { type AnyControlInstance, type ControlInstanceKey, isNotNil } from '@makeswift/controls'\n\nimport { useControlInstances } from './use-control-instances'\n\ntype InstanceConstructor<T extends AnyControlInstance> = abstract new (...args: never[]) => T\n\nexport function useControlInstance<T extends AnyControlInstance>(\n instanceKey: ControlInstanceKey | undefined,\n instanceClass: InstanceConstructor<T>,\n): T | null {\n const { elementKey, propPath } = instanceKey ?? {}\n\n const instances = useControlInstances(elementKey)\n const getInstance = useCallback(\n () =>\n instances != null && propPath != null ? getInstanceByPropPath(instances, propPath) : null,\n [instances, propPath],\n )\n\n const subscribe = useCallback(\n (onStateChange: () => void) => {\n if (instances == null || propPath == null) return () => {}\n\n let unsubscribes: (() => void)[] = []\n\n // recompute the instance chain and resubscribe to its updates on every notification,\n // since a control's own update (e.g. a `ListControl` replacing its children) can replace\n // the instances further down the chain\n const handleStateChange = () => {\n resubscribe()\n onStateChange()\n }\n\n const resubscribe = () => {\n unsubscribes.forEach(unsubscribe => unsubscribe())\n\n const chain = getInstanceChainByPropPath(instances, propPath).filter(isNotNil)\n unsubscribes = chain.map(instance => instance.subscribe(handleStateChange))\n }\n\n resubscribe()\n\n return () => unsubscribes.forEach(unsubscribe => unsubscribe())\n },\n [instances, propPath],\n )\n\n const result = useSyncExternalStore(subscribe, getInstance, getInstance)\n\n if (result) {\n const foundKey = result.instanceKey\n if (foundKey.elementKey !== elementKey || foundKey.propPath != propPath) {\n console.error('Mismatching control instance key: expected', instanceKey, ' got', foundKey)\n return null\n }\n\n if (!(result instanceof instanceClass)) {\n console.error(\n `Unexpected control instance class: requested '${instanceClass.name}', found '${result.constructor.name}'`,\n { instanceKey },\n )\n\n return null\n }\n }\n\n return result\n}\n\nexport function getInstanceChainByPropPath(\n instances: Record<string, AnyControlInstance>,\n propPath: string,\n): (AnyControlInstance | null)[] {\n const [parentProp, ...childPath] = propPath.split('.')\n const parentInstance = instances[parentProp]\n\n const getInstanceChain = (\n parent: AnyControlInstance | undefined,\n childPath: string[],\n ): (AnyControlInstance | null)[] =>\n parent == null || childPath.length === 0\n ? [parent ?? null]\n : [parent, ...getInstanceChain(parent.child(childPath[0]), childPath.slice(1))]\n\n return getInstanceChain(parentInstance, childPath)\n}\n\nexport function getInstanceByPropPath(\n instances: Record<string, AnyControlInstance>,\n propPath: string,\n): AnyControlInstance | null {\n return getInstanceChainByPropPath(instances, propPath).at(-1) ?? null\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkD;AAClD,sBAA2E;AAE3E,mCA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IAAI,SAAS,MAAM,GAAG;AACrD,QAAM,iBAAiB,UAAU,UAAU;AAE3C,QAAM,mBAAmB,CACvB,QACAA,eAEA,UAAU,QAAQA,WAAU,WAAW,IACnC,CAAC,UAAU,IAAI,IACf,CAAC,QAAQ,GAAG,iBAAiB,OAAO,MAAMA,WAAU,CAAC,CAAC,GAAGA,WAAU,MAAM,CAAC,CAAC,CAAC;AAElF,SAAO,iBAAiB,gBAAgB,SAAS;AACnD;AAEO,SAAS,sBACd,WACA,UAC2B;AAC3B,SAAO,2BAA2B,WAAW,QAAQ,EAAE,GAAG,EAAE,KAAK;AACnE;","names":["childPath"]}
|
|
@@ -9,7 +9,7 @@ class MakeswiftGraphQLApiClient {
|
|
|
9
9
|
graphqlClient;
|
|
10
10
|
constructor({ endpoint }) {
|
|
11
11
|
this.graphqlClient = new GraphQLClient(endpoint, {
|
|
12
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
12
|
+
"makeswift-runtime-version": "0.28.8-canary.20"
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
async createTableRecord(tableId, columns) {
|
|
@@ -113,7 +113,7 @@ class MakeswiftRestAPIClient {
|
|
|
113
113
|
const requestHeaders = new Headers({
|
|
114
114
|
"x-api-key": this.apiKey,
|
|
115
115
|
"makeswift-site-api-key": this.apiKey,
|
|
116
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
116
|
+
"makeswift-runtime-version": "0.28.8-canary.20"
|
|
117
117
|
});
|
|
118
118
|
if (siteVersion?.token) {
|
|
119
119
|
requestUrl.searchParams.set("version", siteVersion.version);
|
|
@@ -8,7 +8,7 @@ async function manifestHandler(req, { apiKey, manifest }) {
|
|
|
8
8
|
return ApiResponse.json({ message: "Unauthorized" }, { status: 401 });
|
|
9
9
|
}
|
|
10
10
|
return ApiResponse.json({
|
|
11
|
-
version: "0.28.8-canary.
|
|
11
|
+
version: "0.28.8-canary.20",
|
|
12
12
|
interactionMode: true,
|
|
13
13
|
clientSideNavigation: false,
|
|
14
14
|
elementFromPoint: false,
|
package/dist/esm/client/index.js
CHANGED
|
@@ -669,7 +669,7 @@ Received "${apiKey}" instead.`
|
|
|
669
669
|
headers: {
|
|
670
670
|
"x-api-key": this.apiKey,
|
|
671
671
|
"makeswift-site-api-key": this.apiKey,
|
|
672
|
-
"makeswift-runtime-version": "0.28.8-canary.
|
|
672
|
+
"makeswift-runtime-version": "0.28.8-canary.20",
|
|
673
673
|
"content-type": "application/json"
|
|
674
674
|
},
|
|
675
675
|
body: JSON.stringify({ token }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useContext } from 'react'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useContext } from 'react'\n\nimport { type AnyControlInstance } from '@makeswift/controls'\n\ntype Context = {\n elementKey: string\n instances: Record<string, AnyControlInstance> | null\n}\n\nconst ControlInstancesContext = createContext<Context | null>(null)\n\nexport const ControlInstancesProvider = ControlInstancesContext.Provider\n\nexport function useControlInstances(elementKey: string | undefined): Context['instances'] | null {\n const cx = useContext(ControlInstancesContext)\n return cx && cx.elementKey === elementKey ? cx.instances : null\n}\n"],"mappings":";AAEA,SAAS,eAAe,kBAAkB;AAS1C,MAAM,0BAA0B,cAA8B,IAAI;AAE3D,MAAM,2BAA2B,wBAAwB;AAEzD,SAAS,oBAAoB,YAA6D;AAC/F,QAAM,KAAK,WAAW,uBAAuB;AAC7C,SAAO,MAAM,GAAG,eAAe,aAAa,GAAG,YAAY;AAC7D;","names":[]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from "react";
|
|
2
|
+
import { isNotNil } from "@makeswift/controls";
|
|
3
|
+
import { useControlInstances } from "./use-control-instances";
|
|
4
|
+
function useControlInstance(instanceKey, instanceClass) {
|
|
5
|
+
const { elementKey, propPath } = instanceKey ?? {};
|
|
6
|
+
const instances = useControlInstances(elementKey);
|
|
7
|
+
const getInstance = useCallback(
|
|
8
|
+
() => instances != null && propPath != null ? getInstanceByPropPath(instances, propPath) : null,
|
|
9
|
+
[instances, propPath]
|
|
10
|
+
);
|
|
11
|
+
const subscribe = useCallback(
|
|
12
|
+
(onStateChange) => {
|
|
13
|
+
if (instances == null || propPath == null)
|
|
14
|
+
return () => {
|
|
15
|
+
};
|
|
16
|
+
let unsubscribes = [];
|
|
17
|
+
const handleStateChange = () => {
|
|
18
|
+
resubscribe();
|
|
19
|
+
onStateChange();
|
|
20
|
+
};
|
|
21
|
+
const resubscribe = () => {
|
|
22
|
+
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
23
|
+
const chain = getInstanceChainByPropPath(instances, propPath).filter(isNotNil);
|
|
24
|
+
unsubscribes = chain.map((instance) => instance.subscribe(handleStateChange));
|
|
25
|
+
};
|
|
26
|
+
resubscribe();
|
|
27
|
+
return () => unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
28
|
+
},
|
|
29
|
+
[instances, propPath]
|
|
30
|
+
);
|
|
31
|
+
const result = useSyncExternalStore(subscribe, getInstance, getInstance);
|
|
32
|
+
if (result) {
|
|
33
|
+
const foundKey = result.instanceKey;
|
|
34
|
+
if (foundKey.elementKey !== elementKey || foundKey.propPath != propPath) {
|
|
35
|
+
console.error("Mismatching control instance key: expected", instanceKey, " got", foundKey);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (!(result instanceof instanceClass)) {
|
|
39
|
+
console.error(
|
|
40
|
+
`Unexpected control instance class: requested '${instanceClass.name}', found '${result.constructor.name}'`,
|
|
41
|
+
{ instanceKey }
|
|
42
|
+
);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function getInstanceChainByPropPath(instances, propPath) {
|
|
49
|
+
const [parentProp, ...childPath] = propPath.split(".");
|
|
50
|
+
const parentInstance = instances[parentProp];
|
|
51
|
+
const getInstanceChain = (parent, childPath2) => parent == null || childPath2.length === 0 ? [parent ?? null] : [parent, ...getInstanceChain(parent.child(childPath2[0]), childPath2.slice(1))];
|
|
52
|
+
return getInstanceChain(parentInstance, childPath);
|
|
53
|
+
}
|
|
54
|
+
function getInstanceByPropPath(instances, propPath) {
|
|
55
|
+
return getInstanceChainByPropPath(instances, propPath).at(-1) ?? null;
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
getInstanceByPropPath,
|
|
59
|
+
getInstanceChainByPropPath,
|
|
60
|
+
useControlInstance
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=use-control-instance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-control-instance.ts"],"sourcesContent":["import { useCallback, useSyncExternalStore } from 'react'\nimport { type AnyControlInstance, type ControlInstanceKey, isNotNil } from '@makeswift/controls'\n\nimport { useControlInstances } from './use-control-instances'\n\ntype InstanceConstructor<T extends AnyControlInstance> = abstract new (...args: never[]) => T\n\nexport function useControlInstance<T extends AnyControlInstance>(\n instanceKey: ControlInstanceKey | undefined,\n instanceClass: InstanceConstructor<T>,\n): T | null {\n const { elementKey, propPath } = instanceKey ?? {}\n\n const instances = useControlInstances(elementKey)\n const getInstance = useCallback(\n () =>\n instances != null && propPath != null ? getInstanceByPropPath(instances, propPath) : null,\n [instances, propPath],\n )\n\n const subscribe = useCallback(\n (onStateChange: () => void) => {\n if (instances == null || propPath == null) return () => {}\n\n let unsubscribes: (() => void)[] = []\n\n // recompute the instance chain and resubscribe to its updates on every notification,\n // since a control's own update (e.g. a `ListControl` replacing its children) can replace\n // the instances further down the chain\n const handleStateChange = () => {\n resubscribe()\n onStateChange()\n }\n\n const resubscribe = () => {\n unsubscribes.forEach(unsubscribe => unsubscribe())\n\n const chain = getInstanceChainByPropPath(instances, propPath).filter(isNotNil)\n unsubscribes = chain.map(instance => instance.subscribe(handleStateChange))\n }\n\n resubscribe()\n\n return () => unsubscribes.forEach(unsubscribe => unsubscribe())\n },\n [instances, propPath],\n )\n\n const result = useSyncExternalStore(subscribe, getInstance, getInstance)\n\n if (result) {\n const foundKey = result.instanceKey\n if (foundKey.elementKey !== elementKey || foundKey.propPath != propPath) {\n console.error('Mismatching control instance key: expected', instanceKey, ' got', foundKey)\n return null\n }\n\n if (!(result instanceof instanceClass)) {\n console.error(\n `Unexpected control instance class: requested '${instanceClass.name}', found '${result.constructor.name}'`,\n { instanceKey },\n )\n\n return null\n }\n }\n\n return result\n}\n\nexport function getInstanceChainByPropPath(\n instances: Record<string, AnyControlInstance>,\n propPath: string,\n): (AnyControlInstance | null)[] {\n const [parentProp, ...childPath] = propPath.split('.')\n const parentInstance = instances[parentProp]\n\n const getInstanceChain = (\n parent: AnyControlInstance | undefined,\n childPath: string[],\n ): (AnyControlInstance | null)[] =>\n parent == null || childPath.length === 0\n ? [parent ?? null]\n : [parent, ...getInstanceChain(parent.child(childPath[0]), childPath.slice(1))]\n\n return getInstanceChain(parentInstance, childPath)\n}\n\nexport function getInstanceByPropPath(\n instances: Record<string, AnyControlInstance>,\n propPath: string,\n): AnyControlInstance | null {\n return getInstanceChainByPropPath(instances, propPath).at(-1) ?? null\n}\n"],"mappings":"AAAA,SAAS,aAAa,4BAA4B;AAClD,SAA2D,gBAAgB;AAE3E,SAAS,2BAA2B;AAI7B,SAAS,mBACd,aACA,eACU;AACV,QAAM,EAAE,YAAY,SAAS,IAAI,eAAe,CAAC;AAEjD,QAAM,YAAY,oBAAoB,UAAU;AAChD,QAAM,cAAc;AAAA,IAClB,MACE,aAAa,QAAQ,YAAY,OAAO,sBAAsB,WAAW,QAAQ,IAAI;AAAA,IACvF,CAAC,WAAW,QAAQ;AAAA,EACtB;AAEA,QAAM,YAAY;AAAA,IAChB,CAAC,kBAA8B;AAC7B,UAAI,aAAa,QAAQ,YAAY;AAAM,eAAO,MAA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W,QAAQ,EAAE,GAAG,EAAE,KAAK;AACnE;","names":["childPath"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type AnyControlInstance } from '@makeswift/controls';
|
|
2
2
|
type Context = {
|
|
3
3
|
elementKey: string;
|
|
4
|
-
instances: Record<string,
|
|
4
|
+
instances: Record<string, AnyControlInstance> | null;
|
|
5
5
|
};
|
|
6
6
|
export declare const ControlInstancesProvider: import("react").Provider<Context | null>;
|
|
7
|
-
export declare function useControlInstances(elementKey: string): Context['instances'] | null;
|
|
7
|
+
export declare function useControlInstances(elementKey: string | undefined): Context['instances'] | null;
|
|
8
8
|
export {};
|
|
9
9
|
//# sourceMappingURL=control-instances-context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"control-instances-context.d.ts","sourceRoot":"","sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"control-instances-context.d.ts","sourceRoot":"","sources":["../../../../../src/runtimes/react/components/control-instances-context.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAE7D,KAAK,OAAO,GAAG;IACb,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,GAAG,IAAI,CAAA;CACrD,CAAA;AAID,eAAO,MAAM,wBAAwB,0CAAmC,CAAA;AAExE,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI,CAG/F"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-control-instance.test.d.ts","sourceRoot":"","sources":["../../../../../../src/runtimes/react/hooks/__tests__/use-control-instance.test.tsx"],"names":[],"mappings":"AAAA,8BAA8B;AAI9B,OAAO,2BAA2B,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AnyControlInstance, type ControlInstanceKey } from '@makeswift/controls';
|
|
2
|
+
type InstanceConstructor<T extends AnyControlInstance> = abstract new (...args: never[]) => T;
|
|
3
|
+
export declare function useControlInstance<T extends AnyControlInstance>(instanceKey: ControlInstanceKey | undefined, instanceClass: InstanceConstructor<T>): T | null;
|
|
4
|
+
export declare function getInstanceChainByPropPath(instances: Record<string, AnyControlInstance>, propPath: string): (AnyControlInstance | null)[];
|
|
5
|
+
export declare function getInstanceByPropPath(instances: Record<string, AnyControlInstance>, propPath: string): AnyControlInstance | null;
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=use-control-instance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-control-instance.d.ts","sourceRoot":"","sources":["../../../../../src/runtimes/react/hooks/use-control-instance.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,kBAAkB,EAAY,MAAM,qBAAqB,CAAA;AAIhG,KAAK,mBAAmB,CAAC,CAAC,SAAS,kBAAkB,IAAI,QAAQ,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;AAE7F,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,kBAAkB,EAC7D,WAAW,EAAE,kBAAkB,GAAG,SAAS,EAC3C,aAAa,EAAE,mBAAmB,CAAC,CAAC,CAAC,GACpC,CAAC,GAAG,IAAI,CA0DV;AAED,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,EAC7C,QAAQ,EAAE,MAAM,GACf,CAAC,kBAAkB,GAAG,IAAI,CAAC,EAAE,CAa/B;AAED,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,EAC7C,QAAQ,EAAE,MAAM,GACf,kBAAkB,GAAG,IAAI,CAE3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makeswift/runtime",
|
|
3
|
-
"version": "0.28.8-canary.
|
|
3
|
+
"version": "0.28.8-canary.20",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"url": "makeswift/makeswift",
|
|
@@ -238,9 +238,9 @@
|
|
|
238
238
|
"use-sync-external-store": "^1.5.0",
|
|
239
239
|
"uuid": "^9.0.0",
|
|
240
240
|
"zod": "^3.21.4",
|
|
241
|
-
"@makeswift/controls": "0.1.21-canary.
|
|
241
|
+
"@makeswift/controls": "0.1.21-canary.10",
|
|
242
242
|
"@makeswift/next-plugin": "0.6.1",
|
|
243
|
-
"@makeswift/prop-controllers": "0.4.15-canary.
|
|
243
|
+
"@makeswift/prop-controllers": "0.4.15-canary.10"
|
|
244
244
|
},
|
|
245
245
|
"devDependencies": {
|
|
246
246
|
"@emotion/jest": "^11.11.0",
|