Skip to content

Commit 21e14c0

Browse files
committed
TS changes before splitting database
1 parent 38cb158 commit 21e14c0

32 files changed

+93
-85
lines changed

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"declaration": true,
55
"importHelpers": true,
6+
"strictNullChecks": true,
67
"lib": [
78
"es2015",
89
"dom"

packages/app/src/firebaseApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class FirebaseAppImpl implements FirebaseApp {
115115

116116
return Promise.all(
117117
services.map(service => {
118-
return service.INTERNAL.delete();
118+
return service.INTERNAL ? service.INTERNAL.delete() : Promise.resolve();
119119
})
120120
);
121121
})

packages/app/src/lite/firebaseAppLite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class FirebaseAppLiteImpl implements FirebaseApp {
9797

9898
return Promise.all(
9999
services.map(service => {
100-
return service.INTERNAL.delete();
100+
return service.INTERNAL ? service.INTERNAL.delete() : Promise.resolve();
101101
})
102102
);
103103
})

packages/database/index.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function registerDatabase(instance: FirebaseNamespace) {
8181
ServerValue,
8282
TEST_ACCESS
8383
},
84-
null,
84+
undefined,
8585
true
8686
);
8787

packages/database/src/api/Database.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { RepoInfo } from '../core/RepoInfo';
3232
* @implements {FirebaseService}
3333
*/
3434
export class Database implements FirebaseService {
35-
INTERNAL: DatabaseInternals;
35+
INTERNAL?: DatabaseInternals;
3636
private root_: Reference;
3737

3838
static readonly ServerValue = {
@@ -141,7 +141,7 @@ export class Database implements FirebaseService {
141141

142142
export class DatabaseInternals {
143143
/** @param {!Database} database */
144-
constructor(public database: Database) {}
144+
constructor(public database?: Database) {}
145145

146146
/** @return {Promise<void>} */
147147
async delete(): Promise<void> {
@@ -150,7 +150,9 @@ export class DatabaseInternals {
150150

151151
(this.database as any).repo_ = null;
152152
(this.database as any).root_ = null;
153-
this.database.INTERNAL = null;
154-
this.database = null;
153+
if (this.database) {
154+
this.database.INTERNAL = undefined;
155+
}
156+
this.database = undefined;
155157
}
156158
}

packages/database/src/api/Query.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class Query {
254254
const container = new ChildEventRegistration(
255255
callbacks,
256256
cancelCallback,
257-
context
257+
context || undefined
258258
);
259259
this.repo.addEventCallbackForQuery(this, container);
260260
}
@@ -270,7 +270,7 @@ export class Query {
270270
context?: Object | null
271271
): void {
272272
validateArgCount('Query.off', 0, 3, arguments.length);
273-
validateEventType('Query.off', 1, eventType, true);
273+
validateEventType('Query.off', 1, eventType || '', true);
274274
validateCallback('Query.off', 2, callback, true);
275275
validateContextObject('Query.off', 3, context, true);
276276

@@ -288,7 +288,7 @@ export class Query {
288288
callbacks = {};
289289
callbacks[eventType] = callback;
290290
}
291-
container = new ChildEventRegistration(callbacks, null, context || null);
291+
container = new ChildEventRegistration(callbacks, null, context || undefined);
292292
}
293293
this.repo.removeEventCallbackForQuery(this, container);
294294
}
@@ -498,7 +498,7 @@ export class Query {
498498
): Query {
499499
validateArgCount('Query.startAt', 0, 2, arguments.length);
500500
validateFirebaseDataArg('Query.startAt', 1, value, this.path, true);
501-
validateKey('Query.startAt', 2, name, true);
501+
validateKey('Query.startAt', 2, name || undefined, true);
502502

503503
const newParams = this.queryParams_.startAt(value, name);
504504
Query.validateLimit_(newParams);
@@ -529,7 +529,7 @@ export class Query {
529529
): Query {
530530
validateArgCount('Query.endAt', 0, 2, arguments.length);
531531
validateFirebaseDataArg('Query.endAt', 1, value, this.path, true);
532-
validateKey('Query.endAt', 2, name, true);
532+
validateKey('Query.endAt', 2, name || undefined, true);
533533

534534
const newParams = this.queryParams_.endAt(value, name);
535535
Query.validateLimit_(newParams);

packages/database/src/core/Repo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class Repo {
132132
}
133133

134134
authTokenProvider.addTokenChangeListener(token => {
135-
this.server_.refreshAuthToken(token);
135+
token && this.server_.refreshAuthToken(token);
136136
});
137137

138138
// In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
@@ -240,7 +240,7 @@ export class Repo {
240240
data = this.interceptServerDataCallback_
241241
? this.interceptServerDataCallback_(pathString, data)
242242
: data;
243-
let events = [];
243+
let events: Array<Event> = [];
244244
if (tag) {
245245
if (isMerge) {
246246
const taggedChildren = map(data as { [k: string]: any }, (raw: any) =>
@@ -602,7 +602,7 @@ export class Repo {
602602
*/
603603
removeEventCallbackForQuery(
604604
query: Query,
605-
eventRegistration: EventRegistration
605+
eventRegistration: EventRegistration | null
606606
) {
607607
// These are guaranteed not to raise events, since we're not passing in a cancelError. However, we can future-proof
608608
// a little bit by handling the return values anyways.

packages/database/src/core/RepoManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class RepoManager {
8080
* @return {!Database}
8181
*/
8282
databaseFromApp(app: FirebaseApp, url?: string): Database {
83-
const dbUrl: string = url || app.options[DATABASE_URL_OPTION];
83+
const dbUrl: string | undefined = url || app.options[DATABASE_URL_OPTION];
8484
if (dbUrl === undefined) {
8585
fatal(
8686
"Can't determine Firebase Database URL. Be sure to include " +
@@ -89,7 +89,7 @@ export class RepoManager {
8989
);
9090
}
9191

92-
const parsedUrl = parseRepoInfo(dbUrl);
92+
const parsedUrl = parseRepoInfo(dbUrl || '');
9393
const repoInfo = parsedUrl.repoInfo;
9494

9595
validateUrl('Invalid Firebase Database URL', 1, parsedUrl);

packages/database/src/core/SparseSnapshotTree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class SparseSnapshotTree {
4848
if (this.value_ != null) {
4949
return this.value_.getChild(path);
5050
} else if (!path.isEmpty() && this.children_ != null) {
51-
const childKey = path.getFront();
51+
const childKey = path.getFront() || '';
5252
path = path.popFront();
5353
if (this.children_.contains(childKey)) {
5454
const childTree = this.children_.get(childKey) as SparseSnapshotTree;
@@ -79,7 +79,7 @@ export class SparseSnapshotTree {
7979
this.children_ = new CountedSet<string, SparseSnapshotTree>();
8080
}
8181

82-
const childKey = path.getFront();
82+
const childKey = path.getFront() || '';
8383
if (!this.children_.contains(childKey)) {
8484
this.children_.add(childKey, new SparseSnapshotTree());
8585
}
@@ -118,7 +118,7 @@ export class SparseSnapshotTree {
118118
return this.forget(path);
119119
}
120120
} else if (this.children_ !== null) {
121-
const childKey = path.getFront();
121+
const childKey = path.getFront() || '';
122122
path = path.popFront();
123123
if (this.children_.contains(childKey)) {
124124
const safeToRemove = (this.children_.get(

packages/database/src/core/SyncPoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class SyncPoint {
149149
false
150150
),
151151
new CacheNode(
152-
/** @type {!Node} */ (serverCache),
152+
/** @type {!Node} */ (serverCache as Node),
153153
serverCacheComplete,
154154
false
155155
)

packages/database/src/core/SyncTree.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class SyncTree {
163163
ackUserWrite(writeId: number, revert: boolean = false) {
164164
const write = this.pendingWriteTree_.getWrite(writeId);
165165
const needToReevaluate = this.pendingWriteTree_.removeWrite(writeId);
166-
if (!needToReevaluate) {
166+
if (!needToReevaluate || !write) {
167167
return [];
168168
} else {
169169
let affectedTree = ImmutableTree.Empty;
@@ -350,7 +350,7 @@ export class SyncTree {
350350
const subtree = this.syncPointTree_.subtree(path);
351351
subtree.foreachChild(function(childName, childSyncPoint) {
352352
const completeCache = childSyncPoint.getCompleteServerCache(Path.Empty);
353-
if (completeCache) {
353+
if (completeCache && serverCache) {
354354
serverCache = serverCache.updateImmediateChild(
355355
childName,
356356
completeCache
@@ -381,7 +381,7 @@ export class SyncTree {
381381
serverCacheComplete
382382
);
383383
if (!viewAlreadyExists && !foundAncestorDefaultView) {
384-
const view /** @type !View */ = syncPoint.viewForQuery(query);
384+
const view /** @type !View */ = syncPoint.viewForQuery(query) as View;
385385
events = events.concat(this.setupListener_(query, view));
386386
}
387387
return events;
@@ -530,7 +530,7 @@ export class SyncTree {
530530
});
531531
return writeTree.calcCompleteEventCache(
532532
path,
533-
serverCache,
533+
serverCache || null,
534534
writeIdsToExclude,
535535
includeHiddenSets
536536
);
@@ -551,7 +551,7 @@ export class SyncTree {
551551
(relativePath, maybeChildSyncPoint, childMap) => {
552552
if (maybeChildSyncPoint && maybeChildSyncPoint.hasCompleteView()) {
553553
const completeView = maybeChildSyncPoint.getCompleteView();
554-
return [completeView];
554+
return [completeView as View];
555555
} else {
556556
// No complete view here, flatten any deeper listens into an array
557557
let views: View[] = [];
@@ -628,10 +628,12 @@ export class SyncTree {
628628
// The root of this subtree has our query. We're here because we definitely need to send a listen for that, but we
629629
// may need to shadow other listens as well.
630630
if (tag) {
631-
assert(
632-
!subtree.value.hasCompleteView(),
633-
"If we're adding a query, it shouldn't be shadowed"
634-
);
631+
if (subtree.value) {
632+
assert(
633+
!subtree.value.hasCompleteView(),
634+
"If we're adding a query, it shouldn't be shadowed"
635+
);
636+
}
635637
} else {
636638
// Shadow everything at or below this location, this is a default listener.
637639
const queriesToStop = subtree.fold<Query[]>(function(
@@ -644,7 +646,7 @@ export class SyncTree {
644646
maybeChildSyncPoint &&
645647
maybeChildSyncPoint.hasCompleteView()
646648
) {
647-
return [maybeChildSyncPoint.getCompleteView().getQuery()];
649+
return [(maybeChildSyncPoint.getCompleteView() as View).getQuery()];
648650
} else {
649651
// No default listener here, flatten any deeper queries into an array
650652
let queries: Query[] = [];
@@ -787,7 +789,7 @@ export class SyncTree {
787789
queryPath: Path,
788790
operation: Operation
789791
): Event[] {
790-
const syncPoint = this.syncPointTree_.get(queryPath);
792+
const syncPoint = this.syncPointTree_.get(queryPath) as SyncPoint;
791793
assert(syncPoint, "Missing sync point for query tag that we're tracking");
792794
const writesCache = this.pendingWriteTree_.childWrites(queryPath);
793795
return syncPoint.applyOperation(
@@ -855,7 +857,7 @@ export class SyncTree {
855857
}
856858

857859
let events: Event[] = [];
858-
const childName = operation.path.getFront();
860+
const childName = operation.path.getFront() || '';
859861
const childOperation = operation.operationForChild(childName);
860862
const childTree = syncPointTree.children.get(childName);
861863
if (childTree && childOperation) {

packages/database/src/core/WriteTree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export class WriteTree {
278278
(!writeIdsToExclude ||
279279
!~writeIdsToExclude.indexOf(write.writeId)) &&
280280
(write.path.contains(treePath) || treePath.contains(write.path))
281-
);
281+
) || false;
282282
};
283283
const mergeAtPath = WriteTree.layerTree_(
284284
this.allWrites_,
@@ -398,15 +398,15 @@ export class WriteTree {
398398
const childMerge = this.visibleWrites_.childCompoundWrite(path);
399399
if (childMerge.isEmpty()) {
400400
// We're not shadowing at all. Case 1
401-
return existingServerSnap.getChild(childPath);
401+
return (existingServerSnap as Node).getChild(childPath);
402402
} else {
403403
// This could be more efficient if the serverNode + updates doesn't change the eventSnap
404404
// However this is tricky to find out, since user updates don't necessary change the server
405405
// snap, e.g. priority updates on empty nodes, or deep deletes. Another special case is if the server
406406
// adds nodes, but doesn't change any existing writes. It is therefore not enough to
407407
// only check if the updates change the serverNode.
408408
// Maybe check if the merge tree contains these special cases and only do a full overwrite in that case?
409-
return childMerge.apply(existingServerSnap.getChild(childPath));
409+
return childMerge.apply((existingServerSnap as Node).getChild(childPath));
410410
}
411411
}
412412
}
@@ -486,7 +486,7 @@ export class WriteTree {
486486
}
487487
toIterate = toIterate.withIndex(index);
488488
if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
489-
const nodes = [];
489+
const nodes: Array<NamedNode> = [];
490490
const cmp = index.getCompare();
491491
const iter = reverse
492492
? (toIterate as ChildrenNode).getReverseIteratorFrom(startPost, index)

packages/database/src/core/util/Path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class Path {
131131
parent(): Path | null {
132132
if (this.pieceNum_ >= this.pieces_.length) return null;
133133

134-
const pieces = [];
134+
const pieces: Array<string> = [];
135135
for (let i = this.pieceNum_; i < this.pieces_.length - 1; i++)
136136
pieces.push(this.pieces_[i]);
137137

@@ -143,7 +143,7 @@ export class Path {
143143
* @return {!Path}
144144
*/
145145
child(childPathObj: string | Path): Path {
146-
const pieces = [];
146+
const pieces: Array<string> = [];
147147
for (let i = this.pieceNum_; i < this.pieces_.length; i++)
148148
pieces.push(this.pieces_[i]);
149149

packages/database/src/core/util/ServerValues.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export const resolveDeferredValueSnapshot = function(
9494
const rawPri = node.getPriority().val() as
9595
| object
9696
| boolean
97-
| null
9897
| number
9998
| string;
10099
const priority = resolveDeferredValue(rawPri, serverValues);

0 commit comments

Comments
 (0)