Skip to content

Commit 2c5773e

Browse files
committed
Pass id and data as regular function arguments, updateInfo as an object.
1 parent b9177f3 commit 2c5773e

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/simperium/bucket.js

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

156156
this.onChannelIndexingStateChange = ( isIndexing ) => {
@@ -232,20 +232,21 @@ Bucket.prototype.get = function( id, callback ) {
232232
/**
233233
* Update the bucket object of `id` with the given data.
234234
*
235-
* @param {Object} args - the function arguments
236-
* @param {String} [args.id] - the bucket id for the object to update
237-
* @param {Object} [args.data] - object literal to replace the object data with
238-
* @param {Object} [args.original] - the original object before the udpate
239-
* @param {Object} [args.patch] - the JSONDiff patch to apply to the object
240-
* @param {Boolean} [args.isIndexing] - true if the bucket is currently indexing
235+
* @param {String} id - the bucket id for the object to update
236+
* @param {Object} data - object literal to replace the object data with
237+
* @param {Object} updateInfo - 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
241241
* @param {Object} [options] - optional settings
242242
* @param {Boolean} [options.sync=true] - false if object should not be synced with this update
243243
* @param {?bucketStoreGetCallback} callback - executed when object is updated localy
244244
* @returns {Promise<Object>} - update data
245245
*/
246-
Bucket.prototype.update = function( { id, data, original, patch, isIndexing = false }, options, callback ) {
247-
if ( typeof options === 'function' ) {
248-
callback = options;
246+
Bucket.prototype.update = function( id, data, updateInfo, options, callback ) {
247+
// Callback could be 3rd or 4th argument
248+
if ( typeof updateInfo === 'function' || typeof options === 'function' ) {
249+
callback = typeof updateInfo === 'function' ? updateInfo : options;
249250
options = { sync: true };
250251
}
251252

@@ -255,7 +256,7 @@ Bucket.prototype.update = function( { id, data, original, patch, isIndexing = fa
255256

256257
const task = this.storeAPI.update( id, data, this.isIndexing )
257258
.then( bucketObject => {
258-
this.emit( 'update', id, bucketObject.data, original, patch, isIndexing );
259+
this.emit( 'update', id, bucketObject.data, updateInfo );
259260
this.channel.update( bucketObject, options.sync );
260261
return bucketObject;
261262
} );

test/simperium/bucket_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe( 'Bucket', () => {
3939
const id = 'thing',
4040
data = {one: 'two'};
4141

42-
bucket.update( { id, data }, function() {
42+
bucket.update( id, data, function() {
4343
bucket.get( id, function( err, savedObject ) {
4444
deepEqual( data, savedObject );
4545
done();
@@ -51,7 +51,7 @@ describe( 'Bucket', () => {
5151
const id = 'thing',
5252
data = {one: 'two'}
5353

54-
bucket.update( { id, data }, function() {
54+
bucket.update( id, data, function() {
5555
bucket.get( id, function( err, savedObject ) {
5656
deepEqual( data, savedObject );
5757
done();

test/simperium/channel_test.js

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

111-
const updateArgs = {id: '12345', data: {content: 'Hola mundo!'}}
112-
bucket.update( updateArgs );
111+
bucket.update( '12345', {content: 'Hola mundo!'});
113112
} );
114113

115114
it( 'should not send a change with an empty diff', function( done ) {
@@ -122,7 +121,7 @@ describe( 'Channel', function() {
122121
channel.once( 'unmodified', function() {
123122
done();
124123
} );
125-
bucket.update( {id: 'thing', data} );
124+
bucket.update( 'thing', data );
126125
} );
127126
} );
128127

@@ -137,16 +136,16 @@ describe( 'Channel', function() {
137136
channel.on( 'send', fn.counts( 1, checkSent ) );
138137

139138
channel.on( 'send', function() {
140-
bucket.update( { id, data: data2 } );
139+
bucket.update( id, data2 );
141140
} )
142141

142+
const objectId = '123456';
143143
channel.localQueue.on( 'wait', function( id ) {
144144
equal( id, objectId );
145145
done();
146146
} );
147147

148-
const objectId = '123456';
149-
bucket.update( {id: objectId, data} );
148+
bucket.update( objectId, data );
150149
} );
151150

152151
it( 'should acknowledge sent change', function( done ) {
@@ -161,7 +160,7 @@ describe( 'Channel', function() {
161160
acknowledge( channel, msg );
162161
} );
163162

164-
bucket.update( { id: 'mock-id', data } );
163+
bucket.update( 'mock-id', data );
165164
} );
166165

167166
it( 'should ignore duplicate change error if nothing to acknowledge', ( done ) => {
@@ -228,12 +227,12 @@ describe( 'Channel', function() {
228227

229228
const id = '123';
230229
channel.once( 'send', function() {
231-
bucket.update( {id, data: {title: 'hello again world'}} );
230+
bucket.update( id, {title: 'hello again world'} );
232231
bucket.remove( id );
233232
} );
234233

235234
store.put( '123', 3, {title: 'hello world'} ).then( function() {
236-
bucket.update( {id, data: {title: 'goodbye world'}} );
235+
bucket.update( id, {title: 'goodbye world'} );
237236
} );
238237
} );
239238

@@ -268,7 +267,7 @@ describe( 'Channel', function() {
268267
}, reject );
269268
} );
270269

271-
bucket.update( {id: key, data: {title: 'hello world'}}, function() {
270+
bucket.update( key, {title: 'hello world'}, function() {
272271
channel.handleMessage( 'c:' + JSON.stringify( [{
273272
o: '-', ev: 1, cv: 'cv1', id: key
274273
}] ) );
@@ -323,7 +322,7 @@ describe( 'Channel', function() {
323322

324323
const id = '123';
325324
store.put( id, 3, {title: 'hello world'} ).then( function() {
326-
bucket.update( {id, data: {title: 'goodbye world!!'}} );
325+
bucket.update( id, {title: 'goodbye world!!'} );
327326
} );
328327
} );
329328

@@ -355,7 +354,7 @@ describe( 'Channel', function() {
355354
}] ) );
356355

357356
// We're changing "Hello world" to "Goodbye world"
358-
bucket.update( {id: key, data: {title: 'Goodbye world'}} );
357+
bucket.update( key, {title: 'Goodbye world'} );
359358
} ) );
360359

361360
it( 'should emit errors on the bucket instance', ( done ) => {
@@ -429,7 +428,7 @@ describe( 'Channel', function() {
429428
acknowledge( channel, msg );
430429
} );
431430

432-
bucket.update( {id: 'mock-id', data} );
431+
bucket.update( 'mock-id', data );
433432
} );
434433

435434
it( 'should get version', ( done ) => {

test/simperium/default_storage_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe( 'default store', () => {
1414
const id = 'thing',
1515
data = {one: 'two'};
1616

17-
return bucket.update( {id, data} )
17+
return bucket.update( id, data )
1818
.then( () => bucket.get( id ) )
1919
.then( ( object ) => {
2020
deepEqual( object, { data, id } );
@@ -25,7 +25,7 @@ describe( 'default store', () => {
2525
const id = 'thing',
2626
data = {one: 'two'}
2727

28-
return bucket.update( {id, data} )
28+
return bucket.update( id, data )
2929
.then( () => bucket.get( id ) )
3030
.then( ( object ) => {
3131
deepEqual( object, { data, id } );

0 commit comments

Comments
 (0)