UNPKG

@webrtc-remote-control/core

Version:

Thin abstraction layer above peerjs that will let you be more productive at making WebRTC data channels based apps.

3 lines (2 loc) 3.24 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("eventemitter3")):"function"==typeof define&&define.amd?define(["exports","eventemitter3"],o):o((e||self).webrtcRemoteControl={},e.eventemitter3)}(this,function(e,o){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/n(o);function r(){const e="from-webrtc-remote-control";return{isConnectionFromRemote:o=>o.metadata===e,connMetadata:e}}function i({sessionStorageKey:e,humanErrors:o}={}){const n=function({mapping:e,withTechicalErrorMessage:o}={mapping:{},withTechicalErrorMessage:!0}){const n={"browser-incompatible":"Your browser doesn't support WebRTC features, please try with a recent browser.",disconnected:"You are disconnected and can't make any more peer connection, please reload.",network:"It seems you're experimenting some network problems.","peer-unavailable":"The peer you were connected to seems to have lost connection, try to reload it.","server-error":"An error occured on the signaling server. Sorry, try to come back later.",default:e=>"An error occured"+(e.type?` - type: ${e.type}`:""),...e};return function(e){const t=n[e.type]||("function"==typeof n.default?n.default(e):n.default);return t&&e.message&&o?`${t} (${e.message})`:t}}(o),{isConnectionFromRemote:t}=r(),{getPeerId:i,setPeerIdToSessionStorage:s}=function(e="webrtc-remote-control-peer-id"){return{getPeerId:()=>sessionStorage.getItem(e),setPeerIdToSessionStorage(o){sessionStorage.setItem(e,o)}}}(e);return{humanizeError:n,isConnectionFromRemote:t,getPeerId:i,setPeerIdToSessionStorage:s}}var s={__proto__:null,default:function({humanizeError:e,getPeerId:o,setPeerIdToSessionStorage:n}){return{humanizeError:e,getPeerId:o,bindConnection:(e,o)=>new Promise(i=>{let s=null;const c=new t.default,a={send(e){s?s.send(e):console.warning("You called `send` with no connection")},on:c.on.bind(c),off:c.off.bind(c)},d=n=>{s=null,s=function(e,o,n,t){const{connMetadata:i}=r(),s=e.connect(o,{serialization:"json",metadata:i});return s.on("open",()=>{"function"==typeof t&&t()}),s.on("data",e=>{n.emit("data",{from:"master"},e)}),s}(e,o,c,n),s.on("close",()=>{c.emit("remote.disconnect",{id:e.id}),d(()=>{c.emit("remote.reconnect",{id:e.id})})})};e.on("open",e=>{n(e),d(()=>i(a)),s.on("error",e=>{console.log("conn.error",e)}),window.addEventListener("beforeunload",()=>{s&&s.disconnect&&s.disconnect()})})})}},prepareUtils:i};e.master={__proto__:null,default:function({humanizeError:e,isConnectionFromRemote:o,getPeerId:n,setPeerIdToSessionStorage:r}){return{humanizeError:e,isConnectionFromRemote:o,getPeerId:n,bindConnection:e=>new Promise(n=>{const i=new t.default,s=new Map,c={sendTo(e,o){const n=s.get(e);return n?n.send(o):null},sendAll(e){[...s.values()].forEach(o=>{o.send(e)})},on:i.on.bind(i),off:i.off.bind(i)};e.on("open",e=>{r(e),n(c)}),e.on("connection",e=>{o(e)&&(s.set(e.peer,e),e.on("open",()=>{i.emit("remote.connect",{id:e.peer}),console.log("connections",s)}),e.on("data",o=>{i.emit("data",{id:e.peer,from:"remote"},o)}),e.on("close",()=>{s.delete(e.peer),i.emit("remote.disconnect",{id:e.peer}),console.log("connections",s)}))})})}},prepareUtils:i},e.prepareUtils=i,e.remote=s}); //# sourceMappingURL=index.umd.js.map