Skip to content

Commit 5e7c013

Browse files
Tree-Shake StatsManager (#4536)
1 parent 3862071 commit 5e7c013

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

packages/database/src/core/Repo.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ import {
5555
} from './util/util';
5656

5757
import { AuthTokenProvider } from './AuthTokenProvider';
58-
import { StatsManager } from './stats/StatsManager';
58+
import {
59+
statsManagerGetCollection,
60+
statsManagerGetOrCreateReporter
61+
} from './stats/StatsManager';
5962
import { StatsReporter } from './stats/StatsReporter';
6063
import { StatsListener } from './stats/StatsListener';
6164
import {
@@ -181,7 +184,7 @@ export class Repo {
181184
}
182185

183186
export function repoStart(repo: Repo): void {
184-
repo.stats_ = StatsManager.getCollection(repo.repoInfo_);
187+
repo.stats_ = statsManagerGetCollection(repo.repoInfo_);
185188

186189
if (repo.forceRestClient_ || beingCrawled()) {
187190
repo.server_ = new ReadonlyRestClient(
@@ -245,7 +248,7 @@ export function repoStart(repo: Repo): void {
245248

246249
// In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
247250
// we only want to create one StatsReporter. As such, we'll report stats over the first Repo created.
248-
repo.statsReporter_ = StatsManager.getOrCreateReporter(
251+
repo.statsReporter_ = statsManagerGetOrCreateReporter(
249252
repo.repoInfo_,
250253
() => new StatsReporter(repo.stats_, repo.server_)
251254
);

packages/database/src/core/stats/StatsManager.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,28 @@
1818
import { StatsCollection } from './StatsCollection';
1919
import { RepoInfo } from '../RepoInfo';
2020

21-
export class StatsManager {
22-
private static collections_: { [k: string]: StatsCollection } = {};
23-
private static reporters_: { [k: string]: unknown } = {};
21+
const collections: { [k: string]: StatsCollection } = {};
22+
const reporters: { [k: string]: unknown } = {};
2423

25-
static getCollection(repoInfo: RepoInfo): StatsCollection {
26-
const hashString = repoInfo.toString();
24+
export function statsManagerGetCollection(repoInfo: RepoInfo): StatsCollection {
25+
const hashString = repoInfo.toString();
2726

28-
if (!this.collections_[hashString]) {
29-
this.collections_[hashString] = new StatsCollection();
30-
}
31-
32-
return this.collections_[hashString];
27+
if (!collections[hashString]) {
28+
collections[hashString] = new StatsCollection();
3329
}
3430

35-
static getOrCreateReporter<T>(
36-
repoInfo: RepoInfo,
37-
creatorFunction: () => T
38-
): T {
39-
const hashString = repoInfo.toString();
31+
return collections[hashString];
32+
}
4033

41-
if (!this.reporters_[hashString]) {
42-
this.reporters_[hashString] = creatorFunction();
43-
}
34+
export function statsManagerGetOrCreateReporter<T>(
35+
repoInfo: RepoInfo,
36+
creatorFunction: () => T
37+
): T {
38+
const hashString = repoInfo.toString();
4439

45-
return this.reporters_[hashString] as T;
40+
if (!reporters[hashString]) {
41+
reporters[hashString] = creatorFunction();
4642
}
43+
44+
return reporters[hashString] as T;
4745
}

packages/database/src/realtime/BrowserPollConnection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
LUIDGenerator,
2525
splitStringBySize
2626
} from '../core/util/util';
27-
import { StatsManager } from '../core/stats/StatsManager';
27+
import { statsManagerGetCollection } from '../core/stats/StatsManager';
2828
import { PacketReceiver } from './polling/PacketReceiver';
2929
import {
3030
APPLICATION_ID_PARAM,
@@ -37,7 +37,7 @@ import {
3737
TRANSPORT_SESSION_PARAM,
3838
VERSION_PARAM
3939
} from './Constants';
40-
import { base64Encode, stringify, isNodeSdk } from '@firebase/util';
40+
import { base64Encode, isNodeSdk, stringify } from '@firebase/util';
4141

4242
import { Transport } from './Transport';
4343
import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
@@ -114,7 +114,7 @@ export class BrowserPollConnection implements Transport {
114114
public lastSessionId?: string
115115
) {
116116
this.log_ = logWrapper(connId);
117-
this.stats_ = StatsManager.getCollection(repoInfo);
117+
this.stats_ = statsManagerGetCollection(repoInfo);
118118
this.urlFn = (params: { [k: string]: string }) =>
119119
repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
120120
}

packages/database/src/realtime/WebSocketConnection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717

1818
import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
19-
import { assert, jsonEval, stringify, isNodeSdk } from '@firebase/util';
19+
import { assert, isNodeSdk, jsonEval, stringify } from '@firebase/util';
2020
import { logWrapper, splitStringBySize } from '../core/util/util';
21-
import { StatsManager } from '../core/stats/StatsManager';
21+
import { statsManagerGetCollection } from '../core/stats/StatsManager';
2222
import {
2323
FORGE_DOMAIN_RE,
2424
FORGE_REF,
@@ -86,7 +86,7 @@ export class WebSocketConnection implements Transport {
8686
lastSessionId?: string
8787
) {
8888
this.log_ = logWrapper(this.connId);
89-
this.stats_ = StatsManager.getCollection(repoInfo);
89+
this.stats_ = statsManagerGetCollection(repoInfo);
9090
this.connURL = WebSocketConnection.connectionURL_(
9191
repoInfo,
9292
transportSessionId,

0 commit comments

Comments
 (0)