Socket
Socket
Sign inDemoInstall

twilio-sync

Package Overview
Dependencies
Maintainers
2
Versions
608
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twilio-sync - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0-dev-build.127

26

lib/client.js

@@ -268,7 +268,2 @@ 'use strict';

}
}, {
key: '_removeFromCache',
value: function _removeFromCache(sid) {
this._entities.remove(sid);
}

@@ -293,3 +288,3 @@ /**

return this._getCached(id, 'document') || this._getDocument(id).then(function (body) {
return this._getCached(id, 'doc') || this._getDocument(id).then(function (body) {
if (body) {

@@ -302,5 +297,3 @@ return body;

}).then(function (body) {
return new _syncdocument2.default(_this2._dependencies, body, function (sid) {
return _this2._removeFromCache(sid);
});
return new _syncdocument2.default(_this2._dependencies, body);
}).then(function (entity) {

@@ -337,5 +330,3 @@ return _this2._entities.store(entity);

}).then(function (body) {
return new _syncmap2.default(_this3._dependencies, body, function (sid) {
return _this3._removeFromCache(sid);
});
return new _syncmap2.default(_this3._dependencies, body);
}).then(function (entity) {

@@ -372,5 +363,3 @@ return _this3._entities.store(entity);

}).then(function (body) {
return new _synclist2.default(_this4._dependencies, body, function (sid) {
return _this4._removeFromCache(sid);
});
return new _synclist2.default(_this4._dependencies, body);
}).then(function (entity) {

@@ -391,7 +380,4 @@ return _this4._entities.store(entity);

value: function shutdown() {
var _this5 = this;
return this._subscriptions.shutdown().then(function () {
return _this5._twilsock.disconnect();
}).then(function () {});
this._subscriptions.shutdown();
this._twilsock.disconnect().then(function () {});
}

@@ -398,0 +384,0 @@

@@ -22,3 +22,3 @@ 'use strict';

var CDS_URI = 'https://cds.twilio.com';
var SUBSCRIPTIONS_PATH = '/v3/Subscriptions';
var SUBSCRIPTIONS_PATH = '/v4/Subscriptions';
var MAPS_PATH = '/v3/Maps';

@@ -25,0 +25,0 @@ var LISTS_PATH = '/v3/Lists';

@@ -59,13 +59,2 @@ 'use strict';

}, {
key: 'remove',
value: function remove(sid) {
var cached = this._entities.get(sid);
if (cached) {
this._entities.delete(sid);
if (cached.uniqueName) {
this._names.delete(cached.type + '::' + cached.uniqueName);
}
}
}
}, {
key: '_getResolved',

@@ -72,0 +61,0 @@ value: function _getResolved(id, type) {

@@ -37,2 +37,5 @@ 'use strict';

/**
* Base class for all Sync entity types
*/
var Entity = function (_EventEmitter) {

@@ -73,3 +76,3 @@ (0, _inherits3.default)(Entity, _EventEmitter);

value: function _unsubscribe() {
this._deps.router.unsubscribe(this.sid, this);
this._deps.router.unsubscribe(this.sid);
return this;

@@ -79,2 +82,4 @@ }

/**
* Closes current entity
* Remove server subscription, local cache, and other related stuff
* @public

@@ -81,0 +86,0 @@ */

@@ -7,2 +7,6 @@ 'use strict';

var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _defineProperties = require('babel-runtime/core-js/object/define-properties');

@@ -24,4 +28,2 @@

var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -32,2 +34,3 @@

var COREDATA_MAP_NOTIFICATION_TYPE = 'com.twilio.rtd.cds.map';
var SYNC_NOTIFICATION_TYPE = 'twilio.sync.event';

@@ -54,2 +57,3 @@ /**

this._notifications.subscribe(COREDATA_MAP_NOTIFICATION_TYPE);
this._notifications.subscribe(SYNC_NOTIFICATION_TYPE);

@@ -74,25 +78,6 @@ this._notifications.on('message', this.onMessage.bind(this));

value: function onMessage(type, message) {
// As it happens, all message types are intended for the Subscriptions manager.
//
_logger2.default.trace('Notification type:', type, 'content:', message);
var id = void 0;
switch (type) {
case COREDATA_DOCUMENT_NOTIFICATION_TYPE:
id = message.event.document_sid;
break;
case COREDATA_LIST_NOTIFICATION_TYPE:
id = message.event.list_sid;
break;
case COREDATA_MAP_NOTIFICATION_TYPE:
id = message.event.map_sid;
break;
default:
_logger2.default.warn('Unknown message type:', type, 'ignoring');
}
if (id) {
message.event.type = message.event_type; // Patch message
this._subscriptions.getSubscribers(id).forEach(function (entity) {
entity._update((0, _utils.deepClone)(message.event));
});
}
this._subscriptions.acceptMessage(message);
}

@@ -107,9 +92,4 @@

value: function subscribe(sid, entity) {
return this._subscriptions.add(sid, entity).then(function (isNewSubscription) {
if (isNewSubscription) {
entity._softSync();
}
}).then(function () {
return entity;
});
this._subscriptions.add(sid, entity);
return _promise2.default.resolve(entity);
}

@@ -123,6 +103,4 @@

key: 'unsubscribe',
value: function unsubscribe(sid, entity) {
return this._subscriptions.remove(sid, entity).then(function () {
return entity;
});
value: function unsubscribe(sid) {
return this._subscriptions.remove(sid);
}

@@ -138,5 +116,3 @@

value: function onConnected() {
this._subscriptions.forEach(function (id, endpoint) {
endpoint._softSync();
});
this._subscriptions.poke();
}

@@ -143,0 +119,0 @@ }]);

@@ -7,9 +7,9 @@ 'use strict';

var _set = require('babel-runtime/core-js/set');
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _set2 = _interopRequireDefault(_set);
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _promise = require('babel-runtime/core-js/promise');
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _promise2 = _interopRequireDefault(_promise);
var _getIterator3 = _interopRequireDefault(_getIterator2);

@@ -20,2 +20,6 @@ var _map = require('babel-runtime/core-js/map');

var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _defineProperties = require('babel-runtime/core-js/object/define-properties');

@@ -25,6 +29,2 @@

var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

@@ -38,10 +38,2 @@

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _backoff = require('backoff');

@@ -57,55 +49,75 @@

function substract(p1, p2) {
var result = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
/* eslint-disable key-spacing */
var MAX_BATCH_SIZE = 1000;
try {
for (var _iterator = (0, _getIterator3.default)(p1), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
key = _step$value[0],
value = _step$value[1];
/**
* A data container used by the Subscriptions class to track subscribed entities' local
* representations and their state.
*/
if (!p2.has(key)) {
result.push({ sid: key, type: value.type });
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
var SubscribedEntity = function () {
function SubscribedEntity(entity) {
var _this = this;
(0, _classCallCheck3.default)(this, SubscribedEntity);
(0, _defineProperties2.default)(this, {
sid: { get: function get() {
return entity.sid;
} },
type: { get: function get() {
return entity.type;
} },
lastEventId: { get: function get() {
return entity.lastEventId;
} },
localObject: { value: entity },
correlationId: { value: null, writable: true },
pendingPokeTimer: { value: null, writable: true },
rejectedWithError: { value: false, writable: true },
isInTransition: { get: function get() {
return _this.correlationId !== null;
} }
});
}
return result;
}
(0, _createClass3.default)(SubscribedEntity, [{
key: 'markAsFailed',
value: function markAsFailed(error) {
this.rejectedWithError = error;
}
}]);
return SubscribedEntity;
}();
/**
* @class Subscriptions
* @classdesc Subscriptions container for CDS objects
* @classdesc A manager which, in batches of varying size, continuously persists the
* subscription intent of the caller to the Sync backend until it achieves a
* converged state.
*/
var Subscriptions = function () {
/**
* @constructor
* Prepares a new Subscriptions manager object with zero subscribed or persisted subscriptions.
*
* @param {object} config may include a key 'backoffConfig', wherein any of the parameters
* of Backoff.exponential (from npm 'backoff') are valid and will override the defaults.
*
* @param {Network} must be a viable running Sync Network object, useful for routing requests.
*/
function Subscriptions(config, network) {
var _this = this;
var _this2 = this;
(0, _classCallCheck3.default)(this, Subscriptions);
function createBackoff() {
var defaultBackoffConfig = {
randomisationFactor: 0.2,
initialDelay: 50,
initialDelay: 100,
maxDelay: 2 * 60 * 1000

@@ -115,3 +127,2 @@ };

}
(0, _defineProperties2.default)(this, {

@@ -122,26 +133,27 @@ _config: { value: config },

_types: { value: new _map2.default() },
// This is always the full set of subscribables (SubscribedEntity instances) intended by
// the client. At any point, whatever the state of these subscriptions on the server, this
// is the intent of the user to which the SDK must converge.
_subscriptions: { value: new _map2.default() },
// This includes the set of subscribables (SubscribedEntity instances) for whom a request
// has been dispatched (whether or not this particular request ultimately succeeds) to
// establish a live subscription. Entities are removed when the corresponding "cancel"
// request is dispatched.
_persisted: { value: new _map2.default() }
});
// This block is triggered by #_persist. Every request is executed in a series of (ideally 1)
// backoff 'ready' event, at which point a new subscription set is calculated.
//
this._backoff.on('ready', function () {
var _getAction2 = _this._getAction(),
action = _getAction2.action,
list = _getAction2.list;
var _getSubscriptionUpdat = _this2._getSubscriptionUpdateBatch(),
action = _getSubscriptionUpdat.action,
subscriptionRequests = _getSubscriptionUpdat.subscriptions;
if (action) {
_this._request(action, list.map(function (x) {
return { object_sid: x.sid, object_type: x.type };
})) // eslint-disable-line camelcase
.then(function () {
_this._backoff.reset();
_this._backoff.backoff();
}).catch(function (e) {
_logger2.default.error('Error while persisting subscriptions, retrying', action, e);
_this._backoff.backoff(e);
});
_this2._applyNewSubscriptionUpdateBatch(action, subscriptionRequests);
} else {
_this._backoff.reset();
_logger2.default.info('Subscription list persisted');
_this2._backoff.reset();
_logger2.default.info('All subscriptions resolved.');
}

@@ -152,16 +164,52 @@ });

(0, _createClass3.default)(Subscriptions, [{
key: '_getAction',
value: function _getAction() {
var _this2 = this;
key: '_getSubscriptionUpdateBatch',
value: function _getSubscriptionUpdateBatch() {
function substract(these, those, ignoreCurrentOp, limit) {
var result = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var listToAdd = substract(this._subscriptions, this._persisted);
try {
for (var _iterator = (0, _getIterator3.default)(these), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
thisKey = _step$value[0],
thisValue = _step$value[1];
var otherValue = those.get(thisKey);
if (!otherValue && (ignoreCurrentOp || !thisValue.isInTransition) && !thisValue.rejectedWithError) {
result.push(thisValue);
if (limit && result.length >= limit) {
break;
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return result;
}
var listToAdd = substract(this._subscriptions, this._persisted, false, MAX_BATCH_SIZE).map(function (x) {
return new SubscribedEntity(x);
});
if (listToAdd.length > 0) {
return { action: 'establish', list: listToAdd };
return { action: 'establish', subscriptions: listToAdd };
}
var listToRemove = substract(this._persisted, this._subscriptions).map(function (x) {
return { sid: x.sid, type: _this2._types.get(x.sid) };
});
var listToRemove = substract(this._persisted, this._subscriptions, true, MAX_BATCH_SIZE);
if (listToRemove.length > 0) {
return { action: 'cancel', list: listToRemove };
return { action: 'cancel', subscriptions: listToRemove };
}

@@ -178,3 +226,136 @@

}
}, {
key: '_applyNewSubscriptionUpdateBatch',
value: function _applyNewSubscriptionUpdateBatch(action, requests) {
var _this3 = this;
// Keeping in mind that events may begin flowing _before_ we receive the response
requests = this._processLocalActions(action, requests);
var correlationId = new Date().getTime();
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(requests), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var subscribed = _step2.value;
this._recordActionAttemptOn(subscribed, action, correlationId);
}
// Send this batch to the service
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
this._request(action, correlationId, requests.map(function (object) {
return {
object_sid: object.sid, // eslint-disable-line camelcase
object_type: object.type, // eslint-disable-line camelcase
last_event_id: action === 'establish' ? object.lastEventId : undefined // eslint-disable-line no-undefined, camelcase
};
})).then(function (response) {
if (action === 'establish') {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = (0, _getIterator3.default)(requests), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var subscribed = _step3.value;
_this3._beginReplayTimeout(response.body.estimated_delivery_in_ms, subscribed, action);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
_this3._backoff.reset();
}).catch(function (e) {
_logger2.default.info('failed an attempt to ' + action + ' subscriptions; retrying', e);
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = (0, _getIterator3.default)(requests), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var attemptedSubscription = _step4.value;
_this3._recordActionFailureOn(attemptedSubscription, action);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
_this3._backoff.backoff(e);
});
}
}, {
key: '_processLocalActions',
value: function _processLocalActions(action, requests) {
if (action === 'cancel') {
return requests.filter(function (request) {
return !request.rejectedWithError;
});
}
return requests;
}
}, {
key: '_recordActionAttemptOn',
value: function _recordActionAttemptOn(attemptedSubscription, action, correlationId) {
if (action === 'establish') {
this._persisted.set(attemptedSubscription.sid, attemptedSubscription);
attemptedSubscription.correlationId = correlationId;
} else {
// cancel
var persistedSubscription = this._persisted.get(attemptedSubscription.sid);
if (persistedSubscription) {
persistedSubscription.correlationId = null;
}
}
}
}, {
key: '_recordActionFailureOn',
value: function _recordActionFailureOn(attemptedSubscription, action) {
attemptedSubscription.correlationId = null;
if (action === 'establish') {
this._persisted.delete(attemptedSubscription.sid);
}
}
/**

@@ -186,34 +367,43 @@ * @private

key: '_request',
value: function _request(action, requests) {
var _this3 = this;
value: function _request(action, correlationId, requests) {
_logger2.default.debug('Modifying server subscriptions: ', action, requests);
/* eslint-disable camelcase */
var requestBody = {
event_protocol_version: 3, // eslint-disable-line camelcase
event_protocol_version: 3,
action: action,
correlation_id: correlationId,
requests: requests
};
/* eslint-enable camelcase */
return this._network.post(this._config.subscriptionsUri, requestBody).then(function (response) {
response.body.results.forEach(function (persisted) {
if (!persisted.subscription) {
_this3._persisted.delete(persisted.object_sid);
_this3._types.delete(persisted.object_sid);
} else {
_this3._persisted.set(persisted.object_sid, persisted.subscription);
var subscription = _this3._subscriptions.get(persisted.object_sid);
if (subscription) {
subscription.resolve(true);
}
}
});
});
return this._network.post(this._config.subscriptionsUri, requestBody);
}
}, {
key: '_beginReplayTimeout',
value: function _beginReplayTimeout(timeout, subscription, action) {
var _this4 = this;
var isNumeric = !isNaN(parseFloat(timeout)) && isFinite(timeout);
var isValidTimeout = isNumeric && timeout > 0;
if (isValidTimeout) {
subscription.pendingPokeTimer = setTimeout(function () {
_logger2.default.debug('Attempt to ' + action + ' ' + subscription.sid + ' timed out without confirmation; trying again.');
subscription.correlationId = null;
_this4._persisted.delete(subscription.sid);
_this4._persist();
}, timeout);
}
}
/**
* Add subscription
* @param {string} uri URI to the server object
* @param {object} endpoint Endpoint object
* @return Promise<boolean> true if subscription haven't been there before
* Establishes intent to be subscribed to this entity. That subscription will be effected
* asynchronously.
* If subscription to the given sid already exists, it will be overwritten.
*
* @param {string} sid should be a well-formed SID, uniquely identifying a single instance of a Sync entity.
* @param {object} entity should represent the (singular) local representation of this entity.
* Incoming events and modifications to the entity will be directed at the _update() function
* of this provided reference.
*
* @return undefined
*/

@@ -224,37 +414,24 @@

value: function add(sid, entity) {
var _this4 = this;
var subscribed = this._subscriptions.has(sid);
var persisted = this._persisted.has(sid);
if (subscribed && persisted) {
var _subscription = this._subscriptions.get(sid);
_subscription.subscribers.add(entity);
return _promise2.default.resolve(false);
} else if (this._subscribed && !persisted) {
var _subscription2 = this._subscriptions.get(sid);
return _subscription2.promise;
_logger2.default.debug('Establishing intent to subscribe to ' + sid);
var existingSubscription = this._subscriptions.get(sid);
if (existingSubscription && existingSubscription.lastEventId === entity.lastEventId) {
// If last event id is the same as before - we're fine
return;
}
var subscription = { type: entity.type,
subscribers: new _set2.default([entity]),
promise: null,
resolve: null,
reject: null
};
this._subscriptions.set(sid, subscription);
var promiseToSubscribe = new _promise2.default(function (resolve, reject) {
subscription.resolve = resolve;
subscription.reject = reject;
_this4._persist();
});
subscription.promise = promiseToSubscribe;
return promiseToSubscribe;
this._persisted.delete(sid);
this._subscriptions.set(sid, entity);
this._persist();
}
/**
* Remove subscription for the entity
* @param {string} entityUri URI
* @param {object} endpoint Endpoint object
* Establishes the caller's intent to no longer be subscribed to this entity. Following this
* call, no further events shall be routed to the local representation of the entity, even
* though a server-side subscription may take more time to actually terminate.
*
* @param {string} sid should be any well-formed SID, uniquely identifying a Sync entity.
* This call only has meaningful effect if that entity is subscribed at the
* time of call. Otherwise does nothing.
*
* @return undefined
*/

@@ -264,88 +441,159 @@

key: 'remove',
value: function remove(id, entity) {
if (!this._subscriptions.has(id)) {
return _promise2.default.resolve(true);
value: function remove(sid) {
_logger2.default.debug('Establishing intent to unsubscribe from ' + sid);
var removed = this._subscriptions.delete(sid);
if (removed) {
this._persist();
}
}
var subscription = this._subscriptions.get(id);
if (!subscription.subscribers.has(entity)) {
return _promise2.default.resolve(false);
}
/**
* The point of ingestion for remote incoming messages (e.g. new data was written to a map
* to which we are subscribed).
*
* @param {object} message is the full, unaltered body of the incoming notification.
*
* @return undefined
*/
subscription.subscribers.delete(entity);
if (subscription.subscribers.size > 0) {
return _promise2.default.resolve(false);
}, {
key: 'acceptMessage',
value: function acceptMessage(message) {
_logger2.default.trace('Subscriptions received', message);
switch (message.event_type) {
case 'subscription_established':
this._applySubscriptionEstablishedMessage(message.event, message.correlation_id);
break;
case 'subscription_canceled':
this._applySubscriptionCancelledMessage(message.event, message.correlation_id);
break;
case 'subscription_failed':
this._applySubscriptionFailedMessage(message.event, message.correlation_id);
break;
case (message.event_type.match(/^(?:map|list|document)_/) || {}).input:
{
var typedSid = function typedSid() {
if (message.event_type.match(/^map_/)) return message.event.map_sid;else if (message.event_type.match(/^list_/)) return message.event.list_sid;else if (message.event_type.match(/^document_/)) return message.event.document_sid;else return undefined; // eslint-disable-line no-undefined
};
this._applyEventToSubscribedEntity(typedSid(), message);
}
break;
default:
_logger2.default.debug('Dropping unknown message type ' + message.event_type);
break;
}
}
}, {
key: '_applySubscriptionEstablishedMessage',
value: function _applySubscriptionEstablishedMessage(message, correlationId) {
var sid = message.object_sid;
var subscriptionIntent = this._persisted.get(message.object_sid);
if (subscriptionIntent && subscriptionIntent.correlationId === correlationId) {
if (message.replay_status === 'interrupted') {
_logger2.default.debug('Event Replay for subscription to ' + sid + ' interrupted; continuing eagerly.');
clearTimeout(subscriptionIntent.pendingPokeTimer);
subscriptionIntent.pendingPokeTimer = null;
subscriptionIntent.correlationId = null;
this._persisted.delete(subscriptionIntent.sid);
this._backoff.reset();
} else if (message.replay_status === 'completed') {
_logger2.default.debug('Event Replay for subscription to ' + sid + ' completed. Subscription is ready.');
// It's a hack: since server responce doesn't contain a type, but we need it
// we should always keep it locally
this._types.set(id, subscription.type);
clearTimeout(subscriptionIntent.pendingPokeTimer);
subscriptionIntent.pendingPokeTimer = null;
subscriptionIntent.correlationId = null;
this._persisted.set(message.object_sid, subscriptionIntent);
this._subscriptions.delete(id);
this._backoff.reset();
}
} else {
_logger2.default.debug('Late message (correlation_id: ' + message.correlation_id + ') dropped.');
}
this._persist();
return _promise2.default.resolve(true);
}
}, {
key: '_applySubscriptionCancelledMessage',
value: function _applySubscriptionCancelledMessage(message, correlationId) {
var persistedSubscription = this._persisted.get(message.object_sid);
if (persistedSubscription && persistedSubscription.correlationId === correlationId) {
clearTimeout(persistedSubscription.pendingPokeTimer);
persistedSubscription.pendingPokeTimer = null;
persistedSubscription.correlationId = null;
/**
* Query subscribers for given URI
* @return {set}
*/
this._persisted.delete(message.object_sid);
} else {
_logger2.default.debug('Late message (correlation_id: ' + message.correlation_id + ') dropped.');
}
this._persist();
}
}, {
key: 'getSubscribers',
value: function getSubscribers(id) {
var subscription = this._subscriptions.get(id);
return subscription && subscription.subscribers ? subscription.subscribers : new _set2.default();
key: '_applySubscriptionFailedMessage',
value: function _applySubscriptionFailedMessage(message, correlationId) {
var sid = message.object_sid;
var subscription = this._persisted.get(sid);
if (subscription && subscription.correlationId === correlationId) {
subscription.markAsFailed(message.error);
}
this._persist();
}
/**
* Iterates through all subscriptions
* @param {function} hanlder function to call for each subscription
*/
}, {
key: 'forEach',
value: function forEach(handler) {
this._subscriptions.forEach(function (subscription, id) {
subscription.subscribers.forEach(function (subscriber) {
return handler(id, subscriber);
});
});
key: '_applyEventToSubscribedEntity',
value: function _applyEventToSubscribedEntity(sid, message) {
var subscriptionIntent = sid ? this._subscriptions.get(sid) : null;
if (subscriptionIntent) {
message.event.type = message.event_type;
subscriptionIntent._update(message.event);
} else {
_logger2.default.debug('Message dropped for SID \'' + sid + '\', for which there is no subscription.');
}
}
/**
* Currently just resets retries
* Should remove all subscriptions
* Prompts a playback of any missed changes made to any subscribed object. This method
* should be invoked whenever the connectivity layer has experienced cross-cutting
* delivery failures that would affect the entire local sync set. Any tangible result
* of this operation will result in calls to the _update() function of subscribed
* Sync entities.
*/
}, {
key: 'shutdown',
value: function shutdown() {
this._backoff.reset();
this._subscriptions.clear();
key: 'poke',
value: function poke() {
_logger2.default.info('Triggering event replay for all subscriptions.');
var listToRemove = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
var failedSubscriptions = [];
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(this._persisted), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _step2$value = (0, _slicedToArray3.default)(_step2.value, 2),
key = _step2$value[0],
value = _step2$value[1];
for (var _iterator5 = (0, _getIterator3.default)(this._persisted), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var _step5$value = (0, _slicedToArray3.default)(_step5.value, 2),
_ = _step5$value[0],
it = _step5$value[1];
listToRemove.push({ object_sid: key, type: value.type }); // eslint-disable-line camelcase
// eslint-disable-line no-unused-vars
clearTimeout(it.pendingPokeTimer);
it.pendingPokeTimer = null;
it.correlationId = null;
if (it.rejectedWithError) {
failedSubscriptions.push(it);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
if (_didIteratorError5) {
throw _iteratorError5;
}

@@ -355,7 +603,41 @@ }

if (listToRemove.length > 0) {
return this._request('cancel', listToRemove).catch(function () {});
this._persisted.clear();
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = (0, _getIterator3.default)(failedSubscriptions), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var it = _step6.value;
this._persisted.set(it.sid, it);
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
return _promise2.default.resolve();
this._persist();
}
/**
* Stops all communication, clears any subscription intent, and returns.
*/
}, {
key: 'shutdown',
value: function shutdown() {
this._backoff.reset();
this._subscriptions.clear();
}
}]);

@@ -362,0 +644,0 @@ return Subscriptions;

@@ -74,3 +74,3 @@ 'use strict';

*/
function SyncDocument(params, descriptor, onRemoveDocument) {
function SyncDocument(params, descriptor) {
(0, _classCallCheck3.default)(this, SyncDocument);

@@ -88,3 +88,2 @@

_pendingListeners: { value: {} },
_onRemoveDocument: { value: onRemoveDocument },

@@ -377,3 +376,2 @@ _descriptor: { value: descriptor },

this._onRemoveDocument(this.sid);
return this._network.delete(this.uri).then(function () {

@@ -388,2 +386,7 @@ _this8.emit('removed', true);

}
}, {
key: 'lastEventId',
get: function get() {
return this._lastEventId;
}
}]);

@@ -390,0 +393,0 @@ return SyncDocument;

@@ -92,3 +92,3 @@ 'use strict';

*/
function SyncList(deps, descriptor, onRemoveList) {
function SyncList(deps, descriptor) {
(0, _classCallCheck3.default)(this, SyncList);

@@ -106,3 +106,2 @@

_cache: { value: new _cache2.default() },
_onRemoveList: { value: onRemoveList },

@@ -437,3 +436,2 @@ _links: { get: function get() {

this._onRemoveList(this.sid);
return this._deps.network.delete(this.uri).then(function () {

@@ -572,2 +570,7 @@ _this12.emit('collectionRemoved', true);

}
}, {
key: 'lastEventId',
get: function get() {
return this._lastEventId;
}
}]);

@@ -574,0 +577,0 @@ return SyncList;

@@ -90,3 +90,3 @@ 'use strict';

*/
function SyncMap(deps, descriptor, onRemoveMap) {
function SyncMap(deps, descriptor) {
(0, _classCallCheck3.default)(this, SyncMap);

@@ -106,3 +106,2 @@

_updateQueue: { value: new _retryingqueue2.default() },
_onRemoveMap: { value: onRemoveMap },

@@ -684,3 +683,2 @@ _links: { get: function get() {

this._onRemoveMap(this.sid);
return this._deps.network.delete(this.uri).then(function () {

@@ -695,2 +693,7 @@ _this13.emit('collectionRemoved', true);

}
}, {
key: 'lastEventId',
get: function get() {
return this._lastEventId;
}
}]);

@@ -697,0 +700,0 @@ return SyncMap;

{
"name": "twilio-sync",
"version": "0.3.1",
"version": "0.4.0-dev-build.127",
"description": "Twilio Sync client library",

@@ -5,0 +5,0 @@ "main": "lib/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

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

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

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

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

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

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

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

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

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc