UNPKG

@webrtc-remote-control/vue

Version:

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

3 lines (2 loc) 8.23 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("vue")):"function"==typeof define&&define.amd?define(["exports","vue"],n):n((e||self).webrtcRemoteControlVue={},e.Vue)}(this,function(e,n){var t=function(e){var n={exports:{}};return function(e){var n=Object.prototype.hasOwnProperty,t="~";function o(){}function r(e,n,t){this.fn=e,this.context=n,this.once=t||!1}function i(e,n,o,i,s){if("function"!=typeof o)throw new TypeError("The listener must be a function");var c=new r(o,i||e,s),a=t?t+n:n;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 s(e,n){0==--e._eventsCount?e._events=new o:delete e._events[n]}function c(){this._events=new o,this._eventsCount=0}Object.create&&(o.prototype=Object.create(null),(new o).__proto__||(t=!1)),c.prototype.eventNames=function(){var e,o,r=[];if(0===this._eventsCount)return r;for(o in e=this._events)n.call(e,o)&&r.push(t?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r},c.prototype.listeners=function(e){var n=this._events[t?t+e:e];if(!n)return[];if(n.fn)return[n.fn];for(var o=0,r=n.length,i=new Array(r);o<r;o++)i[o]=n[o].fn;return i},c.prototype.listenerCount=function(e){var n=this._events[t?t+e:e];return n?n.fn?1:n.length:0},c.prototype.emit=function(e,n,o,r,i,s){var c=t?t+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,n),!0;case 3:return f.fn.call(f.context,n,o),!0;case 4:return f.fn.call(f.context,n,o,r),!0;case 5:return f.fn.call(f.context,n,o,r,i),!0;case 6:return f.fn.call(f.context,n,o,r,i,s),!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,n);break;case 3:f[u].fn.call(f[u].context,n,o);break;case 4:f[u].fn.call(f[u].context,n,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,n,t){return i(this,e,n,t,!1)},c.prototype.once=function(e,n,t){return i(this,e,n,t,!0)},c.prototype.removeListener=function(e,n,o,r){var i=t?t+e:e;if(!this._events[i])return this;if(!n)return s(this,i),this;var c=this._events[i];if(c.fn)c.fn!==n||r&&!c.once||o&&c.context!==o||s(this,i);else{for(var a=0,u=[],f=c.length;a<f;a++)(c[a].fn!==n||r&&!c[a].once||o&&c[a].context!==o)&&u.push(c[a]);u.length?this._events[i]=1===u.length?u[0]:u:s(this,i)}return this},c.prototype.removeAllListeners=function(e){var n;return e?this._events[n=t?t+e:e]&&s(this,n):(this._events=new o,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=t,c.EventEmitter=c,e.exports=c}(n),n.exports}();function o(){return o=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},o.apply(this,arguments)}function r(){var e="from-webrtc-remote-control";return{isConnectionFromRemote:function(n){return n.metadata===e},connMetadata:e}}function i(e){var n=void 0===e?{}:e,t=n.sessionStorageKey,i=function(e){var n=void 0===e?{mapping:{},withTechicalErrorMessage:!0}:e,t=n.withTechicalErrorMessage,r=o({"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:function(e){return"An error occured"+(e.type?" - type: "+e.type:"")}},n.mapping);return function(e){var n=r[e.type]||("function"==typeof r.default?r.default(e):r.default);return n&&e.message&&t?n+" ("+e.message+")":n}}(n.humanErrors),s=r().isConnectionFromRemote,c=function(e){return void 0===e&&(e="webrtc-remote-control-peer-id"),{getPeerId:function(){return sessionStorage.getItem(e)},setPeerIdToSessionStorage:function(n){sessionStorage.setItem(e,n)}}}(t);return{humanizeError:i,isConnectionFromRemote:s,getPeerId:c.getPeerId,setPeerIdToSessionStorage:c.setPeerIdToSessionStorage}}var s={__proto__:null,default:function(e){var n=e.isConnectionFromRemote,o=e.setPeerIdToSessionStorage;return{humanizeError:e.humanizeError,isConnectionFromRemote:n,getPeerId:e.getPeerId,bindConnection:function(e){return new Promise(function(r){var i=new t,s=new Map,c={sendTo:function(e,n){var t=s.get(e);return t?t.send(n):null},sendAll:function(e){[].concat(s.values()).forEach(function(n){n.send(e)})},on:i.on.bind(i),off:i.off.bind(i)};e.on("open",function(e){o(e),r(c)}),e.on("connection",function(e){n(e)&&(s.set(e.peer,e),e.on("open",function(){i.emit("remote.connect",{id:e.peer}),console.log("connections",s)}),e.on("data",function(n){i.emit("data",{id:e.peer,from:"remote"},n)}),e.on("close",function(){s.delete(e.peer),i.emit("remote.disconnect",{id:e.peer}),console.log("connections",s)}))})})}}},prepareUtils:i},c={__proto__:null,default:function(e){var n=e.setPeerIdToSessionStorage;return{humanizeError:e.humanizeError,getPeerId:e.getPeerId,bindConnection:function(e,o){return new Promise(function(i){var s=null,c=new t,a={send:function(e){s?s.send(e):console.warning("You called `send` with no connection")},on:c.on.bind(c),off:c.off.bind(c)},u=function n(t){s=null,s=function(e,n,t,o){var i=r(),s=e.connect(n,{serialization:"json",metadata:i.connMetadata});return s.on("open",function(){"function"==typeof o&&o()}),s.on("data",function(e){t.emit("data",{from:"master"},e)}),s}(e,o,c,t),s.on("close",function(){c.emit("remote.disconnect",{id:e.id}),n(function(){c.emit("remote.reconnect",{id:e.id})})})};e.on("open",function(e){n(e),u(function(){return i(a)}),s.on("error",function(e){console.log("conn.error",e)}),window.addEventListener("beforeunload",function(){s&&s.disconnect&&s.disconnect()})})})}}},prepareUtils:i},a=Symbol("context-webrtc-remote-control");function u(){return u=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},u.apply(this,arguments)}e.provideWebTCRemoteControl=function(e,t,o){var r=void 0===o?{}:o,u=r.masterPeerId,f=r.sessionStorageKey,l=r.humanErrors,p=["master","remote"];if(!p.includes(t))throw new Error('Unsupported "'+t+'" mode. Only '+p.map(function(e){return'"'+e+'"'}).join(", ")+" accepted.");if("master"===t&&u)throw console.log(typeof u),new Error('`masterPeerId` prop not allowed in "master" mode - "'+u+'" was passed.');if("remote"===t&&!u)throw new Error('`masterPeerId` prop required in "remote" mode.');var d=i({sessionStorageKey:f,humanErrors:l}),m=n.shallowRef({peer:null,promise:null,mode:t,masterPeerId:u});n.provide(a,m),n.watchEffect(function(n){console.log("Provider.watch"),m.value.mode=t,m.value.humanizeError=d.humanizeError,"master"===t&&(m.value.isConnectionFromRemote=d.isConnectionFromRemote),m.value.peer=e({humanizeError:d.humanizeError,getPeerId:d.getPeerId,isConnectionFromRemote:"master"===t?d.isConnectionFromRemote:void 0}),m.value.promise=("master"===t?s:c).default(d).bindConnection(m.value.peer,c?u:void 0),m.value.promise.then(function(e){console.log("Provider.then",e)}),n(function(){console.log("Provider.onInvalidate",m.value),m.value&&m.value.peer.disconnect()})})},e.usePeer=function(){console.log("usePeer");var e=n.inject(a);console.log("context",e);var t=n.reactive(u({},n.unref(e),{peerReady:!1,ready:!1,api:null}));return n.watchEffect(function(){Promise.resolve().then(function(){var n,o;console.log("hooks.Promise.resolve",e),t.peerReady=!0,null==(n=e.value)||null==(o=n.promise)||o.then(function(e){console.log("hooks.Promise.resolve - context.promise.then",e),t.ready=!0,t.api=e})})}),n.toRefs(t)}}); //# sourceMappingURL=webrtc-remote-control-vue.umd.prod.js.map