@machinemetrics/mm-react-embeddable 1.2.1-0 → 1.3.0-1

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