@xyo-network/react-payload-diviner 2.64.0-rc.6 → 2.64.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/dist/browser/contexts/DivinedPayload/Context.cjs +28 -0
  2. package/dist/{node/contexts/DivinedPayload/Context.mjs.map → browser/contexts/DivinedPayload/Context.cjs.map} +1 -1
  3. package/dist/browser/contexts/DivinedPayload/Context.d.cts +4 -0
  4. package/dist/browser/contexts/DivinedPayload/Context.d.cts.map +1 -0
  5. package/dist/browser/contexts/DivinedPayload/Provider.cjs +111 -0
  6. package/dist/browser/contexts/DivinedPayload/Provider.cjs.map +1 -0
  7. package/dist/browser/contexts/DivinedPayload/Provider.d.cts +9 -0
  8. package/dist/browser/contexts/DivinedPayload/Provider.d.cts.map +1 -0
  9. package/dist/browser/contexts/DivinedPayload/State.cjs +19 -0
  10. package/dist/browser/contexts/DivinedPayload/State.cjs.map +1 -0
  11. package/dist/browser/contexts/DivinedPayload/State.d.cts +9 -0
  12. package/dist/browser/contexts/DivinedPayload/State.d.cts.map +1 -0
  13. package/dist/browser/contexts/DivinedPayload/index.cjs +115 -0
  14. package/dist/browser/contexts/DivinedPayload/index.cjs.map +1 -0
  15. package/dist/browser/contexts/DivinedPayload/index.d.cts +5 -0
  16. package/dist/browser/contexts/DivinedPayload/index.d.cts.map +1 -0
  17. package/dist/browser/contexts/DivinedPayload/use.cjs +34 -0
  18. package/dist/browser/contexts/DivinedPayload/use.cjs.map +1 -0
  19. package/dist/browser/contexts/DivinedPayload/use.d.cts +2 -0
  20. package/dist/browser/contexts/DivinedPayload/use.d.cts.map +1 -0
  21. package/dist/browser/contexts/PayloadDiviner/Context.cjs +28 -0
  22. package/dist/{node/contexts/PayloadDiviner/Context.mjs.map → browser/contexts/PayloadDiviner/Context.cjs.map} +1 -1
  23. package/dist/browser/contexts/PayloadDiviner/Context.d.cts +5 -0
  24. package/dist/browser/contexts/PayloadDiviner/Context.d.cts.map +1 -0
  25. package/dist/browser/contexts/PayloadDiviner/Provider.cjs +53 -0
  26. package/dist/browser/contexts/PayloadDiviner/Provider.cjs.map +1 -0
  27. package/dist/browser/contexts/PayloadDiviner/Provider.d.cts +9 -0
  28. package/dist/browser/contexts/PayloadDiviner/Provider.d.cts.map +1 -0
  29. package/dist/browser/contexts/PayloadDiviner/State.cjs +19 -0
  30. package/dist/browser/contexts/PayloadDiviner/State.cjs.map +1 -0
  31. package/dist/browser/contexts/PayloadDiviner/State.d.cts +7 -0
  32. package/dist/browser/contexts/PayloadDiviner/State.d.cts.map +1 -0
  33. package/dist/browser/contexts/PayloadDiviner/index.cjs +136 -0
  34. package/dist/browser/contexts/PayloadDiviner/index.cjs.map +1 -0
  35. package/dist/browser/contexts/PayloadDiviner/index.d.cts +5 -0
  36. package/dist/browser/contexts/PayloadDiviner/index.d.cts.map +1 -0
  37. package/dist/browser/contexts/PayloadDiviner/use.cjs +111 -0
  38. package/dist/browser/contexts/PayloadDiviner/use.cjs.map +1 -0
  39. package/dist/browser/contexts/PayloadDiviner/use.d.cts +13 -0
  40. package/dist/browser/contexts/PayloadDiviner/use.d.cts.map +1 -0
  41. package/dist/browser/contexts/index.cjs +182 -0
  42. package/dist/browser/contexts/index.cjs.map +1 -0
  43. package/dist/browser/contexts/index.d.cts +3 -0
  44. package/dist/browser/contexts/index.d.cts.map +1 -0
  45. package/dist/browser/index.cjs +182 -0
  46. package/dist/browser/index.cjs.map +1 -0
  47. package/dist/browser/index.d.cts +2 -0
  48. package/dist/browser/index.d.cts.map +1 -0
  49. package/dist/browser/types/images.d.cjs +2 -0
  50. package/dist/docs.json +1796 -0
  51. package/dist/node/contexts/DivinedPayload/Context.cjs +32 -0
  52. package/dist/node/contexts/DivinedPayload/Context.cjs.map +1 -0
  53. package/dist/node/contexts/DivinedPayload/Context.d.cts +4 -0
  54. package/dist/node/contexts/DivinedPayload/Context.d.cts.map +1 -0
  55. package/dist/node/contexts/DivinedPayload/Context.js +5 -28
  56. package/dist/node/contexts/DivinedPayload/Context.js.map +1 -1
  57. package/dist/node/contexts/DivinedPayload/Provider.cjs +117 -0
  58. package/dist/node/contexts/DivinedPayload/Provider.cjs.map +1 -0
  59. package/dist/node/contexts/DivinedPayload/Provider.d.cts +9 -0
  60. package/dist/node/contexts/DivinedPayload/Provider.d.cts.map +1 -0
  61. package/dist/node/contexts/DivinedPayload/Provider.js +73 -47
  62. package/dist/node/contexts/DivinedPayload/Provider.js.map +1 -1
  63. package/dist/node/contexts/DivinedPayload/State.cjs +19 -0
  64. package/dist/node/contexts/DivinedPayload/State.cjs.map +1 -0
  65. package/dist/node/contexts/DivinedPayload/State.d.cts +9 -0
  66. package/dist/node/contexts/DivinedPayload/State.d.cts.map +1 -0
  67. package/dist/node/contexts/DivinedPayload/State.js +0 -16
  68. package/dist/node/contexts/DivinedPayload/State.js.map +1 -1
  69. package/dist/node/contexts/DivinedPayload/index.cjs +123 -0
  70. package/dist/node/contexts/DivinedPayload/index.cjs.map +1 -0
  71. package/dist/node/contexts/DivinedPayload/index.d.cts +5 -0
  72. package/dist/node/contexts/DivinedPayload/index.d.cts.map +1 -0
  73. package/dist/node/contexts/DivinedPayload/index.js +90 -27
  74. package/dist/node/contexts/DivinedPayload/index.js.map +1 -1
  75. package/dist/node/contexts/DivinedPayload/use.cjs +38 -0
  76. package/dist/node/contexts/DivinedPayload/use.cjs.map +1 -0
  77. package/dist/node/contexts/DivinedPayload/use.d.cts +2 -0
  78. package/dist/node/contexts/DivinedPayload/use.d.cts.map +1 -0
  79. package/dist/node/contexts/DivinedPayload/use.js +11 -29
  80. package/dist/node/contexts/DivinedPayload/use.js.map +1 -1
  81. package/dist/node/contexts/PayloadDiviner/Context.cjs +32 -0
  82. package/dist/node/contexts/PayloadDiviner/Context.cjs.map +1 -0
  83. package/dist/node/contexts/PayloadDiviner/Context.d.cts +5 -0
  84. package/dist/node/contexts/PayloadDiviner/Context.d.cts.map +1 -0
  85. package/dist/node/contexts/PayloadDiviner/Context.js +5 -28
  86. package/dist/node/contexts/PayloadDiviner/Context.js.map +1 -1
  87. package/dist/node/contexts/PayloadDiviner/Provider.cjs +57 -0
  88. package/dist/node/contexts/PayloadDiviner/Provider.cjs.map +1 -0
  89. package/dist/node/contexts/PayloadDiviner/Provider.d.cts +9 -0
  90. package/dist/node/contexts/PayloadDiviner/Provider.d.cts.map +1 -0
  91. package/dist/node/contexts/PayloadDiviner/Provider.js +16 -34
  92. package/dist/node/contexts/PayloadDiviner/Provider.js.map +1 -1
  93. package/dist/node/contexts/PayloadDiviner/State.cjs +19 -0
  94. package/dist/node/contexts/PayloadDiviner/State.cjs.map +1 -0
  95. package/dist/node/contexts/PayloadDiviner/State.d.cts +7 -0
  96. package/dist/node/contexts/PayloadDiviner/State.d.cts.map +1 -0
  97. package/dist/node/contexts/PayloadDiviner/State.js +0 -16
  98. package/dist/node/contexts/PayloadDiviner/State.js.map +1 -1
  99. package/dist/node/contexts/PayloadDiviner/index.cjs +144 -0
  100. package/dist/node/contexts/PayloadDiviner/index.cjs.map +1 -0
  101. package/dist/node/contexts/PayloadDiviner/index.d.cts +5 -0
  102. package/dist/node/contexts/PayloadDiviner/index.d.cts.map +1 -0
  103. package/dist/node/contexts/PayloadDiviner/index.js +111 -27
  104. package/dist/node/contexts/PayloadDiviner/index.js.map +1 -1
  105. package/dist/node/contexts/PayloadDiviner/use.cjs +117 -0
  106. package/dist/node/contexts/PayloadDiviner/use.cjs.map +1 -0
  107. package/dist/node/contexts/PayloadDiviner/use.d.cts +13 -0
  108. package/dist/node/contexts/PayloadDiviner/use.d.cts.map +1 -0
  109. package/dist/node/contexts/PayloadDiviner/use.js +32 -52
  110. package/dist/node/contexts/PayloadDiviner/use.js.map +1 -1
  111. package/dist/node/contexts/index.cjs +195 -0
  112. package/dist/node/contexts/index.cjs.map +1 -0
  113. package/dist/node/contexts/index.d.cts +3 -0
  114. package/dist/node/contexts/index.d.cts.map +1 -0
  115. package/dist/node/contexts/index.js +158 -24
  116. package/dist/node/contexts/index.js.map +1 -1
  117. package/dist/node/index.cjs +195 -0
  118. package/dist/node/index.cjs.map +1 -0
  119. package/dist/node/index.d.cts +2 -0
  120. package/dist/node/index.d.cts.map +1 -0
  121. package/dist/node/index.js +158 -22
  122. package/dist/node/index.js.map +1 -1
  123. package/dist/node/types/images.d.cjs +2 -0
  124. package/dist/node/types/images.d.js +0 -1
  125. package/package.json +19 -19
  126. package/dist/node/contexts/DivinedPayload/Context.mjs +0 -6
  127. package/dist/node/contexts/DivinedPayload/Provider.mjs +0 -38
  128. package/dist/node/contexts/DivinedPayload/Provider.mjs.map +0 -1
  129. package/dist/node/contexts/DivinedPayload/State.mjs +0 -1
  130. package/dist/node/contexts/DivinedPayload/index.mjs +0 -5
  131. package/dist/node/contexts/DivinedPayload/index.mjs.map +0 -1
  132. package/dist/node/contexts/DivinedPayload/use.mjs +0 -7
  133. package/dist/node/contexts/DivinedPayload/use.mjs.map +0 -1
  134. package/dist/node/contexts/PayloadDiviner/Context.mjs +0 -6
  135. package/dist/node/contexts/PayloadDiviner/Provider.mjs +0 -26
  136. package/dist/node/contexts/PayloadDiviner/Provider.mjs.map +0 -1
  137. package/dist/node/contexts/PayloadDiviner/State.mjs +0 -1
  138. package/dist/node/contexts/PayloadDiviner/index.mjs +0 -5
  139. package/dist/node/contexts/PayloadDiviner/index.mjs.map +0 -1
  140. package/dist/node/contexts/PayloadDiviner/use.mjs +0 -84
  141. package/dist/node/contexts/PayloadDiviner/use.mjs.map +0 -1
  142. package/dist/node/contexts/index.mjs +0 -3
  143. package/dist/node/contexts/index.mjs.map +0 -1
  144. package/dist/node/index.mjs +0 -2
  145. package/dist/node/index.mjs.map +0 -1
  146. package/dist/node/types/images.d.mjs +0 -1
  147. package/dist/node/types/images.d.mjs.map +0 -1
  148. /package/dist/{node/contexts/DivinedPayload/State.mjs.map → browser/types/images.d.cjs.map} +0 -0
  149. /package/dist/node/{contexts/PayloadDiviner/State.mjs.map → types/images.d.cjs.map} +0 -0
