This document outlines the changes between the 0.6.2 and 0.7.0 versions of the twilio-sync.js library. Documentation for 0.7.0 release: https://media.twiliocdn.com/sdk/js/sync/releases/0.7.0/docs
To include via CDN:
<script type="text/javascript" src="https://media.twiliocdn.com/sdk/js/sync/releases/0.7.0/twilio-sync.min.js"></script>
Via NPM:
npm install --save twilio-sync@0.7.0
The documentation page for the SDK has been improved, including links to useful resources and code snippets for all methods and event handlers in the SDK.
New APIs
The client.document(), client.list(), client.map(), client.stream() methods now take in an optional OpenOptions object, which allows for more granular control over how the SDK handles opening nonexistent objects. Refer to documentation for more details.
It is now possible to specify a TTL for Documents and List/Map Items on creation, update, and via a dedicated setTtl(ttlInSeconds) method.
thingHappenedRemotely
events were removedthingHappened
events now include a Boolean isLocal property for determining the locality of the event:
isLocal == true
: the event was triggered by the current endpoint (i.e., the current SDK instance)isLocal == false
: the event was triggered by a different endpoint (i.e., a different SDK instance)SDK version 0.6
1map.on('itemUpdatedRemotely', function(item) {2console.log('Remote update:', item);3});
SDK version 0.7
1map.on('itemUpdated', function(args) {2if (!args.isLocal) {3let item = args.item;4console.log('Remote update:', item);5}6});
collectionRemoved
event for Lists and Maps was renamed to removed
.SDK version 0.6
1list.on('collectionRemoved', function(isLocal) {2console.log('isLocal:', isLocal);3});45map.on('collectionRemoved', function(isLocal) {6console.log('isLocal:', isLocal);7});
SDK version 0.7
1list.on('removed', function(args) {2console.log('args.isLocal:', args.isLocal);3});45map.on('removed', function(args) {6console.log('args.isLocal:', args.isLocal);7});
5. Update squashing
For Documents and List/Map Items, it is no longer guaranteed that every state update via set(), mutate(), update() is observable by subscribers. Convergence is guaranteed only to latest state.
Given the following example:
SDK client 1
1client.document('MyDoc')2.then(doc => {3let promise1 = doc.set({ state: 1 });4let promise2 = doc.set({ state: 2 });5let promise3 = doc.set({ state: 3 });6});
SDK client 2
1client.document('MyDoc')2.then(doc => {3doc.on('updated', function(newState) {4console.log('Document updated:', newState);5});6});
Console output from SDK client 2 :
In SDK version 0.6
1Document updated: { state: 1 }2Document updated: { state: 2 }3Document updated: { state: 3 }
In SDK version 0.7
1Document updated: { state: 1 }2Document updated: { state: 3 }
The underlying reason is that when the 0.6 SDK triggered a request to the Sync backend for every set() call, the 0.7 SDK may squash successive set() calls into one, and making a request to the Sync backend only with the latest state. This means that subscribers also receive an updated event only for the latest state.
With the example above, to ensure that subscribers receive all state updates in v0.7, the set/update/mutate method should be called after the previous one has completed:
1var doc; client.document("MyDoc")2.then((document) => {3doc = document;4let promise1 = doc.set({ state: 1 });5return promise1;6})7.then(() => {8let promise2 = doc.set({ state: 2 });9return promise2;10})11.then(() => {12let promise3 = doc.set({ state: 3 });13return promise3;14});