@@ -2,21 +2,21 @@ import { EventEmitter } from 'events'
22import { inherits } from 'util'
33import { 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 */
1218const 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 */
53122const 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+ */
64140export 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 */
140216Bucket . 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 */
152228Bucket . 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 */
166242Bucket . 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 */
191273Bucket . 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 */
204292Bucket . 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 */
216304Bucket . 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 */
230318Bucket . prototype . remove = function ( id , callback ) {
0 commit comments