Skip to content

Commit a103b8d

Browse files
committed
A bunch more jsdoc
- implements suggested fixes by @danroundhill
1 parent 603ea7b commit a103b8d

File tree

5 files changed

+244
-49
lines changed

5 files changed

+244
-49
lines changed

src/simperium/bucket.js

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import { EventEmitter } from 'events'
22
import { inherits } from 'util'
33
import { v4 as uuid } from 'uuid';
44

5+
/**
6+
* @callback taskCallback
7+
* @param {?Error} - if an error occurred it will be provided, otherwise null
8+
* @param {Any} - the result of task
9+
*/
10+
511
/**
612
* Convenience function to turn a function that uses a callback into a function
713
* that returns a Promise.
814
*
9-
* @param {Function} task - function that expects a single callback argument
15+
* @param {taskCallback} task - function that expects a single callback argument
1016
* @returns {Promise} callback wrapped in a promise interface
1117
*/
1218
const callbackAsPromise = ( task ) => new Promise( ( resolve, reject ) => {
13-
task( ( error, result ) => {
14-
if ( error ) {
15-
reject( error );
16-
return;
17-
}
18-
resolve( result );
19-
} );
19+
task( ( error, result ) => error ? reject( error ) : resolve( result ) );
2020
} );
2121

2222
/**
@@ -44,23 +44,99 @@ const deprecateCallback = ( callback, promise ) => {
4444
return promise;
4545
};
4646

47+
/**
48+
* A bucket object represents the data stored in Simperium for the given id
49+
*
50+
* @typedef {Object} BucketObject
51+
* @param {String} id - bucket object id
52+
* @param {Object} data - object literal of bucket object data stored at the id
53+
* @param {?Boolean} isIndexing - used to indicate that the bucket is being indexed
54+
*/
55+
56+
/**
57+
* @callback bucketStoreGetCallback
58+
* @param {?Error}
59+
* @param {?BucketObject}
60+
*/
61+
62+
/**
63+
* @callback bucketStoreRemoveCallback
64+
* @param {?Error}
65+
*/
66+
67+
/**
68+
* @callback bucketStoreFindCallback
69+
* @param {?Error}
70+
* @param {?BucketObject[]}
71+
*/
72+
73+
/**
74+
* Used by a bucket to store bucket object data.
75+
*
76+
* @interface BucketStore
77+
*/
78+
79+
/**
80+
* Retrieve a bucket object from the store
81+
* @function
82+
* @name BucketStore#get
83+
* @param {String} id - the bucket object id to fetch
84+
* @param {bucketStoreGetCallback} - callback once the object is fetched
85+
*/
86+
87+
/**
88+
* Updates the data for the given object id.
89+
*
90+
* @function
91+
* @name BucketStore#update
92+
* @param {String} id - to of object to update
93+
* @param {Object} data - data to update the object to
94+
* @param {Boolean} isIndexing - indicates the object is being downloaded during an index
95+
* @param {bucketStoreGetCallback}
96+
*/
97+
98+
/**
99+
* Deletes the object at id from the datastore.
100+
*
101+
* @function
102+
* @name BucketStore#remove
103+
* @param {String} id - object to delete from the bucket
104+
* @param {bucketStoreRemoveCallback} - called once the object is deleted
105+
*/
106+
107+
/**
108+
* Fetchs all bucket objects from the datastore.
109+
*
110+
* @function
111+
* @name BucketStore#find
112+
* @param {?Object} query - currently undefined
113+
* @param {bucketStoreFindCallback} - called with results
114+
*/
115+
47116
/**
48117
* Turns existing bucket storage provider callback api into a promise based API
49118
*
50-
* @param {Object} store - a bucket storage object
119+
* @param {BucketStore} store - a bucket storage object
51120
* @returns {Object} store api methods that use Promises instead of callbacks
52121
*/
53122
const promiseAPI = store => ( {
54-
get: ( id ) =>
123+
get: id =>
55124
callbackAsPromise( store.get.bind( store, id ) ),
56125
update: ( id, object, isIndexing ) =>
57126
callbackAsPromise( store.update.bind( store, id, object, isIndexing ) ),
58-
remove: ( id ) =>
127+
remove: id =>
59128
callbackAsPromise( store.remove.bind( store, id ) ),
60-
find: ( query ) =>
129+
find: query =>
61130
callbackAsPromise( store.find.bind( store, query ) )
62131
} );
63132