@@ -0,0 +1,136 @@
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?.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?.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
+ //# 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,IAAM,yBAAgE,CAAC,EAAE,SAAS,aAAa,WAAW,OAAO,SAAS,MAAM;AACrI,QAAM,CAAC,SAAS,UAAU,QAAI,uBAA6C,WAAW;AAEtF,8BAAU,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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 export * from './Provider';
3
+ export * from './State';
4
+ export * from './use';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -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"}
@@ -0,0 +1,111 @@
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/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?.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?.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
+ //# 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,IA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MC,YAAW,MAAM,QAAQ;AAAA,QAC7B,SAAS,IAAI,OAAO,SAAS;AAC3B,gBAAM,cAA2B,EAAE,MAAM,CAAC,IAAI,GAAG,QAAQ,0BAAW;AACpE,gBAAM,CAAC,OAAO,IAAK,MAAM,SAAS,OAAO,CAAC,WAAW,CAAC,KAAM,CAAC;AAC7D,iBAAO;AAAA,QACT,CAAC;AAAA,MACH;AACA,UAAI,QAAQ,GAAG;AACb,oBAAY,CAAC,GAAGA,UAAS,OAAO,CAAC,EAAE,IAAI,CAAC,UAAW,MAAM,WAAW,aAAa,OAAO,MAAM,KAAM,CAAiB;AACrH;AAAA,cACE,uBAAQ,CAAC,GAAGA,UAAS,OAAO,CAAC,EAAE,IAAI,CAAC,UAAW,MAAM,WAAW,aAAa,MAAM,iBAAiB,EAAE,OAAO,MAAM,OAAO,CAAC,IAAI,MAAU,CAAC;AAAA,QAC5I;AACA,YAAI,QAAQ,GAAG;AACb,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"}
@@ -0,0 +1,182 @@
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/index.ts
21
+ var contexts_exports = {};
22
+ __export(contexts_exports, {
23
+ DivinedPayloadContext: () => DivinedPayloadContext,
24
+ DivinedPayloadProvider: () => DivinedPayloadProvider,
25
+ DivinedPayloadWithHandleInner: () => DivinedPayloadWithHandleInner,
26
+ DivinedPayloadWithHandleProvider: () => DivinedPayloadWithHandleProvider,
27
+ PayloadDivinerContext: () => PayloadDivinerContext,
28
+ PayloadDivinerProvider: () => PayloadDivinerProvider,
29
+ useDivinePayload: () => useDivinePayload,
30
+ useDivinePayloads: () => useDivinePayloads,
31
+ useDivinedPayload: () => useDivinedPayload,
32
+ usePayloadDiviner: () => usePayloadDiviner
33
+ });
34
+ module.exports = __toCommonJS(contexts_exports);
35
+
36
+ // src/contexts/DivinedPayload/Context.tsx
37
+ var import_react_shared = require("@xyo-network/react-shared");
38
+ var DivinedPayloadContext = (0, import_react_shared.createContextEx)();
39
+
40
+ // src/contexts/DivinedPayload/Provider.tsx
41
+ var import_payload_model = require("@xyo-network/payload-model");
42
+ var import_react_error = require("@xyo-network/react-error");
43
+ var import_react_payload_huri = require("@xyo-network/react-payload-huri");
44
+ var import_react_router_dom = require("react-router-dom");
45
+
46
+ // src/contexts/PayloadDiviner/Context.ts
47
+ var import_react_shared2 = require("@xyo-network/react-shared");
48
+ var PayloadDivinerContext = (0, import_react_shared2.createContextEx)();
49
+
50
+ // src/contexts/PayloadDiviner/Provider.tsx
51
+ var import_react = require("react");
52
+ var import_jsx_runtime = require("react/jsx-runtime");
53
+ var PayloadDivinerProvider = ({ diviner: divinerProp, required = false, children }) => {
54
+ const [diviner, setDiviner] = (0, import_react.useState)(divinerProp);
55
+ (0, import_react.useEffect)(() => {
56
+ if (divinerProp) {
57
+ setDiviner(divinerProp);
58
+ }
59
+ }, [divinerProp]);
60
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
61
+ PayloadDivinerContext.Provider,
62
+ {
63
+ value: {
64
+ diviner: diviner === divinerProp ? diviner : void 0,
65
+ provided: true,
66
+ setDiviner
67
+ },
68
+ children: diviner ? children : required ? null : children
69
+ }
70
+ );
71
+ };
72
+
73
+ // src/contexts/PayloadDiviner/use.ts
74
+ var import_lodash = require("@xylabs/lodash");
75
+ var import_react_async_effect = require("@xylabs/react-async-effect");
76
+ var import_diviner = require("@xyo-network/diviner");
77
+ var import_react_shared3 = require("@xyo-network/react-shared");
78
+ var import_react2 = require("react");
79
+ var usePayloadDiviner = (required = false) => {
80
+ return (0, import_react_shared3.useContextEx)(PayloadDivinerContext, "PayloadDiviner", required);
81
+ };
82
+ var useDivinePayload = (huri) => {
83
+ const { diviner } = usePayloadDiviner();
84
+ const [payload, setPayload] = (0, import_react2.useState)();
85
+ const [error, setError] = (0, import_react2.useState)();
86
+ (0, import_react2.useEffect)(() => {
87
+ if (diviner) {
88
+ setPayload(void 0);
89
+ }
90
+ }, [diviner]);
91
+ (0, import_react_async_effect.useAsyncEffect)(
92
+ // eslint-disable-next-line react-hooks/exhaustive-deps
93
+ async (mounted) => {
94
+ if (huri && diviner && payload === void 0) {
95
+ try {
96
+ const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
97
+ const [payload2] = await diviner?.divine([huriPayload]) ?? [];
98
+ if (mounted()) {
99
+ setPayload(payload2 ? payload2 : null);
100
+ }
101
+ } catch (ex) {
102
+ if (mounted()) {
103
+ setError(ex);
104
+ }
105
+ }
106
+ }
107
+ },
108
+ [diviner, huri, payload]
109
+ );
110
+ return [payload, setPayload, error];
111
+ };
112
+ var useDivinePayloads = (huriList) => {
113
+ const { diviner } = usePayloadDiviner();
114
+ const [payloads, setPayloads] = (0, import_react2.useState)();
115
+ const [errors, setErrors] = (0, import_react2.useState)();
116
+ (0, import_react2.useEffect)(() => {
117
+ if (diviner) {
118
+ setPayloads(void 0);
119
+ }
120
+ }, [diviner]);
121
+ (0, import_react_async_effect.useAsyncEffect)(
122
+ // eslint-disable-next-line react-hooks/exhaustive-deps
123
+ async (mounted) => {
124
+ console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`);
125
+ const payloads2 = await Promise.allSettled(
126
+ huriList.map(async (huri) => {
127
+ const huriPayload = { huri: [huri], schema: import_diviner.HuriSchema };
128
+ const [payload] = await diviner?.divine([huriPayload]) ?? [];
129
+ return payload;
130
+ })
131
+ );
132
+ if (mounted()) {
133
+ setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
134
+ setErrors(
135
+ (0, import_lodash.compact)([...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0))
136
+ );
137
+ if (mounted()) {
138
+ setPayloads([...payloads2.values()].map((value) => value.status === "rejected" ? null : value.value));
139
+ setErrors(
140
+ (0, import_lodash.compact)(
141
+ [...payloads2.values()].map((value) => value.status === "rejected" ? Error("divine failed", { cause: value.reason }) : void 0)
142
+ )
143
+ );
144
+ }
145
+ }
146
+ },
147
+ [diviner, huriList, payloads]
148
+ );
149
+ return [payloads, setPayloads, errors];
150
+ };
151
+
152
+ // src/contexts/DivinedPayload/use.ts
153
+ var import_react_shared4 = require("@xyo-network/react-shared");
154
+ var useDivinedPayload = () => (0, import_react_shared4.useContextEx)(DivinedPayloadContext, "DivinedPayload", true);
155
+
156
+ // src/contexts/DivinedPayload/Provider.tsx
157
+ var import_jsx_runtime2 = require("react/jsx-runtime");
158
+ var DivinedPayloadProvider = ({ children, hash }) => {
159
+ const { hash: hashParam } = (0, import_react_router_dom.useParams)();
160
+ const huriFromHashParam = (0, import_react_payload_huri.useBuildHuri)(hashParam);
161
+ const [params] = (0, import_react_router_dom.useSearchParams)();
162
+ const huriSearchParameter = params.get("huri");
163
+ const decodedHuriParam = decodeURIComponent(huriSearchParameter ?? "");
164
+ const huriUri = hash ?? (decodedHuriParam ? decodedHuriParam : huriFromHashParam);
165
+ const [payload, setPayload, payloadError] = useDivinePayload(huriUri);
166
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DivinedPayloadContext.Provider, { value: { payload, payloadError, provided: true, setPayload }, children });
167
+ };
168
+ var DivinedPayloadWithHandleInner = ({ children }) => {
169
+ const { payloadError } = useDivinedPayload();
170
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
171
+ import_react_error.ErrorRender,
172
+ {
173
+ error: payloadError ? { message: payloadError.message, schema: import_payload_model.ModuleErrorSchema, sources: [] } : void 0,
174
+ errorContext: "Divined Payload Provider",
175
+ children
176
+ }
177
+ );
178
+ };
179
+ var DivinedPayloadWithHandleProvider = ({ children, ...props }) => {
180
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DivinedPayloadProvider, { ...props, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DivinedPayloadWithHandleInner, { children }) });
181
+ };
182
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/contexts/index.ts","../../../src/contexts/DivinedPayload/Context.tsx","../../../src/contexts/DivinedPayload/Provider.tsx","../../../src/contexts/PayloadDiviner/Context.ts","../../../src/contexts/PayloadDiviner/Provider.tsx","../../../src/contexts/PayloadDiviner/use.ts","../../../src/contexts/DivinedPayload/use.ts"],"sourcesContent":["export * from './DivinedPayload'\nexport * from './PayloadDiviner'\n","import { createContextEx } from '@xyo-network/react-shared'\n\nimport { DivinedPayloadState } from './State'\n\nexport const DivinedPayloadContext = createContextEx<DivinedPayloadState>()\n","import type { WithChildren } from '@xylabs/react-shared'\nimport { ModuleErrorSchema } from '@xyo-network/payload-model'\nimport { ErrorRender } from '@xyo-network/react-error'\nimport { useBuildHuri } from '@xyo-network/react-payload-huri'\nimport { useParams, useSearchParams } from 'react-router-dom'\n\nimport { useDivinePayload } from '../PayloadDiviner'\nimport { DivinedPayloadContext } from './Context'\nimport { useDivinedPayload } from './use'\n\nexport interface DivinedPayloadProviderProps extends WithChildren {\n hash?: string\n}\n\nexport const DivinedPayloadProvider: React.FC<DivinedPayloadProviderProps> = ({ children, hash }) => {\n const { hash: hashParam } = useParams()\n\n const huriFromHashParam = useBuildHuri(hashParam)\n\n const [params] = useSearchParams()\n const huriSearchParameter = params.get('huri')\n const decodedHuriParam = decodeURIComponent(huriSearchParameter ?? '')\n const huriUri = hash ?? (decodedHuriParam ? decodedHuriParam : huriFromHashParam)\n\n const [payload, setPayload, payloadError] = useDivinePayload(huriUri)\n\n return <DivinedPayloadContext.Provider value={{ payload, payloadError, provided: true, setPayload }}>{children}</DivinedPayloadContext.Provider>\n}\n\nexport const DivinedPayloadWithHandleInner: React.FC<WithChildren> = ({ children }) => {\n const { payloadError } = useDivinedPayload()\n\n return (\n <ErrorRender\n error={payloadError ? { message: payloadError.message, schema: ModuleErrorSchema, sources: [] } : undefined}\n errorContext=\"Divined Payload Provider\"\n >\n {children}\n </ErrorRender>\n )\n}\n\nexport const DivinedPayloadWithHandleProvider: React.FC<DivinedPayloadProviderProps> = ({ children, ...props }) => {\n return (\n <DivinedPayloadProvider {...props}>\n <DivinedPayloadWithHandleInner>{children}</DivinedPayloadWithHandleInner>\n </DivinedPayloadProvider>\n )\n}\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","import { useContextEx } from '@xyo-network/react-shared'\n\nimport { DivinedPayloadContext } from './Context'\n\nexport const useDivinedPayload = () => useContextEx(DivinedPayloadContext, 'DivinedPayload', true)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,0BAAgC;AAIzB,IAAM,4BAAwB,qCAAqC;;;ACH1E,2BAAkC;AAClC,yBAA4B;AAC5B,gCAA6B;AAC7B,8BAA2C;;;ACJ3C,IAAAA,uBAAgC;AAIhC,IAAM,4BAAwB,sCAAqC;;;ACFnE,mBAAoC;AAmBhC;AAVG,IAAM,yBAAgE,CAAC,EAAE,SAAS,aAAa,WAAW,OAAO,SAAS,MAAM;AACrI,QAAM,CAAC,SAAS,UAAU,QAAI,uBAA6C,WAAW;AAEtF,8BAAU,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,oBAAwB;AACxB,gCAA+B;AAC/B,qBAAwC;AAExC,IA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AAI,mCAAU;AAEtC,QAAM,wBAAoB,wCAAa,SAAS;AAEhD,QAAM,CAAC,MAAM,QAAI,yCAAgB;AACjC,QAAM,sBAAsB,OAAO,IAAI,MAAM;AAC7C,QAAM,mBAAmB,mBAAmB,uBAAuB,EAAE;AACrE,QAAM,UAAU,SAAS,mBAAmB,mBAAmB;AAE/D,QAAM,CAAC,SAAS,YAAY,YAAY,IAAI,iBAAiB,OAAO;AAEpE,SAAO,6CAAC,sBAAsB,UAAtB,EAA+B,OAAO,EAAE,SAAS,cAAc,UAAU,MAAM,WAAW,GAAI,UAAS;AACjH;AAEO,IAAM,gCAAwD,CAAC,EAAE,SAAS,MAAM;AACrF,QAAM,EAAE,aAAa,IAAI,kBAAkB;AAE3C,SACE;AAAA,IAAC;AAAA;AAAA,MACC,OAAO,eAAe,EAAE,SAAS,aAAa,SAAS,QAAQ,wCAAmB,SAAS,CAAC,EAAE,IAAI;AAAA,MAClG,cAAa;AAAA,MAEZ;AAAA;AAAA,EACH;AAEJ;AAEO,IAAM,mCAA0E,CAAC,EAAE,UAAU,GAAG,MAAM,MAAM;AACjH,SACE,6CAAC,0BAAwB,GAAG,OAC1B,uDAAC,iCAA+B,UAAS,GAC3C;AAEJ;","names":["import_react_shared","import_react_shared","import_react","payload","payloads","import_react_shared","import_jsx_runtime"]}
@@ -0,0 +1,3 @@
1
+ export * from './DivinedPayload';
2
+ export * from './PayloadDiviner';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/contexts/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA"}