@roomservice/browser
Advanced tools
Comparing version 0.5.0 to 0.5.1-0
@@ -112,2 +112,5 @@ 'use strict'; | ||
}, | ||
off: function off(socket, event) { | ||
socket.off(event); | ||
}, | ||
emit: function emit(socket, event) { | ||
@@ -129,2 +132,5 @@ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
!socket ? "development" !== "production" ? invariant(false, 'Requires socket to be defined') : invariant(false) : void 0; | ||
var timeout = setTimeout(function () { | ||
resolve(false); | ||
}, 15000); | ||
Sockets.emit(socket, 'authenticate', { | ||
@@ -134,7 +140,6 @@ payload: token | ||
Sockets.on(socket, 'authenticated', function () { | ||
clearTimeout(timeout); | ||
Sockets.off(socket, 'authenticated'); | ||
resolve(true); | ||
}); | ||
setTimeout(function () { | ||
resolve(false); | ||
}, 10000); | ||
})); | ||
@@ -455,5 +460,10 @@ } catch (e) { | ||
_this9._saveOffline('default', _this9._doc); | ||
_this9._saveOffline('default', _this9._doc); // From a user's perspective, the document should only update | ||
// if we've actually made changes (since only we care about the | ||
// clock position of everyone else). | ||
callback(_this9._doc); | ||
if (payload.msg.changes) { | ||
callback(_this9._doc); | ||
} | ||
} catch (err) { | ||
@@ -460,0 +470,0 @@ console.error(err); |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("automerge"),o=e(t),n=require("immutable"),r=e(require("invariant")),i=require("lodash"),c=require("manymerge"),s=e(require("safe-json-stringify")),a=require("idb-keyval"),u=e(require("uuid/v4")),f=e(require("socket.io-client")),l=e(require("ky-universal"));function d(e,t){try{var o=e()}catch(e){return t(e)}return o&&o.then?o.then(void 0,t):o}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var h=function(){try{return"undefined"==typeof window&&r(!1),Promise.resolve(a.get("rs:actor")).then((function(e){if(e)return e;var t=u();return a.set("rs:actor",t),t}))}catch(e){return Promise.reject(e)}},m={newSocket:function(e,t){return f(e,t)},on:function(e,t,o){e.on(t,o)},emit:function(e,t){for(var o=arguments.length,n=new Array(o>2?o-2:0),r=2;r<o;r++)n[r-2]=arguments[r];e.emit.apply(e,[t].concat(n))},disconnect:function(e){e.disconnect()}},_=function(e,t){try{return Promise.resolve(new Promise((function(o){e||r(!1),m.emit(e,"authenticate",{payload:t}),m.on(e,"authenticated",(function(){o(!0)})),setTimeout((function(){o(!1)}),1e4)})))}catch(e){return Promise.reject(e)}},v=function(){function e(e){var o=this,n=this;this._sendMsgToSocket=function(e){try{return n._socket?Promise.resolve(n._authorized).then((function(t){t?(n._roomId||r(!1),m.emit(n._socket,"sync_room_state",s({meta:{roomId:n._roomId},payload:{msg:e}}))):console.error("Room Service is unable to authorize")})):Promise.resolve()}catch(e){return Promise.reject(e)}},this._roomReference=e.roomReference,this._defaultDoc=e.defaultDoc,this._peer=new c.Peer(this._sendMsgToSocket),this._socketURL="https://api.roomservice.dev",this._saveOffline=i.debounce((function(e,n){!function(e,t,o){try{var n=d((function(){return Promise.resolve(a.set("rs:"+e+"/"+t,o)).then((function(){}))}),(function(e){console.warn("Something went wrong saving Room Service's state offline",e)}));Promise.resolve(n&&n.then?n.then((function(){})):void 0)}catch(e){return Promise.reject(e)}}(o._roomReference,e,t.save(n))}),120)}var u=e.prototype;return u.readActorIdThenCreateDoc=function(e){try{var t=this;return Promise.resolve(h()).then((function(o){return t._actorId=o,t.createDoc(o,e)}))}catch(e){return Promise.reject(e)}},u.createDoc=function(e,t){if(this._doc)return this._doc;var n=o.from(t||{},{actorId:e});return this._doc=n,this._peer.notify(this._doc),this._doc},u.restore=function(){try{var e=function(){return t.syncOfflineCache()},t=this;if("undefined"==typeof window||"undefined"==typeof indexedDB)return Promise.resolve({});var o=function(){if(!t._doc)return Promise.resolve(t.readActorIdThenCreateDoc(t._defaultDoc)).then((function(){}))}();return Promise.resolve(o&&o.then?o.then(e):e())}catch(e){return Promise.reject(e)}},u.init=function(e){var n=e.room,i=e.session;try{var c=function(){var e=!1;function c(c){return e?c:(s._roomId=n.id,s._socket=m.newSocket(s._socketURL+"/v1/doc",{transports:["websocket"]}),m.on(s._socket,"reconnect_attempt",(function(){s._socket||r(!1),s._socket.io.opts.transports=["websocket"]})),m.on(s._socket,"error",(function(e){try{var t=JSON.parse(e);console.error("Error from Socket: "+t.message)}catch(t){console.error("Unparsable error from socket: "+e)}})),s._authorized=_(s._socket,i.token),m.on(s._socket,"connect",(function(){s._peer.notify(s._doc),s.syncOfflineCache()})),m.on(s._socket,"disconnect",(function(e){"io server disconnect"===e&&console.warn("The RoomService client was forcibly disconnected from the server, likely due to invalid auth.")})),s._onUpdateSocketCallback&&m.on(s._socket,"sync_room_state",s._onUpdateSocketCallback),s._onConnectSocketCallback&&m.on(s._socket,"connect",s._onConnectSocketCallback),s._onDisconnectSocketCallback&&m.on(s._socket,"disconnect",s._onDisconnectSocketCallback),Promise.resolve(fetch(s._socketURL+"/client/v1/rooms/"+n.id+"/documents/default",{headers:{authorization:"Bearer "+i.token}})).then((function(e){if(200!==e.status)throw new Error("Unexpectedly did not find document for room "+n.reference);return Promise.resolve(e.text()).then((function(e){function n(){return{doc:r}}var r,i=d((function(){return r=o.load(e),Promise.resolve(s.syncOfflineCache()).then((function(e){r=t.merge(e,r),s._doc=r,s._peer.notify(s._doc)}))}),(function(e){console.error(e),r={}}));return i&&i.then?i.then(n):n()}))})))}var a=function(){if(!n||!i)return Promise.resolve(s.syncOfflineCache()).then((function(){return e=!0,{doc:s._doc}}))}();return a&&a.then?a.then(c):c(a)},s=this;if("undefined"==typeof window)return Promise.resolve({doc:void 0});var a=function(){if(!s._doc)return Promise.resolve(s.readActorIdThenCreateDoc(s._defaultDoc)).then((function(){}))}();return Promise.resolve(a&&a.then?a.then(c):c())}catch(e){return Promise.reject(e)}},u.disconnect=function(){"undefined"!=typeof window?(this._socket&&m.disconnect(this._socket),this._socket=void 0):console.warn("Attempting to call disconnect on the server, this is a no-op.")},u.onSetDoc=function(e){var t=this;if("undefined"!=typeof window){this._onUpdateSocketCallback&&r(!1);var o=function(o){try{var r=function(){s.msg.clock=n.Map(s.msg.clock);try{var o=t._peer.applyMessage(s.msg,t._doc);if(!o)return;t._doc=o,t._saveOffline("default",t._doc),e(t._doc)}catch(e){console.error(e)}},i=JSON.parse(o),c=i.meta,s=i.payload;if(!t._roomId)throw new Error("Expected a _roomId to be defined before we invoked the the onSetDoc callback. This is a sign of a broken client, please contact us if you're seeing this.");if(c.roomId!==t._roomId)return Promise.resolve();if(!s.msg)throw new Error("The room's state object does not include an 'msg' attribute, which could signal a corrupted room. If you're seeing this in production, that's quite bad and represents a fixable bug within the SDK itself. Please let us know and we'll fix it immediately!");var a=function(){if(!t._doc)return Promise.resolve(t.readActorIdThenCreateDoc(t._defaultDoc)).then((function(){}))}();return Promise.resolve(a&&a.then?a.then(r):r())}catch(e){return Promise.reject(e)}};this._socket?m.on(this._socket,"sync_room_state",o):this._onUpdateSocketCallback=o}else console.warn("Attempting to call onSetDoc on the server, this is a no-op.")},u.onConnect=function(e){"undefined"!=typeof window?this._socket?this._socket.on("connect",e):this._onConnectSocketCallback=e:console.warn("Attempting to call onConnect on the server, this is a no-op.")},u.onDisconnect=function(e){"undefined"!=typeof window?this._socket?this._socket.on("disconnect",e):this._onDisconnectSocketCallback=e:console.warn("Attempting to call onDisconnect on the server, this is a no-op.")},u.syncOfflineCache=function(){try{var e=this;return Promise.resolve(function(e,t){try{return Promise.resolve(d((function(){return Promise.resolve(a.get("rs:"+e+"/default"))}),(function(e){return console.warn("Something went wrong getting Room Service's state offline",e),""})))}catch(e){return Promise.reject(e)}}(e._roomReference)).then((function(o){return o?Promise.resolve(h()).then((function(n){n||console.error("Unexpectedly didn't find offline support in an environment like a browser where we should have offline support.");var r=t.load(o,{actorId:n});return e._doc=r,e._peer.notify(e._doc),r})):e._doc}))}catch(e){return Promise.reject(e)}},u.setDoc=function(e){try{var t=function(){if("function"!=typeof e)throw new Error("room.publishDoc expects a function.");var t=o.change(n._doc,e);return t||(n._actorId||r(!1),t=n.createDoc(n._actorId,n._defaultDoc)),n._doc=t,n._saveOffline("default",t),n._peer.notify(t),t},n=this;if("undefined"==typeof window)return console.warn("Attempting to call setDoc on the server, this is a no-op."),Promise.resolve({});var i=function(){if(!n._doc)return Promise.resolve(n.readActorIdThenCreateDoc(n._defaultDoc)).then((function(e){n._doc=e}))}();return Promise.resolve(i&&i.then?i.then(t):t())}catch(e){return Promise.reject(e)}},e}(),p=i.throttle((function(e,t){for(var o=arguments.length,n=new Array(o>2?o-2:0),r=2;r<o;r++)n[r-2]=arguments[r];return m.emit.apply(m,[e,t].concat(n))}),40,{leading:!0}),y=function(){function e(e){this._socketURL="https://api.roomservice.dev",this._authorizationUrl=e.authUrl,this._roomReference=e.roomReference}var t=e.prototype;return t.init=function(e){var t=this,o=e.room,n=e.session;o&&n?(this._roomId=o.id,this._socket=m.newSocket(this._socketURL+"/v1/presence",{transports:["websocket"]}),m.on(this._socket,"reconnect_attempt",(function(){t._socket||r(!1),t._socket.io.opts.transports=["websocket"]})),this._authorized=_(this._socket,n.token)):console.warn("Room Service is offline.")},t.setPresence=function(e,t,o){try{var n=function(){var n=(null==o?void 0:o.ttl)||2e3;if(t)if("object"==typeof(r=t)&&null!==r){var r,c={meta:{roomId:i._roomId,createdAt:(new Date).getTime(),namespace:e,ttl:n},payload:t};p(i._socket,"update_presence",c)}else console.error("Expected the function call 'setPresence(\""+e+"\", value)' to use a stringifiable object for variable 'value', instead got '"+t+"'.");else console.error("The function call 'setPresence(\""+e+"\", value)' passed in an undefined, null, or falsey 'value'.")},i=this;if(!i._socket)return Promise.resolve();i._roomId||r(!1);var c=function(){if(i._authorized)return Promise.resolve(i._authorized).then((function(){}))}();return Promise.resolve(c&&c.then?c.then(n):n())}catch(e){return Promise.reject(e)}},t.onSetPresence=function(e){var t=this;this._socket?m.on(this._socket,"update_presence",(function(o){try{var n=JSON.parse(o),r=n.meta,i=n.payload;if(!t._roomId)throw new Error("Expected a _roomId to be defined before we invoked the the onSetPresence callback. This is a sign of a broken client, please contact us if you're seeing this.");return r.connectionId||console.error("Unexpectedly got a packet without a connection id. We're skipping this for now, but this could be a sign of a service outage or a broken client."),r.connectionId===t._socket.id||r.roomId!==t._roomId||e(r,i),Promise.resolve()}catch(e){return Promise.reject(e)}})):console.warn("offline")},e}(),k=function(){function e(e){var t=this;this._init=i.throttle((function(){try{var e,o,n=function(){if("undefined"==typeof window){if(!e)throw new Error("Room Service can't access the auth endpoint on the server. More details: https://err.sh/getroomservice/browser/server-side-no-network");return{doc:void 0}}return t._presenceClient.init({room:e,session:o}),Promise.resolve(t._docClient.init({room:e,session:o})).then((function(e){return{doc:e.doc}}))},i=d((function(){return Promise.resolve(function(e,t,o){try{return Promise.resolve(l.post(e,{json:{room:{reference:t}},headers:o||void 0,credentials:e.includes("https://api.roomservice.dev")&&e.includes("debugger-auth-endpoint")?"include":void 0,throwHttpErrors:!1})).then((function(t){if(405===t.status&&r(!1),t.status<200||t.status>=400)throw new Error("Your Auth endpoint at '"+e+"' is not functioning properly, returned status of "+t.status+".");return Promise.resolve(t.json()).then((function(e){return{room:e.room,session:e.session}}))}))}catch(e){return Promise.reject(e)}}(t._authorizationUrl,t._roomReference,t._headers)).then((function(t){e=t.room,o=t.session}))}),(function(e){console.error("Room Service can't access the auth endpoint. More details: https://err.sh/getroomservice/browser/cant-access-auth-endpoint"),console.warn(e)}));return Promise.resolve(i&&i.then?i.then(n):n())}catch(e){return Promise.reject(e)}}),100,{leading:!0}),this._docClient=new v(e),this._presenceClient=new y(e),this._authorizationUrl=e.authUrl,this._roomReference=e.roomReference,this._headers=e.headers}var t,o=e.prototype;return o.init=function(){try{return Promise.resolve(this._init())}catch(e){return Promise.reject(e)}},o.restore=function(){try{return Promise.resolve(this._docClient.restore())}catch(e){return Promise.reject(e)}},o.onConnect=function(e){this._docClient.onConnect(e)},o.onDisconnect=function(e){this._docClient.onDisconnect(e)},o.disconnect=function(){this._docClient.disconnect()},o.setDoc=function(e){try{return Promise.resolve(this._docClient.setDoc(e))}catch(e){return Promise.reject(e)}},o.onSetDoc=function(e){this._docClient.onSetDoc(e)},o.setPresence=function(e,t){this._presenceClient.setPresence(e,t)},o.onSetPresence=function(e){this._presenceClient.onSetPresence(e)},(t=[{key:"_socketURL",set:function(e){this._docClient._socketURL=e,this._presenceClient._socketURL=e}}])&&function(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(e.prototype,t),e}();exports.default=function(){function e(e){this._roomPool={},this._authorizationUrl=e.authUrl,this._headers=e.headers}return e.prototype.room=function(e,t){if(this._roomPool[e])return this._roomPool[e];var o=new k({authUrl:this._authorizationUrl,roomReference:e,defaultDoc:t,headers:this._headers});return this._roomPool[e]=o,o},e}(); | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("automerge"),o=e(t),n=require("immutable"),r=e(require("invariant")),i=require("lodash"),c=require("manymerge"),s=e(require("safe-json-stringify")),a=require("idb-keyval"),u=e(require("uuid/v4")),f=e(require("socket.io-client")),l=e(require("ky-universal"));function d(e,t){try{var o=e()}catch(e){return t(e)}return o&&o.then?o.then(void 0,t):o}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var h=function(){try{return"undefined"==typeof window&&r(!1),Promise.resolve(a.get("rs:actor")).then((function(e){if(e)return e;var t=u();return a.set("rs:actor",t),t}))}catch(e){return Promise.reject(e)}},m={newSocket:function(e,t){return f(e,t)},on:function(e,t,o){e.on(t,o)},off:function(e,t){e.off(t)},emit:function(e,t){for(var o=arguments.length,n=new Array(o>2?o-2:0),r=2;r<o;r++)n[r-2]=arguments[r];e.emit.apply(e,[t].concat(n))},disconnect:function(e){e.disconnect()}},_=function(e,t){try{return Promise.resolve(new Promise((function(o){e||r(!1);var n=setTimeout((function(){o(!1)}),15e3);m.emit(e,"authenticate",{payload:t}),m.on(e,"authenticated",(function(){clearTimeout(n),m.off(e,"authenticated"),o(!0)}))})))}catch(e){return Promise.reject(e)}},v=function(){function e(e){var o=this,n=this;this._sendMsgToSocket=function(e){try{return n._socket?Promise.resolve(n._authorized).then((function(t){t?(n._roomId||r(!1),m.emit(n._socket,"sync_room_state",s({meta:{roomId:n._roomId},payload:{msg:e}}))):console.error("Room Service is unable to authorize")})):Promise.resolve()}catch(e){return Promise.reject(e)}},this._roomReference=e.roomReference,this._defaultDoc=e.defaultDoc,this._peer=new c.Peer(this._sendMsgToSocket),this._socketURL="https://api.roomservice.dev",this._saveOffline=i.debounce((function(e,n){!function(e,t,o){try{var n=d((function(){return Promise.resolve(a.set("rs:"+e+"/"+t,o)).then((function(){}))}),(function(e){console.warn("Something went wrong saving Room Service's state offline",e)}));Promise.resolve(n&&n.then?n.then((function(){})):void 0)}catch(e){return Promise.reject(e)}}(o._roomReference,e,t.save(n))}),120)}var u=e.prototype;return u.readActorIdThenCreateDoc=function(e){try{var t=this;return Promise.resolve(h()).then((function(o){return t._actorId=o,t.createDoc(o,e)}))}catch(e){return Promise.reject(e)}},u.createDoc=function(e,t){if(this._doc)return this._doc;var n=o.from(t||{},{actorId:e});return this._doc=n,this._peer.notify(this._doc),this._doc},u.restore=function(){try{var e=function(){return t.syncOfflineCache()},t=this;if("undefined"==typeof window||"undefined"==typeof indexedDB)return Promise.resolve({});var o=function(){if(!t._doc)return Promise.resolve(t.readActorIdThenCreateDoc(t._defaultDoc)).then((function(){}))}();return Promise.resolve(o&&o.then?o.then(e):e())}catch(e){return Promise.reject(e)}},u.init=function(e){var n=e.room,i=e.session;try{var c=function(){var e=!1;function c(c){return e?c:(s._roomId=n.id,s._socket=m.newSocket(s._socketURL+"/v1/doc",{transports:["websocket"]}),m.on(s._socket,"reconnect_attempt",(function(){s._socket||r(!1),s._socket.io.opts.transports=["websocket"]})),m.on(s._socket,"error",(function(e){try{var t=JSON.parse(e);console.error("Error from Socket: "+t.message)}catch(t){console.error("Unparsable error from socket: "+e)}})),s._authorized=_(s._socket,i.token),m.on(s._socket,"connect",(function(){s._peer.notify(s._doc),s.syncOfflineCache()})),m.on(s._socket,"disconnect",(function(e){"io server disconnect"===e&&console.warn("The RoomService client was forcibly disconnected from the server, likely due to invalid auth.")})),s._onUpdateSocketCallback&&m.on(s._socket,"sync_room_state",s._onUpdateSocketCallback),s._onConnectSocketCallback&&m.on(s._socket,"connect",s._onConnectSocketCallback),s._onDisconnectSocketCallback&&m.on(s._socket,"disconnect",s._onDisconnectSocketCallback),Promise.resolve(fetch(s._socketURL+"/client/v1/rooms/"+n.id+"/documents/default",{headers:{authorization:"Bearer "+i.token}})).then((function(e){if(200!==e.status)throw new Error("Unexpectedly did not find document for room "+n.reference);return Promise.resolve(e.text()).then((function(e){function n(){return{doc:r}}var r,i=d((function(){return r=o.load(e),Promise.resolve(s.syncOfflineCache()).then((function(e){r=t.merge(e,r),s._doc=r,s._peer.notify(s._doc)}))}),(function(e){console.error(e),r={}}));return i&&i.then?i.then(n):n()}))})))}var a=function(){if(!n||!i)return Promise.resolve(s.syncOfflineCache()).then((function(){return e=!0,{doc:s._doc}}))}();return a&&a.then?a.then(c):c(a)},s=this;if("undefined"==typeof window)return Promise.resolve({doc:void 0});var a=function(){if(!s._doc)return Promise.resolve(s.readActorIdThenCreateDoc(s._defaultDoc)).then((function(){}))}();return Promise.resolve(a&&a.then?a.then(c):c())}catch(e){return Promise.reject(e)}},u.disconnect=function(){"undefined"!=typeof window?(this._socket&&m.disconnect(this._socket),this._socket=void 0):console.warn("Attempting to call disconnect on the server, this is a no-op.")},u.onSetDoc=function(e){var t=this;if("undefined"!=typeof window){this._onUpdateSocketCallback&&r(!1);var o=function(o){try{var r=function(){s.msg.clock=n.Map(s.msg.clock);try{var o=t._peer.applyMessage(s.msg,t._doc);if(!o)return;t._doc=o,t._saveOffline("default",t._doc),s.msg.changes&&e(t._doc)}catch(e){console.error(e)}},i=JSON.parse(o),c=i.meta,s=i.payload;if(!t._roomId)throw new Error("Expected a _roomId to be defined before we invoked the the onSetDoc callback. This is a sign of a broken client, please contact us if you're seeing this.");if(c.roomId!==t._roomId)return Promise.resolve();if(!s.msg)throw new Error("The room's state object does not include an 'msg' attribute, which could signal a corrupted room. If you're seeing this in production, that's quite bad and represents a fixable bug within the SDK itself. Please let us know and we'll fix it immediately!");var a=function(){if(!t._doc)return Promise.resolve(t.readActorIdThenCreateDoc(t._defaultDoc)).then((function(){}))}();return Promise.resolve(a&&a.then?a.then(r):r())}catch(e){return Promise.reject(e)}};this._socket?m.on(this._socket,"sync_room_state",o):this._onUpdateSocketCallback=o}else console.warn("Attempting to call onSetDoc on the server, this is a no-op.")},u.onConnect=function(e){"undefined"!=typeof window?this._socket?this._socket.on("connect",e):this._onConnectSocketCallback=e:console.warn("Attempting to call onConnect on the server, this is a no-op.")},u.onDisconnect=function(e){"undefined"!=typeof window?this._socket?this._socket.on("disconnect",e):this._onDisconnectSocketCallback=e:console.warn("Attempting to call onDisconnect on the server, this is a no-op.")},u.syncOfflineCache=function(){try{var e=this;return Promise.resolve(function(e,t){try{return Promise.resolve(d((function(){return Promise.resolve(a.get("rs:"+e+"/default"))}),(function(e){return console.warn("Something went wrong getting Room Service's state offline",e),""})))}catch(e){return Promise.reject(e)}}(e._roomReference)).then((function(o){return o?Promise.resolve(h()).then((function(n){n||console.error("Unexpectedly didn't find offline support in an environment like a browser where we should have offline support.");var r=t.load(o,{actorId:n});return e._doc=r,e._peer.notify(e._doc),r})):e._doc}))}catch(e){return Promise.reject(e)}},u.setDoc=function(e){try{var t=function(){if("function"!=typeof e)throw new Error("room.publishDoc expects a function.");var t=o.change(n._doc,e);return t||(n._actorId||r(!1),t=n.createDoc(n._actorId,n._defaultDoc)),n._doc=t,n._saveOffline("default",t),n._peer.notify(t),t},n=this;if("undefined"==typeof window)return console.warn("Attempting to call setDoc on the server, this is a no-op."),Promise.resolve({});var i=function(){if(!n._doc)return Promise.resolve(n.readActorIdThenCreateDoc(n._defaultDoc)).then((function(e){n._doc=e}))}();return Promise.resolve(i&&i.then?i.then(t):t())}catch(e){return Promise.reject(e)}},e}(),p=i.throttle((function(e,t){for(var o=arguments.length,n=new Array(o>2?o-2:0),r=2;r<o;r++)n[r-2]=arguments[r];return m.emit.apply(m,[e,t].concat(n))}),40,{leading:!0}),y=function(){function e(e){this._socketURL="https://api.roomservice.dev",this._authorizationUrl=e.authUrl,this._roomReference=e.roomReference}var t=e.prototype;return t.init=function(e){var t=this,o=e.room,n=e.session;o&&n?(this._roomId=o.id,this._socket=m.newSocket(this._socketURL+"/v1/presence",{transports:["websocket"]}),m.on(this._socket,"reconnect_attempt",(function(){t._socket||r(!1),t._socket.io.opts.transports=["websocket"]})),this._authorized=_(this._socket,n.token)):console.warn("Room Service is offline.")},t.setPresence=function(e,t,o){try{var n=function(){var n=(null==o?void 0:o.ttl)||2e3;if(t)if("object"==typeof(r=t)&&null!==r){var r,c={meta:{roomId:i._roomId,createdAt:(new Date).getTime(),namespace:e,ttl:n},payload:t};p(i._socket,"update_presence",c)}else console.error("Expected the function call 'setPresence(\""+e+"\", value)' to use a stringifiable object for variable 'value', instead got '"+t+"'.");else console.error("The function call 'setPresence(\""+e+"\", value)' passed in an undefined, null, or falsey 'value'.")},i=this;if(!i._socket)return Promise.resolve();i._roomId||r(!1);var c=function(){if(i._authorized)return Promise.resolve(i._authorized).then((function(){}))}();return Promise.resolve(c&&c.then?c.then(n):n())}catch(e){return Promise.reject(e)}},t.onSetPresence=function(e){var t=this;this._socket?m.on(this._socket,"update_presence",(function(o){try{var n=JSON.parse(o),r=n.meta,i=n.payload;if(!t._roomId)throw new Error("Expected a _roomId to be defined before we invoked the the onSetPresence callback. This is a sign of a broken client, please contact us if you're seeing this.");return r.connectionId||console.error("Unexpectedly got a packet without a connection id. We're skipping this for now, but this could be a sign of a service outage or a broken client."),r.connectionId===t._socket.id||r.roomId!==t._roomId||e(r,i),Promise.resolve()}catch(e){return Promise.reject(e)}})):console.warn("offline")},e}(),k=function(){function e(e){var t=this;this._init=i.throttle((function(){try{var e,o,n=function(){if("undefined"==typeof window){if(!e)throw new Error("Room Service can't access the auth endpoint on the server. More details: https://err.sh/getroomservice/browser/server-side-no-network");return{doc:void 0}}return t._presenceClient.init({room:e,session:o}),Promise.resolve(t._docClient.init({room:e,session:o})).then((function(e){return{doc:e.doc}}))},i=d((function(){return Promise.resolve(function(e,t,o){try{return Promise.resolve(l.post(e,{json:{room:{reference:t}},headers:o||void 0,credentials:e.includes("https://api.roomservice.dev")&&e.includes("debugger-auth-endpoint")?"include":void 0,throwHttpErrors:!1})).then((function(t){if(405===t.status&&r(!1),t.status<200||t.status>=400)throw new Error("Your Auth endpoint at '"+e+"' is not functioning properly, returned status of "+t.status+".");return Promise.resolve(t.json()).then((function(e){return{room:e.room,session:e.session}}))}))}catch(e){return Promise.reject(e)}}(t._authorizationUrl,t._roomReference,t._headers)).then((function(t){e=t.room,o=t.session}))}),(function(e){console.error("Room Service can't access the auth endpoint. More details: https://err.sh/getroomservice/browser/cant-access-auth-endpoint"),console.warn(e)}));return Promise.resolve(i&&i.then?i.then(n):n())}catch(e){return Promise.reject(e)}}),100,{leading:!0}),this._docClient=new v(e),this._presenceClient=new y(e),this._authorizationUrl=e.authUrl,this._roomReference=e.roomReference,this._headers=e.headers}var t,o=e.prototype;return o.init=function(){try{return Promise.resolve(this._init())}catch(e){return Promise.reject(e)}},o.restore=function(){try{return Promise.resolve(this._docClient.restore())}catch(e){return Promise.reject(e)}},o.onConnect=function(e){this._docClient.onConnect(e)},o.onDisconnect=function(e){this._docClient.onDisconnect(e)},o.disconnect=function(){this._docClient.disconnect()},o.setDoc=function(e){try{return Promise.resolve(this._docClient.setDoc(e))}catch(e){return Promise.reject(e)}},o.onSetDoc=function(e){this._docClient.onSetDoc(e)},o.setPresence=function(e,t){this._presenceClient.setPresence(e,t)},o.onSetPresence=function(e){this._presenceClient.onSetPresence(e)},(t=[{key:"_socketURL",set:function(e){this._docClient._socketURL=e,this._presenceClient._socketURL=e}}])&&function(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(e.prototype,t),e}();exports.default=function(){function e(e){this._roomPool={},this._authorizationUrl=e.authUrl,this._headers=e.headers}return e.prototype.room=function(e,t){if(this._roomPool[e])return this._roomPool[e];var o=new k({authUrl:this._authorizationUrl,roomReference:e,defaultDoc:t,headers:this._headers});return this._roomPool[e]=o,o},e}(); | ||
//# sourceMappingURL=browser.cjs.production.min.js.map |
@@ -105,2 +105,5 @@ import Automerge, { merge, load, save } from 'automerge'; | ||
}, | ||
off: function off(socket, event) { | ||
socket.off(event); | ||
}, | ||
emit: function emit(socket, event) { | ||
@@ -122,2 +125,5 @@ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
!socket ? process.env.NODE_ENV !== "production" ? invariant(false, 'Requires socket to be defined') : invariant(false) : void 0; | ||
var timeout = setTimeout(function () { | ||
resolve(false); | ||
}, 15000); | ||
Sockets.emit(socket, 'authenticate', { | ||
@@ -127,7 +133,6 @@ payload: token | ||
Sockets.on(socket, 'authenticated', function () { | ||
clearTimeout(timeout); | ||
Sockets.off(socket, 'authenticated'); | ||
resolve(true); | ||
}); | ||
setTimeout(function () { | ||
resolve(false); | ||
}, 10000); | ||
})); | ||
@@ -448,5 +453,10 @@ } catch (e) { | ||
_this9._saveOffline('default', _this9._doc); | ||
_this9._saveOffline('default', _this9._doc); // From a user's perspective, the document should only update | ||
// if we've actually made changes (since only we care about the | ||
// clock position of everyone else). | ||
callback(_this9._doc); | ||
if (payload.msg.changes) { | ||
callback(_this9._doc); | ||
} | ||
} catch (err) { | ||
@@ -453,0 +463,0 @@ console.error(err); |
@@ -5,2 +5,3 @@ /// <reference types="socket.io-client" /> | ||
on(socket: SocketIOClient.Socket, event: "connect" | "disconnect" | "error" | "sync_room_state" | "update_presence" | "authenticated" | "reconnect_attempt", fn: (...args: any[]) => void): void; | ||
off(socket: SocketIOClient.Socket, event: string): void; | ||
emit(socket: SocketIOClient.Socket, event: "sync_room_state" | "update_presence" | "authenticate", ...args: any[]): void; | ||
@@ -7,0 +8,0 @@ disconnect(socket: SocketIOClient.Socket): void; |
{ | ||
"version": "0.5.0", | ||
"version": "0.5.1-0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
277214
1797