@xyo-network/react-payload-diviner 2.64.0-rc.7 → 2.64.1
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/browser/contexts/DivinedPayload/Context.cjs +28 -0
- package/dist/{node/contexts/DivinedPayload/Context.mjs.map → browser/contexts/DivinedPayload/Context.cjs.map} +1 -1
- package/dist/browser/contexts/DivinedPayload/Context.d.cts +4 -0
- package/dist/browser/contexts/DivinedPayload/Context.d.cts.map +1 -0
- package/dist/browser/contexts/DivinedPayload/Context.js +2 -1
- package/dist/browser/contexts/DivinedPayload/Context.js.map +1 -1
- package/dist/browser/contexts/DivinedPayload/Provider.cjs +111 -0
- package/dist/browser/contexts/DivinedPayload/Provider.cjs.map +1 -0
- package/dist/browser/contexts/DivinedPayload/Provider.d.cts +9 -0
- package/dist/browser/contexts/DivinedPayload/Provider.d.cts.map +1 -0
- package/dist/browser/contexts/DivinedPayload/Provider.js +59 -7
- package/dist/browser/contexts/DivinedPayload/Provider.js.map +1 -1
- package/dist/browser/contexts/DivinedPayload/State.cjs +19 -0
- package/dist/browser/contexts/DivinedPayload/State.cjs.map +1 -0
- package/dist/browser/contexts/DivinedPayload/State.d.cts +9 -0
- package/dist/browser/contexts/DivinedPayload/State.d.cts.map +1 -0
- package/dist/browser/contexts/DivinedPayload/index.cjs +115 -0
- package/dist/browser/contexts/DivinedPayload/index.cjs.map +1 -0
- package/dist/browser/contexts/DivinedPayload/index.d.cts +5 -0
- package/dist/browser/contexts/DivinedPayload/index.d.cts.map +1 -0
- package/dist/browser/contexts/DivinedPayload/index.js +91 -4
- package/dist/browser/contexts/DivinedPayload/index.js.map +1 -1
- package/dist/browser/contexts/DivinedPayload/use.cjs +34 -0
- package/dist/browser/contexts/DivinedPayload/use.cjs.map +1 -0
- package/dist/browser/contexts/DivinedPayload/use.d.cts +2 -0
- package/dist/browser/contexts/DivinedPayload/use.d.cts.map +1 -0
- package/dist/browser/contexts/DivinedPayload/use.js +8 -2
- package/dist/browser/contexts/DivinedPayload/use.js.map +1 -1
- package/dist/browser/contexts/PayloadDiviner/Context.cjs +28 -0
- package/dist/{node/contexts/PayloadDiviner/Context.mjs.map → browser/contexts/PayloadDiviner/Context.cjs.map} +1 -1
- package/dist/browser/contexts/PayloadDiviner/Context.d.cts +5 -0
- package/dist/browser/contexts/PayloadDiviner/Context.d.cts.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/Context.js +2 -1
- package/dist/browser/contexts/PayloadDiviner/Context.js.map +1 -1
- package/dist/browser/contexts/PayloadDiviner/Provider.cjs +53 -0
- package/dist/browser/contexts/PayloadDiviner/Provider.cjs.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/Provider.d.cts +9 -0
- package/dist/browser/contexts/PayloadDiviner/Provider.d.cts.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/Provider.js +9 -3
- package/dist/browser/contexts/PayloadDiviner/Provider.js.map +1 -1
- package/dist/browser/contexts/PayloadDiviner/State.cjs +19 -0
- package/dist/browser/contexts/PayloadDiviner/State.cjs.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/State.d.cts +7 -0
- package/dist/browser/contexts/PayloadDiviner/State.d.cts.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/index.cjs +136 -0
- package/dist/browser/contexts/PayloadDiviner/index.cjs.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/index.d.cts +5 -0
- package/dist/browser/contexts/PayloadDiviner/index.d.cts.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/index.js +112 -4
- package/dist/browser/contexts/PayloadDiviner/index.js.map +1 -1
- package/dist/browser/contexts/PayloadDiviner/use.cjs +111 -0
- package/dist/browser/contexts/PayloadDiviner/use.cjs.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/use.d.cts +13 -0
- package/dist/browser/contexts/PayloadDiviner/use.d.cts.map +1 -0
- package/dist/browser/contexts/PayloadDiviner/use.js +10 -4
- package/dist/browser/contexts/PayloadDiviner/use.js.map +1 -1
- package/dist/browser/contexts/index.cjs +182 -0
- package/dist/browser/contexts/index.cjs.map +1 -0
- package/dist/browser/contexts/index.d.cts +3 -0
- package/dist/browser/contexts/index.d.cts.map +1 -0
- package/dist/browser/contexts/index.js +158 -2
- package/dist/browser/contexts/index.js.map +1 -1
- package/dist/browser/index.cjs +182 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +2 -0
- package/dist/browser/index.d.cts.map +1 -0
- package/dist/browser/index.js +158 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/types/images.d.cjs +2 -0
- package/dist/docs.json +1796 -0
- package/dist/node/contexts/DivinedPayload/Context.cjs +32 -0
- package/dist/node/contexts/DivinedPayload/Context.cjs.map +1 -0
- package/dist/node/contexts/DivinedPayload/Context.d.cts +4 -0
- package/dist/node/contexts/DivinedPayload/Context.d.cts.map +1 -0
- package/dist/node/contexts/DivinedPayload/Context.js +5 -28
- package/dist/node/contexts/DivinedPayload/Context.js.map +1 -1
- package/dist/node/contexts/DivinedPayload/Provider.cjs +117 -0
- package/dist/node/contexts/DivinedPayload/Provider.cjs.map +1 -0
- package/dist/node/contexts/DivinedPayload/Provider.d.cts +9 -0
- package/dist/node/contexts/DivinedPayload/Provider.d.cts.map +1 -0
- package/dist/node/contexts/DivinedPayload/Provider.js +73 -47
- package/dist/node/contexts/DivinedPayload/Provider.js.map +1 -1
- package/dist/node/contexts/DivinedPayload/State.cjs +19 -0
- package/dist/node/contexts/DivinedPayload/State.cjs.map +1 -0
- package/dist/node/contexts/DivinedPayload/State.d.cts +9 -0
- package/dist/node/contexts/DivinedPayload/State.d.cts.map +1 -0
- package/dist/node/contexts/DivinedPayload/State.js +0 -16
- package/dist/node/contexts/DivinedPayload/State.js.map +1 -1
- package/dist/node/contexts/DivinedPayload/index.cjs +123 -0
- package/dist/node/contexts/DivinedPayload/index.cjs.map +1 -0
- package/dist/node/contexts/DivinedPayload/index.d.cts +5 -0
- package/dist/node/contexts/DivinedPayload/index.d.cts.map +1 -0
- package/dist/node/contexts/DivinedPayload/index.js +90 -27
- package/dist/node/contexts/DivinedPayload/index.js.map +1 -1
- package/dist/node/contexts/DivinedPayload/use.cjs +38 -0
- package/dist/node/contexts/DivinedPayload/use.cjs.map +1 -0
- package/dist/node/contexts/DivinedPayload/use.d.cts +2 -0
- package/dist/node/contexts/DivinedPayload/use.d.cts.map +1 -0
- package/dist/node/contexts/DivinedPayload/use.js +11 -29
- package/dist/node/contexts/DivinedPayload/use.js.map +1 -1
- package/dist/node/contexts/PayloadDiviner/Context.cjs +32 -0
- package/dist/node/contexts/PayloadDiviner/Context.cjs.map +1 -0
- package/dist/node/contexts/PayloadDiviner/Context.d.cts +5 -0
- package/dist/node/contexts/PayloadDiviner/Context.d.cts.map +1 -0
- package/dist/node/contexts/PayloadDiviner/Context.js +5 -28
- package/dist/node/contexts/PayloadDiviner/Context.js.map +1 -1
- package/dist/node/contexts/PayloadDiviner/Provider.cjs +57 -0
- package/dist/node/contexts/PayloadDiviner/Provider.cjs.map +1 -0
- package/dist/node/contexts/PayloadDiviner/Provider.d.cts +9 -0
- package/dist/node/contexts/PayloadDiviner/Provider.d.cts.map +1 -0
- package/dist/node/contexts/PayloadDiviner/Provider.js +16 -34
- package/dist/node/contexts/PayloadDiviner/Provider.js.map +1 -1
- package/dist/node/contexts/PayloadDiviner/State.cjs +19 -0
- package/dist/node/contexts/PayloadDiviner/State.cjs.map +1 -0
- package/dist/node/contexts/PayloadDiviner/State.d.cts +7 -0
- package/dist/node/contexts/PayloadDiviner/State.d.cts.map +1 -0
- package/dist/node/contexts/PayloadDiviner/State.js +0 -16
- package/dist/node/contexts/PayloadDiviner/State.js.map +1 -1
- package/dist/node/contexts/PayloadDiviner/index.cjs +144 -0
- package/dist/node/contexts/PayloadDiviner/index.cjs.map +1 -0
- package/dist/node/contexts/PayloadDiviner/index.d.cts +5 -0
- package/dist/node/contexts/PayloadDiviner/index.d.cts.map +1 -0
- package/dist/node/contexts/PayloadDiviner/index.js +111 -27
- package/dist/node/contexts/PayloadDiviner/index.js.map +1 -1
- package/dist/node/contexts/PayloadDiviner/use.cjs +117 -0
- package/dist/node/contexts/PayloadDiviner/use.cjs.map +1 -0
- package/dist/node/contexts/PayloadDiviner/use.d.cts +13 -0
- package/dist/node/contexts/PayloadDiviner/use.d.cts.map +1 -0
- package/dist/node/contexts/PayloadDiviner/use.js +32 -52
- package/dist/node/contexts/PayloadDiviner/use.js.map +1 -1
- package/dist/node/contexts/index.cjs +195 -0
- package/dist/node/contexts/index.cjs.map +1 -0
- package/dist/node/contexts/index.d.cts +3 -0
- package/dist/node/contexts/index.d.cts.map +1 -0
- package/dist/node/contexts/index.js +158 -24
- package/dist/node/contexts/index.js.map +1 -1
- package/dist/node/index.cjs +195 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +2 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.js +158 -22
- package/dist/node/index.js.map +1 -1
- package/dist/node/types/images.d.cjs +2 -0
- package/dist/node/types/images.d.js +0 -1
- package/package.json +19 -19
- package/dist/node/contexts/DivinedPayload/Context.mjs +0 -6
- package/dist/node/contexts/DivinedPayload/Provider.mjs +0 -38
- package/dist/node/contexts/DivinedPayload/Provider.mjs.map +0 -1
- package/dist/node/contexts/DivinedPayload/State.mjs +0 -1
- package/dist/node/contexts/DivinedPayload/index.mjs +0 -5
- package/dist/node/contexts/DivinedPayload/index.mjs.map +0 -1
- package/dist/node/contexts/DivinedPayload/use.mjs +0 -7
- package/dist/node/contexts/DivinedPayload/use.mjs.map +0 -1
- package/dist/node/contexts/PayloadDiviner/Context.mjs +0 -6
- package/dist/node/contexts/PayloadDiviner/Provider.mjs +0 -26
- package/dist/node/contexts/PayloadDiviner/Provider.mjs.map +0 -1
- package/dist/node/contexts/PayloadDiviner/State.mjs +0 -1
- package/dist/node/contexts/PayloadDiviner/index.mjs +0 -5
- package/dist/node/contexts/PayloadDiviner/index.mjs.map +0 -1
- package/dist/node/contexts/PayloadDiviner/use.mjs +0 -84
- package/dist/node/contexts/PayloadDiviner/use.mjs.map +0 -1
- package/dist/node/contexts/index.mjs +0 -3
- package/dist/node/contexts/index.mjs.map +0 -1
- package/dist/node/index.mjs +0 -2
- package/dist/node/index.mjs.map +0 -1
- package/dist/node/types/images.d.mjs +0 -1
- package/dist/node/types/images.d.mjs.map +0 -1
- /package/dist/{node/contexts/DivinedPayload/State.mjs.map → browser/types/images.d.cjs.map} +0 -0
- /package/dist/node/{contexts/PayloadDiviner/State.mjs.map → types/images.d.cjs.map} +0 -0
|
@@ -0,0 +1,144 @@
|
|
|
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
|
+
|
|
20
|
+
// src/contexts/PayloadDiviner/index.ts
|
|
21
|
+
var PayloadDiviner_exports = {};
|
|
22
|
+
__export(PayloadDiviner_exports, {
|
|
23
|
+
PayloadDivinerContext: () => PayloadDivinerContext,
|
|
24
|
+
PayloadDivinerProvider: () => PayloadDivinerProvider,
|
|
25
|
+
useDivinePayload: () => useDivinePayload,
|
|
26
|
+
useDivinePayloads: () => useDivinePayloads,
|
|
27
|
+
usePayloadDiviner: () => usePayloadDiviner
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(PayloadDiviner_exports);
|
|
30
|
+
|
|
31
|
+
// src/contexts/PayloadDiviner/Context.ts
|
|
32
|
+
var import_react_shared = require("@xyo-network/react-shared");
|
|
33
|
+
var PayloadDivinerContext = (0, import_react_shared.createContextEx)();
|
|
34
|
+
|
|
35
|
+
// src/contexts/PayloadDiviner/Provider.tsx
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
38
|
+
var PayloadDivinerProvider = ({ diviner: divinerProp, required = false, children }) => {
|
|
39
|
+
const [diviner, setDiviner] = (0, import_react.useState)(divinerProp);
|
|
40
|
+
(0, import_react.useEffect)(() => {
|
|
41
|
+
if (divinerProp) {
|
|
42
|
+
setDiviner(divinerProp);
|
|
43
|
+
}
|
|
44
|
+
}, [divinerProp]);
|
|
45
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
46
|
+
PayloadDivinerContext.Provider,
|
|
47
|
+
{
|
|
48
|
+
value: {
|
|
49
|
+
diviner: diviner === divinerProp ? diviner : void 0,
|
|
50
|
+
provided: true,
|
|
51
|
+
setDiviner
|
|
52
|
+
},
|
|
53
|
+
children: diviner ? children : required ? null : children
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
59
|
+
var import_lodash = require("@xylabs/lodash");
|
|
60
|
+
var import_react_async_effect = require("@xylabs/react-async-effect");
|
|
61
|
+
var import_diviner = require("@xyo-network/diviner");
|
|
62
|
+
var import_react_shared2 = require("@xyo-network/react-shared");
|
|
63
|
+
var import_react2 = require("react");
|
|
64
|
+
var usePayloadDiviner = (required = false) => {
|
|
65
|
+
return (0, import_react_shared2.useContextEx)(PayloadDivinerContext, "PayloadDiviner", required);
|
|
66
|
+
};
|
|
67
|
+
var useDivinePayload = (huri) => {
|
|
68
|
+
const { diviner } = usePayloadDiviner();
|
|
69
|
+
const [payload, setPayload] = (0, import_react2.useState)();
|
|
70
|
+
const [error, setError] = (0, import_react2.useState)();
|
|
71
|
+
(0, import_react2.useEffect)(() => {
|
|
72
|
+
if (diviner) {
|
|
73
|
+
setPayload(void 0);
|
|
74
|
+
}
|
|
75
|
+
}, [diviner]);
|
|
76
|
+
(0, import_react_async_effect.useAsyncEffect)(
|
|
77
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
78
|
+
async (mounted) => {
|
|
79
|
+
if (huri && diviner && payload === void 0) {
|
|
80
|
+
try {
|
|
81
|
+
const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
|
|
82
|
+
const [payload2] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
83
|
+
if (mounted()) {
|
|
84
|
+
setPayload(payload2 ? payload2 : null);
|
|
85
|
+
}
|
|
86
|
+
} catch (ex) {
|
|
87
|
+
if (mounted()) {
|
|
88
|
+
setError(ex);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
[diviner, huri, payload]
|
|
94
|
+
);
|
|
95
|
+
return [payload, setPayload, error];
|
|
96
|
+
};
|
|
97
|
+
var useDivinePayloads = (huriList) => {
|
|
98
|
+
const { diviner } = usePayloadDiviner();
|
|
99
|
+
const [payloads, setPayloads] = (0, import_react2.useState)();
|
|
100
|
+
const [errors, setErrors] = (0, import_react2.useState)();
|
|
101
|
+
(0, import_react2.useEffect)(() => {
|
|
102
|
+
if (diviner) {
|
|
103
|
+
setPayloads(void 0);
|
|
104
|
+
}
|
|
105
|
+
}, [diviner]);
|
|
106
|
+
(0, import_react_async_effect.useAsyncEffect)(
|
|
107
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
108
|
+
async (mounted) => {
|
|
109
|
+
console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`);
|
|
110
|
+
const payloads2 = await Promise.allSettled(
|
|
111
|
+
huriList.map(async (huri) => {
|
|
112
|
+
const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
|
|
113
|
+
const [payload] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
114
|
+
return payload;
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
if (mounted()) {
|
|
118
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
119
|
+
setErrors(
|
|
120
|
+
(0, import_lodash.compact)([...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0))
|
|
121
|
+
);
|
|
122
|
+
if (mounted()) {
|
|
123
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
124
|
+
setErrors(
|
|
125
|
+
(0, import_lodash.compact)(
|
|
126
|
+
[...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0)
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
[diviner, huriList, payloads]
|
|
133
|
+
);
|
|
134
|
+
return [payloads, setPayloads, errors];
|
|
135
|
+
};
|
|
136
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
137
|
+
0 && (module.exports = {
|
|
138
|
+
PayloadDivinerContext,
|
|
139
|
+
PayloadDivinerProvider,
|
|
140
|
+
useDivinePayload,
|
|
141
|
+
useDivinePayloads,
|
|
142
|
+
usePayloadDiviner
|
|
143
|
+
});
|
|
144
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/index.ts","../../../../src/contexts/PayloadDiviner/Context.ts","../../../../src/contexts/PayloadDiviner/Provider.tsx","../../../../src/contexts/PayloadDiviner/use.ts"],"sourcesContent":["export * from './Context'\nexport * from './Provider'\nexport * from './State'\nexport * from './use'\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { PayloadDivinerState } from './State'\n\nconst PayloadDivinerContext = createContextEx<PayloadDivinerState>()\n\nexport { PayloadDivinerContext }\n","import { AbstractPayloadDiviner } from '@xyo-network/diviner'\nimport { ContextExProviderProps } from '@xyo-network/react-shared'\nimport { useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport type PayloadDivinerProviderProps = ContextExProviderProps<{\n /** Required */\n diviner?: AbstractPayloadDiviner\n}>\n\nexport const PayloadDivinerProvider: React.FC<PayloadDivinerProviderProps> = ({ diviner: divinerProp, required = false, children }) => {\n const [diviner, setDiviner] = useState<AbstractPayloadDiviner | undefined>(divinerProp)\n\n useEffect(() => {\n if (divinerProp) {\n setDiviner(divinerProp)\n }\n }, [divinerProp])\n\n return (\n <PayloadDivinerContext.Provider\n value={{\n diviner: diviner === divinerProp ? diviner : undefined,\n provided: true,\n setDiviner,\n }}\n >\n {diviner ? children : required ? null : children}\n </PayloadDivinerContext.Provider>\n )\n}\n","import { compact } from '@xylabs/lodash'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { HuriPayload, HuriSchema } from '@xyo-network/diviner'\nimport { Payload } from '@xyo-network/payload-model'\nimport { useContextEx } from '@xyo-network/react-shared'\nimport { Dispatch, useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport const usePayloadDiviner = (required = false) => {\n return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\nexport const useDivinePayload = <T extends Payload = Payload>(\n huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payload, setPayload] = useState<T | null>()\n const [error, setError] = useState<Error>()\n\n useEffect(() => {\n if (diviner) {\n setPayload(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n if (huri && diviner && payload === undefined) {\n try {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n if (mounted()) {\n setPayload(payload ? (payload as T) : null)\n }\n } catch (ex) {\n if (mounted()) {\n setError(ex as Error)\n }\n }\n }\n },\n [diviner, huri, payload],\n )\n\n return [payload, setPayload, error]\n}\n\nexport const useDivinePayloads = <T extends Payload = Payload>(\n huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payloads, setPayloads] = useState<(T | null)[]>()\n const [errors, setErrors] = useState<Error[]>()\n\n useEffect(() => {\n if (diviner) {\n setPayloads(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n const payloads = await Promise.allSettled(\n huriList.map(async (huri) => {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n return payload\n }),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact([...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined))),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact(\n [...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined)),\n ),\n )\n }\n }\n },\n [diviner, huriList, payloads],\n )\n\n return [payloads, setPayloads, errors]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,0BAAgC;AAIhC,IAAM,4BAAwB,qCAAqC;;;ACFnE,mBAAoC;AAmBhC;AAVG,I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AC,GAAGA,UAAS,OAAO,CAAC,EAAE,IAAI,CAAC,UAAW,MAAM,WAAW,aAAa,MAAM,iBAAiB,EAAE,OAAO,MAAM,OAAO,CAAC,IAAI,MAAU;AAAA,YACnI;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,CAAC,SAAS,UAAU,QAAQ;AAAA,EAC9B;AAEA,SAAO,CAAC,UAAU,aAAa,MAAM;AACvC;","names":["import_react_shared","import_react","payload","payloads"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/contexts/PayloadDiviner/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,OAAO,CAAA"}
|
|
@@ -1,29 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
// src/contexts/PayloadDiviner/Context.ts
|
|
2
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
3
|
+
var PayloadDivinerContext = createContextEx();
|
|
4
|
+
|
|
5
|
+
// src/contexts/PayloadDiviner/Provider.tsx
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
var PayloadDivinerProvider = ({ diviner: divinerProp, required = false, children }) => {
|
|
9
|
+
const [diviner, setDiviner] = useState(divinerProp);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (divinerProp) {
|
|
12
|
+
setDiviner(divinerProp);
|
|
13
|
+
}
|
|
14
|
+
}, [divinerProp]);
|
|
15
|
+
return /* @__PURE__ */ jsx(
|
|
16
|
+
PayloadDivinerContext.Provider,
|
|
17
|
+
{
|
|
18
|
+
value: {
|
|
19
|
+
diviner: diviner === divinerProp ? diviner : void 0,
|
|
20
|
+
provided: true,
|
|
21
|
+
setDiviner
|
|
22
|
+
},
|
|
23
|
+
children: diviner ? children : required ? null : children
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
29
|
+
import { compact } from "@xylabs/lodash";
|
|
30
|
+
import { useAsyncEffect } from "@xylabs/react-async-effect";
|
|
31
|
+
import { HuriSchema } from "@xyo-network/diviner";
|
|
32
|
+
import { useContextEx } from "@xyo-network/react-shared";
|
|
33
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
34
|
+
var usePayloadDiviner = (required = false) => {
|
|
35
|
+
return useContextEx(PayloadDivinerContext, "PayloadDiviner", required);
|
|
36
|
+
};
|
|
37
|
+
var useDivinePayload = (huri) => {
|
|
38
|
+
const { diviner } = usePayloadDiviner();
|
|
39
|
+
const [payload, setPayload] = useState2();
|
|
40
|
+
const [error, setError] = useState2();
|
|
41
|
+
useEffect2(() => {
|
|
42
|
+
if (diviner) {
|
|
43
|
+
setPayload(void 0);
|
|
44
|
+
}
|
|
45
|
+
}, [diviner]);
|
|
46
|
+
useAsyncEffect(
|
|
47
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
48
|
+
async (mounted) => {
|
|
49
|
+
if (huri && diviner && payload === void 0) {
|
|
50
|
+
try {
|
|
51
|
+
const huriPayload = { huri: [huri], schema: HuriSchema };
|
|
52
|
+
const [payload2] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
53
|
+
if (mounted()) {
|
|
54
|
+
setPayload(payload2 ? payload2 : null);
|
|
55
|
+
}
|
|
56
|
+
} catch (ex) {
|
|
57
|
+
if (mounted()) {
|
|
58
|
+
setError(ex);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
[diviner, huri, payload]
|
|
64
|
+
);
|
|
65
|
+
return [payload, setPayload, error];
|
|
66
|
+
};
|
|
67
|
+
var useDivinePayloads = (huriList) => {
|
|
68
|
+
const { diviner } = usePayloadDiviner();
|
|
69
|
+
const [payloads, setPayloads] = useState2();
|
|
70
|
+
const [errors, setErrors] = useState2();
|
|
71
|
+
useEffect2(() => {
|
|
72
|
+
if (diviner) {
|
|
73
|
+
setPayloads(void 0);
|
|
74
|
+
}
|
|
75
|
+
}, [diviner]);
|
|
76
|
+
useAsyncEffect(
|
|
77
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
78
|
+
async (mounted) => {
|
|
79
|
+
console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`);
|
|
80
|
+
const payloads2 = await Promise.allSettled(
|
|
81
|
+
huriList.map(async (huri) => {
|
|
82
|
+
const huriPayload = { huri: [huri], schema: HuriSchema };
|
|
83
|
+
const [payload] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
84
|
+
return payload;
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
if (mounted()) {
|
|
88
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
89
|
+
setErrors(
|
|
90
|
+
compact([...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0))
|
|
91
|
+
);
|
|
92
|
+
if (mounted()) {
|
|
93
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
94
|
+
setErrors(
|
|
95
|
+
compact(
|
|
96
|
+
[...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0)
|
|
97
|
+
)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
[diviner, huriList, payloads]
|
|
103
|
+
);
|
|
104
|
+
return [payloads, setPayloads, errors];
|
|
105
|
+
};
|
|
106
|
+
export {
|
|
107
|
+
PayloadDivinerContext,
|
|
108
|
+
PayloadDivinerProvider,
|
|
109
|
+
useDivinePayload,
|
|
110
|
+
useDivinePayloads,
|
|
111
|
+
usePayloadDiviner
|
|
13
112
|
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
var PayloadDiviner_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(PayloadDiviner_exports);
|
|
18
|
-
__reExport(PayloadDiviner_exports, require("./Context"), module.exports);
|
|
19
|
-
__reExport(PayloadDiviner_exports, require("./Provider"), module.exports);
|
|
20
|
-
__reExport(PayloadDiviner_exports, require("./State"), module.exports);
|
|
21
|
-
__reExport(PayloadDiviner_exports, require("./use"), module.exports);
|
|
22
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
23
|
-
0 && (module.exports = {
|
|
24
|
-
...require("./Context"),
|
|
25
|
-
...require("./Provider"),
|
|
26
|
-
...require("./State"),
|
|
27
|
-
...require("./use")
|
|
28
|
-
});
|
|
29
113
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/
|
|
1
|
+
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/Context.ts","../../../../src/contexts/PayloadDiviner/Provider.tsx","../../../../src/contexts/PayloadDiviner/use.ts"],"sourcesContent":["import { createContextEx } from '@xyo-network/react-shared'\n\nimport { PayloadDivinerState } from './State'\n\nconst PayloadDivinerContext = createContextEx<PayloadDivinerState>()\n\nexport { PayloadDivinerContext }\n","import { AbstractPayloadDiviner } from '@xyo-network/diviner'\nimport { ContextExProviderProps } from '@xyo-network/react-shared'\nimport { useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport type PayloadDivinerProviderProps = ContextExProviderProps<{\n /** Required */\n diviner?: AbstractPayloadDiviner\n}>\n\nexport const PayloadDivinerProvider: React.FC<PayloadDivinerProviderProps> = ({ diviner: divinerProp, required = false, children }) => {\n const [diviner, setDiviner] = useState<AbstractPayloadDiviner | undefined>(divinerProp)\n\n useEffect(() => {\n if (divinerProp) {\n setDiviner(divinerProp)\n }\n }, [divinerProp])\n\n return (\n <PayloadDivinerContext.Provider\n value={{\n diviner: diviner === divinerProp ? diviner : undefined,\n provided: true,\n setDiviner,\n }}\n >\n {diviner ? children : required ? null : children}\n </PayloadDivinerContext.Provider>\n )\n}\n","import { compact } from '@xylabs/lodash'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { HuriPayload, HuriSchema } from '@xyo-network/diviner'\nimport { Payload } from '@xyo-network/payload-model'\nimport { useContextEx } from '@xyo-network/react-shared'\nimport { Dispatch, useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport const usePayloadDiviner = (required = false) => {\n return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\nexport const useDivinePayload = <T extends Payload = Payload>(\n huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payload, setPayload] = useState<T | null>()\n const [error, setError] = useState<Error>()\n\n useEffect(() => {\n if (diviner) {\n setPayload(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n if (huri && diviner && payload === undefined) {\n try {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n if (mounted()) {\n setPayload(payload ? (payload as T) : null)\n }\n } catch (ex) {\n if (mounted()) {\n setError(ex as Error)\n }\n }\n }\n },\n [diviner, huri, payload],\n )\n\n return [payload, setPayload, error]\n}\n\nexport const useDivinePayloads = <T extends Payload = Payload>(\n huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payloads, setPayloads] = useState<(T | null)[]>()\n const [errors, setErrors] = useState<Error[]>()\n\n useEffect(() => {\n if (diviner) {\n setPayloads(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n const payloads = await Promise.allSettled(\n huriList.map(async (huri) => {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n return payload\n }),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact([...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined))),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact(\n [...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined)),\n ),\n )\n }\n }\n },\n [diviner, huriList, payloads],\n )\n\n return [payloads, setPayloads, errors]\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAIhC,IAAM,wBAAwB,gBAAqC;;;ACFnE,SAAS,WAAW,gBAAgB;AAmBhC;AAVG,IAAM,yBAAgE,CAAC,EAAE,SAAS,aAAa,WAAW,OAAO,SAAS,MAAM;AACrI,QAAM,CAAC,SAAS,UAAU,IAAI,SAA6C,WAAW;AAEtF,YAAU,MAAM;AACd,QAAI,aAAa;AACf,iBAAW,WAAW;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,WAAW,CAAC;AAEhB,SACE;AAAA,IAAC,sBAAsB;AAAA,IAAtB;AAAA,MACC,OAAO;AAAA,QACL,SAAS,YAAY,cAAc,UAAU;AAAA,QAC7C,UAAU;AAAA,QACV;AAAA,MACF;AAAA,MAEC,oBAAU,WAAW,WAAW,OAAO;AAAA;AAAA,EAC1C;AAEJ;;;AC/BA,S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
|
|
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
|
+
|
|
20
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
21
|
+
var use_exports = {};
|
|
22
|
+
__export(use_exports, {
|
|
23
|
+
useDivinePayload: () => useDivinePayload,
|
|
24
|
+
useDivinePayloads: () => useDivinePayloads,
|
|
25
|
+
usePayloadDiviner: () => usePayloadDiviner
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(use_exports);
|
|
28
|
+
var import_lodash = require("@xylabs/lodash");
|
|
29
|
+
var import_react_async_effect = require("@xylabs/react-async-effect");
|
|
30
|
+
var import_diviner = require("@xyo-network/diviner");
|
|
31
|
+
var import_react_shared2 = require("@xyo-network/react-shared");
|
|
32
|
+
var import_react = require("react");
|
|
33
|
+
|
|
34
|
+
// src/contexts/PayloadDiviner/Context.ts
|
|
35
|
+
var import_react_shared = require("@xyo-network/react-shared");
|
|
36
|
+
var PayloadDivinerContext = (0, import_react_shared.createContextEx)();
|
|
37
|
+
|
|
38
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
39
|
+
var usePayloadDiviner = (required = false) => {
|
|
40
|
+
return (0, import_react_shared2.useContextEx)(PayloadDivinerContext, "PayloadDiviner", required);
|
|
41
|
+
};
|
|
42
|
+
var useDivinePayload = (huri) => {
|
|
43
|
+
const { diviner } = usePayloadDiviner();
|
|
44
|
+
const [payload, setPayload] = (0, import_react.useState)();
|
|
45
|
+
const [error, setError] = (0, import_react.useState)();
|
|
46
|
+
(0, import_react.useEffect)(() => {
|
|
47
|
+
if (diviner) {
|
|
48
|
+
setPayload(void 0);
|
|
49
|
+
}
|
|
50
|
+
}, [diviner]);
|
|
51
|
+
(0, import_react_async_effect.useAsyncEffect)(
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
async (mounted) => {
|
|
54
|
+
if (huri && diviner && payload === void 0) {
|
|
55
|
+
try {
|
|
56
|
+
const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
|
|
57
|
+
const [payload2] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
58
|
+
if (mounted()) {
|
|
59
|
+
setPayload(payload2 ? payload2 : null);
|
|
60
|
+
}
|
|
61
|
+
} catch (ex) {
|
|
62
|
+
if (mounted()) {
|
|
63
|
+
setError(ex);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
[diviner, huri, payload]
|
|
69
|
+
);
|
|
70
|
+
return [payload, setPayload, error];
|
|
71
|
+
};
|
|
72
|
+
var useDivinePayloads = (huriList) => {
|
|
73
|
+
const { diviner } = usePayloadDiviner();
|
|
74
|
+
const [payloads, setPayloads] = (0, import_react.useState)();
|
|
75
|
+
const [errors, setErrors] = (0, import_react.useState)();
|
|
76
|
+
(0, import_react.useEffect)(() => {
|
|
77
|
+
if (diviner) {
|
|
78
|
+
setPayloads(void 0);
|
|
79
|
+
}
|
|
80
|
+
}, [diviner]);
|
|
81
|
+
(0, import_react_async_effect.useAsyncEffect)(
|
|
82
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
83
|
+
async (mounted) => {
|
|
84
|
+
console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`);
|
|
85
|
+
const payloads2 = await Promise.allSettled(
|
|
86
|
+
huriList.map(async (huri) => {
|
|
87
|
+
const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
|
|
88
|
+
const [payload] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
89
|
+
return payload;
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
if (mounted()) {
|
|
93
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
94
|
+
setErrors(
|
|
95
|
+
(0, import_lodash.compact)([...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0))
|
|
96
|
+
);
|
|
97
|
+
if (mounted()) {
|
|
98
|
+
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
99
|
+
setErrors(
|
|
100
|
+
(0, import_lodash.compact)(
|
|
101
|
+
[...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0)
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
[diviner, huriList, payloads]
|
|
108
|
+
);
|
|
109
|
+
return [payloads, setPayloads, errors];
|
|
110
|
+
};
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
useDivinePayload,
|
|
114
|
+
useDivinePayloads,
|
|
115
|
+
usePayloadDiviner
|
|
116
|
+
});
|
|
117
|
+
//# sourceMappingURL=use.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/use.ts","../../../../src/contexts/PayloadDiviner/Context.ts"],"sourcesContent":["import { compact } from '@xylabs/lodash'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { HuriPayload, HuriSchema } from '@xyo-network/diviner'\nimport { Payload } from '@xyo-network/payload-model'\nimport { useContextEx } from '@xyo-network/react-shared'\nimport { Dispatch, useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport const usePayloadDiviner = (required = false) => {\n return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\nexport const useDivinePayload = <T extends Payload = Payload>(\n huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payload, setPayload] = useState<T | null>()\n const [error, setError] = useState<Error>()\n\n useEffect(() => {\n if (diviner) {\n setPayload(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n if (huri && diviner && payload === undefined) {\n try {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n if (mounted()) {\n setPayload(payload ? (payload as T) : null)\n }\n } catch (ex) {\n if (mounted()) {\n setError(ex as Error)\n }\n }\n }\n },\n [diviner, huri, payload],\n )\n\n return [payload, setPayload, error]\n}\n\nexport const useDivinePayloads = <T extends Payload = Payload>(\n huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payloads, setPayloads] = useState<(T | null)[]>()\n const [errors, setErrors] = useState<Error[]>()\n\n useEffect(() => {\n if (diviner) {\n setPayloads(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n const payloads = await Promise.allSettled(\n huriList.map(async (huri) => {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n return payload\n }),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact([...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined))),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact(\n [...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined)),\n ),\n )\n }\n }\n },\n [diviner, huriList, payloads],\n )\n\n return [payloads, setPayloads, errors]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { PayloadDivinerState } from './State'\n\nconst PayloadDivinerContext = createContextEx<PayloadDivinerState>()\n\nexport { PayloadDivinerContext }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAwB;AACxB,gCAA+B;AAC/B,qBAAwC;AAExC,IAAAA,uBAA6B;AAC7B,mBAA8C;;;ACL9C,0BAAgC;AAIhC,IAAM,4BAAwB,qCAAqC;;;ADK5D,IAAM,oBAAoB,CAAC,WAAW,UAAU;AACrD,aAAO,mCAAa,uBAAuB,kBAAkB,QAAQ;AACvE;AAEO,IAAM,mBAAmB,CAC9B,SAC8E;AAC9E,QAAM,EAAE,QAAQ,IAAI,kBAAkB;AACtC,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAmB;AACjD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAgB;AAE1C,8BAAU,MAAM;AACd,QAAI,SAAS;AACX,iBAAW,MAAS;AAAA,IACtB;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ;AAAA;AAAA,IAEE,OAAO,YAAY;AACjB,UAAI,QAAQ,WAAW,YAAY,QAAW;AAC5C,YAAI;AACF,gBAAM,cAA2B,EAAE,MAAM,CAAC,IAAI,GAAG,QAAQ,0BAAW;AACpE,gBAAM,CAACC,QAAO,IAAK,OAAM,mCAAS,OAAO,CAAC,WAAW,OAAO,CAAC;A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,sBAAY,CAAC,GAAGA,UAAS,OAAO,CAAC,EAAE,IAAI,CAAC,UAAW,MAAM,WAAW,aAAa,OAAO,MAAM,KAAM,CAAiB;AACrH;AAAA,gBACE;AAAA,cACE,CAAC,GAAGA,UAAS,OAAO,CAAC,EAAE,IAAI,CAAC,UAAW,MAAM,WAAW,aAAa,MAAM,iBAAiB,EAAE,OAAO,MAAM,OAAO,CAAC,IAAI,MAAU;AAAA,YACnI;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,CAAC,SAAS,UAAU,QAAQ;AAAA,EAC9B;AAEA,SAAO,CAAC,UAAU,aAAa,MAAM;AACvC;","names":["import_react_shared","payload","payloads"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Dispatch } from 'react';
|
|
2
|
+
export declare const usePayloadDiviner: (required?: boolean) => Omit<import("./State").PayloadDivinerState & import("@xyo-network/react-shared").ContextExState, "provided">;
|
|
3
|
+
export declare const useDivinePayload: <T extends {
|
|
4
|
+
schema: string;
|
|
5
|
+
} & import("@xyo-network/payload-model").PayloadFields = {
|
|
6
|
+
schema: string;
|
|
7
|
+
} & import("@xyo-network/payload-model").PayloadFields>(huri?: string) => [T | null | undefined, Dispatch<T | null | undefined>, Error | undefined];
|
|
8
|
+
export declare const useDivinePayloads: <T extends {
|
|
9
|
+
schema: string;
|
|
10
|
+
} & import("@xyo-network/payload-model").PayloadFields = {
|
|
11
|
+
schema: string;
|
|
12
|
+
} & import("@xyo-network/payload-model").PayloadFields>(huriList: string[]) => [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined];
|
|
13
|
+
//# sourceMappingURL=use.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use.d.ts","sourceRoot":"","sources":["../../../../src/contexts/PayloadDiviner/use.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAuB,MAAM,OAAO,CAAA;AAIrD,eAAO,MAAM,iBAAiB,sIAE7B,CAAA;AAED,eAAO,MAAM,gBAAgB;;;;+DACpB,MAAM,8EAiCd,CAAA;AAED,eAAO,MAAM,iBAAiB;;;;kEAClB,MAAM,EAAE,wFA0CnB,CAAA"}
|
|
@@ -1,53 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
2
|
+
import { compact } from "@xylabs/lodash";
|
|
3
|
+
import { useAsyncEffect } from "@xylabs/react-async-effect";
|
|
4
|
+
import { HuriSchema } from "@xyo-network/diviner";
|
|
5
|
+
import { useContextEx } from "@xyo-network/react-shared";
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
|
|
8
|
+
// src/contexts/PayloadDiviner/Context.ts
|
|
9
|
+
import { createContextEx } from "@xyo-network/react-shared";
|
|
10
|
+
var PayloadDivinerContext = createContextEx();
|
|
11
|
+
|
|
12
|
+
// src/contexts/PayloadDiviner/use.ts
|
|
13
|
+
var usePayloadDiviner = (required = false) => {
|
|
14
|
+
return useContextEx(PayloadDivinerContext, "PayloadDiviner", required);
|
|
9
15
|
};
|
|
10
|
-
var
|
|
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_exports = {};
|
|
20
|
-
__export(use_exports, {
|
|
21
|
-
useDivinePayload: () => useDivinePayload,
|
|
22
|
-
useDivinePayloads: () => useDivinePayloads,
|
|
23
|
-
usePayloadDiviner: () => usePayloadDiviner
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(use_exports);
|
|
26
|
-
var import_lodash = require("@xylabs/lodash");
|
|
27
|
-
var import_react_async_effect = require("@xylabs/react-async-effect");
|
|
28
|
-
var import_diviner = require("@xyo-network/diviner");
|
|
29
|
-
var import_react_shared = require("@xyo-network/react-shared");
|
|
30
|
-
var import_react = require("react");
|
|
31
|
-
var import_Context = require("./Context");
|
|
32
|
-
const usePayloadDiviner = (required = false) => {
|
|
33
|
-
return (0, import_react_shared.useContextEx)(import_Context.PayloadDivinerContext, "PayloadDiviner", required);
|
|
34
|
-
};
|
|
35
|
-
const useDivinePayload = (huri) => {
|
|
16
|
+
var useDivinePayload = (huri) => {
|
|
36
17
|
const { diviner } = usePayloadDiviner();
|
|
37
|
-
const [payload, setPayload] =
|
|
38
|
-
const [error, setError] =
|
|
39
|
-
|
|
18
|
+
const [payload, setPayload] = useState();
|
|
19
|
+
const [error, setError] = useState();
|
|
20
|
+
useEffect(() => {
|
|
40
21
|
if (diviner) {
|
|
41
22
|
setPayload(void 0);
|
|
42
23
|
}
|
|
43
24
|
}, [diviner]);
|
|
44
|
-
|
|
25
|
+
useAsyncEffect(
|
|
45
26
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
27
|
async (mounted) => {
|
|
47
28
|
if (huri && diviner && payload === void 0) {
|
|
48
29
|
try {
|
|
49
|
-
const huriPayload = { huri: [huri], schema:
|
|
50
|
-
const [payload2] = await diviner
|
|
30
|
+
const huriPayload = { huri: [huri], schema: HuriSchema };
|
|
31
|
+
const [payload2] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
51
32
|
if (mounted()) {
|
|
52
33
|
setPayload(payload2 ? payload2 : null);
|
|
53
34
|
}
|
|
@@ -62,35 +43,35 @@ const useDivinePayload = (huri) => {
|
|
|
62
43
|
);
|
|
63
44
|
return [payload, setPayload, error];
|
|
64
45
|
};
|
|
65
|
-
|
|
46
|
+
var useDivinePayloads = (huriList) => {
|
|
66
47
|
const { diviner } = usePayloadDiviner();
|
|
67
|
-
const [payloads, setPayloads] =
|
|
68
|
-
const [errors, setErrors] =
|
|
69
|
-
|
|
48
|
+
const [payloads, setPayloads] = useState();
|
|
49
|
+
const [errors, setErrors] = useState();
|
|
50
|
+
useEffect(() => {
|
|
70
51
|
if (diviner) {
|
|
71
52
|
setPayloads(void 0);
|
|
72
53
|
}
|
|
73
54
|
}, [diviner]);
|
|
74
|
-
|
|
55
|
+
useAsyncEffect(
|
|
75
56
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
76
57
|
async (mounted) => {
|
|
77
58
|
console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`);
|
|
78
59
|
const payloads2 = await Promise.allSettled(
|
|
79
60
|
huriList.map(async (huri) => {
|
|
80
|
-
const huriPayload = { huri: [huri], schema:
|
|
81
|
-
const [payload] = await diviner
|
|
61
|
+
const huriPayload = { huri: [huri], schema: HuriSchema };
|
|
62
|
+
const [payload] = await (diviner == null ? void 0 : diviner.divine([huriPayload])) ?? [];
|
|
82
63
|
return payload;
|
|
83
64
|
})
|
|
84
65
|
);
|
|
85
66
|
if (mounted()) {
|
|
86
67
|
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
87
68
|
setErrors(
|
|
88
|
-
|
|
69
|
+
compact([...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0))
|
|
89
70
|
);
|
|
90
71
|
if (mounted()) {
|
|
91
72
|
setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
|
|
92
73
|
setErrors(
|
|
93
|
-
|
|
74
|
+
compact(
|
|
94
75
|
[...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0)
|
|
95
76
|
)
|
|
96
77
|
);
|
|
@@ -101,10 +82,9 @@ const useDivinePayloads = (huriList) => {
|
|
|
101
82
|
);
|
|
102
83
|
return [payloads, setPayloads, errors];
|
|
103
84
|
};
|
|
104
|
-
|
|
105
|
-
0 && (module.exports = {
|
|
85
|
+
export {
|
|
106
86
|
useDivinePayload,
|
|
107
87
|
useDivinePayloads,
|
|
108
88
|
usePayloadDiviner
|
|
109
|
-
}
|
|
89
|
+
};
|
|
110
90
|
//# sourceMappingURL=use.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/use.ts"],"sourcesContent":["import { compact } from '@xylabs/lodash'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { HuriPayload, HuriSchema } from '@xyo-network/diviner'\nimport { Payload } from '@xyo-network/payload-model'\nimport { useContextEx } from '@xyo-network/react-shared'\nimport { Dispatch, useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport const usePayloadDiviner = (required = false) => {\n return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\nexport const useDivinePayload = <T extends Payload = Payload>(\n huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payload, setPayload] = useState<T | null>()\n const [error, setError] = useState<Error>()\n\n useEffect(() => {\n if (diviner) {\n setPayload(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n if (huri && diviner && payload === undefined) {\n try {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n if (mounted()) {\n setPayload(payload ? (payload as T) : null)\n }\n } catch (ex) {\n if (mounted()) {\n setError(ex as Error)\n }\n }\n }\n },\n [diviner, huri, payload],\n )\n\n return [payload, setPayload, error]\n}\n\nexport const useDivinePayloads = <T extends Payload = Payload>(\n huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payloads, setPayloads] = useState<(T | null)[]>()\n const [errors, setErrors] = useState<Error[]>()\n\n useEffect(() => {\n if (diviner) {\n setPayloads(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n const payloads = await Promise.allSettled(\n huriList.map(async (huri) => {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n return payload\n }),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact([...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined))),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact(\n [...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined)),\n ),\n )\n }\n }\n },\n [diviner, huriList, payloads],\n )\n\n return [payloads, setPayloads, errors]\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../../src/contexts/PayloadDiviner/use.ts","../../../../src/contexts/PayloadDiviner/Context.ts"],"sourcesContent":["import { compact } from '@xylabs/lodash'\nimport { useAsyncEffect } from '@xylabs/react-async-effect'\nimport { HuriPayload, HuriSchema } from '@xyo-network/diviner'\nimport { Payload } from '@xyo-network/payload-model'\nimport { useContextEx } from '@xyo-network/react-shared'\nimport { Dispatch, useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context'\n\nexport const usePayloadDiviner = (required = false) => {\n return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\nexport const useDivinePayload = <T extends Payload = Payload>(\n huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payload, setPayload] = useState<T | null>()\n const [error, setError] = useState<Error>()\n\n useEffect(() => {\n if (diviner) {\n setPayload(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n if (huri && diviner && payload === undefined) {\n try {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n if (mounted()) {\n setPayload(payload ? (payload as T) : null)\n }\n } catch (ex) {\n if (mounted()) {\n setError(ex as Error)\n }\n }\n }\n },\n [diviner, huri, payload],\n )\n\n return [payload, setPayload, error]\n}\n\nexport const useDivinePayloads = <T extends Payload = Payload>(\n huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n const { diviner } = usePayloadDiviner()\n const [payloads, setPayloads] = useState<(T | null)[]>()\n const [errors, setErrors] = useState<Error[]>()\n\n useEffect(() => {\n if (diviner) {\n setPayloads(undefined)\n }\n }, [diviner])\n\n useAsyncEffect(\n // eslint-disable-next-line react-hooks/exhaustive-deps\n async (mounted) => {\n console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n const payloads = await Promise.allSettled(\n huriList.map(async (huri) => {\n const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n const [payload] = (await diviner?.divine([huriPayload])) ?? []\n return payload\n }),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact([...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined))),\n )\n if (mounted()) {\n setPayloads([...payloads.values()].map((value) => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n setErrors(\n compact(\n [...payloads.values()].map((value) => (value.status === 'rejected' ? Error('divine failed', { cause: value.reason }) : undefined)),\n ),\n )\n }\n }\n },\n [diviner, huriList, payloads],\n )\n\n return [payloads, setPayloads, errors]\n}\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { PayloadDivinerState } from './State'\n\nconst PayloadDiv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