@opensea/vessel
Advanced tools
Comparing version 0.0.18 to 0.0.19
var m = Object.defineProperty; | ||
var O = (s, e, t) => e in s ? m(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; | ||
var i = (s, e, t) => (O(s, typeof e != "symbol" ? e + "" : e, t), t); | ||
var o = (s, e, t) => (O(s, typeof e != "symbol" ? e + "" : e, t), t); | ||
const l = /* @__PURE__ */ new Map(); | ||
@@ -56,7 +56,7 @@ function f(s, e, t) { | ||
constructor(e, t) { | ||
i(this, "type"); | ||
i(this, "id"); | ||
i(this, "from"); | ||
i(this, "operation"); | ||
i(this, "payload"); | ||
o(this, "type"); | ||
o(this, "id"); | ||
o(this, "from"); | ||
o(this, "operation"); | ||
o(this, "payload"); | ||
this.type = e, this.id = (t == null ? void 0 : t.id) ?? C(), this.from = (t == null ? void 0 : t.from) ?? v, this.operation = (t == null ? void 0 : t.operation) ?? I(), this.payload = t == null ? void 0 : t.payload; | ||
@@ -90,7 +90,7 @@ } | ||
constructor(e) { | ||
i(this, "log"); | ||
i(this, "parent", window.parent); | ||
i(this, "subscriptions"); | ||
i(this, "closed", !1); | ||
i(this, "parentOrigin"); | ||
o(this, "log"); | ||
o(this, "parent", window.parent); | ||
o(this, "subscriptions"); | ||
o(this, "closed", !1); | ||
o(this, "parentOrigin"); | ||
this.parentOrigin = e.parentOrigin, this.subscriptions = { | ||
@@ -104,7 +104,7 @@ [r.Message]: [], | ||
if (e === r.Close) { | ||
this.close(), this.subscriptions[e].forEach((o) => o(t)); | ||
this.close(), this.subscriptions[e].forEach((i) => i(t.payload)); | ||
return; | ||
} | ||
this.subscriptions[e].forEach((o) => { | ||
o(t, (a) => { | ||
this.subscriptions[e].forEach((i) => { | ||
i(t.payload, (a) => { | ||
n( | ||
@@ -157,3 +157,3 @@ new h(r.MessageReply, { | ||
this.log.info(`Resolving action ${e.operation}`); | ||
const { resolve: o, reject: a } = n; | ||
const { resolve: i, reject: a } = n; | ||
if (p(e.operation), (t = e.payload) != null && t.error) { | ||
@@ -163,3 +163,3 @@ a(e.payload.error); | ||
} | ||
o(e.payload); | ||
i(e.payload); | ||
} | ||
@@ -185,11 +185,11 @@ } catch (n) { | ||
async send(e, t = 5e3) { | ||
return new Promise((n, o) => { | ||
return new Promise((n, i) => { | ||
const a = new h(r.Message, { | ||
payload: e | ||
}); | ||
f(a.operation, { resolve: n, reject: o }, t); | ||
f(a.operation, { resolve: n, reject: i }, t); | ||
try { | ||
this.sendMessage(a); | ||
} catch (d) { | ||
this.log.error("Error sending message to parent", d), o(d); | ||
this.log.error("Error sending message to parent", d), i(d); | ||
} | ||
@@ -214,4 +214,4 @@ }); | ||
once(e, t) { | ||
const n = this.on(e, (...o) => { | ||
n(), t(...o); | ||
const n = this.on(e, (...i) => { | ||
n(), t(...i); | ||
}); | ||
@@ -230,14 +230,14 @@ } | ||
constructor(e) { | ||
i(this, "parentFrame"); | ||
i(this, "child"); | ||
i(this, "frame"); | ||
i(this, "handshakeOperation"); | ||
i(this, "handshakeInterval"); | ||
i(this, "handshakeIntervalTimeout", 500); | ||
i(this, "handshakeActionResolverId"); | ||
i(this, "subscriptions"); | ||
i(this, "log"); | ||
i(this, "childOrigin"); | ||
i(this, "childUrl"); | ||
i(this, "connected"); | ||
o(this, "parentFrame"); | ||
o(this, "child"); | ||
o(this, "frame"); | ||
o(this, "handshakeOperation"); | ||
o(this, "handshakeInterval"); | ||
o(this, "handshakeIntervalTimeout", 500); | ||
o(this, "handshakeActionResolverId"); | ||
o(this, "subscriptions"); | ||
o(this, "log"); | ||
o(this, "childOrigin"); | ||
o(this, "childUrl"); | ||
o(this, "connected"); | ||
this.frame = e.frame, this.parentFrame = window, this.child = null, this.childUrl = e.url, this.childOrigin = H(e.url), this.connected = !1, this.handshakeOperation = null, this.handshakeInterval = null, e.handshakeIntervalTimeout && (this.handshakeIntervalTimeout = e.handshakeIntervalTimeout), this.subscriptions = { | ||
@@ -274,7 +274,7 @@ [r.Message]: [], | ||
if (e === r.Close) { | ||
this.close(), this.subscriptions[e].forEach((o) => o(t)); | ||
this.close(), this.subscriptions[e].forEach((i) => i(t.payload)); | ||
return; | ||
} | ||
this.subscriptions[e].forEach((o) => { | ||
o(t, (a) => { | ||
this.subscriptions[e].forEach((i) => { | ||
i(t.payload, (a) => { | ||
n( | ||
@@ -298,5 +298,5 @@ new h(r.MessageReply, { | ||
this.log.info(`Resolving action ${this.handshakeActionResolverId}`); | ||
const { resolve: n, reject: o } = t; | ||
const { resolve: n, reject: i } = t; | ||
if (p(this.handshakeActionResolverId), (a = e.payload) != null && a.error) { | ||
o(e.payload.error); | ||
i(e.payload.error); | ||
return; | ||
@@ -338,3 +338,3 @@ } | ||
this.log.info(`Resolving action ${e.operation}`); | ||
const { resolve: o, reject: a } = n; | ||
const { resolve: i, reject: a } = n; | ||
if (p(e.operation), (t = e.payload) != null && t.error) { | ||
@@ -344,3 +344,3 @@ a(e.payload.error); | ||
} | ||
o(e.payload); | ||
i(e.payload); | ||
} | ||
@@ -366,11 +366,11 @@ } catch (n) { | ||
async send(e, t = 5e3) { | ||
return new Promise((n, o) => { | ||
return new Promise((n, i) => { | ||
const a = new h(r.Message, { | ||
payload: e | ||
}); | ||
f(a.operation, { resolve: n, reject: o }, t); | ||
f(a.operation, { resolve: n, reject: i }, t); | ||
try { | ||
this.sendMessage(a); | ||
} catch (d) { | ||
this.log.error("Error sending message to child", d), o(d); | ||
this.log.error("Error sending message to child", d), i(d); | ||
} | ||
@@ -400,4 +400,4 @@ }); | ||
once(e, t) { | ||
const n = this.on(e, (...o) => { | ||
n(), t(...o); | ||
const n = this.on(e, (...i) => { | ||
n(), t(...i); | ||
}); | ||
@@ -420,3 +420,3 @@ } | ||
} | ||
i(y, "ParentFrame", k), i(y, "ChildFrame", w); | ||
o(y, "ParentFrame", k), o(y, "ChildFrame", w); | ||
export { | ||
@@ -423,0 +423,0 @@ w as ChildFrame, |
@@ -1,1 +0,1 @@ | ||
(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.Vessel={}))})(this,function(l){"use strict";var $=Object.defineProperty;var S=(l,a,d)=>a in l?$(l,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):l[a]=d;var r=(l,a,d)=>(S(l,typeof a!="symbol"?a+"":a,d),d);const a=new Map;function d(s,e,t){const n=a.get(s);n&&(n.timeout&&clearTimeout(n.timeout),n.reject(new Error("[Vessel] Same action fired again. Keeping only latest.")),a.delete(s)),a.set(s,e),typeof t=="number"&&t>0&&(e.timeout=setTimeout(()=>{f(s),e.reject(new Error("[Vessel] Action timed out."))},t))}function f(s){const e=a.get(s);e&&(e.timeout&&clearTimeout(e.timeout),a.delete(s))}function g(s){return a.get(s)}function b(s){return`${s?`[VESSEL-${s.toUpperCase()}]:`:"[VESSEL]:"}`.trimStart()}function v(s="log",e){const t=b(e==null?void 0:e.scope);return(...n)=>{e!=null&&e.debug&&console[s](t,...n)}}function R(s){return{info:v("info",s),warn:v("warn",s),error:v("error",s)}}class p extends Error{constructor(e,t){super(`${b(t)} ${e}`),this.name="VesselError"}}let m=(s=21)=>crypto.getRandomValues(new Uint8Array(s)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");const w="application/x-opensea-vessel-v1+json";function I(){return`op-${m()}`}function O(){return`msg-${m()}`}function C(s){const e=new URL(s);return`${e.protocol}//${e.host}`}var i=(s=>(s.Handshake="handshake",s.HandshakeReply="handshake-reply",s.Message="message",s.MessageReply="message-reply",s.Close="close",s))(i||{});class c{constructor(e,t){r(this,"type");r(this,"id");r(this,"from");r(this,"operation");r(this,"payload");this.type=e,this.id=(t==null?void 0:t.id)??O(),this.from=(t==null?void 0:t.from)??w,this.operation=(t==null?void 0:t.operation)??I(),this.payload=t==null?void 0:t.payload}toObject(){return{type:this.type,id:this.id,from:this.from,operation:this.operation,payload:this.payload}}}function H(s,e=w){if(s&&typeof s=="object"&&"from"in s&&s.from===e&&"type"in s&&Object.values(i).includes(s.type)&&"id"in s&&"operation"in s)return new c(s.type,{id:s.id,from:s.from,operation:s.operation,payload:s.payload});throw new p("Invalid message.")}function E(s,e,t){if(e==="*"||s.origin===e)return H(s.data,t??w)}class k{constructor(e){r(this,"log");r(this,"parent",window.parent);r(this,"subscriptions");r(this,"closed",!1);r(this,"parentOrigin");this.parentOrigin=e.parentOrigin,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=R({debug:e.debug,scope:"CHILD"}),this.onMessage=this.onMessage.bind(this),window.addEventListener("message",this.onMessage,!1)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(o=>o(t));return}this.subscriptions[e].forEach(o=>{o(t,h=>{n(new c(i.MessageReply,{operation:t.operation,payload:h}))})})}onMessage(e){try{const t=E(e,this.parentOrigin);if(!t)return;switch(t.type){case i.Handshake:this.sendHandshakeReply(t);return;case i.Message:this.callSubscriptions(i.Message,t);return;case i.MessageReply:this.resolveReply(t);return}}catch(t){this.log.error("Error parsing message from parent",t)}}sendHandshakeReply(e){this.log.info(`Received handshake from ${this.parentOrigin}`);const t=new c(i.HandshakeReply,{operation:e.operation});try{this.parent.postMessage(t.toObject(),this.parentOrigin)}catch(n){this.log.error("Error sending handshake reply",n)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=g(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:o,reject:h}=n;if(f(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}o(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.parent)throw new p("Parent frame not found.");if(this.closed)throw new p("Cannot send message, frame is closed.");this.parent.postMessage(e.toObject(),this.parentOrigin)}async send(e,t=5e3){return new Promise((n,o)=>{const h=new c(i.Message,{payload:e});d(h.operation,{resolve:n,reject:o},t);try{this.sendMessage(h)}catch(u){this.log.error("Error sending message to parent",u),o(u)}})}emit(e){const t=new c(i.Message,{payload:e});this.sendMessage(t)}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...o)=>{n(),t(...o)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.closed=!0,window.removeEventListener("message",this.onMessage);const e=new c(i.Close);this.parent.postMessage(e.toObject(),this.parentOrigin)}}class y{constructor(e){r(this,"parentFrame");r(this,"child");r(this,"frame");r(this,"handshakeOperation");r(this,"handshakeInterval");r(this,"handshakeIntervalTimeout",500);r(this,"handshakeActionResolverId");r(this,"subscriptions");r(this,"log");r(this,"childOrigin");r(this,"childUrl");r(this,"connected");this.frame=e.frame,this.parentFrame=window,this.child=null,this.childUrl=e.url,this.childOrigin=C(e.url),this.connected=!1,this.handshakeOperation=null,this.handshakeInterval=null,e.handshakeIntervalTimeout&&(this.handshakeIntervalTimeout=e.handshakeIntervalTimeout),this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=R({debug:e.debug,scope:"PARENT"}),this.parentFrame.addEventListener("message",this.onMessage.bind(this),!1),this.handshakeActionResolverId=I()}startHandshake(){if(this.log.info(`Sending handshake to ${this.childUrl}`),this.child=this.frame.contentWindow,!this.child)throw new Error("Child frame not found");let e=0;const t=()=>{if(this.log.info("Attempting handshake..."),this.child){e+=1;const n=new c(i.Handshake);this.handshakeOperation=n.operation,this.sendMessage(n)}};t(),this.handshakeInterval=window.setInterval(()=>{if(e>=5){this.log.warn("Handshake failed after 5 attempts. Giving up."),this.handshakeInterval&&window.clearInterval(this.handshakeInterval);return}t()},this.handshakeIntervalTimeout)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(o=>o(t));return}this.subscriptions[e].forEach(o=>{o(t,h=>{n(new c(i.MessageReply,{operation:t.operation,payload:h}))})})}handleHandshakeReply(e){var h;if(this.log.info(`Received handshake reply from ${this.childUrl}`),e.operation!==this.handshakeOperation)throw new Error("Handshake reply operation does not match");this.handshakeInterval&&window.clearInterval(this.handshakeInterval),this.connected=!0;const t=g(this.handshakeActionResolverId);if(!t)throw new p("Handshake resolver not found");this.log.info(`Resolving action ${this.handshakeActionResolverId}`);const{resolve:n,reject:o}=t;if(f(this.handshakeActionResolverId),(h=e.payload)!=null&&h.error){o(e.payload.error);return}n(e.payload)}onMessage(e){try{const t=E(e,this.childOrigin);if(!t)return;switch(t.type){case i.HandshakeReply:this.handleHandshakeReply(t);break;case i.Message:this.callSubscriptions(i.Message,t);break;case i.MessageReply:this.resolveReply(t);break;case i.Close:this.callSubscriptions(i.Close,t);break;default:this.log.warn(`Unknown message type: ${t.type}`)}}catch(t){this.log.error("Error handling message",t)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=g(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:o,reject:h}=n;if(f(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}o(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.child)throw new p("Child frame not found.");if(e.type!==i.Handshake&&!this.connected)throw new p("Not connected to child frame.");this.child.postMessage(e.toObject(),this.childOrigin)}async send(e,t=5e3){return new Promise((n,o)=>{const h=new c(i.Message,{payload:e});d(h.operation,{resolve:n,reject:o},t);try{this.sendMessage(h)}catch(u){this.log.error("Error sending message to child",u),o(u)}})}emit(e){const t=new c(i.Message,{payload:e});this.sendMessage(t)}connect({isFrameLoaded:e=!1}={}){return new Promise((t,n)=>{this.log.info(`Connecting to child iframe ${this.childUrl}`),e?this.startHandshake():this.frame.addEventListener("load",this.startHandshake.bind(this)),d(this.handshakeActionResolverId,{resolve:t,reject:n})})}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...o)=>{n(),t(...o)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.log.info("Closing frames"),this.subscriptions[i.Message]=[],this.subscriptions[i.Close]=[],this.handshakeOperation=null,this.handshakeInterval&&window.clearInterval(this.handshakeInterval),f(this.handshakeActionResolverId),this.connected=!1}}class M{static createParentFrame(e){return new y(e)}static createChildFrame(e){return new k(e)}}r(M,"ParentFrame",y),r(M,"ChildFrame",k),l.ChildFrame=k,l.MessageType=i,l.ParentFrame=y,l.Vessel=M,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}); | ||
(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.Vessel={}))})(this,function(l){"use strict";var $=Object.defineProperty;var S=(l,a,d)=>a in l?$(l,a,{enumerable:!0,configurable:!0,writable:!0,value:d}):l[a]=d;var o=(l,a,d)=>(S(l,typeof a!="symbol"?a+"":a,d),d);const a=new Map;function d(s,e,t){const n=a.get(s);n&&(n.timeout&&clearTimeout(n.timeout),n.reject(new Error("[Vessel] Same action fired again. Keeping only latest.")),a.delete(s)),a.set(s,e),typeof t=="number"&&t>0&&(e.timeout=setTimeout(()=>{f(s),e.reject(new Error("[Vessel] Action timed out."))},t))}function f(s){const e=a.get(s);e&&(e.timeout&&clearTimeout(e.timeout),a.delete(s))}function g(s){return a.get(s)}function b(s){return`${s?`[VESSEL-${s.toUpperCase()}]:`:"[VESSEL]:"}`.trimStart()}function v(s="log",e){const t=b(e==null?void 0:e.scope);return(...n)=>{e!=null&&e.debug&&console[s](t,...n)}}function R(s){return{info:v("info",s),warn:v("warn",s),error:v("error",s)}}class p extends Error{constructor(e,t){super(`${b(t)} ${e}`),this.name="VesselError"}}let m=(s=21)=>crypto.getRandomValues(new Uint8Array(s)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");const w="application/x-opensea-vessel-v1+json";function I(){return`op-${m()}`}function O(){return`msg-${m()}`}function C(s){const e=new URL(s);return`${e.protocol}//${e.host}`}var i=(s=>(s.Handshake="handshake",s.HandshakeReply="handshake-reply",s.Message="message",s.MessageReply="message-reply",s.Close="close",s))(i||{});class c{constructor(e,t){o(this,"type");o(this,"id");o(this,"from");o(this,"operation");o(this,"payload");this.type=e,this.id=(t==null?void 0:t.id)??O(),this.from=(t==null?void 0:t.from)??w,this.operation=(t==null?void 0:t.operation)??I(),this.payload=t==null?void 0:t.payload}toObject(){return{type:this.type,id:this.id,from:this.from,operation:this.operation,payload:this.payload}}}function H(s,e=w){if(s&&typeof s=="object"&&"from"in s&&s.from===e&&"type"in s&&Object.values(i).includes(s.type)&&"id"in s&&"operation"in s)return new c(s.type,{id:s.id,from:s.from,operation:s.operation,payload:s.payload});throw new p("Invalid message.")}function E(s,e,t){if(e==="*"||s.origin===e)return H(s.data,t??w)}class y{constructor(e){o(this,"log");o(this,"parent",window.parent);o(this,"subscriptions");o(this,"closed",!1);o(this,"parentOrigin");this.parentOrigin=e.parentOrigin,this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=R({debug:e.debug,scope:"CHILD"}),this.onMessage=this.onMessage.bind(this),window.addEventListener("message",this.onMessage,!1)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t.payload));return}this.subscriptions[e].forEach(r=>{r(t.payload,h=>{n(new c(i.MessageReply,{operation:t.operation,payload:h}))})})}onMessage(e){try{const t=E(e,this.parentOrigin);if(!t)return;switch(t.type){case i.Handshake:this.sendHandshakeReply(t);return;case i.Message:this.callSubscriptions(i.Message,t);return;case i.MessageReply:this.resolveReply(t);return}}catch(t){this.log.error("Error parsing message from parent",t)}}sendHandshakeReply(e){this.log.info(`Received handshake from ${this.parentOrigin}`);const t=new c(i.HandshakeReply,{operation:e.operation});try{this.parent.postMessage(t.toObject(),this.parentOrigin)}catch(n){this.log.error("Error sending handshake reply",n)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=g(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(f(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.parent)throw new p("Parent frame not found.");if(this.closed)throw new p("Cannot send message, frame is closed.");this.parent.postMessage(e.toObject(),this.parentOrigin)}async send(e,t=5e3){return new Promise((n,r)=>{const h=new c(i.Message,{payload:e});d(h.operation,{resolve:n,reject:r},t);try{this.sendMessage(h)}catch(u){this.log.error("Error sending message to parent",u),r(u)}})}emit(e){const t=new c(i.Message,{payload:e});this.sendMessage(t)}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.closed=!0,window.removeEventListener("message",this.onMessage);const e=new c(i.Close);this.parent.postMessage(e.toObject(),this.parentOrigin)}}class k{constructor(e){o(this,"parentFrame");o(this,"child");o(this,"frame");o(this,"handshakeOperation");o(this,"handshakeInterval");o(this,"handshakeIntervalTimeout",500);o(this,"handshakeActionResolverId");o(this,"subscriptions");o(this,"log");o(this,"childOrigin");o(this,"childUrl");o(this,"connected");this.frame=e.frame,this.parentFrame=window,this.child=null,this.childUrl=e.url,this.childOrigin=C(e.url),this.connected=!1,this.handshakeOperation=null,this.handshakeInterval=null,e.handshakeIntervalTimeout&&(this.handshakeIntervalTimeout=e.handshakeIntervalTimeout),this.subscriptions={[i.Message]:[],[i.Close]:[]},this.log=R({debug:e.debug,scope:"PARENT"}),this.parentFrame.addEventListener("message",this.onMessage.bind(this),!1),this.handshakeActionResolverId=I()}startHandshake(){if(this.log.info(`Sending handshake to ${this.childUrl}`),this.child=this.frame.contentWindow,!this.child)throw new Error("Child frame not found");let e=0;const t=()=>{if(this.log.info("Attempting handshake..."),this.child){e+=1;const n=new c(i.Handshake);this.handshakeOperation=n.operation,this.sendMessage(n)}};t(),this.handshakeInterval=window.setInterval(()=>{if(e>=5){this.log.warn("Handshake failed after 5 attempts. Giving up."),this.handshakeInterval&&window.clearInterval(this.handshakeInterval);return}t()},this.handshakeIntervalTimeout)}callSubscriptions(e,t){const n=this.sendMessage.bind(this);if(e===i.Close){this.close(),this.subscriptions[e].forEach(r=>r(t.payload));return}this.subscriptions[e].forEach(r=>{r(t.payload,h=>{n(new c(i.MessageReply,{operation:t.operation,payload:h}))})})}handleHandshakeReply(e){var h;if(this.log.info(`Received handshake reply from ${this.childUrl}`),e.operation!==this.handshakeOperation)throw new Error("Handshake reply operation does not match");this.handshakeInterval&&window.clearInterval(this.handshakeInterval),this.connected=!0;const t=g(this.handshakeActionResolverId);if(!t)throw new p("Handshake resolver not found");this.log.info(`Resolving action ${this.handshakeActionResolverId}`);const{resolve:n,reject:r}=t;if(f(this.handshakeActionResolverId),(h=e.payload)!=null&&h.error){r(e.payload.error);return}n(e.payload)}onMessage(e){try{const t=E(e,this.childOrigin);if(!t)return;switch(t.type){case i.HandshakeReply:this.handleHandshakeReply(t);break;case i.Message:this.callSubscriptions(i.Message,t);break;case i.MessageReply:this.resolveReply(t);break;case i.Close:this.callSubscriptions(i.Close,t);break;default:this.log.warn(`Unknown message type: ${t.type}`)}}catch(t){this.log.error("Error handling message",t)}}resolveReply(e){var t;this.log.info(`Received reply for action ${e.operation}`);try{const n=g(e.operation);if(n){this.log.info(`Resolving action ${e.operation}`);const{resolve:r,reject:h}=n;if(f(e.operation),(t=e.payload)!=null&&t.error){h(e.payload.error);return}r(e.payload)}}catch(n){this.log.error("Error resolving reply",n)}}sendMessage(e){if(!this.child)throw new p("Child frame not found.");if(e.type!==i.Handshake&&!this.connected)throw new p("Not connected to child frame.");this.child.postMessage(e.toObject(),this.childOrigin)}async send(e,t=5e3){return new Promise((n,r)=>{const h=new c(i.Message,{payload:e});d(h.operation,{resolve:n,reject:r},t);try{this.sendMessage(h)}catch(u){this.log.error("Error sending message to child",u),r(u)}})}emit(e){const t=new c(i.Message,{payload:e});this.sendMessage(t)}connect({isFrameLoaded:e=!1}={}){return new Promise((t,n)=>{this.log.info(`Connecting to child iframe ${this.childUrl}`),e?this.startHandshake():this.frame.addEventListener("load",this.startHandshake.bind(this)),d(this.handshakeActionResolverId,{resolve:t,reject:n})})}on(e,t){return this.subscriptions[e].push(t),()=>{this.off.call(this,e,t)}}once(e,t){const n=this.on(e,(...r)=>{n(),t(...r)})}off(e,t){this.subscriptions[e]=this.subscriptions[e].filter(n=>n!==t)}close(){this.log.info("Closing frames"),this.subscriptions[i.Message]=[],this.subscriptions[i.Close]=[],this.handshakeOperation=null,this.handshakeInterval&&window.clearInterval(this.handshakeInterval),f(this.handshakeActionResolverId),this.connected=!1}}class M{static createParentFrame(e){return new k(e)}static createChildFrame(e){return new y(e)}}o(M,"ParentFrame",k),o(M,"ChildFrame",y),l.ChildFrame=y,l.MessageType=i,l.ParentFrame=k,l.Vessel=M,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}); |
@@ -1,2 +0,2 @@ | ||
import { Message, MessagePayloadType, MessageType } from "./utils/messaging"; | ||
import { MessagePayloadType, MessageType } from "./utils/messaging"; | ||
type ParentFrameParams = { | ||
@@ -8,4 +8,4 @@ frame: HTMLIFrameElement; | ||
}; | ||
export type ActionSubscriptionFn = (message: Message<any>, reply: (payload: MessagePayloadType) => void) => void; | ||
export type CloseSubscriptionFn = (message: Message<any>) => void; | ||
export type ActionSubscriptionFn = (message: MessagePayloadType, reply: (payload: MessagePayloadType) => void) => void; | ||
export type CloseSubscriptionFn = (message: MessagePayloadType) => void; | ||
type ConnectOptions = { | ||
@@ -12,0 +12,0 @@ isFrameLoaded?: boolean; |
{ | ||
"name": "@opensea/vessel", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "🚢 Vessel: a promise-based postMessage library that sails your data smoothly across the Opensea.", | ||
@@ -56,7 +56,4 @@ "files": [ | ||
"no-hats" | ||
], | ||
".{ts,tsx}": [ | ||
"yarn lint:fix" | ||
] | ||
} | ||
} |
@@ -56,3 +56,3 @@ import { ActionSubscriptionFn, CloseSubscriptionFn } from "./parent" | ||
this.close() | ||
this.subscriptions[type].forEach(fn => fn(message)) | ||
this.subscriptions[type].forEach(fn => fn(message.payload)) | ||
return | ||
@@ -62,3 +62,3 @@ } | ||
this.subscriptions[type].forEach(fn => { | ||
fn(message, payload => { | ||
fn(message.payload, payload => { | ||
send( | ||
@@ -65,0 +65,0 @@ new Message(MessageType.MessageReply, { |
@@ -24,7 +24,7 @@ import { | ||
export type ActionSubscriptionFn = ( | ||
message: Message<any>, | ||
message: MessagePayloadType, | ||
reply: (payload: MessagePayloadType) => void, | ||
) => void | ||
export type CloseSubscriptionFn = (message: Message<any>) => void | ||
export type CloseSubscriptionFn = (message: MessagePayloadType) => void | ||
@@ -130,3 +130,3 @@ type ConnectOptions = { | ||
this.close() | ||
this.subscriptions[type].forEach(fn => fn(message)) | ||
this.subscriptions[type].forEach(fn => fn(message.payload)) | ||
return | ||
@@ -136,3 +136,3 @@ } | ||
this.subscriptions[type].forEach(fn => { | ||
fn(message, payload => { | ||
fn(message.payload, payload => { | ||
send( | ||
@@ -139,0 +139,0 @@ new Message(MessageType.MessageReply, { |
75894