react-sync-ui 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/react-sync-ui.cjs.development.js +23 -11
- package/dist/react-sync-ui.cjs.development.js.map +1 -1
- package/dist/react-sync-ui.cjs.production.min.js +1 -1
- package/dist/react-sync-ui.cjs.production.min.js.map +1 -1
- package/dist/react-sync-ui.esm.js +23 -11
- package/dist/react-sync-ui.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/syncUI.tsx +6 -2
package/README.md
CHANGED
|
@@ -10,11 +10,14 @@ var React__default = _interopDefault(React);
|
|
|
10
10
|
var useComponentDidMount = function useComponentDidMount(fn) {
|
|
11
11
|
React.useEffect(fn, []);
|
|
12
12
|
};
|
|
13
|
+
|
|
13
14
|
var usePromiseQueue = function usePromiseQueue() {
|
|
14
15
|
var _asyncQueue$;
|
|
16
|
+
|
|
15
17
|
var _useState = React.useState([]),
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
asyncQueue = _useState[0],
|
|
19
|
+
setAsyncQueue = _useState[1];
|
|
20
|
+
|
|
18
21
|
var push = React.useCallback(function (data) {
|
|
19
22
|
return new Promise(function (resolve, reject) {
|
|
20
23
|
return setAsyncQueue(function (p) {
|
|
@@ -29,7 +32,7 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
29
32
|
var resolveHeadItem = React.useCallback(function (value) {
|
|
30
33
|
setAsyncQueue(function (queue) {
|
|
31
34
|
var first = queue[0],
|
|
32
|
-
|
|
35
|
+
rest = queue.slice(1);
|
|
33
36
|
first == null ? void 0 : first.resolve(value);
|
|
34
37
|
return rest;
|
|
35
38
|
});
|
|
@@ -37,7 +40,7 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
37
40
|
var rejectHeadItem = React.useCallback(function (reason) {
|
|
38
41
|
setAsyncQueue(function (queue) {
|
|
39
42
|
var first = queue[0],
|
|
40
|
-
|
|
43
|
+
rest = queue.slice(1);
|
|
41
44
|
first == null ? void 0 : first.reject(reason);
|
|
42
45
|
return rest;
|
|
43
46
|
});
|
|
@@ -50,8 +53,8 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
50
53
|
} : undefined,
|
|
51
54
|
push: push
|
|
52
55
|
};
|
|
53
|
-
};
|
|
54
|
-
|
|
56
|
+
}; // ------------------------------------------------------------------------------------
|
|
57
|
+
|
|
55
58
|
var syncUIFactory = function syncUIFactory() {
|
|
56
59
|
var mutSyncUIComponentsRenderQueue = [];
|
|
57
60
|
/*
|
|
@@ -60,15 +63,19 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
60
63
|
"<SyncUI /> has to be initialized only once"
|
|
61
64
|
);
|
|
62
65
|
*/
|
|
66
|
+
|
|
63
67
|
return {
|
|
64
68
|
makeSyncUI: function makeSyncUI(SyncUIUserComp) {
|
|
65
69
|
var _ref, _SyncUIUserComp$displ;
|
|
70
|
+
|
|
66
71
|
var _debugName = (_ref = (_SyncUIUserComp$displ = SyncUIUserComp.displayName) != null ? _SyncUIUserComp$displ : SyncUIUserComp.name) != null ? _ref : "uniqSymbolMessageType";
|
|
67
|
-
|
|
68
|
-
// object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called
|
|
72
|
+
|
|
73
|
+
var syncUIComponentType = Symbol(_debugName); // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called
|
|
74
|
+
|
|
69
75
|
var singletonSyncUIRef = {
|
|
70
76
|
push: undefined
|
|
71
77
|
};
|
|
78
|
+
|
|
72
79
|
var SyncUISingletonComponent = function SyncUISingletonComponent(props) {
|
|
73
80
|
useComponentDidMount(function () {
|
|
74
81
|
singletonSyncUIRef.push = props.push;
|
|
@@ -79,17 +86,21 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
79
86
|
if (!props.head) return null;
|
|
80
87
|
if (props.head.data.type !== syncUIComponentType) return null;
|
|
81
88
|
return React__default.createElement(SyncUIUserComp, {
|
|
89
|
+
key: props.head.data.reactCompKey,
|
|
82
90
|
data: props.head.data.inputData,
|
|
83
91
|
resolve: props.head.resolve,
|
|
84
92
|
reject: props.head.reject
|
|
85
93
|
});
|
|
86
94
|
};
|
|
95
|
+
|
|
87
96
|
mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);
|
|
88
97
|
return function (input) {
|
|
89
98
|
if (!singletonSyncUIRef.push) throw new Error("You have to initialize <SyncUI />");
|
|
99
|
+
var reactCompKey = Math.random().toString();
|
|
90
100
|
return singletonSyncUIRef.push({
|
|
91
101
|
type: syncUIComponentType,
|
|
92
|
-
inputData: input
|
|
102
|
+
inputData: input,
|
|
103
|
+
reactCompKey: reactCompKey
|
|
93
104
|
});
|
|
94
105
|
};
|
|
95
106
|
},
|
|
@@ -105,9 +116,10 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
105
116
|
};
|
|
106
117
|
|
|
107
118
|
var syncUIFactory$1 = syncUIFactory;
|
|
119
|
+
|
|
108
120
|
var _syncUIFactory2 = /*#__PURE__*/syncUIFactory$1(),
|
|
109
|
-
|
|
110
|
-
|
|
121
|
+
makeSyncUI = _syncUIFactory2.makeSyncUI,
|
|
122
|
+
SyncUI = _syncUIFactory2.SyncUI;
|
|
111
123
|
|
|
112
124
|
exports.SyncUI = SyncUI;
|
|
113
125
|
exports.makeSyncUI = makeSyncUI;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-sync-ui.cjs.development.js","sources":["../src/syncUI.tsx","../src/index.ts"],"sourcesContent":["import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n","import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n"],"names":["useComponentDidMount","fn","useEffect","usePromiseQueue","
|
|
1
|
+
{"version":3,"file":"react-sync-ui.cjs.development.js","sources":["../src/syncUI.tsx","../src/index.ts"],"sourcesContent":["import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData; reactCompKey: string },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n key={props.head.data.reactCompKey}\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n\n const reactCompKey = Math.random().toString();\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input,\n reactCompKey\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n","import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n"],"names":["useComponentDidMount","fn","useEffect","usePromiseQueue","useState","asyncQueue","setAsyncQueue","push","useCallback","data","Promise","resolve","reject","p","resolveHeadItem","value","queue","first","rest","rejectHeadItem","reason","head","undefined","syncUIFactory","mutSyncUIComponentsRenderQueue","makeSyncUI","SyncUIUserComp","_debugName","displayName","name","syncUIComponentType","Symbol","singletonSyncUIRef","SyncUISingletonComponent","props","type","React","key","reactCompKey","inputData","input","Error","Math","random","toString","SyncUI","map","SyncComp","Fragment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AA,OAAO,EAAEuB,KAAK,CAACb,IAAN,CAAWV;AACpBC,UAAAA,MAAM,EAAEsB,KAAK,CAACb,IAAN,CAAWT;SAJrB,CADF;AAQD,OAjBD;;AAmBAY,MAAAA,8BAA8B,CAACjB,IAA/B,CAAoC0B,wBAApC;AAEA,aAAO,UAACO,KAAD;AACL,YAAI,CAACR,kBAAkB,CAACzB,IAAxB,EACE,MAAM,IAAIkC,KAAJ,qCAAN;AAEF,YAAMH,YAAY,GAAGI,IAAI,CAACC,MAAL,GAAcC,QAAd,EAArB;AACA,eAAOZ,kBAAkB,CAACzB,IAAnB,CAAwB;AAC7B4B,UAAAA,IAAI,EAAEL,mBADuB;AAE7BS,UAAAA,SAAS,EAAEC,KAFkB;AAG7BF,UAAAA,YAAY,EAAZA;AAH6B,SAAxB,CAAP;AAKD,OAVD;AAWD,KAzDI;AA0DLO,IAAAA,MAAM,EAAE;AACN,UAAM7B,KAAK,GAAGb,eAAe,EAA7B;AACA,aACEiC,4BAAA,wBAAA,MAAA,EAEGZ,8BAA8B,CAACsB,GAA/B,CAAmC,UAACC,QAAD,EAAWV,GAAX;AAAA,eAClCD,4BAAA,CAACA,cAAK,CAACY,QAAP;AAAgBX,UAAAA,GAAG,EAAEA;SAArB,EACED,4BAAA,CAACW,QAAD,oBAAc/B,MAAd,CADF,CADkC;AAAA,OAAnC,CAFH,CADF;AAUD;AAtEI,GAAP;AAwED,CApFM;;ICjFMO,eAAa,GAAG0B;;AACtB,mCAA+B1B,eAAa,EAA5C;AAAA,IAAQE,UAAR,mBAAQA,UAAR;AAAA,IAAoBoB,MAApB,mBAAoBA,MAApB;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=function(){var e=[];return{makeSyncUI:function(r){var u,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=function(){var e=[];return{makeSyncUI:function(r){var a,u,c=null!=(a=null!=(u=r.displayName)?u:r.name)?a:"uniqSymbolMessageType",o=Symbol(c),l={push:void 0};return e.push((function(e){return t.useEffect((function(){return l.push=e.push,function(){return l.push=void 0}}),[]),e.head?e.head.data.type!==o?null:n.createElement(r,{key:e.head.data.reactCompKey,data:e.head.data.inputData,resolve:e.head.resolve,reject:e.head.reject}):null})),function(e){if(!l.push)throw new Error("You have to initialize <SyncUI />");var t=Math.random().toString();return l.push({type:o,inputData:e,reactCompKey:t})}},SyncUI:function(){var r,a,u,c,o,l,i,s=(u=(a=t.useState([]))[0],c=a[1],o=t.useCallback((function(e){return new Promise((function(t,n){return c((function(r){return[].concat(r,[{data:e,resolve:t,reject:n}])}))}))}),[]),l=t.useCallback((function(e){c((function(t){var n=t[0],r=t.slice(1);return null==n||n.resolve(e),r}))}),[]),i=t.useCallback((function(e){c((function(t){var n=t[0],r=t.slice(1);return null==n||n.reject(e),r}))}),[]),{head:u[0]?{data:null==(r=u[0])?void 0:r.data,resolve:l,reject:i}:void 0,push:o});return n.createElement(n.Fragment,null,e.map((function(e,t){return n.createElement(n.Fragment,{key:t},n.createElement(e,Object.assign({},s)))})))}}},a=r(),u=a.makeSyncUI;exports.SyncUI=a.SyncUI,exports.makeSyncUI=u,exports.syncUIFactory=r;
|
|
2
2
|
//# sourceMappingURL=react-sync-ui.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-sync-ui.cjs.production.min.js","sources":["../src/index.ts","../src/syncUI.tsx"],"sourcesContent":["import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n","import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n"],"names":["syncUIFactory","mutSyncUIComponentsRenderQueue","makeSyncUI","SyncUIUserComp","_debugName","
|
|
1
|
+
{"version":3,"file":"react-sync-ui.cjs.production.min.js","sources":["../src/index.ts","../src/syncUI.tsx"],"sourcesContent":["import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n","import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData; reactCompKey: string },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n key={props.head.data.reactCompKey}\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n\n const reactCompKey = Math.random().toString();\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input,\n reactCompKey\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n"],"names":["syncUIFactory","mutSyncUIComponentsRenderQueue","makeSyncUI","SyncUIUserComp","_debugName","displayName","name","syncUIComponentType","Symbol","singletonSyncUIRef","push","undefined","props","useEffect","head","data","type","React","key","reactCompKey","inputData","resolve","reject","input","Error","Math","random","toString","SyncUI","asyncQueue","setAsyncQueue","resolveHeadItem","rejectHeadItem","queue","useState","useCallback","Promise","p","value","first","rest","reason","_asyncQueue$","map","SyncComp","Fragment"],"mappings":"oJACaA,ECiFgB,eACrBC,EAAiC,SAWhC,CACLC,WAAY,SACVC,WAWMC,oBACJD,EAAeE,eACfF,EAAeG,QACf,wBAEIC,EAAsBC,OAAOJ,GAG7BK,EAAqB,CACzBC,UAAMC,UAsBRV,EAA+BS,MAnBE,SAACE,UApHtCC,aAqH2B,kBACnBJ,EAAmBC,KAAOE,EAAMF,KACzB,kBAAOD,EAAmBC,UAAOC,KAvHlC,IA0HHC,EAAME,KACPF,EAAME,KAAKC,KAAKC,OAAST,EAA4B,KAGvDU,gBAACd,GACCe,IAAKN,EAAME,KAAKC,KAAKI,aACrBJ,KAAMH,EAAME,KAAKC,KAAKK,UACtBC,QAAST,EAAME,KAAKO,QACpBC,OAAQV,EAAME,KAAKQ,SARC,QAenB,SAACC,OACDd,EAAmBC,KACtB,MAAM,IAAIc,+CAENL,EAAeM,KAAKC,SAASC,kBAC5BlB,EAAmBC,KAAK,CAC7BM,KAAMT,EACNa,UAAWG,EACXJ,aAAAA,MAINS,OAAQ,mBAlHHC,EAAYC,EAQbpB,EAMAqB,EAQAC,EA6FIC,GAnHHJ,KAA6BK,WAClC,QADiBJ,OAQbpB,EAAOyB,eAAY,SAACpB,UACjB,IAAIqB,SAAsB,SAACf,EAASC,UACzCQ,GAAc,SAAAO,mBAASA,GAAG,CAAEtB,KAAAA,EAAMM,QAAAA,EAASC,OAAAA,aAE5C,IAEGS,EAAkBI,eAAY,SAACG,GACnCR,GAAc,SAAAG,OACLM,EAAkBN,KAARO,EAAQP,wBACzBM,GAAAA,EAAOlB,QAAQiB,GACRE,OAER,IAEGR,EAAiBG,eAAY,SAACM,GAClCX,GAAc,SAAAG,OACLM,EAAkBN,KAARO,EAAQP,wBACzBM,GAAAA,EAAOjB,OAAOmB,GACPD,OAER,IAEI,CACL1B,KAAMe,EAAW,GACb,CACEd,cAAMc,EAAW,WAAXa,EAAe3B,KACrBM,QAASU,EACTT,OAAQU,QAEVrB,EACJD,KAAAA,WA+EIO,gCAEGhB,EAA+B0C,KAAI,SAACC,EAAU1B,UAC7CD,gBAACA,EAAM4B,UAAS3B,IAAKA,GACnBD,gBAAC2B,mBAAaX,cD7JUjC,IAAvBE,IAAAA,4BAAY0B"}
|
|
@@ -3,11 +3,14 @@ import React, { useState, useCallback, useEffect } from 'react';
|
|
|
3
3
|
var useComponentDidMount = function useComponentDidMount(fn) {
|
|
4
4
|
useEffect(fn, []);
|
|
5
5
|
};
|
|
6
|
+
|
|
6
7
|
var usePromiseQueue = function usePromiseQueue() {
|
|
7
8
|
var _asyncQueue$;
|
|
9
|
+
|
|
8
10
|
var _useState = useState([]),
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
asyncQueue = _useState[0],
|
|
12
|
+
setAsyncQueue = _useState[1];
|
|
13
|
+
|
|
11
14
|
var push = useCallback(function (data) {
|
|
12
15
|
return new Promise(function (resolve, reject) {
|
|
13
16
|
return setAsyncQueue(function (p) {
|
|
@@ -22,7 +25,7 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
22
25
|
var resolveHeadItem = useCallback(function (value) {
|
|
23
26
|
setAsyncQueue(function (queue) {
|
|
24
27
|
var first = queue[0],
|
|
25
|
-
|
|
28
|
+
rest = queue.slice(1);
|
|
26
29
|
first == null ? void 0 : first.resolve(value);
|
|
27
30
|
return rest;
|
|
28
31
|
});
|
|
@@ -30,7 +33,7 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
30
33
|
var rejectHeadItem = useCallback(function (reason) {
|
|
31
34
|
setAsyncQueue(function (queue) {
|
|
32
35
|
var first = queue[0],
|
|
33
|
-
|
|
36
|
+
rest = queue.slice(1);
|
|
34
37
|
first == null ? void 0 : first.reject(reason);
|
|
35
38
|
return rest;
|
|
36
39
|
});
|
|
@@ -43,8 +46,8 @@ var usePromiseQueue = function usePromiseQueue() {
|
|
|
43
46
|
} : undefined,
|
|
44
47
|
push: push
|
|
45
48
|
};
|
|
46
|
-
};
|
|
47
|
-
|
|
49
|
+
}; // ------------------------------------------------------------------------------------
|
|
50
|
+
|
|
48
51
|
var syncUIFactory = function syncUIFactory() {
|
|
49
52
|
var mutSyncUIComponentsRenderQueue = [];
|
|
50
53
|
/*
|
|
@@ -53,15 +56,19 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
53
56
|
"<SyncUI /> has to be initialized only once"
|
|
54
57
|
);
|
|
55
58
|
*/
|
|
59
|
+
|
|
56
60
|
return {
|
|
57
61
|
makeSyncUI: function makeSyncUI(SyncUIUserComp) {
|
|
58
62
|
var _ref, _SyncUIUserComp$displ;
|
|
63
|
+
|
|
59
64
|
var _debugName = (_ref = (_SyncUIUserComp$displ = SyncUIUserComp.displayName) != null ? _SyncUIUserComp$displ : SyncUIUserComp.name) != null ? _ref : "uniqSymbolMessageType";
|
|
60
|
-
|
|
61
|
-
// object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called
|
|
65
|
+
|
|
66
|
+
var syncUIComponentType = Symbol(_debugName); // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called
|
|
67
|
+
|
|
62
68
|
var singletonSyncUIRef = {
|
|
63
69
|
push: undefined
|
|
64
70
|
};
|
|
71
|
+
|
|
65
72
|
var SyncUISingletonComponent = function SyncUISingletonComponent(props) {
|
|
66
73
|
useComponentDidMount(function () {
|
|
67
74
|
singletonSyncUIRef.push = props.push;
|
|
@@ -72,17 +79,21 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
72
79
|
if (!props.head) return null;
|
|
73
80
|
if (props.head.data.type !== syncUIComponentType) return null;
|
|
74
81
|
return React.createElement(SyncUIUserComp, {
|
|
82
|
+
key: props.head.data.reactCompKey,
|
|
75
83
|
data: props.head.data.inputData,
|
|
76
84
|
resolve: props.head.resolve,
|
|
77
85
|
reject: props.head.reject
|
|
78
86
|
});
|
|
79
87
|
};
|
|
88
|
+
|
|
80
89
|
mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);
|
|
81
90
|
return function (input) {
|
|
82
91
|
if (!singletonSyncUIRef.push) throw new Error("You have to initialize <SyncUI />");
|
|
92
|
+
var reactCompKey = Math.random().toString();
|
|
83
93
|
return singletonSyncUIRef.push({
|
|
84
94
|
type: syncUIComponentType,
|
|
85
|
-
inputData: input
|
|
95
|
+
inputData: input,
|
|
96
|
+
reactCompKey: reactCompKey
|
|
86
97
|
});
|
|
87
98
|
};
|
|
88
99
|
},
|
|
@@ -98,9 +109,10 @@ var syncUIFactory = function syncUIFactory() {
|
|
|
98
109
|
};
|
|
99
110
|
|
|
100
111
|
var syncUIFactory$1 = syncUIFactory;
|
|
112
|
+
|
|
101
113
|
var _syncUIFactory2 = /*#__PURE__*/syncUIFactory$1(),
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
makeSyncUI = _syncUIFactory2.makeSyncUI,
|
|
115
|
+
SyncUI = _syncUIFactory2.SyncUI;
|
|
104
116
|
|
|
105
117
|
export { SyncUI, makeSyncUI, syncUIFactory$1 as syncUIFactory };
|
|
106
118
|
//# sourceMappingURL=react-sync-ui.esm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-sync-ui.esm.js","sources":["../src/syncUI.tsx","../src/index.ts"],"sourcesContent":["import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n","import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n"],"names":["useComponentDidMount","fn","useEffect","usePromiseQueue","
|
|
1
|
+
{"version":3,"file":"react-sync-ui.esm.js","sources":["../src/syncUI.tsx","../src/index.ts"],"sourcesContent":["import React, { useCallback, useEffect, useState } from \"react\";\n\nconst useComponentDidMount = (fn: Parameters<typeof useEffect>[0]) => {\n useEffect(fn, []);\n};\n\n// this function broke the working of the hot reloading\n/*\nconst getSingletonComponentCheck = (errorMsg: string) => {\n let globalMountCounter = 0;\n\n return () => {\n useComponentDidMount(() => {\n if (globalMountCounter > 0) throw new Error(errorMsg);\n globalMountCounter++;\n });\n return <React.Fragment />;\n };\n};\n*/\n\n// ------------------------------------------------------------------------------------\n// TODO: there is tsdx old typescript parser and new ts fancy syntax is not working...\n// https://github.com/jaredpalmer/tsdx/issues/200\n// type PromiseQueueAPI<Data, ResolveValue> = ReturnType<typeof usePromiseQueue<any, any>>\ntype PromiseQueueAPI<Data, ResolveValue> = {\n head?: {\n data: Data;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n };\n push: (data: Data) => Promise<ResolveValue>;\n};\n\nexport const usePromiseQueue = <Data, ResolveValue = void>(): PromiseQueueAPI<\n Data,\n ResolveValue\n> => {\n const [asyncQueue, setAsyncQueue] = useState(\n [] as {\n data: Data;\n resolve: (arg: ResolveValue) => void;\n reject: (arg: any) => void;\n }[]\n );\n\n const push = useCallback((data: Data) => {\n return new Promise<ResolveValue>((resolve, reject) =>\n setAsyncQueue(p => [...p, { data, resolve, reject }])\n );\n }, []);\n\n const resolveHeadItem = useCallback((value: ResolveValue) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.resolve(value);\n return rest;\n });\n }, []);\n\n const rejectHeadItem = useCallback((reason?: any) => {\n setAsyncQueue(queue => {\n const [first, ...rest] = queue;\n first?.reject(reason);\n return rest;\n });\n }, []);\n\n return {\n head: asyncQueue[0]\n ? {\n data: asyncQueue[0]?.data,\n resolve: resolveHeadItem,\n reject: rejectHeadItem\n }\n : undefined,\n push\n };\n};\n\n// ------------------------------------------------------------------------------------\n\nexport const syncUIFactory = () => {\n const mutSyncUIComponentsRenderQueue = [] as React.FC<\n PromiseQueueAPI<any, any>\n >[];\n\n /*\n // this check broke the working of the hot reloading\n const ThrowIfMoreInstances = getSingletonComponentCheck(\n \"<SyncUI /> has to be initialized only once\"\n );\n */\n\n return {\n makeSyncUI: <InputData, ResolveValue = void>(\n SyncUIUserComp: React.FC<{\n data: InputData;\n resolve: (value: ResolveValue) => void;\n reject: (reason?: any) => void;\n }>\n ) => {\n type QItem = PromiseQueueAPI<\n { type: Symbol; inputData: InputData; reactCompKey: string },\n ResolveValue\n >;\n\n const _debugName =\n SyncUIUserComp.displayName ??\n SyncUIUserComp.name ??\n \"uniqSymbolMessageType\";\n\n const syncUIComponentType = Symbol(_debugName);\n\n // object pointer reference with the push key has to be there to change returned mutable reference object while the function is already called\n const singletonSyncUIRef = {\n push: undefined as undefined | QItem[\"push\"]\n };\n\n const SyncUISingletonComponent = (props: QItem) => {\n useComponentDidMount(() => {\n singletonSyncUIRef.push = props.push;\n return () => (singletonSyncUIRef.push = undefined);\n });\n\n if (!props.head) return null;\n if (props.head.data.type !== syncUIComponentType) return null;\n\n return (\n <SyncUIUserComp\n key={props.head.data.reactCompKey}\n data={props.head.data.inputData}\n resolve={props.head.resolve}\n reject={props.head.reject}\n />\n );\n };\n\n mutSyncUIComponentsRenderQueue.push(SyncUISingletonComponent);\n\n return (input: InputData) => {\n if (!singletonSyncUIRef.push)\n throw new Error(`You have to initialize <SyncUI />`);\n\n const reactCompKey = Math.random().toString();\n return singletonSyncUIRef.push({\n type: syncUIComponentType,\n inputData: input,\n reactCompKey\n });\n };\n },\n SyncUI: () => {\n const queue = usePromiseQueue();\n return (\n <>\n {/* <ThrowIfMoreInstances /> */}\n {mutSyncUIComponentsRenderQueue.map((SyncComp, key) => (\n <React.Fragment key={key}>\n <SyncComp {...queue} />\n </React.Fragment>\n ))}\n </>\n );\n }\n };\n};\n","import { syncUIFactory as _syncUIFactory } from \"./syncUI\";\nexport const syncUIFactory = _syncUIFactory;\nexport const { makeSyncUI, SyncUI } = syncUIFactory();\n"],"names":["useComponentDidMount","fn","useEffect","usePromiseQueue","useState","asyncQueue","setAsyncQueue","push","useCallback","data","Promise","resolve","reject","p","resolveHeadItem","value","queue","first","rest","rejectHeadItem","reason","head","undefined","syncUIFactory","mutSyncUIComponentsRenderQueue","makeSyncUI","SyncUIUserComp","_debugName","displayName","name","syncUIComponentType","Symbol","singletonSyncUIRef","SyncUISingletonComponent","props","type","React","key","reactCompKey","inputData","input","Error","Math","random","toString","SyncUI","map","SyncComp","Fragment","_syncUIFactory"],"mappings":";;AAEA,IAAMA,oBAAoB,GAAG,SAAvBA,oBAAuB,CAACC,EAAD;AAC3BC,EAAAA,SAAS,CAACD,EAAD,EAAK,EAAL,CAAT;A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A8B,CAACjB,IAA/B,CAAoC0B,wBAApC;AAEA,aAAO,UAACO,KAAD;AACL,YAAI,CAACR,kBAAkB,CAACzB,IAAxB,EACE,MAAM,IAAIkC,KAAJ,qCAAN;AAEF,YAAMH,YAAY,GAAGI,IAAI,CAACC,MAAL,GAAcC,QAAd,EAArB;AACA,eAAOZ,kBAAkB,CAACzB,IAAnB,CAAwB;AAC7B4B,UAAAA,IAAI,EAAEL,mBADuB;AAE7BS,UAAAA,SAAS,EAAEC,KAFkB;AAG7BF,UAAAA,YAAY,EAAZA;AAH6B,SAAxB,CAAP;AAKD,OAVD;AAWD,KAzDI;AA0DLO,IAAAA,MAAM,EAAE;AACN,UAAM7B,KAAK,GAAGb,eAAe,EAA7B;AACA,aACEiC,mBAAA,eAAA,MAAA,EAEGZ,8BAA8B,CAACsB,GAA/B,CAAmC,UAACC,QAAD,EAAWV,GAAX;AAAA,eAClCD,mBAAA,CAACA,KAAK,CAACY,QAAP;AAAgBX,UAAAA,GAAG,EAAEA;SAArB,EACED,mBAAA,CAACW,QAAD,oBAAc/B,MAAd,CADF,CADkC;AAAA,OAAnC,CAFH,CADF;AAUD;AAtEI,GAAP;AAwED,CApFM;;ICjFMO,eAAa,GAAG0B;;AACtB,mCAA+B1B,eAAa,EAA5C;AAAA,IAAQE,UAAR,mBAAQA,UAAR;AAAA,IAAoBoB,MAApB,mBAAoBA,MAApB;;;;"}
|
package/package.json
CHANGED
package/src/syncUI.tsx
CHANGED
|
@@ -101,7 +101,7 @@ export const syncUIFactory = () => {
|
|
|
101
101
|
}>
|
|
102
102
|
) => {
|
|
103
103
|
type QItem = PromiseQueueAPI<
|
|
104
|
-
{ type: Symbol; inputData: InputData },
|
|
104
|
+
{ type: Symbol; inputData: InputData; reactCompKey: string },
|
|
105
105
|
ResolveValue
|
|
106
106
|
>;
|
|
107
107
|
|
|
@@ -128,6 +128,7 @@ export const syncUIFactory = () => {
|
|
|
128
128
|
|
|
129
129
|
return (
|
|
130
130
|
<SyncUIUserComp
|
|
131
|
+
key={props.head.data.reactCompKey}
|
|
131
132
|
data={props.head.data.inputData}
|
|
132
133
|
resolve={props.head.resolve}
|
|
133
134
|
reject={props.head.reject}
|
|
@@ -140,9 +141,12 @@ export const syncUIFactory = () => {
|
|
|
140
141
|
return (input: InputData) => {
|
|
141
142
|
if (!singletonSyncUIRef.push)
|
|
142
143
|
throw new Error(`You have to initialize <SyncUI />`);
|
|
144
|
+
|
|
145
|
+
const reactCompKey = Math.random().toString();
|
|
143
146
|
return singletonSyncUIRef.push({
|
|
144
147
|
type: syncUIComponentType,
|
|
145
|
-
inputData: input
|
|
148
|
+
inputData: input,
|
|
149
|
+
reactCompKey
|
|
146
150
|
});
|
|
147
151
|
};
|
|
148
152
|
},
|