UNPKG

@nativescript/firebase-crashlytics

Version:
107 lines 3.48 kB
import { FirebaseApp, FirebaseError, firebase } from '@nativescript/firebase-core'; import lazy from '@nativescript/core/utils/lazy'; import StackTrace from 'stacktrace-js'; const NSCrashlyticsReference = lazy(() => org.nativescript.firebase.crashlytics.FirebaseCrashlytics); let defaultCrashlytics; const fb = firebase(); Object.defineProperty(fb, 'crashlytics', { value: () => { if (!defaultCrashlytics) { defaultCrashlytics = new Crashlytics(); } return defaultCrashlytics; }, writable: false, }); export class Crashlytics { constructor() { if (defaultCrashlytics) { return defaultCrashlytics; } defaultCrashlytics = this; } get native() { if (!this._native) { this._native = com.google.firebase.crashlytics.FirebaseCrashlytics.getInstance(); } return this._native; } get android() { return this.native; } get app() { if (!this._app) { // @ts-ignore this._app = FirebaseApp.fromNative(this.native.app); } return this._app; } checkForUnsentReports() { return new Promise((resolve, reject) => { NSCrashlyticsReference().checkForUnsentReports(this.native, new org.nativescript.firebase.crashlytics.FirebaseCrashlytics.Callback({ onSuccess(param0) { resolve(param0); }, onError(param0) { const err = FirebaseError.fromNative(param0); reject(err); }, })); }); } crash() { NSCrashlyticsReference().crash(); } deleteUnsentReports() { this.native.deleteUnsentReports(); } didCrashOnPreviousExecution() { return this.native.didCrashOnPreviousExecution(); } log(message) { this.native.log(message); } recordError(error) { if (error instanceof Error) { StackTrace.fromError(error).then((stack) => { const traceElements = Array.create('java.lang.StackTraceElement', stack.length); stack.forEach((item, i) => { traceElements[i] = new java.lang.StackTraceElement('', item.functionName || '(anonymous)', item.fileName, -1); }); const t = new java.lang.Throwable(error.message); t.setStackTrace(traceElements); this.native.recordException(t); }); } else { this.native.recordException(error); } } sendUnsentReports() { this.native.sendUnsentReports(); } setAttribute(name, value) { if (typeof value === 'string') { this.native.setCustomKey(name, value); } else if (typeof value === 'number') { this.native.setCustomKey(name, value); } else if (typeof value === 'boolean') { this.native.setCustomKey(name, value); } } setAttributes(attributes) { try { NSCrashlyticsReference().setAttributes(this.native, JSON.stringify(attributes)); } catch (e) { } } setCrashlyticsCollectionEnabled(enabled) { this.native.setCrashlyticsCollectionEnabled(java.lang.Boolean.valueOf(enabled)); } setUserId(userId) { this.native.setUserId(userId); } } //# sourceMappingURL=index.android.js.map