Skip to content

Commit 3956203

Browse files
Remove static member variables (#4433)
1 parent d987763 commit 3956203

File tree

11 files changed

+114
-119
lines changed

11 files changed

+114
-119
lines changed

packages/database/src/api/Reference.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import { Repo } from '../core/Repo';
2424
import { Path } from '../core/util/Path';
2525
import { QueryParams } from '../core/view/QueryParams';
2626
import {
27-
validateRootPathString,
28-
validatePathString,
29-
validateFirebaseMergeDataArg,
3027
validateBoolean,
31-
validatePriority,
3228
validateFirebaseDataArg,
29+
validateFirebaseMergeDataArg,
30+
validatePathString,
31+
validatePriority,
32+
validateRootPathString,
3333
validateWritablePath
3434
} from '../core/util/validation';
35-
import { validateArgCount, validateCallback, Deferred } from '@firebase/util';
35+
import { Deferred, validateArgCount, validateCallback } from '@firebase/util';
3636

3737
import { SyncPoint } from '../core/SyncPoint';
3838
import { Database } from './Database';
@@ -61,7 +61,7 @@ export class Reference extends Query {
6161
}
6262

6363
// call Query's constructor, passing in the repo and path.
64-
super(repo, path, QueryParams.DEFAULT, false);
64+
super(repo, path, new QueryParams(), false);
6565
}
6666

6767
/** @return {?string} */

packages/database/src/core/CompoundWrite.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { ImmutableTree } from './util/ImmutableTree';
1919
import { Path } from './util/Path';
20-
import { Node, NamedNode } from './snap/Node';
20+
import { NamedNode, Node } from './snap/Node';
2121
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
2222
import { assert } from '@firebase/util';
2323
import { ChildrenNode } from './snap/ChildrenNode';
@@ -32,7 +32,9 @@ import { each } from './util/util';
3232
export class CompoundWrite {
3333
constructor(private writeTree_: ImmutableTree<Node>) {}
3434

35-
static Empty = new CompoundWrite(new ImmutableTree(null));
35+
static empty(): CompoundWrite {
36+
return new CompoundWrite(new ImmutableTree(null));
37+
}
3638

3739
addWrite(path: Path, node: Node): CompoundWrite {
3840
if (path.isEmpty()) {
@@ -70,9 +72,12 @@ export class CompoundWrite {
7072
*/
7173
removeWrite(path: Path): CompoundWrite {
7274
if (path.isEmpty()) {
73-
return CompoundWrite.Empty;
75+
return CompoundWrite.empty();
7476
} else {
75-
const newWriteTree = this.writeTree_.setTree(path, ImmutableTree.Empty);
77+
const newWriteTree = this.writeTree_.setTree(
78+
path,
79+
new ImmutableTree<Node>(null)
80+
);
7681
return new CompoundWrite(newWriteTree);
7782
}
7883
}

packages/database/src/core/SyncTree.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { assert } from '@firebase/util';
19-
import { errorForServerCode, each } from './util/util';
19+
import { each, errorForServerCode } from './util/util';
2020
import { AckUserWrite } from './operation/AckUserWrite';
2121
import { ChildrenNode } from './snap/ChildrenNode';
2222
import { ImmutableTree } from './util/ImmutableTree';
@@ -81,7 +81,9 @@ export class SyncTree {
8181
/**
8282
* Tree of SyncPoints. There's a SyncPoint at any location that has 1 or more views.
8383
*/
84-
private syncPointTree_: ImmutableTree<SyncPoint> = ImmutableTree.Empty;
84+
private syncPointTree_: ImmutableTree<SyncPoint> = new ImmutableTree<SyncPoint>(
85+
null
86+
);
8587

8688
/**
8789
* A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
@@ -115,7 +117,7 @@ export class SyncTree {
115117
return [];
116118
} else {
117119
return this.applyOperationToSyncPoints_(
118-
new Overwrite(OperationSource.User, path, newData)
120+
new Overwrite(OperationSource.user(), path, newData)
119121
);
120122
}
121123
}
@@ -136,7 +138,7 @@ export class SyncTree {
136138
const changeTree = ImmutableTree.fromObject(changedChildren);
137139

138140
return this.applyOperationToSyncPoints_(
139-
new Merge(OperationSource.User, path, changeTree)
141+
new Merge(OperationSource.user(), path, changeTree)
140142
);
141143
}
142144

@@ -152,13 +154,13 @@ export class SyncTree {
152154
if (!needToReevaluate) {
153155
return [];
154156
} else {
155-
let affectedTree = ImmutableTree.Empty;
157+
let affectedTree = new ImmutableTree<boolean>(null);
156158
if (write.snap != null) {
157159
// overwrite
158160
affectedTree = affectedTree.set(Path.Empty, true);
159161
} else {
160-
each(write.children, (pathString: string, node: Node) => {
161-
affectedTree = affectedTree.set(new Path(pathString), node);
162+
each(write.children, (pathString: string) => {
163+
affectedTree = affectedTree.set(new Path(pathString), true);
162164
});
163165
}
164166
return this.applyOperationToSyncPoints_(
@@ -174,7 +176,7 @@ export class SyncTree {
174176
*/
175177
applyServerOverwrite(path: Path, newData: Node): Event[] {
176178
return this.applyOperationToSyncPoints_(
177-
new Overwrite(OperationSource.Server, path, newData)
179+
new Overwrite(OperationSource.server(), path, newData)
178180
);
179181
}
180182

@@ -190,7 +192,7 @@ export class SyncTree {
190192
const changeTree = ImmutableTree.fromObject(changedChildren);
191193

192194
return this.applyOperationToSyncPoints_(
193-
new Merge(OperationSource.Server, path, changeTree)
195+
new Merge(OperationSource.server(), path, changeTree)
194196
);
195197
}
196198

@@ -201,7 +203,7 @@ export class SyncTree {
201203
*/
202204
applyListenComplete(path: Path): Event[] {
203205
return this.applyOperationToSyncPoints_(
204-
new ListenComplete(OperationSource.Server, path)
206+
new ListenComplete(OperationSource.server(), path)
205207
);
206208
}
207209

packages/database/src/core/WriteTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class WriteTree {
4848
* A tree tracking the result of applying all visible writes. This does not include transactions with
4949
* applyLocally=false or writes that are completely shadowed by other writes.
5050
*/
51-
private visibleWrites_: CompoundWrite = CompoundWrite.Empty;
51+
private visibleWrites_: CompoundWrite = CompoundWrite.empty();
5252

5353
/**
5454
* A list of all pending writes, regardless of visibility and shadowed-ness. Used to calculate arbitrary
@@ -497,7 +497,7 @@ export class WriteTree {
497497
filter: (w: WriteRecord) => boolean,
498498
treeRoot: Path
499499
): CompoundWrite {
500-
let compoundWrite = CompoundWrite.Empty;
500+
let compoundWrite = CompoundWrite.empty();
501501
for (let i = 0; i < writes.length; ++i) {
502502
const write = writes[i];
503503
// Theory, a later set will either:

packages/database/src/core/operation/AckUserWrite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class AckUserWrite implements Operation {
2525
type = OperationType.ACK_USER_WRITE;
2626

2727
/** @inheritDoc */
28-
source = OperationSource.User;
28+
source = OperationSource.user();
2929

3030
/**
3131
* @param affectedTree A tree containing true for each affected path. Affected paths can't overlap.

packages/database/src/core/operation/Operation.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,23 @@ export class OperationSource {
5252
assert(!tagged || fromServer, 'Tagged queries must be from server.');
5353
}
5454

55-
static User = new OperationSource(
56-
/*fromUser=*/ true,
57-
false,
58-
null,
59-
/*tagged=*/ false
60-
);
55+
static user() {
56+
return new OperationSource(
57+
/*fromUser=*/ true,
58+
false,
59+
null,
60+
/*tagged=*/ false
61+
);
62+
}
6163

62-
static Server = new OperationSource(
63-
false,
64-
/*fromServer=*/ true,
65-
null,
66-
/*tagged=*/ false
67-
);
64+
static server() {
65+
return new OperationSource(
66+
false,
67+
/*fromServer=*/ true,
68+
null,
69+
/*tagged=*/ false
70+
);
71+
}
6872

6973
static forServerTaggedQuery = function (queryId: string): OperationSource {
7074
return new OperationSource(

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { SortedMap } from './SortedMap';
1919
import { Path } from './Path';
20-
import { stringCompare, each } from './util';
20+
import { each, stringCompare } from './util';
2121

2222
let emptyChildrenSingleton: SortedMap<string, ImmutableTree<null>>;
2323

@@ -38,11 +38,8 @@ const EmptyChildren = (): SortedMap<string, ImmutableTree<null>> => {
3838
* A tree with immutable elements.
3939
*/
4040
export class ImmutableTree<T> {
41-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
42-
static Empty = new ImmutableTree<any>(null);
43-
4441
static fromObject<T>(obj: { [k: string]: T }): ImmutableTree<T> {
45-
let tree: ImmutableTree<T> = ImmutableTree.Empty;
42+
let tree: ImmutableTree<T> = new ImmutableTree<T>(null);
4643
each(obj, (childPath: string, childSnap: T) => {
4744
tree = tree.set(new Path(childPath), childSnap);
4845
});
@@ -128,7 +125,7 @@ export class ImmutableTree<T> {
128125
if (childTree !== null) {
129126
return childTree.subtree(relativePath.popFront());
130127
} else {
131-
return ImmutableTree.Empty;
128+
return new ImmutableTree<T>(null);
132129
}
133130
}
134131
}
@@ -145,7 +142,7 @@ export class ImmutableTree<T> {
145142
return new ImmutableTree(toSet, this.children);
146143
} else {
147144
const front = relativePath.getFront();
148-
const child = this.children.get(front) || ImmutableTree.Empty;
145+
const child = this.children.get(front) || new ImmutableTree<T>(null);
149146
const newChild = child.set(relativePath.popFront(), toSet);
150147
const newChildren = this.children.insert(front, newChild);
151148
return new ImmutableTree(this.value, newChildren);
@@ -161,7 +158,7 @@ export class ImmutableTree<T> {
161158
remove(relativePath: Path): ImmutableTree<T> {
162159
if (relativePath.isEmpty()) {
163160
if (this.children.isEmpty()) {
164-
return ImmutableTree.Empty;
161+
return new ImmutableTree<T>(null);
165162
} else {
166163
return new ImmutableTree(null, this.children);
167164
}
@@ -177,7 +174,7 @@ export class ImmutableTree<T> {
177174
newChildren = this.children.insert(front, newChild);
178175
}
179176
if (this.value === null && newChildren.isEmpty()) {
180-
return ImmutableTree.Empty;
177+
return new ImmutableTree<T>(null);
181178
} else {
182179
return new ImmutableTree(this.value, newChildren);
183180
}
@@ -219,7 +216,7 @@ export class ImmutableTree<T> {
219216
return newTree;
220217
} else {
221218
const front = relativePath.getFront();
222-
const child = this.children.get(front) || ImmutableTree.Empty;
219+
const child = this.children.get(front) || new ImmutableTree<T>(null);
223220
const newChild = child.setTree(relativePath.popFront(), newTree);
224221
let newChildren;
225222
if (newChild.isEmpty()) {
@@ -317,7 +314,7 @@ export class ImmutableTree<T> {
317314
f
318315
);
319316
} else {
320-
return ImmutableTree.Empty;
317+
return new ImmutableTree<T>(null);
321318
}
322319
}
323320
}

packages/database/src/core/view/QueryParams.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ export class QueryParams {
8080
LIMIT_TO_LAST: 'limitToLast'
8181
};
8282

83-
/**
84-
* Default, empty query parameters
85-
*/
86-
static readonly DEFAULT = new QueryParams();
87-
8883
hasStart(): boolean {
8984
return this.startSet_;
9085
}

packages/database/src/core/view/ViewCache.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { ChildrenNode } from '../snap/ChildrenNode';
1918
import { CacheNode } from './CacheNode';
2019
import { Node } from '../snap/Node';
2120

@@ -30,19 +29,6 @@ export class ViewCache {
3029
private readonly serverCache_: CacheNode
3130
) {}
3231

33-
static Empty = new ViewCache(
34-
new CacheNode(
35-
ChildrenNode.EMPTY_NODE,
36-
/*fullyInitialized=*/ false,
37-
/*filtered=*/ false
38-
),
39-
new CacheNode(
40-
ChildrenNode.EMPTY_NODE,
41-
/*fullyInitialized=*/ false,
42-
/*filtered=*/ false
43-
)
44-
);
45-
4632
updateEventSnap(
4733
eventSnap: Node,
4834
complete: boolean,

packages/database/src/core/view/ViewProcessor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ export class ViewProcessor {
551551
if (path.isEmpty()) {
552552
viewMergeTree = changedChildren;
553553
} else {
554-
viewMergeTree = ImmutableTree.Empty.setTree(path, changedChildren);
554+
viewMergeTree = new ImmutableTree<Node>(null).setTree(
555+
path,
556+
changedChildren
557+
);
555558
}
556559
const serverNode = viewCache.getServerCache().getNode();
557560
viewMergeTree.children.inorderTraversal((childKey, childTree) => {
@@ -633,7 +636,7 @@ export class ViewProcessor {
633636
} else if (ackPath.isEmpty()) {
634637
// This is a goofy edge case where we are acking data at this location but don't have full data. We
635638
// should just re-apply whatever we have in our cache as a merge.
636-
let changedChildren = ImmutableTree.Empty;
639+
let changedChildren = new ImmutableTree<Node>(null);
637640
serverCache.getNode().forEachChild(KEY_INDEX, (name, node) => {
638641
changedChildren = changedChildren.set(new Path(name), node);
639642
});
@@ -651,7 +654,7 @@ export class ViewProcessor {
651654
}
652655
} else {
653656
// This is a merge.
654-
let changedChildren = ImmutableTree.Empty;
657+
let changedChildren = new ImmutableTree<Node>(null);
655658
affectedTree.foreach((mergePath, value) => {
656659
const serverCachePath = ackPath.child(mergePath);
657660
if (serverCache.isCompleteForPath(serverCachePath)) {

0 commit comments

Comments
 (0)