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