@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) • 4.86 kB
JavaScript
var e,t,n=(e=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,u,f=this._events[c],l=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),l){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,o),!0;case 4:return f.fn.call(f.context,t,o,r),!0;case 5:return f.fn.call(f.context,t,o,r,s),!0;case 6:return f.fn.call(f.context,t,o,r,s,i),!0}for(u=1,a=new Array(l-1);u<l;u++)a[u-1]=arguments[u];f.fn.apply(f.context,a)}else{var p,d=f.length;for(u=0;u<d;u++)switch(f[u].once&&this.removeListener(e,f[u].fn,void 0,!0),l){case 1:f[u].fn.call(f[u].context);break;case 2:f[u].fn.call(f[u].context,t);break;case 3:f[u].fn.call(f[u].context,t,o);break;case 4:f[u].fn.call(f[u].context,t,o,r);break;default:if(!a)for(p=1,a=new Array(l-1);p<l;p++)a[p-1]=arguments[p];f[u].fn.apply(f[u].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,u=[],f=c.length;a<f;a++)(c[a].fn!==t||r&&!c[a].once||o&&c[a].context!==o)&&u.push(c[a]);u.length?this._events[s]=1===u.length?u[0]:u: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},e(t={exports:{}}),t.exports);function o(){const e="from-webrtc-remote-control";return{isConnectionFromRemote:t=>t.metadata===e,connMetadata:e}}exports.default=function({humanizeError:e,getPeerId:t,setPeerIdToSessionStorage:r}){return{humanizeError:e,getPeerId:t,bindConnection:(e,t)=>new Promise(s=>{let i=null;const c=new n,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)},u=n=>{i=null,i=function(e,t,n,r){const{connMetadata:s}=o(),i=e.connect(t,{serialization:"json",metadata:s});return i.on("open",()=>{"function"==typeof r&&r()}),i.on("data",e=>{n.emit("data",{from:"master"},e)}),i}(e,t,c,n),i.on("close",()=>{c.emit("remote.disconnect",{id:e.id}),u(()=>{c.emit("remote.reconnect",{id:e.id})})})};e.on("open",e=>{r(e),u(()=>s(a)),i.on("error",e=>{console.log("conn.error",e)}),window.addEventListener("beforeunload",()=>{i&&i.disconnect&&i.disconnect()})})})}},exports.prepareUtils=function({sessionStorageKey:e,humanErrors:t}={}){const n=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}=o(),{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:n,isConnectionFromRemote:r,getPeerId:s,setPeerIdToSessionStorage:i}};
//# sourceMappingURL=remote.js.map