Skip to content

Commit b279521

Browse files
committed
WIP: fix forEach invocations from legacy code
1 parent 963b75c commit b279521

File tree

12 files changed

+53
-52
lines changed

12 files changed

+53
-52
lines changed

src/database/core/PersistentConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,8 @@ export class PersistentConnection {
834834
// Puts depend on having received the corresponding data update from the server before they complete, so we must
835835
// make sure to send listens before puts.
836836
var self = this;
837-
forEach(this.listens_, function(queries, pathString) {
838-
forEach(queries, function(listenSpec) {
837+
forEach(this.listens_, function(pathString, queries) {
838+
forEach(queries, function(key, listenSpec) {
839839
self.sendListen_(listenSpec);
840840
});
841841
});

src/database/core/Repo.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,17 @@ export class Repo {
327327
* @param {!Object} childrenToMerge
328328
* @param {?function(?Error, *=)} onComplete
329329
*/
330-
update(path, childrenToMerge, onComplete) {
330+
update(path: Path, childrenToMerge, onComplete) {
331331
this.log_('update', {path: path.toString(), value: childrenToMerge});
332332

333333
// Start with our existing data and merge each child into it.
334334
var empty = true;
335335
var serverValues = this.generateServerValues();
336336
var changedChildren = {};
337-
forEach(childrenToMerge, function(changedValue, changedKey) {
337+
forEach(childrenToMerge, function(changedKey, changedValue) {
338338
empty = false;
339339
var newNodeUnresolved = nodeFromJSON(changedValue);
340-
changedChildren[changedKey] =
341-
resolveDeferredValueSnapshot(newNodeUnresolved, serverValues);
340+
changedChildren[changedKey] = resolveDeferredValueSnapshot(newNodeUnresolved, serverValues);
342341
});
343342

344343
if (!empty) {
@@ -361,7 +360,7 @@ export class Repo {
361360
self.callOnCompleteCallback(onComplete, status, errorReason);
362361
});
363362

364-
forEach(childrenToMerge, function(changedValue, changedPath) {
363+
forEach(childrenToMerge, function(changedPath, changedValue) {
365364
var affectedPath = self.abortTransactions_(path.child(changedPath));
366365
self.rerunTransactions_(affectedPath);
367366
});

src/database/core/SyncTree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export class SyncTree {
520520
})
521521
);
522522
}
523-
forEach(childMap, function(childQueries) {
523+
forEach(childMap, function(key, childQueries) {
524524
queries = queries.concat(childQueries);
525525
});
526526
return queries;

src/database/core/WriteTree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class WriteTree {
164164
} else {
165165
var children = writeToRemove.children;
166166
var self = this;
167-
forEach(children, function(childSnap, childName) {
167+
forEach(children, function(childName, childSnap) {
168168
self.visibleWrites_ = self.visibleWrites_.removeWrite(writeToRemove.path.child(childName));
169169
});
170170
}

src/database/core/util/CountedSet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class CountedSet {
7272
* @param {function(K, V)} fn
7373
*/
7474
each(fn) {
75-
forEach(this.set, function(v, k) {
75+
forEach(this.set, function(k, v) {
7676
fn(k, v);
7777
});
7878
}
@@ -83,7 +83,7 @@ export class CountedSet {
8383
*/
8484
keys() {
8585
var keys = [];
86-
forEach(this.set, function(v, k) {
86+
forEach(this.set, function(k, v) {
8787
keys.push(k);
8888
});
8989
return keys;

src/database/core/util/ImmutableTree.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ImmutableTree {
3030
*/
3131
static fromObject(obj) {
3232
var tree = ImmutableTree.Empty;
33-
forEach(obj, function(childSnap, childPath) {
33+
forEach(obj, function(childPath, childSnap) {
3434
tree = tree.set(new Path(childPath), childSnap);
3535
});
3636
return tree;
@@ -75,7 +75,7 @@ export class ImmutableTree {
7575
* node
7676
* @return {?{path:!Path, value:!T}}
7777
*/
78-
findRootMostMatchingPathAndValue(relativePath, predicate) {
78+
findRootMostMatchingPathAndValue(relativePath: Path, predicate) {
7979
if (this.value != null && predicate(this.value)) {
8080
return {path: Path.Empty, value: this.value};
8181
} else {
@@ -89,8 +89,7 @@ export class ImmutableTree {
8989
child.findRootMostMatchingPathAndValue(relativePath.popFront(),
9090
predicate);
9191
if (childExistingPathAndValue != null) {
92-
var fullPath = new Path(front)
93-
.child(childExistingPathAndValue.path);
92+
var fullPath = new Path(front).child(childExistingPathAndValue.path);
9493
return {path: fullPath, value: childExistingPathAndValue.value};
9594
} else {
9695
return null;

src/database/core/util/Path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class Path {
2323
* or another path, or the raw tokens array
2424
* @param {number=} opt_pieceNum
2525
*/
26-
constructor(pathOrString, opt_pieceNum?) {
26+
constructor(pathOrString: string|string[], opt_pieceNum?) {
2727
if (arguments.length == 1) {
28-
this.pieces_ = pathOrString.split('/');
28+
this.pieces_ = (<string>pathOrString).split('/');
2929

3030
// Remove empty pieces.
3131
var copyTo = 0;

src/database/core/util/Tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class Tree {
113113
*/
114114
forEachChild(action) {
115115
var self = this;
116-
forEach(this.node_.children, function(childTree, child) {
116+
forEach(this.node_.children, function(child, childTree) {
117117
action(new Tree(child, self, childTree));
118118
});
119119
}

src/database/core/util/util.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,13 @@ export const each = function(obj, fn) {
447447
fn(i, obj[i]);
448448
}
449449
} else {
450-
forEach(obj, fn);
450+
/**
451+
* in the conversion of code we removed the goog.object.forEach
452+
* function which did a value,key callback. We standardized on
453+
* a single impl that does a key, value callback. So we invert
454+
* to not have to touch the `each` code points
455+
*/
456+
forEach(obj, (key, val) => fn(val, key));
451457
}
452458
};
453459

src/database/core/util/validation.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Path, ValidationPath } from "./Path";
22
import { forEach, contains } from "../../../utils/obj";
33
import { isInvalidJSONNumber } from "./util";
4-
import { errorPrefix } from "../../../utils/validation";
4+
import { errorPrefix as errorPrefixFxn } from "../../../utils/validation";
55
import { stringLength } from "../../../utils/utf8";
66

77
/**
@@ -83,7 +83,7 @@ export const validateFirebaseDataArg = function(fnName, argumentNumber, data, pa
8383
return;
8484

8585
validateFirebaseData(
86-
errorPrefix(fnName, argumentNumber, optional),
86+
errorPrefixFxn(fnName, argumentNumber, optional),
8787
data, path
8888
);
8989
}
@@ -204,7 +204,7 @@ export const validateFirebaseMergeDataArg = function(fnName, argumentNumber, dat
204204
if (optional && data === undefined)
205205
return;
206206

207-
var errorPrefix = errorPrefix(fnName, argumentNumber, optional);
207+
var errorPrefix = errorPrefixFxn(fnName, argumentNumber, optional);
208208

209209
if (!(data && typeof data === 'object') || Array.isArray(data)) {
210210
throw new Error(errorPrefix + ' must be an object containing the children to replace.');
@@ -231,14 +231,14 @@ export const validatePriority = function(fnName, argumentNumber, priority, optio
231231
return;
232232
if (isInvalidJSONNumber(priority))
233233
throw new Error(
234-
errorPrefix(fnName, argumentNumber, optional) +
234+
errorPrefixFxn(fnName, argumentNumber, optional) +
235235
'is ' + priority.toString() +
236236
', but must be a valid Firebase priority (a string, finite number, ' +
237237
'server value, or null).');
238238
// Special case to allow importing data with a .sv.
239239
if (!isValidPriority(priority))
240240
throw new Error(
241-
errorPrefix(fnName, argumentNumber, optional) +
241+
errorPrefixFxn(fnName, argumentNumber, optional) +
242242
'must be a valid Firebase priority ' +
243243
'(a string, finite number, server value, or null).');
244244
}
@@ -256,7 +256,7 @@ export const validateEventType = function(fnName, argumentNumber, eventType, opt
256256
break;
257257
default:
258258
throw new Error(
259-
errorPrefix(fnName, argumentNumber, optional) +
259+
errorPrefixFxn(fnName, argumentNumber, optional) +
260260
'must be a valid event type = "value", "child_added", "child_removed", ' +
261261
'"child_changed", or "child_moved".');
262262
}
@@ -266,7 +266,7 @@ export const validateKey = function(fnName, argumentNumber, key, optional) {
266266
if (optional && key === undefined)
267267
return;
268268
if (!isValidKey(key))
269-
throw new Error(errorPrefix(fnName, argumentNumber, optional) +
269+
throw new Error(errorPrefixFxn(fnName, argumentNumber, optional) +
270270
'was an invalid key = "' + key +
271271
'". Firebase keys must be non-empty strings and ' +
272272
'can\'t contain ".", "#", "$", "/", "[", or "]").');
@@ -277,7 +277,7 @@ export const validatePathString = function(fnName, argumentNumber, pathString, o
277277
return;
278278

279279
if (!isValidPathString(pathString))
280-
throw new Error(errorPrefix(fnName, argumentNumber, optional) +
280+
throw new Error(errorPrefixFxn(fnName, argumentNumber, optional) +
281281
'was an invalid path = "' +
282282
pathString +
283283
'". Paths must be non-empty strings and ' +
@@ -305,7 +305,7 @@ export const validateUrl = function(fnName, argumentNumber, parsedUrl) {
305305
if (!(typeof parsedUrl.repoInfo.host === 'string') || parsedUrl.repoInfo.host.length === 0 ||
306306
!isValidKey(parsedUrl.repoInfo.namespace) ||
307307
(pathString.length !== 0 && !isValidRootPathString(pathString))) {
308-
throw new Error(errorPrefix(fnName, argumentNumber, false) +
308+
throw new Error(errorPrefixFxn(fnName, argumentNumber, false) +
309309
'must be a valid firebase URL and ' +
310310
'the path can\'t contain ".", "#", "$", "[", or "]".');
311311
}
@@ -316,15 +316,15 @@ export const validateCredential = function(fnName, argumentNumber, cred, optiona
316316
return;
317317
if (!(typeof cred === 'string'))
318318
throw new Error(
319-
errorPrefix(fnName, argumentNumber, optional) +
319+
errorPrefixFxn(fnName, argumentNumber, optional) +
320320
'must be a valid credential (a string).');
321321
}
322322

323323
export const validateBoolean = function(fnName, argumentNumber, bool, optional) {
324324
if (optional && bool === undefined)
325325
return;
326326
if (typeof bool !== 'boolean')
327-
throw new Error(errorPrefix(fnName, argumentNumber, optional) +
327+
throw new Error(errorPrefixFxn(fnName, argumentNumber, optional) +
328328
'must be a boolean.');
329329
}
330330

@@ -333,7 +333,7 @@ export const validateString = function(fnName, argumentNumber, string, optional)
333333
return;
334334
if (!(typeof string === 'string')) {
335335
throw new Error(
336-
errorPrefix(fnName, argumentNumber, optional) +
336+
errorPrefixFxn(fnName, argumentNumber, optional) +
337337
'must be a valid string.');
338338
}
339339
}
@@ -343,7 +343,7 @@ export const validateObject = function(fnName, argumentNumber, obj, optional) {
343343
return;
344344
if (!(obj && typeof obj === 'object') || obj === null) {
345345
throw new Error(
346-
errorPrefix(fnName, argumentNumber, optional) +
346+
errorPrefixFxn(fnName, argumentNumber, optional) +
347347
'must be a valid object.');
348348
}
349349
}
@@ -356,7 +356,7 @@ export const validateObjectContainsKey = function(fnName, argumentNumber, obj, k
356356
return;
357357
} else {
358358
throw new Error(
359-
errorPrefix(fnName, argumentNumber, optional) +
359+
errorPrefixFxn(fnName, argumentNumber, optional) +
360360
'must contain the key "' + key + '"');
361361
}
362362
}
@@ -369,10 +369,10 @@ export const validateObjectContainsKey = function(fnName, argumentNumber, obj, k
369369
(opt_type === 'function' && !(typeof val === 'function')) ||
370370
(opt_type === 'object' && !(typeof val === 'object') && val)) {
371371
if (optional) {
372-
throw new Error(errorPrefix(fnName, argumentNumber, optional) +
372+
throw new Error(errorPrefixFxn(fnName, argumentNumber, optional) +
373373
'contains invalid value for key "' + key + '" (must be of type "' + opt_type + '")');
374374
} else {
375-
throw new Error(errorPrefix(fnName, argumentNumber, optional) +
375+
throw new Error(errorPrefixFxn(fnName, argumentNumber, optional) +
376376
'must contain the key "' + key + '" with type "' + opt_type + '"');
377377
}
378378
}

src/database/core/view/filter/LimitedFilter.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RangedFilter } from "./RangedFilter";
22
import { ChildrenNode } from "../../snap/ChildrenNode";
3-
import { NamedNode } from "../../snap/Node";
3+
import { Node, NamedNode } from "../../snap/Node";
44
import { assert } from "../../../../utils/assert";
55
import { Change } from "../Change";
66
/**
@@ -202,22 +202,19 @@ export class LimitedFilter {
202202
* @return {!Node}
203203
* @private
204204
*/
205-
fullLimitUpdateChild_(snap, childKey, childSnap, source, optChangeAccumulator) {
205+
fullLimitUpdateChild_(snap: Node, childKey: string, childSnap: Node, source, changeAccumulator?) {
206206
// TODO: rename all cache stuff etc to general snap terminology
207-
var Change = Change;
208207
var cmp;
209208
if (this.reverse_) {
210209
var indexCmp = this.index_.getCompare();
211210
cmp = function(a, b) { return indexCmp(b, a); };
212211
} else {
213212
cmp = this.index_.getCompare();
214213
}
215-
var oldEventCache = /** @type {!ChildrenNode} */ (snap);
214+
var oldEventCache = <ChildrenNode>snap;
216215
assert(oldEventCache.numChildren() == this.limit_, '');
217216
var newChildNamedNode = new NamedNode(childKey, childSnap);
218-
var windowBoundary = /** @type {!NamedNode} */
219-
(this.reverse_ ? oldEventCache.getFirstChild(this.index_) :
220-
oldEventCache.getLastChild(this.index_));
217+
var windowBoundary = <NamedNode>(this.reverse_ ? oldEventCache.getFirstChild(this.index_) : oldEventCache.getLastChild(this.index_));
221218
var inRange = this.rangedFilter_.matches(newChildNamedNode);
222219
if (oldEventCache.hasChild(childKey)) {
223220
var oldChildSnap = oldEventCache.getImmediateChild(childKey);
@@ -231,19 +228,19 @@ export class LimitedFilter {
231228
var compareNext = nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
232229
var remainsInWindow = inRange && !childSnap.isEmpty() && compareNext >= 0;
233230
if (remainsInWindow) {
234-
if (optChangeAccumulator != null) {
235-
optChangeAccumulator.trackChildChange(Change.childChangedChange(childKey, childSnap, oldChildSnap));
231+
if (changeAccumulator != null) {
232+
changeAccumulator.trackChildChange(Change.childChangedChange(childKey, childSnap, oldChildSnap));
236233
}
237234
return oldEventCache.updateImmediateChild(childKey, childSnap);
238235
} else {
239-
if (optChangeAccumulator != null) {
240-
optChangeAccumulator.trackChildChange(Change.childRemovedChange(childKey, oldChildSnap));
236+
if (changeAccumulator != null) {
237+
changeAccumulator.trackChildChange(Change.childRemovedChange(childKey, oldChildSnap));
241238
}
242239
var newEventCache = oldEventCache.updateImmediateChild(childKey, ChildrenNode.EMPTY_NODE);
243240
var nextChildInRange = nextChild != null && this.rangedFilter_.matches(nextChild);
244241
if (nextChildInRange) {
245-
if (optChangeAccumulator != null) {
246-
optChangeAccumulator.trackChildChange(Change.childAddedChange(nextChild.name, nextChild.node));
242+
if (changeAccumulator != null) {
243+
changeAccumulator.trackChildChange(Change.childAddedChange(nextChild.name, nextChild.node));
247244
}
248245
return newEventCache.updateImmediateChild(nextChild.name, nextChild.node);
249246
} else {
@@ -255,9 +252,9 @@ export class LimitedFilter {
255252
return snap;
256253
} else if (inRange) {
257254
if (cmp(windowBoundary, newChildNamedNode) >= 0) {
258-
if (optChangeAccumulator != null) {
259-
optChangeAccumulator.trackChildChange(Change.childRemovedChange(windowBoundary.name, windowBoundary.node));
260-
optChangeAccumulator.trackChildChange(Change.childAddedChange(childKey, childSnap));
255+
if (changeAccumulator != null) {
256+
changeAccumulator.trackChildChange(Change.childRemovedChange(windowBoundary.name, windowBoundary.node));
257+
changeAccumulator.trackChildChange(Change.childAddedChange(childKey, childSnap));
261258
}
262259
return oldEventCache.updateImmediateChild(childKey, childSnap).updateImmediateChild(windowBoundary.name,
263260
ChildrenNode.EMPTY_NODE);

src/utils/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const validateArgCount = function(fnName, minCount, maxCount, argCount) {
3030
* @param {boolean} optional Whether or not the argument is optional
3131
* @return {!string} The prefix to add to the error thrown for validation.
3232
*/
33-
export const errorPrefix = function(fnName, argumentNumber, optional) {
33+
export function errorPrefix(fnName, argumentNumber, optional) {
3434
var argName = '';
3535
switch (argumentNumber) {
3636
case 1:

0 commit comments

Comments
 (0)