@machinemetrics/mm-react-embeddable 1.0.12 → 1.1.1-0

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