Skip to content

Commit 511573a

Browse files
authored
Merge pull request #67 from Simperium/fix/bucket-update-emit
Pass additional arguments to the bucket.update emit
2 parents cebb92f + d9ec5f9 commit 511573a

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

src/simperium/bucket.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export default function Bucket( name, storeProvider, channel ) {
149149
*/
150150
this.onChannelIndex = this.emit.bind( this, 'index' );
151151
this.onChannelError = this.emit.bind( this, 'error' );
152-
this.onChannelUpdate = ( id, data ) => {
153-
this.update( id, data, { sync: false } );
152+
this.onChannelUpdate = ( id, data, original, patch, isIndexing ) => {
153+
this.update( id, data, { original, patch, isIndexing }, { sync: false } );
154154
};
155155

156156
this.onChannelIndexingStateChange = ( isIndexing ) => {
@@ -209,13 +209,13 @@ Bucket.prototype.reload = function() {
209209
* Stores an object in the bucket and syncs it to simperium. Generates an
210210
* object ID to represent the object in simperium.
211211
*
212-
* @param {Object} object - plain js object literal to be saved/synced
212+
* @param {Object} data - plain js object literal to be saved/synced
213213
* @param {?bucketStoreGetCallback} callback - runs when object has been saved
214214
* @return {Promise<Object>} data stored in the bucket
215215
*/
216-
Bucket.prototype.add = function( object, callback ) {
216+
Bucket.prototype.add = function( data, callback ) {
217217
var id = uuid();
218-
return this.update( id, object, callback );
218+
return this.update( id, data, callback );
219219
};
220220

221221
/**
@@ -234,13 +234,21 @@ Bucket.prototype.get = function( id, callback ) {
234234
*
235235
* @param {String} id - the bucket id for the object to update
236236
* @param {Object} data - object literal to replace the object data with
237+
* @param {Object} remoteUpdateInfo - object containing data about a remote change
238+
* @param {Object} [updateInfo.original] - the original object before the udpate
239+
* @param {Object} [updateInfo.patch] - the JSONDiff patch to apply to the object
240+
* @param {Boolean} [updateInfo.isIndexing] - true if the bucket is currently indexing
237241
* @param {Object} [options] - optional settings
238242
* @param {Boolean} [options.sync=true] - false if object should not be synced with this update
239243
* @param {?bucketStoreGetCallback} callback - executed when object is updated localy
240244
* @returns {Promise<Object>} - update data
241245
*/
242-
Bucket.prototype.update = function( id, data, options, callback ) {
243-
if ( typeof options === 'function' ) {
246+
Bucket.prototype.update = function( id, data, remoteUpdateInfo, options, callback ) {
247+
// Callback could be 3rd or 4th argument
248+
if ( typeof remoteUpdateInfo === 'function' ) {
249+
callback = remoteUpdateInfo;
250+
options = { sync: true };
251+
} else if ( typeof options === 'function' ) {
244252
callback = options;
245253
options = { sync: true };
246254
}
@@ -251,7 +259,7 @@ Bucket.prototype.update = function( id, data, options, callback ) {
251259

252260
const task = this.storeAPI.update( id, data, this.isIndexing )
253261
.then( bucketObject => {
254-
this.emit( 'update', id, bucketObject.data );
262+
this.emit( 'update', id, bucketObject.data, remoteUpdateInfo );
255263
this.channel.update( bucketObject, options.sync );
256264
return bucketObject;
257265
} );

test/simperium/bucket_test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ describe( 'Bucket', () => {
3737

3838
it( 'should store object update callback', ( done ) => {
3939
const id = 'thing',
40-
object = {one: 'two'};
40+
data = {one: 'two'};
4141

42-
bucket.update( id, object, function() {
42+
bucket.update( id, data, function() {
4343
bucket.get( id, function( err, savedObject ) {
44-
deepEqual( object, savedObject );
44+
deepEqual( data, savedObject );
4545
done();
4646
} );
4747
} );
4848
} );
4949

5050
it( 'should update with options callback', ( done ) => {
5151
const id = 'thing',
52-
object = {one: 'two'}
52+
data = {one: 'two'}
5353

54-
bucket.update( id, object, {}, function() {
54+
bucket.update( id, data, function() {
5555
bucket.get( id, function( err, savedObject ) {
56-
deepEqual( object, savedObject );
56+
deepEqual( data, savedObject );
5757
done();
5858
} )
5959
} )
@@ -99,4 +99,3 @@ describe( 'Bucket', () => {
9999
} );
100100
} );
101101
} );
102-

test/simperium/channel_test.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe( 'Channel', function() {
108108
done();
109109
} );
110110

111-
bucket.update( '12345', {content: 'Hola mundo!'} );
111+
bucket.update( '12345', {content: 'Hola mundo!'});
112112
} );
113113

114114
it( 'should not send a change with an empty diff', function( done ) {
@@ -131,20 +131,20 @@ describe( 'Channel', function() {
131131
checkSent = function() {
132132
throw new Error( 'Sent too many changes' );
133133
},
134-
objectId = '123456';
134+
id = '123456';
135135

136136
channel.on( 'send', fn.counts( 1, checkSent ) );
137137

138138
channel.on( 'send', function() {
139-
bucket.update( objectId, data2 );
139+
bucket.update( id, data2 );
140140
} )
141141

142+
const objectId = '123456';
142143
channel.localQueue.on( 'wait', function( id ) {
143144
equal( id, objectId );
144145
done();
145146
} );
146147

147-
objectId = '123456';
148148
bucket.update( objectId, data );
149149
} );
150150

@@ -225,13 +225,14 @@ describe( 'Channel', function() {
225225

226226
channel.localQueue.on( 'wait', validate );
227227

228+
const id = '123';
228229
channel.once( 'send', function() {
229-
bucket.update( '123', {title: 'hello again world'} );
230-
bucket.remove( '123' );
230+
bucket.update( id, {title: 'hello again world'} );
231+
bucket.remove( id );
231232
} );
232233

233234
store.put( '123', 3, {title: 'hello world'} ).then( function() {
234-
bucket.update( '123', {title: 'goodbye world'} );
235+
bucket.update( id, {title: 'goodbye world'} );
235236
} );
236237
} );
237238

@@ -319,8 +320,9 @@ describe( 'Channel', function() {
319320
} );
320321
} );
321322

322-
store.put( '123', 3, {title: 'hello world'} ).then( function() {
323-
bucket.update( '123', {title: 'goodbye world!!'} );
323+
const id = '123';
324+
store.put( id, 3, {title: 'hello world'} ).then( function() {
325+
bucket.update( id, {title: 'goodbye world!!'} );
324326
} );
325327
} );
326328

0 commit comments

Comments
 (0)