Skip to content
Open
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
10 changes: 6 additions & 4 deletions packages/perf/__tests__/perf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
CheckV9DeprecationFunction,
} from '../../app/lib/common/unitTestUtils';

import type { PerformanceModuleWithModularArg } from '../lib/types/internal';

// @ts-ignore test
import FirebaseModule from '../../app/lib/internal/FirebaseModule';

Expand Down Expand Up @@ -197,7 +199,7 @@ describe('Performance Monitoring', function () {
});

it('newTrace()', function () {
const perf = getPerformance();
const perf = getPerformance() as PerformanceModuleWithModularArg;
perfV9Deprecation(
() => trace(perf, 'invertase'),
() => perf.newTrace('invertase'),
Expand All @@ -206,7 +208,7 @@ describe('Performance Monitoring', function () {
});

it('newHttpMetric()', function () {
const perf = getPerformance();
const perf = getPerformance() as PerformanceModuleWithModularArg;
perfV9Deprecation(
() => httpMetric(perf, 'https://invertase.io', 'GET'),
() => perf.newHttpMetric('https://invertase.io', 'GET'),
Expand All @@ -215,7 +217,7 @@ describe('Performance Monitoring', function () {
});

it('newScreenTrace()', function () {
const perf = getPerformance();
const perf = getPerformance() as PerformanceModuleWithModularArg;
perfV9Deprecation(
() => newScreenTrace(perf, 'invertase'),
() => perf.newScreenTrace('invertase'),
Expand All @@ -224,7 +226,7 @@ describe('Performance Monitoring', function () {
});

it('startScreenTrace()', function () {
const perf = getPerformance();
const perf = getPerformance() as PerformanceModuleWithModularArg;
perfV9Deprecation(
() => startScreenTrace(perf, 'invertase'),
() => perf.startScreenTrace('invertase'),
Expand Down
45 changes: 35 additions & 10 deletions packages/perf/lib/HttpMetric.js → packages/perf/lib/HttpMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@
*/

import { isNull, isNumber, isString } from '@react-native-firebase/app/dist/module/common';
import type { FirebasePerformanceTypes } from './types/namespaced';
import MetricWithAttributes from './MetricWithAttributes';
import type { RNFBPerfNativeModule } from './types/internal';

export default class HttpMetric extends MetricWithAttributes {
constructor(native, url, httpMethod) {
private _url: string;
private _httpMethod: FirebasePerformanceTypes.HttpMethod;
private _httpResponseCode: number | null;
private _requestPayloadSize: number | null;
private _responsePayloadSize: number | null;
private _responseContentType: string | null;
private _started: boolean;
private _stopped: boolean;

constructor(
native: RNFBPerfNativeModule,
url: string,
httpMethod: FirebasePerformanceTypes.HttpMethod,
) {
super(native);

this._url = url;
Expand All @@ -34,7 +49,7 @@ export default class HttpMetric extends MetricWithAttributes {
this._stopped = false;
}

setHttpResponseCode(code) {
setHttpResponseCode(code: number | null): void {
if (!isNumber(code) && !isNull(code)) {
throw new Error(
"firebase.perf.HttpMetric.setHttpResponseCode(*) 'code' must be a number or null.",
Expand All @@ -44,7 +59,7 @@ export default class HttpMetric extends MetricWithAttributes {
this._httpResponseCode = code;
}

setRequestPayloadSize(bytes) {
setRequestPayloadSize(bytes: number | null): void {
if (!isNumber(bytes) && !isNull(bytes)) {
throw new Error(
"firebase.perf.HttpMetric.setRequestPayloadSize(*) 'bytes' must be a number or null.",
Expand All @@ -54,7 +69,7 @@ export default class HttpMetric extends MetricWithAttributes {
this._requestPayloadSize = bytes;
}

setResponsePayloadSize(bytes) {
setResponsePayloadSize(bytes: number | null): void {
if (!isNumber(bytes) && !isNull(bytes)) {
throw new Error(
"firebase.perf.HttpMetric.setResponsePayloadSize(*) 'bytes' must be a number or null.",
Expand All @@ -64,7 +79,7 @@ export default class HttpMetric extends MetricWithAttributes {
this._responsePayloadSize = bytes;
}

setResponseContentType(contentType) {
setResponseContentType(contentType: string | null): void {
if (!isString(contentType) && !isNull(contentType)) {
throw new Error(
"firebase.perf.HttpMetric.setResponseContentType(*) 'contentType' must be a string or null.",
Expand All @@ -74,22 +89,32 @@ export default class HttpMetric extends MetricWithAttributes {
this._responseContentType = contentType;
}

start() {
start(): Promise<null> {
if (this._started) {
return Promise.resolve(null);
}
this._started = true;

return this.native.startHttpMetric(this._id, this._url, this._httpMethod);
return (this.native as RNFBPerfNativeModule).startHttpMetric(
this._id,
this._url,
this._httpMethod,
);
}

stop() {
stop(): Promise<null> {
if (this._stopped) {
return Promise.resolve(null);
}
this._stopped = true;

const metricData = {
const metricData: {
attributes: Record<string, string>;
httpResponseCode?: number;
requestPayloadSize?: number;
responsePayloadSize?: number;
responseContentType?: string;
} = {
attributes: Object.assign({}, this._attributes),
};

Expand All @@ -112,6 +137,6 @@ export default class HttpMetric extends MetricWithAttributes {
metricData.responseContentType = this._responseContentType;
}

return this.native.stopHttpMetric(this._id, metricData);
return (this.native as RNFBPerfNativeModule).stopHttpMetric(this._id, metricData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ import { hasOwnProperty, isString } from '@react-native-firebase/app/dist/module
let id = 0;

export default class MetricWithAttributes {
constructor(native) {
readonly native: unknown;
readonly _id: number;
protected _attributes: Record<string, string>;

constructor(native: unknown) {
this._id = id++;
this.native = native;
this._attributes = {};
}

getAttribute(attribute) {
getAttribute(attribute: string): string | null {
if (!isString(attribute)) {
throw new Error("firebase.perf.*.getAttribute(*) 'attribute' must be a string.");
}
return this._attributes[attribute] || null;
}

putAttribute(attribute, value) {
putAttribute(attribute: string, value: string): void {
// TODO(VALIDATION): attribute: no leading or trailing whitespace, no leading underscore '_'
if (!isString(attribute) || attribute.length > 40) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@
*/

import { isIOS } from '@react-native-firebase/app/dist/module/common';
import type { RNFBPerfScreenTraceNativeModule } from './types/internal';

let id = 0;

export default class ScreenTrace {
constructor(native, identifier) {
native: RNFBPerfScreenTraceNativeModule;
private _identifier: string;
private _id: number;
private _started: boolean;
private _stopped: boolean;

constructor(native: RNFBPerfScreenTraceNativeModule, identifier: string) {
this.native = native;
this._identifier = identifier;
this._id = id++;
this._started = false;
this._stopped = false;
}

start() {
start(): Promise<null> {
if (isIOS) {
return Promise.reject(new Error('Custom screentraces are currently not supported on iOS.'));
}
Expand All @@ -40,7 +47,7 @@ export default class ScreenTrace {
return this.native.startScreenTrace(this._id, this._identifier);
}

stop() {
stop(): Promise<null> {
if (!this._started || this._stopped) {
return Promise.resolve(null);
}
Expand Down
33 changes: 19 additions & 14 deletions packages/perf/lib/Trace.js → packages/perf/lib/Trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,38 @@
*
*/

import { hasOwnProperty, isNumber, isString } from '@react-native-firebase/app/dist/module/common';
import { isNumber, isString } from '@react-native-firebase/app/dist/module/common';
import MetricWithAttributes from './MetricWithAttributes';
import type { RNFBPerfTraceNativeModule } from './types/internal';

export default class Trace extends MetricWithAttributes {
constructor(native, identifier) {
private _identifier: string;
private _metrics: Record<string, number>;
private _started: boolean;
private _stopped: boolean;

constructor(native: RNFBPerfTraceNativeModule, identifier: string) {
super(native);
this._identifier = identifier;

this._identifier = identifier;
this._metrics = {};

this._started = false;
this._stopped = false;
}

getMetric(metricName) {
getMetric(metricName: string): number {
if (!isString(metricName)) {
throw new Error("firebase.perf.Trace.getMetric(*) 'metricName' must be a string.");
}

return hasOwnProperty(this._metrics, metricName) ? this._metrics[metricName] : 0;
return this._metrics[metricName] ?? 0;
}

getMetrics() {
getMetrics(): Record<string, number> {
return Object.assign({}, this._metrics);
}

putMetric(metricName, value) {
putMetric(metricName: string, value: number): void {
// TODO(VALIDATION): metricName: no leading or trailing whitespace, no leading underscore '_' character, max length is 32 characters
// TODO(VALIDATION): value: >= 0
if (!isString(metricName)) {
Expand All @@ -55,7 +60,7 @@ export default class Trace extends MetricWithAttributes {
this._metrics[metricName] = value;
}

incrementMetric(metricName, incrementBy) {
incrementMetric(metricName: string, incrementBy: number): void {
// TODO(VALIDATION): metricName: no leading or trailing whitespace, no leading underscore '_' character, max length is 32 characters
// TODO(VALIDATION): value: >= 0
if (!isString(metricName)) {
Expand All @@ -69,24 +74,24 @@ export default class Trace extends MetricWithAttributes {
this._metrics[metricName] = this.getMetric(metricName) + incrementBy;
}

removeMetric(metric) {
removeMetric(metric: string): void {
if (!isString(metric)) {
throw new Error("firebase.perf.Trace.removeMetric(*) 'metric' must be a string.");
}

delete this._metrics[metric];
}

start() {
start(): Promise<null> {
if (this._started) {
return Promise.resolve(null);
}
this._started = true;

return this.native.startTrace(this._id, this._identifier);
return (this.native as RNFBPerfTraceNativeModule).startTrace(this._id, this._identifier);
}

stop() {
stop(): Promise<null> {
if (this._stopped) {
return Promise.resolve(null);
}
Expand All @@ -97,6 +102,6 @@ export default class Trace extends MetricWithAttributes {
attributes: Object.assign({}, this._attributes),
};

return this.native.stopTrace(this._id, traceData);
return (this.native as RNFBPerfTraceNativeModule).stopTrace(this._id, traceData);
}
}
32 changes: 32 additions & 0 deletions packages/perf/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

export type {
FirebaseApp,
FirebasePerformance,
HttpMethod,
HttpMetric,
PerformanceSettings,
PerformanceTrace,
ScreenTrace,
} from './types/modular';

export * from './modular';

export type { FirebasePerformanceTypes } from './types/namespaced';
export * from './namespaced';
export { default } from './namespaced';
Loading
Loading