This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (166 loc) · 4.93 KB
/
index.js
File metadata and controls
220 lines (166 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict';
/**
* Fireproofs an existing Firebase reference, giving it magic promise powers.
* @name Fireproof
* @constructor
* @global
* @param {Firebase} firebaseRef A Firebase reference object.
* @property then A promise shortcut for .once('value'),
* except for references created by .push(), where it resolves on success
* and rejects on failure of the property object.
* @example
* var fp = new Fireproof(new Firebase('https://test.firebaseio.com/something'));
* fp.then(function(snap) { console.log(snap.val()); });
*/
function Fireproof(firebaseRef, promise) {
if (!Fireproof.Promise) {
try {
Fireproof.Promise = Promise;
} catch(e) {
throw new Error('You must supply a Promise library to Fireproof!');
}
} else if (typeof Fireproof.Promise !== 'function') {
throw new Error('The supplied value of Fireproof.Promise is not a constructor (got ' +
Fireproof.Promise + ')');
}
this._ref = firebaseRef;
if (promise && promise.then) {
this.then = promise.then.bind(promise);
} else {
this.then = function(ok, fail) {
return this.once('value', function() {})
.then(ok || null, fail || null);
};
}
}
/**
* Tell Fireproof to use a given defer-style promise library from now on.
* If you have native promises, you don't need to call this;
* if you want to substitute a different promise constructor, just set it on Fireproof.Promise directly.
* @deprecated
* @method Fireproof.bless
* @param {Function} Deferrable a deferrable promise constructor with .all().
* @throws if you don't provide a valid promise library.
*/
Fireproof.bless = function(Deferrable) {
Fireproof.Promise = function(fn) {
var deferred = Deferrable.defer();
this.then = deferred.promise.then.bind(deferred.promise);
fn(deferred.resolve.bind(deferred), deferred.reject.bind(deferred));
};
Fireproof.Promise.all = Deferrable.all;
Fireproof.Promise.resolve = function(value) {
return new Fireproof.Promise(function(resolve) {
resolve(value);
});
};
Fireproof.Promise.reject = function(value) {
return new Fireproof.Promise(function(resolve, reject) {
reject(value);
});
};
};
/**
* Tell Fireproof to use a given function to set timeouts from now on.
* @method Fireproof.setNextTick
* @param {Function} nextTick a function that takes a function and
* runs it in the immediate future.
*/
Fireproof.setNextTick = function(fn) {
Fireproof.__nextTick = fn;
};
Fireproof._nextTick = function(fn) {
if (Fireproof.__nextTick) {
Fireproof.__nextTick(fn, 0);
} else {
setTimeout(fn, 0);
}
};
Fireproof._handleError = function(onComplete) {
var resolve, reject;
var promise = new Fireproof.Promise(function(_resolve_, _reject_) {
resolve = _resolve_;
reject = _reject_;
});
var rv = function(err, val) {
var context = this,
rvArgs = arguments;
// finish stats event, if there is one.
if (rv.id) {
Fireproof.stats._finish(rv.id, err);
}
if (onComplete && typeof onComplete === 'function') {
Fireproof._nextTick(function() {
onComplete.apply(context, rvArgs);
});
}
if (err) {
reject(err);
} else {
resolve(val);
}
};
rv.promise = promise;
return rv;
};
/**
* Delegates Firebase#child, wrapping the child in fireproofing.
* @method Fireproof#child
* @param {string} childPath The subpath to refer to.
* @returns {Fireproof} A reference to the child path.
*/
Fireproof.prototype.child = function(childPath) {
return new Fireproof(this._ref.child(childPath));
};
/**
* Delegates Firebase#parent, wrapping the child in fireproofing.
* @method Fireproof#parent
* @returns {Fireproof} A ref to the parent path, or null if there is none.
*/
Fireproof.prototype.parent = function() {
if (this._ref.parent() === null) {
return null;
} else {
return new Fireproof(this._ref.parent());
}
};
/**
* Delegates Firebase#root, wrapping the root in fireproofing.
* @method Fireproof#root
* @returns {Fireproof} A ref to the root.
*/
Fireproof.prototype.root = function() {
return new Fireproof(this._ref.root());
};
/**
* Hands back the original Firebase reference.
* @method Fireproof#toFirebase
* @returns {Firebase} The proxied Firebase reference.
*/
Fireproof.prototype.toFirebase = function() {
return this._ref;
};
/**
* Delegates Firebase#name.
* @method Fireproof#name
* @returns {string} The last component of this reference object's path.
*/
Fireproof.prototype.name = function() {
return this._ref.name();
};
/**
* Delegates Firebase#key.
* @method Fireproof#key
* @returns {string} The last component of this reference object's path.
*/
Fireproof.prototype.key = function() {
return this._ref.key();
};
/**
* Delegates Firebase#toString.
* @method Fireproof#toString
* @returns {string} The full URL of this reference object.
*/
Fireproof.prototype.toString = function() {
return this._ref.toString();
};