[perf] Return Promise types
This commit is contained in:
parent
417b6ba542
commit
2c776a60e8
|
@ -48,6 +48,7 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule {
|
|||
@ReactMethod
|
||||
public void getTraceAttribute(String identifier, String attribute, Promise promise) {
|
||||
promise.resolve(getOrCreateTrace(identifier).getAttribute(attribute));
|
||||
<<<<<<< Updated upstream
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
@ -75,6 +76,35 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule {
|
|||
}
|
||||
|
||||
@ReactMethod
|
||||
=======
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getTraceAttributes(String identifier, Promise promise) {
|
||||
Map<String, String> attributes = getOrCreateTrace(identifier).getAttributes();
|
||||
WritableMap map = Arguments.createMap();
|
||||
|
||||
for (Map.Entry<String, String> entry : attributes.entrySet()) {
|
||||
map.putString(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
promise.resolve(map);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getTraceLongMetric(String identifier, String metricName, Promise promise) {
|
||||
Integer value = Long.valueOf(getOrCreateTrace(identifier).getLongMetric(metricName)).intValue();
|
||||
promise.resolve(value);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void incrementTraceMetric(String identifier, String metricName, Integer incrementBy, Promise promise) {
|
||||
getOrCreateTrace(identifier).incrementMetric(metricName, incrementBy.longValue());
|
||||
promise.resolve(null);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
>>>>>>> Stashed changes
|
||||
public void putTraceAttribute(String identifier, String attribute, String value, Promise promise) {
|
||||
getOrCreateTrace(identifier).putAttribute(attribute, value);
|
||||
promise.resolve(null);
|
||||
|
@ -197,8 +227,40 @@ public class RNFirebasePerformance extends ReactContextBaseJavaModule {
|
|||
if (httpMetrics.containsKey(identifier)) {
|
||||
return httpMetrics.get(identifier);
|
||||
}
|
||||
<<<<<<< Updated upstream
|
||||
HttpMetric httpMetric = FirebasePerformance.getInstance().newHttpMetric(url, httpMethod);
|
||||
httpMetrics.put(identifier, httpMetric);
|
||||
return httpMetric;
|
||||
}
|
||||
=======
|
||||
HttpMetric httpMetric = FirebasePerformance.getInstance().newHttpMetric(url, this.mapStringToMethod(httpMethod));
|
||||
httpMetrics.put(identifier, httpMetric);
|
||||
return httpMetric;
|
||||
}
|
||||
|
||||
private String mapStringToMethod(String value) {
|
||||
switch (value) {
|
||||
case "CONNECT":
|
||||
return FirebasePerformance.HttpMethod.CONNECT;
|
||||
case "DELETE":
|
||||
return FirebasePerformance.HttpMethod.DELETE;
|
||||
case "GET":
|
||||
return FirebasePerformance.HttpMethod.GET;
|
||||
case "HEAD":
|
||||
return FirebasePerformance.HttpMethod.HEAD;
|
||||
case "OPTIONS":
|
||||
return FirebasePerformance.HttpMethod.OPTIONS;
|
||||
case "PATCH":
|
||||
return FirebasePerformance.HttpMethod.PATCH;
|
||||
case "POST":
|
||||
return FirebasePerformance.HttpMethod.POST;
|
||||
case "PUT":
|
||||
return FirebasePerformance.HttpMethod.PUT;
|
||||
case "TRACE":
|
||||
return FirebasePerformance.HttpMethod.TRACE;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
>>>>>>> Stashed changes
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ describe('perf()', () => {
|
|||
|
||||
describe('newHttpMetric()', () => {
|
||||
it('returns an instance of HttpMetric', async () => {
|
||||
<<<<<<< Updated upstream
|
||||
const trace = firebase.perf().newHttpMetric('foo', 'bar');
|
||||
trace.constructor.name.should.be.equal('HttpMetric');
|
||||
});
|
||||
|
@ -38,6 +39,20 @@ describe('perf()', () => {
|
|||
(() => firebase.perf().newHttpMetric(123, [1, 2])).should.throw(
|
||||
'firebase.perf().newHttpMetric() requires url and httpMethod string values'
|
||||
);
|
||||
=======
|
||||
const trace = firebase.perf().newHttpMetric('foo', 'GET');
|
||||
trace.constructor.name.should.be.equal('HttpMetric');
|
||||
});
|
||||
|
||||
it('errors if url/httpMethod not a string', async () => {
|
||||
(() => firebase.perf().newHttpMetric(123, [1, 2])).should.throw(
|
||||
'firebase.perf().newHttpMetric() requires url and httpMethod string values'
|
||||
);
|
||||
});
|
||||
|
||||
it('errors if httpMethod not a valid type', async () => {
|
||||
(() => firebase.perf().newHttpMetric('foo', 'FOO')).should.throw(); // TODO error
|
||||
>>>>>>> Stashed changes
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -16,7 +16,7 @@ export default class HttpMetric {
|
|||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
getAttribute(attribute: string): Promise<string> {
|
||||
getAttribute(attribute: string): Promise<string | null> {
|
||||
return getNativeModule(this._perf).getHttpMetricAttribute(
|
||||
this.url,
|
||||
this.httpMethod,
|
||||
|
@ -31,7 +31,8 @@ export default class HttpMetric {
|
|||
);
|
||||
}
|
||||
|
||||
putAttribute(attribute: string, value: string): Promise<null> {
|
||||
// TODO return true or false
|
||||
putAttribute(attribute: string, value: string): Promise<true | false> {
|
||||
return getNativeModule(this._perf).putHttpMetricAttribute(
|
||||
this.url,
|
||||
this.httpMethod,
|
||||
|
|
|
@ -14,7 +14,7 @@ export default class Trace {
|
|||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
getAttribute(attribute: string): Promise<string> {
|
||||
getAttribute(attribute: string): Promise<string | null> {
|
||||
return getNativeModule(this._perf).getTraceAttribute(
|
||||
this.identifier,
|
||||
attribute
|
||||
|
@ -40,7 +40,8 @@ export default class Trace {
|
|||
);
|
||||
}
|
||||
|
||||
putAttribute(attribute: string, value: string): Promise<null> {
|
||||
// TODO return true or false
|
||||
putAttribute(attribute: string, value: string): Promise<true | false> {
|
||||
return getNativeModule(this._perf).putTraceAttribute(
|
||||
this.identifier,
|
||||
attribute,
|
||||
|
|
|
@ -12,6 +12,29 @@ import type App from '../core/app';
|
|||
export const MODULE_NAME = 'RNFirebasePerformance';
|
||||
export const NAMESPACE = 'perf';
|
||||
|
||||
const HTTP_METHODS = {
|
||||
CONNECT: true,
|
||||
DELETE: true,
|
||||
GET: true,
|
||||
HEAD: true,
|
||||
OPTIONS: true,
|
||||
PATCH: true,
|
||||
POST: true,
|
||||
PUT: true,
|
||||
TRACE: true,
|
||||
};
|
||||
|
||||
type HttpMethod =
|
||||
| 'CONNECT'
|
||||
| 'DELETE'
|
||||
| 'GET'
|
||||
| 'HEAD'
|
||||
| 'OPTIONS'
|
||||
| 'PATCH'
|
||||
| 'POST'
|
||||
| 'PUT'
|
||||
| 'TRACE';
|
||||
|
||||
export default class PerformanceMonitoring extends ModuleBase {
|
||||
constructor(app: App) {
|
||||
super(app, {
|
||||
|
@ -49,13 +72,27 @@ export default class PerformanceMonitoring extends ModuleBase {
|
|||
return new Trace(this, trace);
|
||||
}
|
||||
|
||||
newHttpMetric(url: string, httpMethod: string) {
|
||||
/**
|
||||
* Return a new HttpMetric instance
|
||||
* @param url
|
||||
* @param httpMethod
|
||||
* @returns {HttpMetric}
|
||||
*/
|
||||
newHttpMetric(url: string, httpMethod: HttpMethod): HttpMetric {
|
||||
if (typeof url !== 'string' || typeof httpMethod !== 'string') {
|
||||
throw new Error(
|
||||
'firebase.perf().newHttpMetric() requires url and httpMethod string values'
|
||||
);
|
||||
}
|
||||
|
||||
if (!HTTP_METHODS[httpMethod]) {
|
||||
throw new Error(
|
||||
`firebase.perf().newHttpMetric() httpMethod should be one of ${Object.keys(
|
||||
HTTP_METHODS
|
||||
).join(', ')}`
|
||||
);
|
||||
}
|
||||
|
||||
return new HttpMetric(this, url, httpMethod);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue