@ea-lab/reactive-json 2.0.1 → 2.1.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.
@@ -1,100 +1,109 @@
1
- import { jsx as _ } from "react/jsx-runtime";
2
- import { useContext as x, useRef as m, useCallback as q, useEffect as T } from "react";
3
- import { ActionDependant as A } from "../../../engine/Actions.js";
4
- import { GlobalDataContext as $ } from "../../../engine/GlobalDataContext.js";
5
- import { useStore as B } from "../../../engine/StoreContext.js";
6
- import { TemplateContext as G } from "../../../engine/TemplateContext.js";
7
- import { dataLocationToPath as v } from "../../../engine/TemplateSystem.js";
8
- import { a as M } from "../../../index-BUPRVmV-.js";
9
- import { l as C } from "../../../lodash-CYNxjS-I.js";
10
- const E = (t) => t === "data" ? "" : t && t.startsWith("data.") ? t.substring(5) : t, Q = ({ props: t }) => {
11
- const S = x($), y = x(G), e = B(), p = m(void 0), d = m(null), a = m(null), u = m(null), g = m(!1), c = m(0), b = t.mode || "onIdle", R = t.idleDelay || 1e3, D = t.maxRetries ?? 0, I = t.retryDelay ?? 5e3;
12
- let o = null;
1
+ import { jsx as L } from "react/jsx-runtime";
2
+ import { useContext as P, useRef as l, useCallback as H, useEffect as k } from "react";
3
+ import { ActionDependant as M } from "../../../engine/Actions.js";
4
+ import { GlobalDataContext as N } from "../../../engine/GlobalDataContext.js";
5
+ import { useStore as U } from "../../../engine/StoreContext.js";
6
+ import { TemplateContext as W } from "../../../engine/TemplateContext.js";
7
+ import { dataLocationToPath as w } from "../../../engine/TemplateSystem.js";
8
+ import { a as j } from "../../../index-BUPRVmV-.js";
9
+ import { l as _ } from "../../../lodash-CYNxjS-I.js";
10
+ const q = (t) => t === "data" ? "" : t && t.startsWith("data.") ? t.substring(5) : t, tt = ({ props: t }) => {
11
+ const x = P(N), h = P(W), e = U(), D = l(void 0), g = l(null), u = l(null), f = l(null), v = l(!1), c = l(0), b = l(null), S = t.mode || "onIdle", R = t.idleDelay || 1e3, C = t.maxRetries ?? 0, A = t.retryDelay ?? 5e3;
12
+ let n = null;
13
13
  try {
14
- o = E(v({
14
+ n = q(w({
15
15
  dataLocation: t.path,
16
- currentPath: y.templatePath,
17
- globalDataContext: S,
18
- templateContext: y
16
+ currentPath: h.templatePath,
17
+ globalDataContext: x,
18
+ templateContext: h
19
19
  }));
20
20
  } catch (r) {
21
21
  console.error("DataSync: Invalid path", t.path, r);
22
22
  }
23
- let i = null;
23
+ let m = null;
24
24
  if (t.trigger)
25
25
  try {
26
- i = E(v({
26
+ m = q(w({
27
27
  dataLocation: t.trigger,
28
- currentPath: y.templatePath,
29
- globalDataContext: S,
30
- templateContext: y
28
+ currentPath: h.templatePath,
29
+ globalDataContext: x,
30
+ templateContext: h
31
31
  }));
32
32
  } catch (r) {
33
33
  console.error("DataSync: Invalid trigger path", t.trigger, r);
34
34
  }
35
- const l = q(async (r) => {
36
- var f;
35
+ const d = H(async (r) => {
36
+ var y, a, i, E, T, I;
37
37
  if (!r || !r.submission_url) {
38
38
  r && console.warn("DataSync: Missing submission_url in data object", r);
39
39
  return;
40
40
  }
41
- if (!g.current && !(c.current > D)) {
42
- g.current = !0, c.current++, p.current = r.data, e.set(`${o}.status`, {
41
+ if (!v.current && !(c.current > C)) {
42
+ v.current = !0, c.current++, D.current = r.data, e.set(`${n}.status`, {
43
43
  type: "info",
44
44
  // TODO: translate this.
45
45
  message: "Synchronisation en cours..."
46
46
  });
47
47
  try {
48
- const { submission_url: n } = r, h = (await M.post(n, r)).data;
49
- d.current = h, c.current = 0, e.set(o, h);
50
- } catch (n) {
51
- console.error("DataSync error:", n);
52
- const s = (f = n.response) == null ? void 0 : f.data;
53
- if (s && typeof s == "object" && s.status) {
54
- d.current = s, c.current = 0, e.set(o, s), g.current = !1;
48
+ const { submission_url: s } = r, o = await j.post(s, r), p = o.data;
49
+ g.current = p, c.current = 0, e.set(n, p), (y = b.current) == null || y.dispatchEvent(new CustomEvent("syncSuccess", {
50
+ bubbles: !0,
51
+ detail: { value: p, responseContext: { status: o.status } }
52
+ }));
53
+ } catch (s) {
54
+ console.error("DataSync error:", s);
55
+ const o = (a = s.response) == null ? void 0 : a.data;
56
+ if (o && typeof o == "object" && o.status) {
57
+ g.current = o, c.current = 0, e.set(n, o), (E = b.current) == null || E.dispatchEvent(new CustomEvent("syncError", {
58
+ bubbles: !0,
59
+ detail: { value: o, responseContext: { status: (i = s.response) == null ? void 0 : i.status } }
60
+ })), v.current = !1;
55
61
  return;
56
62
  }
57
- const h = {
63
+ const p = {
58
64
  type: "error",
59
- message: n.message || "Unknown error"
65
+ message: s.message || "Unknown error"
60
66
  };
61
- e.set(`${o}.status`, h);
62
- const L = n.response && n.response.status >= 500, P = !n.response && typeof navigator < "u" && !navigator.onLine;
63
- (L || P) && c.current <= D && (u.current = setTimeout(() => {
64
- const k = e.get(o);
65
- l(k);
66
- }, I));
67
+ e.set(`${n}.status`, p), (I = b.current) == null || I.dispatchEvent(new CustomEvent("syncError", {
68
+ bubbles: !0,
69
+ detail: { value: o, responseContext: { status: (T = s.response) == null ? void 0 : T.status } }
70
+ }));
71
+ const $ = s.response && s.response.status >= 500, B = !s.response && typeof navigator < "u" && !navigator.onLine;
72
+ ($ || B) && c.current <= C && (f.current = setTimeout(() => {
73
+ const G = e.get(n);
74
+ d(G);
75
+ }, A));
67
76
  } finally {
68
- g.current = !1;
77
+ v.current = !1;
69
78
  }
70
79
  }
71
- }, [e, o, D]);
72
- return T(() => {
73
- if (!e || o === null) return;
80
+ }, [e, n, C]);
81
+ return k(() => {
82
+ if (!e || n === null) return;
74
83
  const r = () => {
75
- const n = e.get(o);
76
- if (!n || n === d.current)
84
+ const a = e.get(n);
85
+ if (!a || a === g.current)
77
86
  return;
78
- const s = n.data;
79
- p.current !== void 0 && C.isEqual(s, p.current) || d.current && C.isEqual(s, d.current.data) || (c.current = 0, u.current && (clearTimeout(u.current), u.current = null), b === "immediate" ? l(n) : b === "onIdle" && (a.current && clearTimeout(a.current), a.current = setTimeout(() => {
80
- l(n);
87
+ const i = a.data;
88
+ D.current !== void 0 && _.isEqual(i, D.current) || g.current && _.isEqual(i, g.current.data) || (c.current = 0, f.current && (clearTimeout(f.current), f.current = null), S === "immediate" ? d(a) : S === "onIdle" && (u.current && clearTimeout(u.current), u.current = setTimeout(() => {
89
+ d(a);
81
90
  }, R)));
82
- }, f = e.subscribe(o, r);
91
+ }, y = e.subscribe(n, r);
83
92
  return () => {
84
- f(), a.current && clearTimeout(a.current), u.current && clearTimeout(u.current);
93
+ y(), u.current && clearTimeout(u.current), f.current && clearTimeout(f.current);
85
94
  };
86
- }, [e, o, b, R, l]), T(() => {
87
- if (!e || i === null) return;
95
+ }, [e, n, S, R, d]), k(() => {
96
+ if (!e || m === null) return;
88
97
  const r = () => {
89
- if (e.get(i) === !0) {
90
- e.set(i, !1), a.current && (clearTimeout(a.current), a.current = null), c.current = 0;
91
- const s = e.get(o);
92
- l(s);
98
+ if (e.get(m) === !0) {
99
+ e.set(m, !1), u.current && (clearTimeout(u.current), u.current = null), c.current = 0;
100
+ const i = e.get(n);
101
+ d(i);
93
102
  }
94
- }, f = e.subscribe(i, r);
95
- return () => f();
96
- }, [e, i, o, l]), /* @__PURE__ */ _(A, { ...t });
103
+ }, y = e.subscribe(m, r);
104
+ return () => y();
105
+ }, [e, m, n, d]), /* @__PURE__ */ L(M, { ...t, attributesHolderRef: b, children: /* @__PURE__ */ L("span", { ref: b, style: { display: "none" } }) });
97
106
  };
98
107
  export {
99
- Q as DataSync
108
+ tt as DataSync
100
109
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ea-lab/reactive-json",
3
3
  "private": false,
4
- "version": "2.0.1",
4
+ "version": "2.1.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",