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