133+
/**
134+
* A bucket that syncs data with Simperium.
135+
*
136+
* @param {String} name - Simperium bucket name
137+
* @param {bucketStoreProvider} storeProvider - a factory function that provides a bucket store
138+
* @param {Channel} channel - a channel instance used for syncing Simperium data
139+
*/
64140
export default function Bucket( name, storeProvider, channel ) {
65141
EventEmitter.call( this );
66142
this.name = name;
@@ -134,7 +210,7 @@ Bucket.prototype.reload = function() {
134210
* object ID to represent the object in simperium.
135211
*
136212
* @param {Object} object - plain js object literal to be saved/synced
137-
* @param {Function} callback - runs when object has been saved
213+
* @param {?bucketStoreGetCallback} callback - runs when object has been saved
138214
* @return {Promise<Object>} data stored in the bucket
139215
*/
140216
Bucket.prototype.add = function( object, callback ) {
@@ -146,7 +222,7 @@ Bucket.prototype.add = function( object, callback ) {
146222
* Requests the object data stored in the bucket for the given id.
147223
*
148224
* @param {String} id - bucket object id
149-
* @param {Function} callback - with the data stored in the bucket
225+
* @param {?bucketStoreGetCallback} callback - with the data stored in the bucket
150226
* @return {Promise<Object>} the object id, data and indexing status
151227
*/
152228
Bucket.prototype.get = function( id, callback ) {
@@ -160,7 +236,7 @@ Bucket.prototype.get = function( id, callback ) {
160236
* @param {Object} data - object literal to replace the object data with
161237
* @param {Object} [options] - optional settings
162238
* @param {Boolean} [options.sync=true] - false if object should not be synced with this update
163-
* @param {Function} callback - executed when object is updated localy
239+
* @param {?bucketStoreGetCallback} callback - executed when object is updated localy
164240
* @returns {Promise<Object>} - update data
165241
*/
166242
Bucket.prototype.update = function( id, data, options, callback ) {
@@ -182,23 +258,35 @@ Bucket.prototype.update = function( id, data, options, callback ) {
182258
return deprecateCallback( callback, task );
183259
};
184260

261+
/**
262+
* @callback bucketHasLocalChanges
263+
* @param {?Error}
264+
* @param {?Boolean}
265+
*/
266+
185267
/**
186268
* Check if the bucket has pending changes that have not yet been synced.
187269
*
188-
* @param {Function} [callback] - optional callback to receive response
270+
* @param {?bucketHasLocalChanges} callback - optional callback to receive response
189271
* @returns {Promise<Boolean>} resolves to true if their are still changes to sync
190272
*/
191273
Bucket.prototype.hasLocalChanges = function( callback ) {
192274
return deprecateCallback( callback, this.channel.hasLocalChanges() );
193275
};
194276

277+
/**
278+
* @callback bucketGetVersion
279+
* @param {?Error}
280+
* @param {Number}
281+
*/
282+
195283
/**
196284
* Gets the currently synced version number for the specified object id.
197285
*
198286
* A version of `0` indicates that an object has not been added to simperium yet.
199287
*
200288
* @param {String} id - object to get the version for
201-
* @param {Function} [callback] - optional callback
289+
* @param {?bucketGetVersionCallback} callback - optional callback
202290
* @returns {Promise<number>} - resolves to the current synced version
203291
*/
204292
Bucket.prototype.getVersion = function( id, callback ) {
@@ -210,7 +298,7 @@ Bucket.prototype.getVersion = function( id, callback ) {
210298
* is locally stored for the object
211299
*
212300
* @param {String} id - object to sync
213-
* @param {Function} [callback] - optional callback
301+
* @param {?bucketStoreGetCallback} callback - optional callback
214302
* @returns {Promise<Object>} - object id, data
215303
*/
216304
Bucket.prototype.touch = function( id, callback ) {
@@ -224,7 +312,7 @@ Bucket.prototype.touch = function( id, callback ) {
224312
* Deletes the object from the bucket
225313
*
226314
* @param {String} id - object to delete
227-
* @param {Function} [callback] - optional callback
315+
* @param {?bucketStoreRemoveCallback} callback - optional callback
228316
* @returns {Promise<Void>} - resolves when object has been deleted
229317
*/
230318
Bucket.prototype.remove = function( id, callback ) {

src/simperium/channel.js

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const internal = {};
2828
*/
2929
internal.updateChangeVersion = function( cv ) {
3030
return this.store.setChangeVersion( cv ).then( () => {
31+
// A unit test currently relies on this event, otherwise we can remove it
3132
this.emit( 'change-version', cv );
3233
return cv;
3334
} );
@@ -93,7 +94,6 @@ internal.buildRemoveChange = function( id, ghost ) {
9394
this.localQueue.queue( payload );
9495
};
9596

96-
9797
internal.diffAndSend = function( id, object ) {
9898
var modify = internal.buildModifyChange.bind( this, id, object );
9999
return this.store.get( id ).then( modify );
@@ -266,21 +266,106 @@ internal.indexingComplete = function() {
266266
}
267267

268268
/**
269-
* Maintains syncing state for a simperium bucket.
269+
* A ghost represents a version of a bucket object as known by Simperium
270+
*
271+
* Generally a client will keep the last known ghost stored locally for efficient
272+
* diffing and patching of Simperium change operations.
273+
*
274+
* @typedef {Object} Ghost
275+
* @property {Number} version - the ghost's version
276+
* @property {String} key - the simperium bucket object id this ghost is for
277+
* @property {Object} data - the data for the given ghost version
278+
*/
279+
280+
/**
281+
* Callback function used by the ghost store to iterate over existing ghosts
270282
*
271-
* A bucket uses a channel to lister for updates that come from simperium while
283+
* @callback ghostIterator
284+
* @param {Ghost} - the current ghost
285+
*/
286+
287+
/**
288+
* A GhostStore provides the store mechanism for ghost data that the Channel
289+
* uses to maintain syncing state and producing change operations for
290+
* Bucket objects.
291+
*
292+
* @interface GhostStore
293+
*/
294+
295+
/**
296+
* Retrieve a Ghost for the given bucket object id
297+
*
298+
* @function
299+
* @name GhostStore#get
300+
* @param {String} id - bucket object id
301+
* @returns {Promise<Ghost>} - the ghost for this object
302+
*/
303+
304+
/**
305+
* Save a ghost in the store.
306+
*
307+
* @function
308+
* @name GhostStore#put
309+
* @param {String} id - bucket object id
310+
* @param {Number} version - version of ghost data
311+
* @param {Object} data - object literal to save as this ghost's data for this version
312+
* @returns {Promise<Ghost>} - the ghost for this object
313+
*/
314+
315+
/**
316+
* Delete a Ghost from the store.
317+
*
318+
* @function
319+
* @name GhostStore#remove
320+
* @param {String} id - bucket object id
321+
* @returns {Promise<Ghost>} - the ghost for this object
322+
*/
323+
324+
/**
325+
* Iterate over existing Ghost objects with the given callback.
326+
*
327+
* @function
328+
* @name GhostStore#eachGhost
329+
* @param {ghostIterator} - function to run against each ghost
330+
*/
331+
332+
/**
333+
* Get the current change version (cv) that this channel has synced.
334+
*
335+
* @function
336+
* @name GhostStore#getChangeVersion
337+
* @returns {Promise<String>} - the current change version for the bucket
338+
*/
339+
340+
/**
341+
* Set the current change version.
342+
*
343+
* @function
344+
* @name GhostStore#setChangeVersion
345+
* @returns {Promise<Void>} - resolves once the change version is saved
346+
*/
347+
348+
349+
/**
350+
* Maintains syncing state for a Simperium bucket.
351+
*
352+
* A bucket uses a channel to listen for updates that come from simperium while
272353
* sending updates that are made on the client.
273354
*
274355
* The channel can handle incoming simperium commands via `handleMessage`. These
275-
* messages are stripped of their channel number that seperates bucket operations.
356+
* messages are stripped of their channel number that separates bucket operations.
276357
* The `Client` maintains which commands should be routed to which channel.
277358
*
278359
* The channel is responsible for creating all change operations and downloading
279360
* bucket data.
280361
*
362+
* @param {String} appid - Simperium app id, used for authenticating
363+
* @param {String} access_token - Simperium user access token
364+
* @param {GhostStore} store - data storage for ghost objects
365+
* @param {String} name - the name of the bucket on Simperium.com
281366
*/
282367
export default function Channel( appid, access_token, store, name ) {
283-
const channel = this;
368+
// Uses an event emitter to handle different Simperium commands
284369
const message = this.message = new EventEmitter();
285370

286371
this.name = name;
@@ -289,7 +374,7 @@ export default function Channel( appid, access_token, store, name ) {
289374
this.store = store;
290375
this.access_token = access_token;
291376

292-
this.session_id = 'node-' + uuid.v4();
377+
this.session_id = 'node-' + uuid();
293378

294379
// These are the simperium bucket commands the channel knows how to handle
295380
message.on( 'auth', this.onAuth.bind( this ) );
@@ -317,26 +402,6 @@ export default function Channel( appid, access_token, store, name ) {
317402
this.localQueue.on( 'error', internal.handleChangeError.bind( this ) );
318403
}
319404

320-
/**
321-
* A ghost represents a version of a bucket object as known by Simperium
322-
*
323-
* Generally a client will keep the last known ghost stored locally for efficient
324-
* diffing and patching of Simperium change operations.
325-
*
326-
* @typedef {Object} Ghost
327-
* @property {Number} version - the ghost's version
328-
* @property {String} key - the simperium bucket object id this ghost is for
329-
* @property {Object} data - the data for the given ghost version
330-
*/
331-
332-
/**
333-
* An object stored is Simperium.
334-
*
335-
* @typedef {Object} BucketObject
336-
* @property {String} id - simperium bucket object id
337-
* @property {Object} data - object literal data to be stored at the id
338-
*/
339-
340405
inherits( Channel, EventEmitter );
341406

342407
/**
@@ -459,9 +524,8 @@ Channel.prototype.send = function( data ) {
459524
* Restores a buckets data to what is currently stored in the ghost data.
460525
*/
461526
Channel.prototype.reload = function() {
462-
var emit = this.emit.bind( this, 'update' );
463-
this.store.eachGhost( function( ghost ) {
464-
emit( ghost.key, ghost.data );
527+
this.store.eachGhost( ghost => {
528+
this.emit( 'update', ghost.key, ghost.data );
465529
} );
466530
};
467531

0 commit comments

Comments
 (0)