Skip to content

Commit 7d33b22

Browse files
committed
Stable Version 2.0.0-beta.1.
Fixes #11.
1 parent 08d5f6c commit 7d33b22

File tree

8 files changed

+313
-200
lines changed

8 files changed

+313
-200
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
##### 2.0.0-beta.1 - 18 April 2015
2+
3+
###### Backwards compatible bug fixes
4+
- #11 - Race condition, tasks need to be atomic
5+
6+
###### Other
7+
- Updated dependencies
8+
19
##### 1.1.1 - 27 March 2015
210

311
###### Backwards compatible bug fixes

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"karma.start.js"
3030
],
3131
"dependencies": {
32-
"js-data": ">=1.5.7",
32+
"js-data": ">=2.0.0-beta.3",
3333
"firebase": ">=1.1.x"
3434
}
3535
}

dist/js-data-firebase.js

+158-100
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* 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>
44
* @author Jason Dobry <[email protected]>
55
* @copyright (c) 2014-2015 Jason Dobry
66
* @license MIT <https://github.com/js-data/js-data-firebase/blob/master/LICENSE>
@@ -63,25 +63,36 @@ return /******/ (function(modules) { // webpackBootstrap
6363
/* 0 */
6464
/***/ function(module, exports, __webpack_require__) {
6565

66-
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
66+
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
6767

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; }; })();
6969

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'); } };
7171

72-
var JSData = _interopRequire(__webpack_require__(1));
72+
Object.defineProperty(exports, '__esModule', {
73+
value: true
74+
});
7375

74-
var Firebase = _interopRequire(__webpack_require__(2));
76+
var _JSData = __webpack_require__(1);
7577

76-
var omit = _interopRequire(__webpack_require__(3));
78+
var _JSData2 = _interopRequireWildcard(_JSData);
7779

78-
var values = _interopRequire(__webpack_require__(4));
80+
var _Firebase = __webpack_require__(2);
7981

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;
8294
var deepMixIn = DSUtils.deepMixIn;
8395
var removeCircular = DSUtils.removeCircular;
84-
var P = DSUtils.Promise;
8596
var forOwn = DSUtils.forOwn;
8697

8798
var filter = emptyStore.defaults.defaultFilter;
@@ -90,7 +101,44 @@ return /******/ (function(modules) { // webpackBootstrap
90101
_classCallCheck(this, Defaults);
91102
};
92103

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+
}
94142

95143
var DSFirebaseAdapter = (function () {
96144
function DSFirebaseAdapter(options) {
@@ -99,92 +147,99 @@ return /******/ (function(modules) { // webpackBootstrap
99147
options = options || {};
100148
this.defaults = new Defaults();
101149
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);
103151
}
104152

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) {
118167
var item = dataSnapshot.val();
119168
if (!item) {
120-
reject(new Error("Not Found!"));
169+
reject(new Error('Not Found!'));
121170
} else {
122171
item[resourceConfig.idAttribute] = item[resourceConfig.idAttribute] || id;
123172
resolve(item);
124173
}
125174
}, reject, _this);
126175
});
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) {
135186
var data = dataSnapshot.val();
136187
forOwn(data, function (value, key) {
137188
if (!value[resourceConfig.idAttribute]) {
138-
value[resourceConfig.idAttribute] = "/" + key;
189+
value[resourceConfig.idAttribute] = '/' + key;
139190
}
140191
});
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);
143194
});
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) {
157210
if (err) {
158211
return reject(err);
159212
} else {
160-
var _id = itemRef.toString().replace(resourceRef.toString(), "");
213+
var _id = itemRef.toString().replace(resourceRef.toString(), '');
161214
itemRef.child(resourceConfig.idAttribute).set(_id, function (err) {
162215
if (err) {
163216
reject(err);
164217
} else {
165-
itemRef.once("value", function (dataSnapshot) {
218+
itemRef.once('value', function (dataSnapshot) {
166219
try {
167220
resolve(dataSnapshot.val());
168221
} catch (err) {
169222
reject(err);
170223
}
171-
}, reject, _this);
224+
}, reject, _this3);
172225
}
173226
});
174227
}
175228
});
176229
});
177-
}
230+
});
178231
}
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) {
188243
try {
189244
(function () {
190245
var item = dataSnapshot.val() || {};
@@ -217,57 +272,60 @@ return /******/ (function(modules) { // webpackBootstrap
217272
} catch (err) {
218273
reject(err);
219274
}
220-
}, reject, _this);
275+
}, reject, _this4);
221276
});
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));
234288
});
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) {
243300
if (err) {
244301
reject(err);
245302
} else {
246303
resolve();
247304
}
248305
});
249306
});
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));
262318
});
263-
}
319+
return DSUtils.Promise.all(tasks);
320+
});
264321
}
265-
});
322+
}]);
266323

267324
return DSFirebaseAdapter;
268325
})();
269326

270-
module.exports = DSFirebaseAdapter;
327+
exports['default'] = DSFirebaseAdapter;
328+
module.exports = exports['default'];
271329

272330
/***/ },
273331
/* 1 */

0 commit comments

Comments
 (0)