forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.ts
182 lines (168 loc) · 4.37 KB
/
index.ios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { deserialize, firebase, FirebaseApp } from '@nativescript/firebase-core';
import { HttpMethod, IHttpMetric, IPerformance, ITrace } from './common';
let defaultPerformance: Performance;
const fb = firebase();
Object.defineProperty(fb, 'performance', {
value: () => {
if (!defaultPerformance) {
defaultPerformance = new Performance();
}
return defaultPerformance;
},
writable: false,
});
function toHttpMethod(method: HttpMethod): FIRHTTPMethod {
switch (method) {
case HttpMethod.CONNECT:
return FIRHTTPMethod.CONNECT;
case HttpMethod.DELETE:
return FIRHTTPMethod.DELETE;
case HttpMethod.GET:
return FIRHTTPMethod.GET;
case HttpMethod.HEAD:
return FIRHTTPMethod.HEAD;
case HttpMethod.OPTIONS:
return FIRHTTPMethod.OPTIONS;
case HttpMethod.PATCH:
return FIRHTTPMethod.PATCH;
case HttpMethod.POST:
return FIRHTTPMethod.POST;
case HttpMethod.PUT:
return FIRHTTPMethod.PUT;
case HttpMethod.TRACE:
return FIRHTTPMethod.TRACE;
}
}
export class HttpMetric implements IHttpMetric {
_native: FIRHTTPMetric;
static fromUrlMethod(url: string, method: HttpMethod) {
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: string): string {
return this.native.valueForAttribute(attribute);
}
getAttributes(): { [key: string]: string } {
return deserialize(this.native.attributes);
}
putAttribute(attribute: string, value: string): void {
this.native.setValueForAttribute(value, attribute);
}
removeAttribute(attribute: string): void {
this.native.removeAttribute(attribute);
}
setHttpResponseCode(code: number): void {
this.native.responseCode = code;
}
setRequestPayloadSize(bytes: number): void {
this.native.requestPayloadSize = bytes;
}
setResponseContentType(contentType: string): void {
this.native.responseContentType = contentType;
}
setResponsePayloadSize(bytes: number): void {
this.native.responsePayloadSize = bytes;
}
start() {
this.native.start();
}
stop() {
this.native.stop();
}
}
export class Trace implements ITrace {
_native: FIRTrace;
static fromNative(trace: FIRTrace) {
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: string): string {
return this.native.valueForAttribute(attribute);
}
getMetric(metricName: string): number {
return this.native.valueForIntMetric(metricName);
}
getMetrics(): { [key: string]: number } {
return deserialize(this.native.attributes);
}
incrementMetric(metricName: string, incrementBy: number): void {
this.native.incrementMetricByInt(metricName, incrementBy);
}
putAttribute(attribute: string, value: string): void {
this.native.setValueForAttribute(attribute, value);
}
putMetric(metricName: string, value: number): void {
this.native.setIntValueForMetric(value, metricName);
}
removeMetric(metricName: string): void {
this.native.removeAttribute(metricName);
}
start() {
this.native.start();
}
stop() {
this.native.stop();
}
}
export class Performance implements IPerformance {
_native: FIRPerformance;
_app: FirebaseApp;
constructor() {
if (defaultPerformance) {
return defaultPerformance;
}
defaultPerformance = this;
this._native = FIRPerformance.sharedInstance();
}
get isPerformanceCollectionEnabled(): boolean {
return this.native.dataCollectionEnabled;
}
set isPerformanceCollectionEnabled(value) {
this.native.dataCollectionEnabled = value;
this.native.instrumentationEnabled = value;
}
newHttpMetric(url: string, httpMethod: HttpMethod): HttpMetric {
return HttpMetric.fromUrlMethod(url, httpMethod);
}
newTrace(identifier: string): Trace {
return Trace.fromNative(this.native.traceWithName(identifier));
}
startTrace(identifier: string): Trace {
const trace = Trace.fromNative(this.native.traceWithName(identifier));
trace.start();
return trace;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get app(): FirebaseApp {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.app);
}
return this._app;
}
}