forked from casetext/fireproof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (168 loc) · 4.93 KB
/
index.js
File metadata and controls
227 lines (168 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
221
222
223
224
225
226
'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) {
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);
};
}
}
Fireproof.defer = function() {
if (Fireproof.Q === undefined) {
throw new Error('You must supply a Promise library to Fireproof by calling Fireproof.bless()!');
}
if (typeof Fireproof.Q.defer === 'function') {
return Fireproof.Q.defer();
} else {
var deferred = {};
deferred.promise = new Fireproof.Q(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
};
/**
* Tell Fireproof to use a given function to set timeouts from now on.
* NB: If you are using AMD/require.js, you MUST call this function!
* @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 deferred = Fireproof.defer();
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) {
deferred.reject(err);
} else {
deferred.resolve(val);
}
};
rv.promise = deferred.promise;
return rv;
};
/**
* Tell Fireproof to use a given promise library from now on.
* @method Fireproof.bless
* @param {Q} Q a Q-style promise constructor with mandatory .all() and optional defer().
* @throws if you don't provide a valid promise library.
*/
Fireproof.bless = function(newQ) {
function assert(value) {
if (!value) {
throw new Error('You tried to give Fireproof an invalid promise constructor!');
}
}
assert(newQ !== null);
assert(typeof newQ.all === 'function');
if (typeof newQ.defer === 'function') {
var deferred = newQ.defer();
assert(typeof deferred.promise.then === 'function');
assert(typeof deferred.resolve === 'function');
assert(typeof deferred.reject === 'function');
} else {
assert(typeof newQ === 'function');
var promise = new newQ();
assert(typeof promise.then === 'function');
assert(typeof promise.catch === 'function');
}
Fireproof.Q = newQ;
};
/**
* 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();
};