Skip to content

Commit b9177f3

Browse files
committed
Changed update() method to take the arguments as an object.
1 parent 0c5e346 commit b9177f3

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
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 ) => {
@@ -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, null, null, null, null, callback );
218+
return this.update( { id, data }, callback );
219219
};
220220

221221
/**
@@ -232,17 +232,18 @@ Bucket.prototype.get = function( id, callback ) {
232232
/**
233233
* Update the bucket object of `id` with the given data.
234234
*
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} original - the original object before the udpate
238-
* @param {Object} patch - the JSONDiff patch to apply to the object
239-
* @param {Boolean} isIndexing - true if the bucket is currently indexing
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
240241
* @param {Object} [options] - optional settings
241242
* @param {Boolean} [options.sync=true] - false if object should not be synced with this update
242243
* @param {?bucketStoreGetCallback} callback - executed when object is updated localy
243244
* @returns {Promise<Object>} - update data
244245
*/
245-
Bucket.prototype.update = function( id, data, original, patch, isIndexing, options, callback ) {
246+
Bucket.prototype.update = function( { id, data, original, patch, isIndexing = false }, options, callback ) {
246247
if ( typeof options === 'function' ) {
247248
callback = options;
248249
options = { sync: true };
@@ -306,7 +307,7 @@ Bucket.prototype.getVersion = function( id, callback ) {
306307
*/
307308
Bucket.prototype.touch = function( id, callback ) {
308309
const task = this.storeAPI.get( id )
309-
.then( object => this.update( object.id, object.data ) );
310+
.then( object => this.update( { id: object.id, data: object.data } ) );
310311

311312
return deprecateCallback( callback, task );
312313
};

test/simperium/bucket_test.js

Lines changed: 6 additions & 6 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, null, null, null, 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, null, null, null, {}, 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
} )

test/simperium/channel_test.js

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

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

114115
it( 'should not send a change with an empty diff', function( done ) {
@@ -121,7 +122,7 @@ describe( 'Channel', function() {
121122
channel.once( 'unmodified', function() {
122123
done();
123124
} );
124-
bucket.update( 'thing', data );
125+
bucket.update( {id: 'thing', data} );
125126
} );
126127
} );
127128

@@ -131,21 +132,21 @@ describe( 'Channel', function() {
131132
checkSent = function() {
132133
throw new Error( 'Sent too many changes' );
133134
},
134-
objectId = '123456';
135+
id = '123456';
135136

136137
channel.on( 'send', fn.counts( 1, checkSent ) );
137138

138139
channel.on( 'send', function() {
139-
bucket.update( objectId, data2 );
140+
bucket.update( { id, data: data2 } );
140141
} )
141142

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

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

151152
it( 'should acknowledge sent change', function( done ) {
@@ -160,7 +161,7 @@ describe( 'Channel', function() {
160161
acknowledge( channel, msg );
161162
} );
162163

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

166167
it( 'should ignore duplicate change error if nothing to acknowledge', ( done ) => {
@@ -225,13 +226,14 @@ describe( 'Channel', function() {
225226

226227
channel.localQueue.on( 'wait', validate );
227228

229+
const id = '123';
228230
channel.once( 'send', function() {
229-
bucket.update( '123', {title: 'hello again world'} );
230-
bucket.remove( '123' );
231+
bucket.update( {id, data: {title: 'hello again world'}} );
232+
bucket.remove( id );
231233
} );
232234

233235
store.put( '123', 3, {title: 'hello world'} ).then( function() {
234-
bucket.update( '123', {title: 'goodbye world'} );
236+
bucket.update( {id, data: {title: 'goodbye world'}} );
235237
} );
236238
} );
237239

@@ -266,7 +268,7 @@ describe( 'Channel', function() {
266268
}, reject );
267269
} );
268270

269-
bucket.update( key, {title: 'hello world'}, null, null, null, function() {
271+
bucket.update( {id: key, data: {title: 'hello world'}}, function() {
270272
channel.handleMessage( 'c:' + JSON.stringify( [{
271273
o: '-', ev: 1, cv: 'cv1', id: key
272274
}] ) );
@@ -319,8 +321,9 @@ describe( 'Channel', function() {
319321
} );
320322
} );
321323

322-
store.put( '123', 3, {title: 'hello world'} ).then( function() {
323-
bucket.update( '123', {title: 'goodbye world!!'} );
324+
const id = '123';
325+
store.put( id, 3, {title: 'hello world'} ).then( function() {
326+
bucket.update( {id, data: {title: 'goodbye world!!'}} );
324327
} );
325328
} );
326329

@@ -352,7 +355,7 @@ describe( 'Channel', function() {
352355
}] ) );
353356

354357
// We're changing "Hello world" to "Goodbye world"
355-
bucket.update( key, {title: 'Goodbye world'} );
358+
bucket.update( {id: key, data: {title: 'Goodbye world'}} );
356359
} ) );
357360

358361
it( 'should emit errors on the bucket instance', ( done ) => {
@@ -426,7 +429,7 @@ describe( 'Channel', function() {
426429
acknowledge( channel, msg );
427430
} );
428431

429-
bucket.update( 'mock-id', data );
432+
bucket.update( {id: 'mock-id', data} );
430433
} );
431434

432435
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)