UNPKG

@nativescript/firebase-performance

Version:

NativeScript Firebase - Performancee

165 lines 4.69 kB
import { deserialize, firebase, FirebaseApp } from '@nativescript/firebase-core'; import { HttpMethod } from './common'; let defaultPerformance; const fb = firebase(); Object.defineProperty(fb, 'performance', { value: () => { if (!defaultPerformance) { defaultPerformance = new Performance(); } return defaultPerformance; }, writable: false, }); function toHttpMethod(method) { switch (method) { case HttpMethod.CONNECT: return 8 /* FIRHTTPMethod.CONNECT */; case HttpMethod.DELETE: return 3 /* FIRHTTPMethod.DELETE */; case HttpMethod.GET: return 0 /* FIRHTTPMethod.GET */; case HttpMethod.HEAD: return 4 /* FIRHTTPMethod.HEAD */; case HttpMethod.OPTIONS: return 6 /* FIRHTTPMethod.OPTIONS */; case HttpMethod.PATCH: return 5 /* FIRHTTPMethod.PATCH */; case HttpMethod.POST: return 2 /* FIRHTTPMethod.POST */; case HttpMethod.PUT: return 1 /* FIRHTTPMethod.PUT */; case HttpMethod.TRACE: return 7 /* FIRHTTPMethod.TRACE */; } } export class HttpMetric { static fromUrlMethod(url, method) { if (url && method) { const result = new HttpMetric(); result._native = FIRHTTPMetric.alloc().initWithURLHTTPMethod(NSURL.URLWithString(url), toHttpMethod(method)); return result; } return null; } get native() { return this._native; } get ios() { return this.native; } getAttribute(attribute) { return this.native.valueForAttribute(attribute); } getAttributes() { return deserialize(this.native.attributes); } putAttribute(attribute, value) { this.native.setValueForAttribute(value, attribute); } removeAttribute(attribute) { this.native.removeAttribute(attribute); } setHttpResponseCode(code) { this.native.responseCode = code; } setRequestPayloadSize(bytes) { this.native.requestPayloadSize = bytes; } setResponseContentType(contentType) { this.native.responseContentType = contentType; } setResponsePayloadSize(bytes) { this.native.responsePayloadSize = bytes; } start() { this.native.start(); } stop() { this.native.stop(); } } export class Trace { static fromNative(trace) { if (trace instanceof FIRTrace) { const result = new Trace(); result._native = trace; return result; } return null; } get native() { return this._native; } get ios() { return this.native; } getAttribute(attribute) { return this.native.valueForAttribute(attribute); } getMetric(metricName) { return this.native.valueForIntMetric(metricName); } getMetrics() { return deserialize(this.native.attributes); } incrementMetric(metricName, incrementBy) { this.native.incrementMetricByInt(metricName, incrementBy); } putAttribute(attribute, value) { this.native.setValueForAttribute(attribute, value); } putMetric(metricName, value) { this.native.setIntValueForMetric(value, metricName); } removeMetric(metricName) { this.native.removeAttribute(metricName); } start() { this.native.start(); } stop() { this.native.stop(); } } export class Performance { constructor() { if (defaultPerformance) { return defaultPerformance; } defaultPerformance = this; this._native = FIRPerformance.sharedInstance(); } get isPerformanceCollectionEnabled() { return this.native.dataCollectionEnabled; } set isPerformanceCollectionEnabled(value) { this.native.dataCollectionEnabled = value; this.native.instrumentationEnabled = value; } newHttpMetric(url, httpMethod) { return HttpMetric.fromUrlMethod(url, httpMethod); } newTrace(identifier) { return Trace.fromNative(this.native.traceWithName(identifier)); } startTrace(identifier) { const trace = Trace.fromNative(this.native.traceWithName(identifier)); trace.start(); return trace; } get native() { return this._native; } get ios() { return this.native; } get app() { if (!this._app) { // @ts-ignore this._app = FirebaseApp.fromNative(this.native.app); } return this._app; } } //# sourceMappingURL=index.ios.js.map