Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 6ac5a0f

Browse files
committed
Merge pull request #752 from firebase/jw-utils-cleanup
Removed promise utils in favor of $q
2 parents c4e1a9c + 21a9ef4 commit 6ac5a0f

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

src/FirebaseArray.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
$add: function(data) {
111111
this._assertNotDestroyed('$add');
112112
var self = this;
113-
var def = $firebaseUtils.defer();
113+
var def = $q.defer();
114114
var ref = this.$ref().ref.push();
115115
var dataJSON;
116116

@@ -149,7 +149,7 @@
149149
var self = this;
150150
var item = self._resolveItem(indexOrItem);
151151
var key = self.$keyAt(item);
152-
var def = $firebaseUtils.defer();
152+
var def = $q.defer();
153153

154154
if( key !== null ) {
155155
var ref = self.$ref().ref.child(key);
@@ -199,7 +199,7 @@
199199
});
200200
}
201201
else {
202-
return $firebaseUtils.reject('Invalid record; could not determine key for '+indexOrItem);
202+
return $q.reject('Invalid record; could not determine key for '+indexOrItem);
203203
}
204204
},
205205

@@ -655,7 +655,7 @@
655655
}
656656
}
657657

658-
var def = $firebaseUtils.defer();
658+
var def = $q.defer();
659659
var created = function(snap, prevChild) {
660660
if (!firebaseArray) {
661661
return;

src/FirebaseAuth.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Define a service which provides user authentication and management.
66
angular.module('firebase').factory('$firebaseAuth', [
7-
'$firebaseUtils', function($firebaseUtils) {
7+
'$q', '$firebaseUtils', function($q, $firebaseUtils) {
88
/**
99
* This factory returns an object allowing you to manage the client's authentication state.
1010
*
@@ -15,13 +15,14 @@
1515
return function(auth) {
1616
auth = auth || firebase.auth();
1717

18-
var firebaseAuth = new FirebaseAuth($firebaseUtils, auth);
18+
var firebaseAuth = new FirebaseAuth($q, $firebaseUtils, auth);
1919
return firebaseAuth.construct();
2020
};
2121
}
2222
]);
2323

24-
FirebaseAuth = function($firebaseUtils, auth) {
24+
FirebaseAuth = function($q, $firebaseUtils, auth) {
25+
this._q = $q;
2526
this._utils = $firebaseUtils;
2627
if (typeof ref === 'string') {
2728
throw new Error('Please provide a Firebase reference instead of a URL when creating a `$firebaseAuth` object.');
@@ -76,7 +77,7 @@
7677
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
7778
*/
7879
signInWithCustomToken: function(authToken) {
79-
return this._utils.promise.when(this._auth.signInWithCustomToken(authToken));
80+
return this._q.when(this._auth.signInWithCustomToken(authToken));
8081
},
8182

8283
/**
@@ -85,7 +86,7 @@
8586
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
8687
*/
8788
signInAnonymously: function() {
88-
return this._utils.promise.when(this._auth.signInAnonymously());
89+
return this._q.when(this._auth.signInAnonymously());
8990
},
9091

9192
/**
@@ -96,7 +97,7 @@
9697
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
9798
*/
9899
signInWithEmailAndPassword: function(email, password) {
99-
return this._utils.promise.when(this._auth.signInWithEmailAndPassword(email, password));
100+
return this._q.when(this._auth.signInWithEmailAndPassword(email, password));
100101
},
101102

102103
/**
@@ -106,7 +107,7 @@
106107
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
107108
*/
108109
signInWithPopup: function(provider) {
109-
return this._utils.promise.when(this._auth.signInWithPopup(this._getProvider(provider)));
110+
return this._q.when(this._auth.signInWithPopup(this._getProvider(provider)));
110111
},
111112

112113
/**
@@ -116,7 +117,7 @@
116117
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
117118
*/
118119
signInWithRedirect: function(provider) {
119-
return this._utils.promise.when(this._auth.signInWithRedirect(this._getProvider(provider)));
120+
return this._q.when(this._auth.signInWithRedirect(this._getProvider(provider)));
120121
},
121122

122123
/**
@@ -126,7 +127,7 @@
126127
* @return {Promise<Object>} A promise fulfilled with an object containing authentication data.
127128
*/
128129
signInWithCredential: function(credential) {
129-
return this._utils.promise.when(this._auth.signInWithCredential(credential));
130+
return this._q.when(this._auth.signInWithCredential(credential));
130131
},
131132

132133
/**
@@ -191,10 +192,10 @@
191192
// to the current auth state and not a stale/initial state
192193
var authData = self.getAuth(), res = null;
193194
if (rejectIfAuthDataIsNull && authData === null) {
194-
res = self._utils.reject("AUTH_REQUIRED");
195+
res = self._q.reject("AUTH_REQUIRED");
195196
}
196197
else {
197-
res = self._utils.resolve(authData);
198+
res = self._q.when(authData);
198199
}
199200
return res;
200201
});
@@ -226,7 +227,7 @@
226227
_initAuthResolver: function() {
227228
var auth = this._auth;
228229

229-
return this._utils.promise(function(resolve) {
230+
return this._q(function(resolve) {
230231
var off;
231232
function callback() {
232233
// Turn off this onAuthStateChanged() callback since we just needed to get the authentication data once.
@@ -274,7 +275,7 @@
274275
* uid of the created user.
275276
*/
276277
createUserWithEmailAndPassword: function(email, password) {
277-
return this._utils.promise.when(this._auth.createUserWithEmailAndPassword(email, password));
278+
return this._q.when(this._auth.createUserWithEmailAndPassword(email, password));
278279
},
279280

280281
/**
@@ -286,9 +287,9 @@
286287
updatePassword: function(password) {
287288
var user = this.getAuth();
288289
if (user) {
289-
return this._utils.promise.when(user.updatePassword(password));
290+
return this._q.when(user.updatePassword(password));
290291
} else {
291-
return this._utils.reject("Cannot update password since there is no logged in user.");
292+
return this._q.reject("Cannot update password since there is no logged in user.");
292293
}
293294
},
294295

@@ -301,9 +302,9 @@
301302
updateEmail: function(email) {
302303
var user = this.getAuth();
303304
if (user) {
304-
return this._utils.promise.when(user.updateEmail(email));
305+
return this._q.when(user.updateEmail(email));
305306
} else {
306-
return this._utils.reject("Cannot update email since there is no logged in user.");
307+
return this._q.reject("Cannot update email since there is no logged in user.");
307308
}
308309
},
309310

@@ -315,9 +316,9 @@
315316
deleteUser: function() {
316317
var user = this.getAuth();
317318
if (user) {
318-
return this._utils.promise.when(user.delete());
319+
return this._q.when(user.delete());
319320
} else {
320-
return this._utils.reject("Cannot delete user since there is no logged in user.");
321+
return this._q.reject("Cannot delete user since there is no logged in user.");
321322
}
322323
},
323324

@@ -329,7 +330,7 @@
329330
* @return {Promise<>} An empty promise fulfilled once the reset password email is sent.
330331
*/
331332
sendPasswordResetEmail: function(email) {
332-
return this._utils.promise.when(this._auth.sendPasswordResetEmail(email));
333+
return this._q.when(this._auth.sendPasswordResetEmail(email));
333334
}
334335
};
335336
})();

src/FirebaseObject.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* </code></pre>
2424
*/
2525
angular.module('firebase').factory('$firebaseObject', [
26-
'$parse', '$firebaseUtils', '$log',
27-
function($parse, $firebaseUtils, $log) {
26+
'$parse', '$firebaseUtils', '$log', '$q',
27+
function($parse, $firebaseUtils, $log, $q) {
2828
/**
2929
* Creates a synchronized object with 2-way bindings between Angular and Firebase.
3030
*
@@ -73,7 +73,7 @@
7373
$save: function () {
7474
var self = this;
7575
var ref = self.$ref();
76-
var def = $firebaseUtils.defer();
76+
var def = $q.defer();
7777
var dataJSON;
7878

7979
try {
@@ -240,7 +240,7 @@
240240
$$scopeUpdated: function(newData) {
241241
// we use a one-directional loop to avoid feedback with 3-way bindings
242242
// since set() is applied locally anyway, this is still performant
243-
var def = $firebaseUtils.defer();
243+
var def = $q.defer();
244244
this.$ref().set($firebaseUtils.toJSON(newData), $firebaseUtils.makeNodeResolver(def));
245245
return def.promise;
246246
},
@@ -330,7 +330,7 @@
330330
this.key + '; one binding per instance ' +
331331
'(call unbind method or create another FirebaseObject instance)';
332332
$log.error(msg);
333-
return $firebaseUtils.reject(msg);
333+
return $q.reject(msg);
334334
}
335335
},
336336

@@ -450,7 +450,7 @@
450450
}
451451

452452
var isResolved = false;
453-
var def = $firebaseUtils.defer();
453+
var def = $q.defer();
454454
var applyUpdate = $firebaseUtils.batch(function(snap) {
455455
var changed = firebaseObject.$$updated(snap);
456456
if( changed ) {

src/utils.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,6 @@
153153
});
154154
},
155155

156-
defer: $q.defer,
157-
158-
reject: $q.reject,
159-
160-
resolve: $q.when,
161-
162-
promise: $q,
163-
164156
makeNodeResolver:function(deferred){
165157
return function(err,result){
166158
if(err === null){
@@ -332,7 +324,7 @@
332324
},
333325

334326
doSet: function(ref, data) {
335-
var def = utils.defer();
327+
var def = $q.defer();
336328
if( angular.isFunction(ref.set) || !angular.isObject(data) ) {
337329
// this is not a query, just do a flat set
338330
// Use try / catch to handle being passed data which is undefined or has invalid keys
@@ -362,7 +354,7 @@
362354
},
363355

364356
doRemove: function(ref) {
365-
var def = utils.defer();
357+
var def = $q.defer();
366358
if( angular.isFunction(ref.remove) ) {
367359
// ref is not a query, just do a flat remove
368360
ref.remove(utils.makeNodeResolver(def));

tests/unit/FirebaseArray.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('$firebaseArray', function () {
9898
it('should wait for promise resolution to update array', function () {
9999
var queue = [];
100100
function addPromise(snap, prevChild){
101-
return new $utils.promise(
101+
return $q(
102102
function(resolve) {
103103
queue.push(resolve);
104104
}).then(function(name) {
@@ -122,7 +122,7 @@ describe('$firebaseArray', function () {
122122
it('should wait to resolve $loaded until $$added promise is resolved', function () {
123123
var queue = [];
124124
function addPromise(snap, prevChild){
125-
return new $utils.promise(
125+
return $q(
126126
function(resolve) {
127127
queue.push(resolve);
128128
}).then(function(name) {

0 commit comments

Comments
 (0)