@machinemetrics/mm-react-embeddable 1.3.0-0 → 1.3.0-2

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,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),w=require("prop-types"),v=require("styled-components"),x=e.createContext(null),A=({children:n})=>{const[i,l]=e.useState({}),[a,b]=e.useState({}),u=e.useRef({}),m=e.useRef({}),g=e.useRef({}),E=e.useCallback((r,o)=>{var t;return!!((t=g.current[r])!=null&&t.has(o))},[]),p=e.useCallback((r,o,t)=>{l(s=>({...s,[r]:{origin:o,frameRef:t}}))},[]),M=e.useCallback(r=>{l(o=>{const t={...o};return delete t[r],t}),b(o=>{const t={...o};return delete t[r],t}),delete g.current[r]},[]);e.useEffect(()=>{const r=(t=[],s)=>{t.forEach(c=>c(s))},o=t=>{var S,k,C,q,P;if(!((S=t==null?void 0:t.data)!=null&&S.instanceId))return;const{instanceId:s,type:c}=t.data,f=u.current;if(c==="response"){const{callbackId:R,response:H,error:B}=t.data,U=m.current[R];U&&(U(H,B),delete m.current[R]);return}if(c==="mark-registered"){const{requestType:R}=t.data;g.current[s]||(g.current[s]=new Set),g.current[s].add(R);return}c==="ready"&&b(R=>({...R,[s]:!0})),r((k=f[s])==null?void 0:k[c],t.data),r((C=f["*"])==null?void 0:C[c],t.data),r((q=f[s])==null?void 0:q["*"],t.data),r((P=f["*"])==null?void 0:P["*"],t.data)};return window.addEventListener("message",o),()=>{window.removeEventListener("message",o)}},[i]);const d=e.useCallback((r,o,t)=>{var c,f;const s=i[r];if(!s){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}(f=(c=s==null?void 0:s.frameRef)==null?void 0:c.current)!=null&&f.contentWindow&&s.frameRef.current.contentWindow.postMessage({type:o,...t,instanceId:r},s.origin)},[i]),L=e.useRef(0),h=e.useCallback(()=>`${Date.now()}-${++L.current}`,[]),$=e.useCallback(async(r,o,t={})=>new Promise((s,c)=>{const f=h(),S=(k,C)=>{delete m.current[f],C?c(C):s(k)};m.current[f]=S,d(r,"request",{callbackId:f,requestType:o,data:t})}),[d]),y=e.useCallback((r,o)=>{d(r,"load-options",o)},[d]),F=e.useCallback((r,o)=>{d(r,"validate",o)},[d]),I=e.useMemo(()=>({registerFrame:p,unregisterFrame:M,sendMessage:d,sendRequest:$,loadOptions:y,validate:F,hasHandler:E,readyFrames:a,addListener:(r,o,t)=>(u.current[r]||(u.current[r]={}),u.current[r][o]||(u.current[r][o]=[]),u.current[r][o].push(t),()=>{u.current[r][o]=u.current[r][o].filter(s=>s!==t)})}),[p,M,d,$,y,F,E,a]);return e.createElement(x.Provider,{value:I},n)},T={},O=async n=>{try{const i=new URL(n).origin;if(T[i]!==void 0)return T[i];const a=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!a.version)throw new Error("Invalid manifest");return T[i]=a,a}catch{}return T[origin]=!1,!1},j=async n=>!!await O(n),Z=(n,i,l)=>{const a=e.useContext(x);if(a===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=e.useRef(l);e.useEffect(()=>{b.current=l},[l]),e.useEffect(()=>{if(!(!n||!i)){if(i.startsWith("key:")){const u=i.substring(4);return a.readyFrames[n]&&a.sendMessage(n,"subscribe-key",{keyChord:u}),a.addListener(n,`key-pressed:${u}`,(...g)=>b.current(...g))}return a.addListener(n,i,(...u)=>b.current(...u))}},[a,n,i])};A.propTypes={children:w.node.isRequired};const D=v.default||v,_=D.iframe`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),p=require("prop-types"),O=require("styled-components"),P=e.createContext(null),j=({children:r})=>{const[u,d]=e.useState({}),[i,E]=e.useState({}),c=e.useRef({}),b=e.useRef({}),m=e.useRef({}),h=e.useCallback((n,s)=>{var t;return!!((t=m.current[n])!=null&&t.has(s))},[]),S=e.useCallback((n,s,t)=>{d(o=>({...o,[n]:{origin:s,frameRef:t}}))},[]),R=e.useCallback(n=>{d(s=>{const t={...s};return delete t[n],t}),E(s=>{const t={...s};return delete t[n],t}),delete m.current[n]},[]);e.useEffect(()=>{const n=(t=[],o)=>{t.forEach(a=>a(o))},s=t=>{var M,w,f,T,x;if(!((M=t==null?void 0:t.data)!=null&&M.instanceId))return;const{instanceId:o,type:a}=t.data,l=c.current;if(a==="response"){const{callbackId:F,response:H,error:z}=t.data,U=b.current[F];U&&(U(H,z),delete b.current[F]);return}if(a==="mark-registered"){const{requestType:F}=t.data;m.current[o]||(m.current[o]=new Set),m.current[o].add(F);return}a==="ready"&&E(F=>({...F,[o]:!0})),n((w=l[o])==null?void 0:w[a],t.data),n((f=l["*"])==null?void 0:f[a],t.data),n((T=l[o])==null?void 0:T["*"],t.data),n((x=l["*"])==null?void 0:x["*"],t.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[u]);const g=e.useCallback((n,s,t)=>{var a,l;const o=u[n];if(!o){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}(l=(a=o==null?void 0:o.frameRef)==null?void 0:a.current)!=null&&l.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...t,instanceId:n},o.origin)},[u]),k=e.useRef(0),y=e.useCallback(()=>`${Date.now()}-${++k.current}`,[]),L=e.useCallback(async(n,s,t={})=>new Promise((o,a)=>{const l=y(),M=(w,f)=>{delete b.current[l],f?a(f):o(w)};b.current[l]=M,g(n,"request",{callbackId:l,requestType:s,data:t})}),[g]),C=e.useCallback((n,s)=>{g(n,"load-options",s)},[g]),Z=e.useCallback((n,s)=>{g(n,"validate",s)},[g]),$=e.useMemo(()=>({registerFrame:S,unregisterFrame:R,sendMessage:g,sendRequest:L,loadOptions:C,validate:Z,hasHandler:h,readyFrames:i,addListener:(n,s,t)=>(c.current[n]||(c.current[n]={}),c.current[n][s]||(c.current[n][s]=[]),c.current[n][s].push(t),()=>{c.current[n][s]=c.current[n][s].filter(o=>o!==t)})}),[S,R,g,L,C,Z,h,i]);return e.createElement(P.Provider,{value:$},r)},q={},v=async r=>{try{const u=new URL(r).origin;if(q[u]!==void 0)return q[u];const i=await(await fetch(`${u}/mm-app-manifest.json`)).json();if(!i.version)throw new Error("Invalid manifest");return q[u]=i,i}catch{}return q[origin]=!1,!1},W=async r=>!!await v(r),I=(r,u,d)=>{const i=e.useContext(P);if(i===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const E=e.useRef(d);e.useEffect(()=>{E.current=d},[d]),e.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const c=u.substring(4);return i.readyFrames[r]&&i.sendMessage(r,"subscribe-key",{keyChord:c}),i.addListener(r,`key-pressed:${c}`,(...m)=>E.current(...m))}return i.addListener(r,u,(...c)=>E.current(...c))}},[i,r,u])};j.propTypes={children:p.node.isRequired};const B=O.default||O,D=B.iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${n=>n.$isReady?"visible":"hidden"};
6
- `,W=({id:n,src:i,path:l,onInit:a=()=>{},context:b={},params:u={},skipManifestCheck:m=!1,...g})=>{if("isConfiguring"in g)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in b)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in b)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const E=e.useRef(),[p,M]=e.useState(),[d,L]=e.useState(0),{registerFrame:h,unregisterFrame:$,sendMessage:y}=e.useContext(x),[F,I]=e.useState(!0),[r,o]=e.useState(),t=e.useRef();e.useEffect(()=>{d&&y(n,"set-context",{context:{...b,colorMode:u.colorMode,isFullScreen:u.isFullScreen}})},[b,u.colorMode,u.isFullScreen,n,d]),e.useEffect(()=>{d&&(y(n,"set-params",{params:u}),y(n,"set-configuring",{isConfiguring:u.isConfiguring}))},[u,n,d]),e.useEffect(()=>{l!==void 0&&o((l==null?void 0:l.indexOf("/"))===0?l:`/${l}`)},[l]),e.useEffect(()=>{r===void 0||!d||y(n,"navigate",{path:r})},[r,d,n,y]),e.useEffect(()=>{if(!(!i||!n||!E)){if(m){const c=new URL(i);h(n,c.origin,E),a(),M(`${c.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`)}else(async()=>{const c=await j(i);if(I(c),c){const f=new URL(i);h(n,f.origin,E),a(),M(`${f.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`)}})();return()=>{$(n)}}},[i,n,h,E,m]),e.useEffect(()=>{if(!(!m||!p))return t.current=setTimeout(()=>I(!1),5e3),()=>clearTimeout(t.current)},[p,m]),Z(n,"ready",()=>{clearTimeout(t.current),L(c=>c+1)});const s=e.useRef();return Z(n,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(E.current){const c=new URL(i);E.current.src=`${c.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),F?e.createElement(_,{$isReady:d,ref:E,src:p,...g}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must support the MM App Framework to be embedded."))};W.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func,skipManifestCheck:w.bool};exports.MMEmbeddableZone=W;exports.MMEmbeddableZoneContext=x;exports.MMEmbeddableZoneProvider=A;exports.getAppInfo=O;exports.testIsEmbeddable=j;exports.useEmbeddedEffect=Z;
5
+ visibility: ${r=>r.$isReady?"visible":"hidden"};
6
+ `,A=({id:r,src:u,path:d,onInit:i=()=>{},onTimeout:E,context:c={},params:b={},skipManifestCheck:m=!1,readyTimeout:h=5e3,...S})=>{if("isConfiguring"in S)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in c)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in c)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const R=e.useRef(),[g,k]=e.useState(),[y,L]=e.useState(0),{registerFrame:C,unregisterFrame:Z,sendMessage:$}=e.useContext(P),[n,s]=e.useState(!0),[t,o]=e.useState(),a=e.useRef(),l=e.useRef(E);e.useEffect(()=>{l.current=E},[E]),e.useEffect(()=>{y&&$(r,"set-context",{context:{...c,colorMode:b.colorMode,isFullScreen:b.isFullScreen}})},[c,b.colorMode,b.isFullScreen,r,y]),e.useEffect(()=>{y&&($(r,"set-params",{params:b}),$(r,"set-configuring",{isConfiguring:b.isConfiguring}))},[b,r,y]),e.useEffect(()=>{d!==void 0&&o((d==null?void 0:d.indexOf("/"))===0?d:`/${d}`)},[d]),e.useEffect(()=>{t===void 0||!y||$(r,"navigate",{path:t})},[t,y,r,$]),e.useEffect(()=>{if(!(!u||!r||!R)){if(m){const f=new URL(u);s(!0),C(r,f.origin,R),i(),k(`${f.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`)}else(async()=>{const f=await W(u);if(s(f),f){const T=new URL(u);C(r,T.origin,R),i(),k(`${T.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`)}})();return()=>{Z(r)}}},[u,r,C,R,m]),e.useEffect(()=>{if(!(!m||!g||h==null))return a.current=setTimeout(()=>{l.current&&l.current()},h),()=>clearTimeout(a.current)},[g,m,h]);const M=()=>{clearTimeout(a.current)};I(r,"init",M),I(r,"authorize",M),I(r,"login",M),I(r,"ready",()=>{M(),L(f=>f+1)});const w=e.useRef();return I(r,"heartbeat",()=>{w.current&&clearTimeout(w.current),w.current=setTimeout(()=>{if(R.current){const f=new URL(u);R.current.src=`${f.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{w.current&&clearTimeout(w.current)},[]),!m&&!n?null:e.createElement(D,{$isReady:y,ref:R,src:g,...S})};A.propTypes={id:p.string,src:p.string.isRequired,path:p.string,onInit:p.func,onTimeout:p.func,skipManifestCheck:p.bool,readyTimeout:p.number};exports.MMEmbeddableZone=A;exports.MMEmbeddableZoneContext=P;exports.MMEmbeddableZoneProvider=j;exports.getAppInfo=v;exports.testIsEmbeddable=W;exports.useEmbeddedEffect=I;
@@ -1,254 +1,254 @@
1
- import Z, { createContext as _, useState as $, useRef as R, useCallback as w, useEffect as p, useMemo as z, useContext as H } from "react";
1
+ import v, { createContext as D, useState as x, useRef as M, useCallback as $, useEffect as b, useMemo as _, useContext as z } from "react";
2
2
  import h from "prop-types";
3
- import v from "styled-components";
4
- const q = _(null), G = ({ children: t }) => {
5
- const [o, d] = $({}), [a, f] = $({}), c = R({}), m = R({}), g = R({}), b = w((e, s) => {
3
+ import H from "styled-components";
4
+ const O = D(null), G = ({ children: e }) => {
5
+ const [s, a] = x({}), [c, w] = x({}), i = M({}), f = M({}), m = M({}), I = $((n, t) => {
6
6
  var r;
7
- return !!((r = g.current[e]) != null && r.has(s));
8
- }, []), E = w((e, s, r) => {
9
- d((n) => ({ ...n, [e]: { origin: s, frameRef: r } }));
10
- }, []), F = w((e) => {
11
- d((s) => {
12
- const r = { ...s };
13
- return delete r[e], r;
14
- }), f((s) => {
15
- const r = { ...s };
16
- return delete r[e], r;
17
- }), delete g.current[e];
7
+ return !!((r = m.current[n]) != null && r.has(t));
8
+ }, []), C = $((n, t, r) => {
9
+ a((o) => ({ ...o, [n]: { origin: t, frameRef: r } }));
10
+ }, []), y = $((n) => {
11
+ a((t) => {
12
+ const r = { ...t };
13
+ return delete r[n], r;
14
+ }), w((t) => {
15
+ const r = { ...t };
16
+ return delete r[n], r;
17
+ }), delete m.current[n];
18
18
  }, []);
19
- p(() => {
20
- const e = (r = [], n) => {
21
- r.forEach((i) => i(n));
22
- }, s = (r) => {
23
- var P, S, T, A, O;
24
- if (!((P = r == null ? void 0 : r.data) != null && P.instanceId))
19
+ b(() => {
20
+ const n = (r = [], o) => {
21
+ r.forEach((u) => u(o));
22
+ }, t = (r) => {
23
+ var E, R, l, P, W;
24
+ if (!((E = r == null ? void 0 : r.data) != null && E.instanceId))
25
25
  return;
26
- const { instanceId: n, type: i } = r.data, l = c.current;
27
- if (i === "response") {
28
- const { callbackId: M, response: B, error: D } = r.data, j = m.current[M];
29
- j && (j(B, D), delete m.current[M]);
26
+ const { instanceId: o, type: u } = r.data, d = i.current;
27
+ if (u === "response") {
28
+ const { callbackId: L, response: A, error: B } = r.data, j = f.current[L];
29
+ j && (j(A, B), delete f.current[L]);
30
30
  return;
31
31
  }
32
- if (i === "mark-registered") {
33
- const { requestType: M } = r.data;
34
- g.current[n] || (g.current[n] = /* @__PURE__ */ new Set()), g.current[n].add(M);
32
+ if (u === "mark-registered") {
33
+ const { requestType: L } = r.data;
34
+ m.current[o] || (m.current[o] = /* @__PURE__ */ new Set()), m.current[o].add(L);
35
35
  return;
36
36
  }
37
- i === "ready" && f((M) => ({ ...M, [n]: !0 })), e((S = l[n]) == null ? void 0 : S[i], r.data), e((T = l["*"]) == null ? void 0 : T[i], r.data), e((A = l[n]) == null ? void 0 : A["*"], r.data), e((O = l["*"]) == null ? void 0 : O["*"], r.data);
37
+ u === "ready" && w((L) => ({ ...L, [o]: !0 })), n((R = d[o]) == null ? void 0 : R[u], r.data), n((l = d["*"]) == null ? void 0 : l[u], r.data), n((P = d[o]) == null ? void 0 : P["*"], r.data), n((W = d["*"]) == null ? void 0 : W["*"], r.data);
38
38
  };
39
- return window.addEventListener("message", s), () => {
40
- window.removeEventListener("message", s);
39
+ return window.addEventListener("message", t), () => {
40
+ window.removeEventListener("message", t);
41
41
  };
42
- }, [o]);
43
- const u = w(
44
- (e, s, r) => {
45
- var i, l;
46
- const n = o[e];
47
- if (!n) {
48
- console.warn(`Origin not set for id: ${e}. Unable to send message.`);
42
+ }, [s]);
43
+ const g = $(
44
+ (n, t, r) => {
45
+ var u, d;
46
+ const o = s[n];
47
+ if (!o) {
48
+ console.warn(`Origin not set for id: ${n}. Unable to send message.`);
49
49
  return;
50
50
  }
51
- (l = (i = n == null ? void 0 : n.frameRef) == null ? void 0 : i.current) != null && l.contentWindow && n.frameRef.current.contentWindow.postMessage(
52
- { type: s, ...r, instanceId: e },
53
- n.origin
51
+ (d = (u = o == null ? void 0 : o.frameRef) == null ? void 0 : u.current) != null && d.contentWindow && o.frameRef.current.contentWindow.postMessage(
52
+ { type: t, ...r, instanceId: n },
53
+ o.origin
54
54
  );
55
55
  },
56
- [o]
57
- ), k = R(0), I = w(() => `${Date.now()}-${++k.current}`, []), x = w(
58
- async (e, s, r = {}) => new Promise((n, i) => {
59
- const l = I(), P = (S, T) => {
60
- delete m.current[l], T ? i(T) : n(S);
56
+ [s]
57
+ ), U = M(0), p = $(() => `${Date.now()}-${++U.current}`, []), Z = $(
58
+ async (n, t, r = {}) => new Promise((o, u) => {
59
+ const d = p(), E = (R, l) => {
60
+ delete f.current[d], l ? u(l) : o(R);
61
61
  };
62
- m.current[l] = P, u(e, "request", { callbackId: l, requestType: s, data: r });
62
+ f.current[d] = E, g(n, "request", { callbackId: d, requestType: t, data: r });
63
63
  }),
64
- [u]
65
- ), y = w(
66
- (e, s) => {
67
- u(e, "load-options", s);
64
+ [g]
65
+ ), T = $(
66
+ (n, t) => {
67
+ g(n, "load-options", t);
68
68
  },
69
- [u]
70
- ), L = w(
71
- (e, s) => {
72
- u(e, "validate", s);
69
+ [g]
70
+ ), q = $(
71
+ (n, t) => {
72
+ g(n, "validate", t);
73
73
  },
74
- [u]
75
- ), C = z(
74
+ [g]
75
+ ), F = _(
76
76
  () => ({
77
- registerFrame: E,
78
- unregisterFrame: F,
79
- sendMessage: u,
80
- sendRequest: x,
81
- loadOptions: y,
82
- validate: L,
83
- hasHandler: b,
84
- readyFrames: a,
85
- addListener: (e, s, r) => (c.current[e] || (c.current[e] = {}), c.current[e][s] || (c.current[e][s] = []), c.current[e][s].push(r), () => {
86
- c.current[e][s] = c.current[e][s].filter((n) => n !== r);
77
+ registerFrame: C,
78
+ unregisterFrame: y,
79
+ sendMessage: g,
80
+ sendRequest: Z,
81
+ loadOptions: T,
82
+ validate: q,
83
+ hasHandler: I,
84
+ readyFrames: c,
85
+ addListener: (n, t, r) => (i.current[n] || (i.current[n] = {}), i.current[n][t] || (i.current[n][t] = []), i.current[n][t].push(r), () => {
86
+ i.current[n][t] = i.current[n][t].filter((o) => o !== r);
87
87
  })
88
88
  }),
89
89
  [
90
- E,
91
- F,
92
- u,
93
- x,
90
+ C,
94
91
  y,
95
- L,
96
- b,
97
- a
92
+ g,
93
+ Z,
94
+ T,
95
+ q,
96
+ I,
97
+ c
98
98
  ]
99
99
  );
100
- return /* @__PURE__ */ Z.createElement(q.Provider, { value: C }, t);
101
- }, U = {}, J = async (t) => {
100
+ return /* @__PURE__ */ v.createElement(O.Provider, { value: F }, e);
101
+ }, k = {}, J = async (e) => {
102
102
  try {
103
- const o = new URL(t).origin;
104
- if (U[o] !== void 0)
105
- return U[o];
106
- const a = await (await fetch(`${o}/mm-app-manifest.json`)).json();
107
- if (!a.version)
103
+ const s = new URL(e).origin;
104
+ if (k[s] !== void 0)
105
+ return k[s];
106
+ const c = await (await fetch(`${s}/mm-app-manifest.json`)).json();
107
+ if (!c.version)
108
108
  throw new Error("Invalid manifest");
109
- return U[o] = a, a;
109
+ return k[s] = c, c;
110
110
  } catch {
111
111
  }
112
- return U[origin] = !1, !1;
113
- }, K = async (t) => !!await J(t), W = (t, o, d) => {
114
- const a = H(q);
115
- if (a === null)
112
+ return k[origin] = !1, !1;
113
+ }, K = async (e) => !!await J(e), S = (e, s, a) => {
114
+ const c = z(O);
115
+ if (c === null)
116
116
  throw new Error(
117
117
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
118
118
  );
119
- const f = R(d);
120
- p(() => {
121
- f.current = d;
122
- }, [d]), p(() => {
123
- if (!(!t || !o)) {
124
- if (o.startsWith("key:")) {
125
- const c = o.substring(4);
126
- return a.readyFrames[t] && a.sendMessage(t, "subscribe-key", { keyChord: c }), a.addListener(
127
- t,
128
- `key-pressed:${c}`,
129
- (...g) => f.current(...g)
119
+ const w = M(a);
120
+ b(() => {
121
+ w.current = a;
122
+ }, [a]), b(() => {
123
+ if (!(!e || !s)) {
124
+ if (s.startsWith("key:")) {
125
+ const i = s.substring(4);
126
+ return c.readyFrames[e] && c.sendMessage(e, "subscribe-key", { keyChord: i }), c.addListener(
127
+ e,
128
+ `key-pressed:${i}`,
129
+ (...m) => w.current(...m)
130
130
  );
131
131
  }
132
- return a.addListener(
133
- t,
134
- o,
135
- (...c) => f.current(...c)
132
+ return c.addListener(
133
+ e,
134
+ s,
135
+ (...i) => w.current(...i)
136
136
  );
137
137
  }
138
- }, [a, t, o]);
138
+ }, [c, e, s]);
139
139
  };
140
140
  G.propTypes = {
141
141
  children: h.node.isRequired
142
142
  };
143
- const Q = v.default || v, V = Q.iframe`
143
+ const Q = H.default || H, V = Q.iframe`
144
144
  width: 100%;
145
145
  height: 100%;
146
146
  border: none;
147
- visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
147
+ visibility: ${(e) => e.$isReady ? "visible" : "hidden"};
148
148
  `, X = ({
149
- id: t,
150
- src: o,
151
- path: d,
152
- onInit: a = () => {
149
+ id: e,
150
+ src: s,
151
+ path: a,
152
+ onInit: c = () => {
153
153
  },
154
- context: f = {},
155
- params: c = {},
154
+ onTimeout: w,
155
+ context: i = {},
156
+ params: f = {},
156
157
  skipManifestCheck: m = !1,
157
- ...g
158
+ readyTimeout: I = 5e3,
159
+ ...C
158
160
  }) => {
159
- if ("isConfiguring" in g)
161
+ if ("isConfiguring" in C)
160
162
  return console.error(
161
163
  "isConfiguring must be passed via the params prop to MMEmbeddableZone"
162
164
  ), null;
163
- if ("colorMode" in f)
165
+ if ("colorMode" in i)
164
166
  return console.error(
165
167
  "colorMode must be passed via the params prop to MMEmbeddableZone"
166
168
  ), null;
167
- if ("isFullScreen" in f)
169
+ if ("isFullScreen" in i)
168
170
  return console.error(
169
171
  "isFullScreen must be passed via the params prop to MMEmbeddableZone"
170
172
  ), null;
171
- const b = R(), [E, F] = $(), [u, k] = $(0), { registerFrame: I, unregisterFrame: x, sendMessage: y } = H(
172
- q
173
- ), [L, C] = $(!0), [e, s] = $(), r = R();
174
- p(() => {
175
- u && y(t, "set-context", {
173
+ const y = M(), [g, U] = x(), [p, Z] = x(0), { registerFrame: T, unregisterFrame: q, sendMessage: F } = z(
174
+ O
175
+ ), [n, t] = x(!0), [r, o] = x(), u = M(), d = M(w);
176
+ b(() => {
177
+ d.current = w;
178
+ }, [w]), b(() => {
179
+ p && F(e, "set-context", {
176
180
  context: {
177
- ...f,
181
+ ...i,
178
182
  // TODO: Remove this once everything is migrated to params
179
- colorMode: c.colorMode,
180
- isFullScreen: c.isFullScreen
183
+ colorMode: f.colorMode,
184
+ isFullScreen: f.isFullScreen
181
185
  }
182
186
  });
183
- }, [f, c.colorMode, c.isFullScreen, t, u]), p(() => {
184
- u && (y(t, "set-params", { params: c }), y(t, "set-configuring", { isConfiguring: c.isConfiguring }));
185
- }, [c, t, u]), p(() => {
186
- d !== void 0 && s((d == null ? void 0 : d.indexOf("/")) === 0 ? d : `/${d}`);
187
- }, [d]), p(() => {
188
- e === void 0 || !u || y(t, "navigate", { path: e });
189
- }, [e, u, t, y]), p(() => {
190
- if (!(!o || !t || !b)) {
187
+ }, [i, f.colorMode, f.isFullScreen, e, p]), b(() => {
188
+ p && (F(e, "set-params", { params: f }), F(e, "set-configuring", { isConfiguring: f.isConfiguring }));
189
+ }, [f, e, p]), b(() => {
190
+ a !== void 0 && o((a == null ? void 0 : a.indexOf("/")) === 0 ? a : `/${a}`);
191
+ }, [a]), b(() => {
192
+ r === void 0 || !p || F(e, "navigate", { path: r });
193
+ }, [r, p, e, F]), b(() => {
194
+ if (!(!s || !e || !y)) {
191
195
  if (m) {
192
- const i = new URL(o);
193
- I(t, i.origin, b), a(), F(
194
- `${i.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
196
+ const l = new URL(s);
197
+ t(!0), T(e, l.origin, y), c(), U(
198
+ `${l.origin}/mm-embedded-interop?id=${e}&origin=${window.location.origin}`
195
199
  );
196
200
  } else
197
201
  (async () => {
198
- const i = await K(o);
199
- if (C(i), i) {
200
- const l = new URL(o);
201
- I(t, l.origin, b), a(), F(
202
- `${l.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
202
+ const l = await K(s);
203
+ if (t(l), l) {
204
+ const P = new URL(s);
205
+ T(e, P.origin, y), c(), U(
206
+ `${P.origin}/mm-embedded-interop?id=${e}&origin=${window.location.origin}`
203
207
  );
204
208
  }
205
209
  })();
206
210
  return () => {
207
- x(t);
211
+ q(e);
208
212
  };
209
213
  }
210
- }, [o, t, I, b, m]), p(() => {
211
- if (!(!m || !E))
212
- return r.current = setTimeout(() => C(!1), 5e3), () => clearTimeout(r.current);
213
- }, [E, m]), W(t, "ready", () => {
214
- clearTimeout(r.current), k((i) => i + 1);
214
+ }, [s, e, T, y, m]), b(() => {
215
+ if (!(!m || !g || I == null))
216
+ return u.current = setTimeout(() => {
217
+ d.current && d.current();
218
+ }, I), () => clearTimeout(u.current);
219
+ }, [g, m, I]);
220
+ const E = () => {
221
+ clearTimeout(u.current);
222
+ };
223
+ S(e, "init", E), S(e, "authorize", E), S(e, "login", E), S(e, "ready", () => {
224
+ E(), Z((l) => l + 1);
215
225
  });
216
- const n = R();
217
- return W(t, "heartbeat", () => {
218
- n.current && clearTimeout(n.current), n.current = setTimeout(() => {
219
- if (b.current) {
220
- const i = new URL(o);
221
- b.current.src = `${i.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`;
226
+ const R = M();
227
+ return S(e, "heartbeat", () => {
228
+ R.current && clearTimeout(R.current), R.current = setTimeout(() => {
229
+ if (y.current) {
230
+ const l = new URL(s);
231
+ y.current.src = `${l.origin}/mm-embedded-interop?id=${e}&origin=${window.location.origin}`;
222
232
  }
223
233
  }, 6e3);
224
- }), p(() => () => {
225
- n.current && clearTimeout(n.current);
226
- }, []), L ? /* @__PURE__ */ Z.createElement(V, { $isReady: u, ref: b, src: E, ...g }) : /* @__PURE__ */ Z.createElement(
227
- "div",
228
- {
229
- style: {
230
- height: "100%",
231
- display: "flex",
232
- flex: 1,
233
- alignItems: "center",
234
- justifyContent: "center"
235
- }
236
- },
237
- /* @__PURE__ */ Z.createElement("div", { style: { textAlign: "center" } }, "Application must support the MM App Framework to be embedded.")
238
- );
234
+ }), b(() => () => {
235
+ R.current && clearTimeout(R.current);
236
+ }, []), !m && !n ? null : /* @__PURE__ */ v.createElement(V, { $isReady: p, ref: y, src: g, ...C });
239
237
  };
240
238
  X.propTypes = {
241
239
  id: h.string,
242
240
  src: h.string.isRequired,
243
241
  path: h.string,
244
242
  onInit: h.func,
245
- skipManifestCheck: h.bool
243
+ onTimeout: h.func,
244
+ skipManifestCheck: h.bool,
245
+ readyTimeout: h.number
246
246
  };
247
247
  export {
248
248
  X as MMEmbeddableZone,
249
- q as MMEmbeddableZoneContext,
249
+ O as MMEmbeddableZoneContext,
250
250
  G as MMEmbeddableZoneProvider,
251
251
  J as getAppInfo,
252
252
  K as testIsEmbeddable,
253
- W as useEmbeddedEffect
253
+ S as useEmbeddedEffect
254
254
  };
@@ -1,6 +1,6 @@
1
- (function(f,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(f=typeof globalThis!="undefined"?globalThis:f||self,e(f["mm-react-embeddable"]={},f.React,f.PropTypes,f.styled))})(this,(function(f,e,w,j){"use strict";const I=e.createContext(null),v=({children:t})=>{const[i,a]=e.useState({}),[c,b]=e.useState({}),d=e.useRef({}),g=e.useRef({}),E=e.useRef({}),y=e.useCallback((r,o)=>{var n;return!!((n=E.current[r])!=null&&n.has(o))},[]),M=e.useCallback((r,o,n)=>{a(s=>({...s,[r]:{origin:o,frameRef:n}}))},[]),C=e.useCallback(r=>{a(o=>{const n={...o};return delete n[r],n}),b(o=>{const n={...o};return delete n[r],n}),delete E.current[r]},[]);e.useEffect(()=>{const r=(n=[],s)=>{n.forEach(u=>u(s))},o=n=>{var Z,q,F,W,H;if(!((Z=n==null?void 0:n.data)!=null&&Z.instanceId))return;const{instanceId:s,type:u}=n.data,m=d.current;if(u==="response"){const{callbackId:h,response:z,error:G}=n.data,B=g.current[h];B&&(B(z,G),delete g.current[h]);return}if(u==="mark-registered"){const{requestType:h}=n.data;E.current[s]||(E.current[s]=new Set),E.current[s].add(h);return}u==="ready"&&b(h=>({...h,[s]:!0})),r((q=m[s])==null?void 0:q[u],n.data),r((F=m["*"])==null?void 0:F[u],n.data),r((W=m[s])==null?void 0:W["*"],n.data),r((H=m["*"])==null?void 0:H["*"],n.data)};return window.addEventListener("message",o),()=>{window.removeEventListener("message",o)}},[i]);const l=e.useCallback((r,o,n)=>{var u,m;const s=i[r];if(!s){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}(m=(u=s==null?void 0:s.frameRef)==null?void 0:u.current)!=null&&m.contentWindow&&s.frameRef.current.contentWindow.postMessage({type:o,...n,instanceId:r},s.origin)},[i]),U=e.useRef(0),$=e.useCallback(()=>`${Date.now()}-${++U.current}`,[]),k=e.useCallback(async(r,o,n={})=>new Promise((s,u)=>{const m=$(),Z=(q,F)=>{delete g.current[m],F?u(F):s(q)};g.current[m]=Z,l(r,"request",{callbackId:m,requestType:o,data:n})}),[l]),p=e.useCallback((r,o)=>{l(r,"load-options",o)},[l]),L=e.useCallback((r,o)=>{l(r,"validate",o)},[l]),T=e.useMemo(()=>({registerFrame:M,unregisterFrame:C,sendMessage:l,sendRequest:k,loadOptions:p,validate:L,hasHandler:y,readyFrames:c,addListener:(r,o,n)=>(d.current[r]||(d.current[r]={}),d.current[r][o]||(d.current[r][o]=[]),d.current[r][o].push(n),()=>{d.current[r][o]=d.current[r][o].filter(s=>s!==n)})}),[M,C,l,k,p,L,y,c]);return e.createElement(I.Provider,{value:T},t)},S={},A=async t=>{try{const i=new URL(t).origin;if(S[i]!==void 0)return S[i];const c=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!c.version)throw new Error("Invalid manifest");return S[i]=c,c}catch{}return S[origin]=!1,!1},O=async t=>!!await A(t),x=(t,i,a)=>{const c=e.useContext(I);if(c===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=e.useRef(a);e.useEffect(()=>{b.current=a},[a]),e.useEffect(()=>{if(!(!t||!i)){if(i.startsWith("key:")){const d=i.substring(4);return c.readyFrames[t]&&c.sendMessage(t,"subscribe-key",{keyChord:d}),c.addListener(t,`key-pressed:${d}`,(...E)=>b.current(...E))}return c.addListener(t,i,(...d)=>b.current(...d))}},[c,t,i])};v.propTypes={children:w.node.isRequired};const D=(j.default||j).iframe`
1
+ (function(m,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],e):(m=typeof globalThis!="undefined"?globalThis:m||self,e(m["mm-react-embeddable"]={},m.React,m.PropTypes,m.styled))})(this,(function(m,e,$,j){"use strict";const q=e.createContext(null),v=({children:r})=>{const[u,l]=e.useState({}),[c,w]=e.useState({}),i=e.useRef({}),b=e.useRef({}),g=e.useRef({}),I=e.useCallback((t,s)=>{var n;return!!((n=g.current[t])!=null&&n.has(s))},[]),L=e.useCallback((t,s,n)=>{l(o=>({...o,[t]:{origin:s,frameRef:n}}))},[]),y=e.useCallback(t=>{l(s=>{const n={...s};return delete n[t],n}),w(s=>{const n={...s};return delete n[t],n}),delete g.current[t]},[]);e.useEffect(()=>{const t=(n=[],o)=>{n.forEach(d=>d(o))},s=n=>{var h,M,a,Z,P;if(!((h=n==null?void 0:n.data)!=null&&h.instanceId))return;const{instanceId:o,type:d}=n.data,f=i.current;if(d==="response"){const{callbackId:k,response:D,error:G}=n.data,z=b.current[k];z&&(z(D,G),delete b.current[k]);return}if(d==="mark-registered"){const{requestType:k}=n.data;g.current[o]||(g.current[o]=new Set),g.current[o].add(k);return}d==="ready"&&w(k=>({...k,[o]:!0})),t((M=f[o])==null?void 0:M[d],n.data),t((a=f["*"])==null?void 0:a[d],n.data),t((Z=f[o])==null?void 0:Z["*"],n.data),t((P=f["*"])==null?void 0:P["*"],n.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[u]);const E=e.useCallback((t,s,n)=>{var d,f;const o=u[t];if(!o){console.warn(`Origin not set for id: ${t}. Unable to send message.`);return}(f=(d=o==null?void 0:o.frameRef)==null?void 0:d.current)!=null&&f.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...n,instanceId:t},o.origin)},[u]),U=e.useRef(0),p=e.useCallback(()=>`${Date.now()}-${++U.current}`,[]),x=e.useCallback(async(t,s,n={})=>new Promise((o,d)=>{const f=p(),h=(M,a)=>{delete b.current[f],a?d(a):o(M)};b.current[f]=h,E(t,"request",{callbackId:f,requestType:s,data:n})}),[E]),S=e.useCallback((t,s)=>{E(t,"load-options",s)},[E]),O=e.useCallback((t,s)=>{E(t,"validate",s)},[E]),C=e.useMemo(()=>({registerFrame:L,unregisterFrame:y,sendMessage:E,sendRequest:x,loadOptions:S,validate:O,hasHandler:I,readyFrames:c,addListener:(t,s,n)=>(i.current[t]||(i.current[t]={}),i.current[t][s]||(i.current[t][s]=[]),i.current[t][s].push(n),()=>{i.current[t][s]=i.current[t][s].filter(o=>o!==n)})}),[L,y,E,x,S,O,I,c]);return e.createElement(q.Provider,{value:C},r)},T={},W=async r=>{try{const u=new URL(r).origin;if(T[u]!==void 0)return T[u];const c=await(await fetch(`${u}/mm-app-manifest.json`)).json();if(!c.version)throw new Error("Invalid manifest");return T[u]=c,c}catch{}return T[origin]=!1,!1},A=async r=>!!await W(r),F=(r,u,l)=>{const c=e.useContext(q);if(c===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const w=e.useRef(l);e.useEffect(()=>{w.current=l},[l]),e.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const i=u.substring(4);return c.readyFrames[r]&&c.sendMessage(r,"subscribe-key",{keyChord:i}),c.addListener(r,`key-pressed:${i}`,(...g)=>w.current(...g))}return c.addListener(r,u,(...i)=>w.current(...i))}},[c,r,u])};v.propTypes={children:$.node.isRequired};const B=(j.default||j).iframe`
2
2
  width: 100%;
3
3
  height: 100%;
4
4
  border: none;
5
- visibility: ${t=>t.$isReady?"visible":"hidden"};
6
- `,P=({id:t,src:i,path:a,onInit:c=()=>{},context:b={},params:d={},skipManifestCheck:g=!1,...E})=>{if("isConfiguring"in E)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in b)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in b)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const y=e.useRef(),[M,C]=e.useState(),[l,U]=e.useState(0),{registerFrame:$,unregisterFrame:k,sendMessage:p}=e.useContext(I),[L,T]=e.useState(!0),[r,o]=e.useState(),n=e.useRef();e.useEffect(()=>{l&&p(t,"set-context",{context:{...b,colorMode:d.colorMode,isFullScreen:d.isFullScreen}})},[b,d.colorMode,d.isFullScreen,t,l]),e.useEffect(()=>{l&&(p(t,"set-params",{params:d}),p(t,"set-configuring",{isConfiguring:d.isConfiguring}))},[d,t,l]),e.useEffect(()=>{a!==void 0&&o((a==null?void 0:a.indexOf("/"))===0?a:`/${a}`)},[a]),e.useEffect(()=>{r===void 0||!l||p(t,"navigate",{path:r})},[r,l,t,p]),e.useEffect(()=>{if(!(!i||!t||!y)){if(g){const u=new URL(i);$(t,u.origin,y),c(),C(`${u.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`)}else(async()=>{const u=await O(i);if(T(u),u){const m=new URL(i);$(t,m.origin,y),c(),C(`${m.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`)}})();return()=>{k(t)}}},[i,t,$,y,g]),e.useEffect(()=>{if(!(!g||!M))return n.current=setTimeout(()=>T(!1),5e3),()=>clearTimeout(n.current)},[M,g]),x(t,"ready",()=>{clearTimeout(n.current),U(u=>u+1)});const s=e.useRef();return x(t,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(y.current){const u=new URL(i);y.current.src=`${u.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),L?e.createElement(D,{$isReady:l,ref:y,src:M,...E}):e.createElement("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"}},e.createElement("div",{style:{textAlign:"center"}},"Application must support the MM App Framework to be embedded."))};P.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func,skipManifestCheck:w.bool},f.MMEmbeddableZone=P,f.MMEmbeddableZoneContext=I,f.MMEmbeddableZoneProvider=v,f.getAppInfo=A,f.testIsEmbeddable=O,f.useEmbeddedEffect=x,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
5
+ visibility: ${r=>r.$isReady?"visible":"hidden"};
6
+ `,H=({id:r,src:u,path:l,onInit:c=()=>{},onTimeout:w,context:i={},params:b={},skipManifestCheck:g=!1,readyTimeout:I=5e3,...L})=>{if("isConfiguring"in L)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in i)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in i)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const y=e.useRef(),[E,U]=e.useState(),[p,x]=e.useState(0),{registerFrame:S,unregisterFrame:O,sendMessage:C}=e.useContext(q),[t,s]=e.useState(!0),[n,o]=e.useState(),d=e.useRef(),f=e.useRef(w);e.useEffect(()=>{f.current=w},[w]),e.useEffect(()=>{p&&C(r,"set-context",{context:{...i,colorMode:b.colorMode,isFullScreen:b.isFullScreen}})},[i,b.colorMode,b.isFullScreen,r,p]),e.useEffect(()=>{p&&(C(r,"set-params",{params:b}),C(r,"set-configuring",{isConfiguring:b.isConfiguring}))},[b,r,p]),e.useEffect(()=>{l!==void 0&&o((l==null?void 0:l.indexOf("/"))===0?l:`/${l}`)},[l]),e.useEffect(()=>{n===void 0||!p||C(r,"navigate",{path:n})},[n,p,r,C]),e.useEffect(()=>{if(!(!u||!r||!y)){if(g){const a=new URL(u);s(!0),S(r,a.origin,y),c(),U(`${a.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`)}else(async()=>{const a=await A(u);if(s(a),a){const Z=new URL(u);S(r,Z.origin,y),c(),U(`${Z.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`)}})();return()=>{O(r)}}},[u,r,S,y,g]),e.useEffect(()=>{if(!(!g||!E||I==null))return d.current=setTimeout(()=>{f.current&&f.current()},I),()=>clearTimeout(d.current)},[E,g,I]);const h=()=>{clearTimeout(d.current)};F(r,"init",h),F(r,"authorize",h),F(r,"login",h),F(r,"ready",()=>{h(),x(a=>a+1)});const M=e.useRef();return F(r,"heartbeat",()=>{M.current&&clearTimeout(M.current),M.current=setTimeout(()=>{if(y.current){const a=new URL(u);y.current.src=`${a.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{M.current&&clearTimeout(M.current)},[]),!g&&!t?null:e.createElement(B,{$isReady:p,ref:y,src:E,...L})};H.propTypes={id:$.string,src:$.string.isRequired,path:$.string,onInit:$.func,onTimeout:$.func,skipManifestCheck:$.bool,readyTimeout:$.number},m.MMEmbeddableZone=H,m.MMEmbeddableZoneContext=q,m.MMEmbeddableZoneProvider=v,m.getAppInfo=W,m.testIsEmbeddable=A,m.useEmbeddedEffect=F,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machinemetrics/mm-react-embeddable",
3
- "version": "1.3.0-0",
3
+ "version": "1.3.0-2",
4
4
  "license": "MIT",
5
5
  "module": "dist/mm-react-embeddable.es.js",
6
6
  "main": "dist/mm-react-embeddable.cjs.js",