1
1
/*!
2
2
* js-data-firebase
3
- * @version 1.1 .1 - Homepage <http://www.js-data.io/docs/dsfirebaseadapter>
3
+ * @version 2.0.0-beta .1 - Homepage <http://www.js-data.io/docs/dsfirebaseadapter>
4
4
* @author Jason Dobry <[email protected] >
5
5
* @copyright (c) 2014-2015 Jason Dobry
6
6
* @license MIT <https://github.com/js-data/js-data-firebase/blob/master/LICENSE>
@@ -63,25 +63,36 @@ return /******/ (function(modules) { // webpackBootstrap
63
63
/* 0 */
64
64
/***/ function ( module , exports , __webpack_require__ ) {
65
65
66
- var _interopRequire = function ( obj ) { return obj && obj . __esModule ? obj [ "default" ] : obj ; } ;
66
+ var _interopRequireWildcard = function ( obj ) { return obj && obj . __esModule ? obj : { 'default' : obj } ; } ;
67
67
68
- var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var key in props ) { var prop = props [ key ] ; prop . configurable = true ; if ( prop . value ) prop . writable = true ; } Object . defineProperties ( target , props ) ; } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
68
+ var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( ' value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
69
69
70
- var _classCallCheck = function ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( " Cannot call a class as a function" ) ; } } ;
70
+ var _classCallCheck = function ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( ' Cannot call a class as a function' ) ; } } ;
71
71
72
- var JSData = _interopRequire ( __webpack_require__ ( 1 ) ) ;
72
+ Object . defineProperty ( exports , '__esModule' , {
73
+ value : true
74
+ } ) ;
73
75
74
- var Firebase = _interopRequire ( __webpack_require__ ( 2 ) ) ;
76
+ var _JSData = __webpack_require__ ( 1 ) ;
75
77
76
- var omit = _interopRequire ( __webpack_require__ ( 3 ) ) ;
78
+ var _JSData2 = _interopRequireWildcard ( _JSData ) ;
77
79
78
- var values = _interopRequire ( __webpack_require__ ( 4 ) ) ;
80
+ var _Firebase = __webpack_require__ ( 2 ) ;
79
81
80
- var emptyStore = new JSData . DS ( ) ;
81
- var DSUtils = JSData . DSUtils ;
82
+ var _Firebase2 = _interopRequireWildcard ( _Firebase ) ;
83
+
84
+ var _omit = __webpack_require__ ( 3 ) ;
85
+
86
+ var _omit2 = _interopRequireWildcard ( _omit ) ;
87
+
88
+ var _values = __webpack_require__ ( 4 ) ;
89
+
90
+ var _values2 = _interopRequireWildcard ( _values ) ;
91
+
92
+ var emptyStore = new _JSData2 [ 'default' ] . DS ( ) ;
93
+ var DSUtils = _JSData2 [ 'default' ] . DSUtils ;
82
94
var deepMixIn = DSUtils . deepMixIn ;
83
95
var removeCircular = DSUtils . removeCircular ;
84
- var P = DSUtils . Promise ;
85
96
var forOwn = DSUtils . forOwn ;
86
97
87
98
var filter = emptyStore . defaults . defaultFilter ;
@@ -90,7 +101,44 @@ return /******/ (function(modules) { // webpackBootstrap
90
101
_classCallCheck ( this , Defaults ) ;
91
102
} ;
92
103
93
- Defaults . prototype . basePath = "" ;
104
+ Defaults . prototype . basePath = '' ;
105
+
106
+ var queue = [ ] ;
107
+ var taskInProcess = false ;
108
+
109
+ function enqueue ( task ) {
110
+ queue . push ( task ) ;
111
+ }
112
+
113
+ function dequeue ( ) {
114
+ if ( queue . length && ! taskInProcess ) {
115
+ taskInProcess = true ;
116
+ queue [ 0 ] ( ) ;
117
+ }
118
+ }
119
+
120
+ function queueTask ( task ) {
121
+ if ( ! queue . length ) {
122
+ enqueue ( task ) ;
123
+ dequeue ( ) ;
124
+ } else {
125
+ enqueue ( task ) ;
126
+ }
127
+ }
128
+
129
+ function createTask ( fn ) {
130
+ return new DSUtils . Promise ( fn ) . then ( function ( result ) {
131
+ taskInProcess = false ;
132
+ queue . shift ( ) ;
133
+ setTimeout ( dequeue , 0 ) ;
134
+ return result ;
135
+ } , function ( err ) {
136
+ taskInProcess = false ;
137
+ queue . shift ( ) ;
138
+ setTimeout ( dequeue , 0 ) ;
139
+ return DSUtils . Promise . reject ( err ) ;
140
+ } ) ;
141
+ }
94
142
95
143
var DSFirebaseAdapter = ( function ( ) {
96
144
function DSFirebaseAdapter ( options ) {
@@ -99,92 +147,99 @@ return /******/ (function(modules) { // webpackBootstrap
99
147
options = options || { } ;
100
148
this . defaults = new Defaults ( ) ;
101
149
deepMixIn ( this . defaults , options ) ;
102
- this . ref = new Firebase ( options . basePath || this . defaults . basePath ) ;
150
+ this . ref = new _Firebase2 [ 'default' ] ( options . basePath || this . defaults . basePath ) ;
103
151
}
104
152
105
- _createClass ( DSFirebaseAdapter , {
106
- getRef : {
107
- value : function getRef ( resourceConfig , options ) {
108
- options = options || { } ;
109
- return this . ref . child ( options . endpoint || resourceConfig . endpoint ) ;
110
- }
111
- } ,
112
- find : {
113
- value : function find ( resourceConfig , id , options ) {
114
- var _this = this ;
115
-
116
- return new P ( function ( resolve , reject ) {
117
- return _this . getRef ( resourceConfig , options ) . child ( id ) . once ( "value" , function ( dataSnapshot ) {
153
+ _createClass ( DSFirebaseAdapter , [ {
154
+ key : 'getRef' ,
155
+ value : function getRef ( resourceConfig , options ) {
156
+ options = options || { } ;
157
+ return this . ref . child ( options . endpoint || resourceConfig . endpoint ) ;
158
+ }
159
+ } , {
160
+ key : 'find' ,
161
+ value : function find ( resourceConfig , id , options ) {
162
+ var _this = this ;
163
+
164
+ return createTask ( function ( resolve , reject ) {
165
+ queueTask ( function ( ) {
166
+ _this . getRef ( resourceConfig , options ) . child ( id ) . once ( 'value' , function ( dataSnapshot ) {
118
167
var item = dataSnapshot . val ( ) ;
119
168
if ( ! item ) {
120
- reject ( new Error ( " Not Found!" ) ) ;
169
+ reject ( new Error ( ' Not Found!' ) ) ;
121
170
} else {
122
171
item [ resourceConfig . idAttribute ] = item [ resourceConfig . idAttribute ] || id ;
123
172
resolve ( item ) ;
124
173
}
125
174
} , reject , _this ) ;
126
175
} ) ;
127
- }
128
- } ,
129
- findAll : {
130
- value : function findAll ( resourceConfig , params , options ) {
131
- var _this = this ;
132
-
133
- return new P ( function ( resolve , reject ) {
134
- return _this . getRef ( resourceConfig , options ) . once ( "value" , function ( dataSnapshot ) {
176
+ } ) ;
177
+ }
178
+ } , {
179
+ key : 'findAll' ,
180
+ value : function findAll ( resourceConfig , params , options ) {
181
+ var _this2 = this ;
182
+
183
+ return createTask ( function ( resolve , reject ) {
184
+ queueTask ( function ( ) {
185
+ _this2 . getRef ( resourceConfig , options ) . once ( 'value' , function ( dataSnapshot ) {
135
186
var data = dataSnapshot . val ( ) ;
136
187
forOwn ( data , function ( value , key ) {
137
188
if ( ! value [ resourceConfig . idAttribute ] ) {
138
- value [ resourceConfig . idAttribute ] = "/" + key ;
189
+ value [ resourceConfig . idAttribute ] = '/' + key ;
139
190
}
140
191
} ) ;
141
- resolve ( filter . call ( emptyStore , values ( data ) , resourceConfig . name , params , options ) ) ;
142
- } , reject , _this ) ;
192
+ resolve ( filter . call ( emptyStore , _values2 [ 'default' ] ( data ) , resourceConfig . name , params , options ) ) ;
193
+ } , reject , _this2 ) ;
143
194
} ) ;
144
- }
145
- } ,
146
- create : {
147
- value : function create ( resourceConfig , attrs , options ) {
148
- var _this = this ;
149
-
150
- var id = attrs [ resourceConfig . idAttribute ] ;
151
- if ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) ) {
152
- return this . update ( resourceConfig , id , attrs , options ) ;
153
- } else {
154
- return new P ( function ( resolve , reject ) {
155
- var resourceRef = _this . getRef ( resourceConfig , options ) ;
156
- var itemRef = resourceRef . push ( removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) , function ( err ) {
195
+ } ) ;
196
+ }
197
+ } , {
198
+ key : 'create' ,
199
+ value : function create ( resourceConfig , attrs , options ) {
200
+ var _this3 = this ;
201
+
202
+ var id = attrs [ resourceConfig . idAttribute ] ;
203
+ if ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) ) {
204
+ return this . update ( resourceConfig , id , attrs , options ) ;
205
+ } else {
206
+ return createTask ( function ( resolve , reject ) {
207
+ queueTask ( function ( ) {
208
+ var resourceRef = _this3 . getRef ( resourceConfig , options ) ;
209
+ var itemRef = resourceRef . push ( removeCircular ( _omit2 [ 'default' ] ( attrs , resourceConfig . relationFields || [ ] ) ) , function ( err ) {
157
210
if ( err ) {
158
211
return reject ( err ) ;
159
212
} else {
160
- var _id = itemRef . toString ( ) . replace ( resourceRef . toString ( ) , "" ) ;
213
+ var _id = itemRef . toString ( ) . replace ( resourceRef . toString ( ) , '' ) ;
161
214
itemRef . child ( resourceConfig . idAttribute ) . set ( _id , function ( err ) {
162
215
if ( err ) {
163
216
reject ( err ) ;
164
217
} else {
165
- itemRef . once ( " value" , function ( dataSnapshot ) {
218
+ itemRef . once ( ' value' , function ( dataSnapshot ) {
166
219
try {
167
220
resolve ( dataSnapshot . val ( ) ) ;
168
221
} catch ( err ) {
169
222
reject ( err ) ;
170
223
}
171
- } , reject , _this ) ;
224
+ } , reject , _this3 ) ;
172
225
}
173
226
} ) ;
174
227
}
175
228
} ) ;
176
229
} ) ;
177
- }
230
+ } ) ;
178
231
}
179
- } ,
180
- update : {
181
- value : function update ( resourceConfig , id , attrs , options ) {
182
- var _this = this ;
183
-
184
- attrs = removeCircular ( omit ( attrs || { } , resourceConfig . relationFields || [ ] ) ) ;
185
- return new P ( function ( resolve , reject ) {
186
- var itemRef = _this . getRef ( resourceConfig , options ) . child ( id ) ;
187
- itemRef . once ( "value" , function ( dataSnapshot ) {
232
+ }
233
+ } , {
234
+ key : 'update' ,
235
+ value : function update ( resourceConfig , id , attrs , options ) {
236
+ var _this4 = this ;
237
+
238
+ return createTask ( function ( resolve , reject ) {
239
+ queueTask ( function ( ) {
240
+ attrs = removeCircular ( _omit2 [ 'default' ] ( attrs || { } , resourceConfig . relationFields || [ ] ) ) ;
241
+ var itemRef = _this4 . getRef ( resourceConfig , options ) . child ( id ) ;
242
+ itemRef . once ( 'value' , function ( dataSnapshot ) {
188
243
try {
189
244
( function ( ) {
190
245
var item = dataSnapshot . val ( ) || { } ;
@@ -217,57 +272,60 @@ return /******/ (function(modules) { // webpackBootstrap
217
272
} catch ( err ) {
218
273
reject ( err ) ;
219
274
}
220
- } , reject , _this ) ;
275
+ } , reject , _this4 ) ;
221
276
} ) ;
222
- }
223
- } ,
224
- updateAll : {
225
- value : function updateAll ( resourceConfig , attrs , params , options ) {
226
- var _this = this ;
227
-
228
- return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
229
- var tasks = [ ] ;
230
- DSUtils . forEach ( items , function ( item ) {
231
- tasks . push ( _this . update ( resourceConfig , item [ resourceConfig . idAttribute ] , attrs , options ) ) ;
232
- } ) ;
233
- return P . all ( tasks ) ;
277
+ } ) ;
278
+ }
279
+ } , {
280
+ key : 'updateAll' ,
281
+ value : function updateAll ( resourceConfig , attrs , params , options ) {
282
+ var _this5 = this ;
283
+
284
+ return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
285
+ var tasks = [ ] ;
286
+ DSUtils . forEach ( items , function ( item ) {
287
+ tasks . push ( _this5 . update ( resourceConfig , item [ resourceConfig . idAttribute ] , attrs , options ) ) ;
234
288
} ) ;
235
- }
236
- } ,
237
- destroy : {
238
- value : function destroy ( resourceConfig , id , options ) {
239
- var _this = this ;
240
-
241
- return new P ( function ( resolve , reject ) {
242
- _this . getRef ( resourceConfig , options ) . child ( id ) . remove ( function ( err ) {
289
+ return DSUtils . Promise . all ( tasks ) ;
290
+ } ) ;
291
+ }
292
+ } , {
293
+ key : 'destroy' ,
294
+ value : function destroy ( resourceConfig , id , options ) {
295
+ var _this6 = this ;
296
+
297
+ return createTask ( function ( resolve , reject ) {
298
+ queueTask ( function ( ) {
299
+ _this6 . getRef ( resourceConfig , options ) . child ( id ) . remove ( function ( err ) {
243
300
if ( err ) {
244
301
reject ( err ) ;
245
302
} else {
246
303
resolve ( ) ;
247
304
}
248
305
} ) ;
249
306
} ) ;
250
- }
251
- } ,
252
- destroyAll : {
253
- value : function destroyAll ( resourceConfig , params , options ) {
254
- var _this = this ;
255
-
256
- return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
257
- var tasks = [ ] ;
258
- DSUtils . forEach ( items , function ( item ) {
259
- tasks . push ( _this . destroy ( resourceConfig , item [ resourceConfig . idAttribute ] , options ) ) ;
260
- } ) ;
261
- return P . all ( tasks ) ;
307
+ } ) ;
308
+ }
309
+ } , {
310
+ key : 'destroyAll' ,
311
+ value : function destroyAll ( resourceConfig , params , options ) {
312
+ var _this7 = this ;
313
+
314
+ return this . findAll ( resourceConfig , params , options ) . then ( function ( items ) {
315
+ var tasks = [ ] ;
316
+ DSUtils . forEach ( items , function ( item ) {
317
+ tasks . push ( _this7 . destroy ( resourceConfig , item [ resourceConfig . idAttribute ] , options ) ) ;
262
318
} ) ;
263
- }
319
+ return DSUtils . Promise . all ( tasks ) ;
320
+ } ) ;
264
321
}
265
- } ) ;
322
+ } ] ) ;
266
323
267
324
return DSFirebaseAdapter ;
268
325
} ) ( ) ;
269
326
270
- module . exports = DSFirebaseAdapter ;
327
+ exports [ 'default' ] = DSFirebaseAdapter ;
328
+ module . exports = exports [ 'default' ] ;
271
329
272
330
/***/ } ,
273
331
/* 1 */
0 commit comments