@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) • 5.88 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).webrtcRemoteControl={})}(this,function(e){var t=function(e){var t={exports:{}};return function(e){var t=Object.prototype.hasOwnProperty,n="~";function o(){}function r(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,o,s,i){if("function"!=typeof o)throw new TypeError("The listener must be a function");var c=new r(o,s||e,i),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],c]:e._events[a].push(c):(e._events[a]=c,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new o:delete e._events[t]}function c(){this._events=new o,this._eventsCount=0}Object.create&&(o.prototype=Object.create(null),(new o).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,o,r=[];if(0===this._eventsCount)return r;for(o in e=this._events)t.call(e,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r},c.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var o=0,r=t.length,s=new Array(r);o<r;o++)s[o]=t[o].fn;return s},c.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},c.prototype.emit=function(e,t,o,r,s,i){var c=n?n+e:e;if(!this._events[c])return!1;var a,f,u=this._events[c],l=arguments.length;if(u.fn){switch(u.once&&this.removeListener(e,u.fn,void 0,!0),l){case 1:return u.fn.call(u.context),!0;case 2:return u.fn.call(u.context,t),!0;case 3:return u.fn.call(u.context,t,o),!0;case 4:return u.fn.call(u.context,t,o,r),!0;case 5:return u.fn.call(u.context,t,o,r,s),!0;case 6:return u.fn.call(u.context,t,o,r,s,i),!0}for(f=1,a=new Array(l-1);f<l;f++)a[f-1]=arguments[f];u.fn.apply(u.context,a)}else{var p,d=u.length;for(f=0;f<d;f++)switch(u[f].once&&this.removeListener(e,u[f].fn,void 0,!0),l){case 1:u[f].fn.call(u[f].context);break;case 2:u[f].fn.call(u[f].context,t);break;case 3:u[f].fn.call(u[f].context,t,o);break;case 4:u[f].fn.call(u[f].context,t,o,r);break;default:if(!a)for(p=1,a=new Array(l-1);p<l;p++)a[p-1]=arguments[p];u[f].fn.apply(u[f].context,a)}}return!0},c.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,o,r){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return i(this,s),this;var c=this._events[s];if(c.fn)c.fn!==t||r&&!c.once||o&&c.context!==o||i(this,s);else{for(var a=0,f=[],u=c.length;a<u;a++)(c[a].fn!==t||r&&!c[a].once||o&&c[a].context!==o)&&f.push(c[a]);f.length?this._events[s]=1===f.length?f[0]:f:i(this,s)}return this},c.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&i(this,t):(this._events=new o,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c}(t),t.exports}();function n(){const e="from-webrtc-remote-control";return{isConnectionFromRemote:t=>t.metadata===e,connMetadata:e}}function o({sessionStorageKey:e,humanErrors:t}={}){const o=function({mapping:e,withTechicalErrorMessage:t}={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 o=n[e.type]||("function"==typeof n.default?n.default(e):n.default);return o&&e.message&&t?`${o} (${e.message})`:o}}(t),{isConnectionFromRemote:r}=n(),{getPeerId:s,setPeerIdToSessionStorage:i}=function(e="webrtc-remote-control-peer-id"){return{getPeerId:()=>sessionStorage.getItem(e),setPeerIdToSessionStorage(t){sessionStorage.setItem(e,t)}}}(e);return{humanizeError:o,isConnectionFromRemote:r,getPeerId:s,setPeerIdToSessionStorage:i}}var r={__proto__:null,default:function({humanizeError:e,getPeerId:o,setPeerIdToSessionStorage:r}){return{humanizeError:e,getPeerId:o,bindConnection:(e,o)=>new Promise(s=>{let i=null;const c=new t,a={send(e){i?i.send(e):console.warning("You called `send` with no connection")},on:c.on.bind(c),off:c.off.bind(c)},f=t=>{i=null,i=function(e,t,o,r){const{connMetadata:s}=n(),i=e.connect(t,{serialization:"json",metadata:s});return i.on("open",()=>{"function"==typeof r&&r()}),i.on("data",e=>{o.emit("data",{from:"master"},e)}),i}(e,o,c,t),i.on("close",()=>{c.emit("remote.disconnect",{id:e.id}),f(()=>{c.emit("remote.reconnect",{id:e.id})})})};e.on("open",e=>{r(e),f(()=>s(a)),i.on("error",e=>{console.log("conn.error",e)}),window.addEventListener("beforeunload",()=>{i&&i.disconnect&&i.disconnect()})})})}},prepareUtils:o};e.master={__proto__:null,default:function({humanizeError:e,isConnectionFromRemote:n,getPeerId:o,setPeerIdToSessionStorage:r}){return{humanizeError:e,isConnectionFromRemote:n,getPeerId:o,bindConnection:e=>new Promise(o=>{const s=new t,i=new Map,c={sendTo(e,t){const n=i.get(e);return n?n.send(t):null},sendAll(e){[...i.values()].forEach(t=>{t.send(e)})},on:s.on.bind(s),off:s.off.bind(s)};e.on("open",e=>{r(e),o(c)}),e.on("connection",e=>{n(e)&&(i.set(e.peer,e),e.on("open",()=>{s.emit("remote.connect",{id:e.peer}),console.log("connections",i)}),e.on("data",t=>{s.emit("data",{id:e.peer,from:"remote"},t)}),e.on("close",()=>{i.delete(e.peer),s.emit("remote.disconnect",{id:e.peer}),console.log("connections",i)}))})})}},prepareUtils:o},e.prepareUtils=o,e.remote=r});
//# sourceMappingURL=webrtc-remote-control.umd.prod.js.map