Skip to content

Commit b47d408

Browse files
authored
Merge pull request #53 from Simperium/object-get-version
Adds method to fetch an object's version
2 parents 6b3b251 + ca414a6 commit b47d408

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/simperium/bucket.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Bucket.prototype.update = function( id, data, options, callback ) {
3131
return this.store.update( id, data, this.isIndexing, callback );
3232
};
3333

34+
Bucket.prototype.getVersion = function( id, callback ) {
35+
callback( null, 0 );
36+
};
37+
3438
Bucket.prototype.touch = function( id, callback ) {
3539
return this.store.get( id, ( e, object ) => {
3640
if ( e ) return callback( e );

src/simperium/channel.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,20 @@ export default function Channel( appid, access_token, bucket, store ) {
293293
collectionRevisions( channel, id, callback );
294294
};
295295

296+
bucket.getVersion = function( id, callback ) {
297+
store.get( id ).then(
298+
( ghost ) => {
299+
var version = 0;
300+
if ( ghost && ghost.version ) {
301+
version = ghost.version;
302+
}
303+
callback( null, version );
304+
},
305+
// callback with error if promise fails
306+
callback
307+
);
308+
};
309+
296310
bucketEvents
297311
.on( 'update', internal.diffAndSend.bind( this ) )
298312
.on( 'remove', internal.removeAndSend.bind( this ) );

test/simperium/bucket_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,15 @@ describe( 'Bucket', function() {
5656
done();
5757
} );
5858
} );
59+
60+
it( 'should fetch object version', () => {
61+
store.objects = {
62+
thing: { other: 'thing' }
63+
};
64+
bucket.getVersion( 'thing', ( error, version ) => {
65+
assert.equal( version, 0 );
66+
} );
67+
} );
68+
5969
} );
6070

test/simperium/channel_test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ describe( 'Channel', function() {
403403
// send a 405 error
404404
channel.handleMessage( 'c:' + JSON.stringify( [{error: 405, id: 'thing', ccids: ['abc']}] ) );
405405
} );
406+
407+
describe( 'with synced object', () => {
408+
beforeEach( ( done ) => {
409+
var data = { title: 'hola mundo' };
410+
411+
channel.on( 'acknowledge', function( id ) {
412+
done();
413+
} );
414+
415+
channel.on( 'send', function( msg ) {
416+
acknowledge( channel, msg );
417+
} );
418+
419+
bucket.update( 'mock-id', data );
420+
} );
421+
422+
it( 'should get version', ( done ) => {
423+
bucket.getVersion( 'mock-id', ( error, version ) => {
424+
assert.equal( version, 1 );
425+
done()
426+
} );
427+
} );
428+
429+
} );
430+
406431
} );
407432

408433
it( 'should request index when cv is unknown', done => {
@@ -513,7 +538,7 @@ function acknowledge( channel, msg, cv ) {
513538
id: change.id,
514539
o: change.o,
515540
v: change.v,
516-
ev: change.sv ? change.sv + 1 : 0,
541+
ev: change.sv ? change.sv + 1 : 1,
517542
ccids: [change.ccid],
518543
cv: cv || uuid.v4()
519544
};

0 commit comments

Comments
 (0)