Skip to content

Tree-Shake StatsManager #4536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ import {
} from './util/util';

import { AuthTokenProvider } from './AuthTokenProvider';
import { StatsManager } from './stats/StatsManager';
import {
statsManagerGetCollection,
statsManagerGetOrCreateReporter
} from './stats/StatsManager';
import { StatsReporter } from './stats/StatsReporter';
import { StatsListener } from './stats/StatsListener';
import {
Expand Down Expand Up @@ -181,7 +184,7 @@ export class Repo {
}

export function repoStart(repo: Repo): void {
repo.stats_ = StatsManager.getCollection(repo.repoInfo_);
repo.stats_ = statsManagerGetCollection(repo.repoInfo_);

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

// In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
// we only want to create one StatsReporter. As such, we'll report stats over the first Repo created.
repo.statsReporter_ = StatsManager.getOrCreateReporter(
repo.statsReporter_ = statsManagerGetOrCreateReporter(
repo.repoInfo_,
() => new StatsReporter(repo.stats_, repo.server_)
);
Expand Down
36 changes: 17 additions & 19 deletions packages/database/src/core/stats/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,28 @@
import { StatsCollection } from './StatsCollection';
import { RepoInfo } from '../RepoInfo';

export class StatsManager {
private static collections_: { [k: string]: StatsCollection } = {};
private static reporters_: { [k: string]: unknown } = {};
const collections: { [k: string]: StatsCollection } = {};
const reporters: { [k: string]: unknown } = {};

static getCollection(repoInfo: RepoInfo): StatsCollection {
const hashString = repoInfo.toString();
export function statsManagerGetCollection(repoInfo: RepoInfo): StatsCollection {
const hashString = repoInfo.toString();

if (!this.collections_[hashString]) {
this.collections_[hashString] = new StatsCollection();
}

return this.collections_[hashString];
if (!collections[hashString]) {
collections[hashString] = new StatsCollection();
}

static getOrCreateReporter<T>(
repoInfo: RepoInfo,
creatorFunction: () => T
): T {
const hashString = repoInfo.toString();
return collections[hashString];
}

if (!this.reporters_[hashString]) {
this.reporters_[hashString] = creatorFunction();
}
export function statsManagerGetOrCreateReporter<T>(
repoInfo: RepoInfo,
creatorFunction: () => T
): T {
const hashString = repoInfo.toString();

return this.reporters_[hashString] as T;
if (!reporters[hashString]) {
reporters[hashString] = creatorFunction();
}

return reporters[hashString] as T;
}
6 changes: 3 additions & 3 deletions packages/database/src/realtime/BrowserPollConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
LUIDGenerator,
splitStringBySize
} from '../core/util/util';
import { StatsManager } from '../core/stats/StatsManager';
import { statsManagerGetCollection } from '../core/stats/StatsManager';
import { PacketReceiver } from './polling/PacketReceiver';
import {
APPLICATION_ID_PARAM,
Expand All @@ -37,7 +37,7 @@ import {
TRANSPORT_SESSION_PARAM,
VERSION_PARAM
} from './Constants';
import { base64Encode, stringify, isNodeSdk } from '@firebase/util';
import { base64Encode, isNodeSdk, stringify } from '@firebase/util';

import { Transport } from './Transport';
import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
Expand Down Expand Up @@ -114,7 +114,7 @@ export class BrowserPollConnection implements Transport {
public lastSessionId?: string
) {
this.log_ = logWrapper(connId);
this.stats_ = StatsManager.getCollection(repoInfo);
this.stats_ = statsManagerGetCollection(repoInfo);
this.urlFn = (params: { [k: string]: string }) =>
repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/database/src/realtime/WebSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/

import { RepoInfo, repoInfoConnectionURL } from '../core/RepoInfo';
import { assert, jsonEval, stringify, isNodeSdk } from '@firebase/util';
import { assert, isNodeSdk, jsonEval, stringify } from '@firebase/util';
import { logWrapper, splitStringBySize } from '../core/util/util';
import { StatsManager } from '../core/stats/StatsManager';
import { statsManagerGetCollection } from '../core/stats/StatsManager';
import {
FORGE_DOMAIN_RE,
FORGE_REF,
Expand Down Expand Up @@ -86,7 +86,7 @@ export class WebSocketConnection implements Transport {
lastSessionId?: string
) {
this.log_ = logWrapper(this.connId);
this.stats_ = StatsManager.getCollection(repoInfo);
this.stats_ = statsManagerGetCollection(repoInfo);
this.connURL = WebSocketConnection.connectionURL_(
repoInfo,
transportSessionId,
Expand Down