Skip to content

Commit 603ea7b

Browse files
committed
More docs
1 parent 05c0913 commit 603ea7b

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/simperium/bucket.js

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

5-
const callbackAsPromise = ( callback, task ) => new Promise( ( resolve, reject ) => {
5+
/**
6+
* Convenience function to turn a function that uses a callback into a function
7+
* that returns a Promise.
8+
*
9+
* @param {Function} task - function that expects a single callback argument
10+
* @returns {Promise} callback wrapped in a promise interface
11+
*/
12+
const callbackAsPromise = ( task ) => new Promise( ( resolve, reject ) => {
613
task( ( error, result ) => {
714
if ( error ) {
815
reject( error );
@@ -12,10 +19,17 @@ const callbackAsPromise = ( callback, task ) => new Promise( ( resolve, reject )
1219
} );
1320
} );
1421

22+
/**
23+
* Runs a promise with a callback (if one is provided) to support the old callback API.
24+
* NOTE: if the callback API is removed this is a place to warn users
25+
*
26+
* @param {Function} [callback] - if provided, will be called with the expected values
27+
* @param {Promise} promise - promise to run, executes callback if provieded
28+
* @returns {Promise} promise is passed through
29+
*/
1530
const deprecateCallback = ( callback, promise ) => {
1631
if ( typeof callback === 'function' ) {
17-
// TODO: warn about deprecating callback API
18-
// and convert to promises
32+
// Potentially could warn here if we decide to remove callback API
1933
return promise.then(
2034
result => {
2135
callback( null, result );
@@ -30,15 +44,21 @@ const deprecateCallback = ( callback, promise ) => {
3044
return promise;
3145
};
3246

47+
/**
48+
* Turns existing bucket storage provider callback api into a promise based API
49+
*
50+
* @param {Object} store - a bucket storage object
51+
* @returns {Object} store api methods that use Promises instead of callbacks
52+
*/
3353
const promiseAPI = store => ( {
34-
get: ( id, callback ) =>
35-
callbackAsPromise( callback, store.get.bind( store, id ) ),
36-
update: ( id, object, isIndexing, callback ) =>
37-
callbackAsPromise( callback, store.update.bind( store, id, object, isIndexing ) ),
38-
remove: ( id, callback ) =>
39-
callbackAsPromise( callback, store.remove.bind( store, id ) ),
40-
find: ( query, callback ) =>
41-
callbackAsPromise( callback, store.find.bind( store, query ) )
54+
get: ( id ) =>
55+
callbackAsPromise( store.get.bind( store, id ) ),
56+
update: ( id, object, isIndexing ) =>
57+
callbackAsPromise( store.update.bind( store, id, object, isIndexing ) ),
58+
remove: ( id ) =>
59+
callbackAsPromise( store.remove.bind( store, id ) ),
60+
find: ( query ) =>
61+
callbackAsPromise( store.find.bind( store, query ) )
4262
} );
4363

4464
export default function Bucket( name, storeProvider, channel ) {
@@ -49,7 +69,7 @@ export default function Bucket( name, storeProvider, channel ) {
4969
this.isIndexing = false;
5070

5171
/**
52-
* Listeners for channel events
72+
* Listeners for channel events that will be added to Channel instance
5373
*/
5474
this.onChannelIndex = this.emit.bind( this, 'index' );
5575
this.onChannelError = this.emit.bind( this, 'error' );
@@ -73,6 +93,14 @@ export default function Bucket( name, storeProvider, channel ) {
7393

7494
inherits( Bucket, EventEmitter );
7595

96+
/**
97+
* Sets the channel the Bucket will use to sync changes.
98+
*
99+
* This exists to allow the Client to provide a backwards compatible API. There
100+
* is probably no reason to change the Channel once it's already set.
101+
*
102+
* @param {Channel} channel - channel instance to use for syncing
103+
*/
76104
Bucket.prototype.setChannel = function( channel ) {
77105
if ( this.channel ) {
78106
this.channel
@@ -95,7 +123,7 @@ Bucket.prototype.setChannel = function( channel ) {
95123
};
96124

97125
/**
98-
* Reloads all the data from Simperium
126+
* Reloads all the data from the currently cached set of ghost data
99127
*/
100128
Bucket.prototype.reload = function() {
101129
this.channel.reload();

0 commit comments

Comments
 (0)