From f01716bcb25654cf0ee2fa614ae6ddd6cff6e9f7 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Sat, 24 Feb 2024 11:36:58 +0800 Subject: [PATCH] feat: add logger --- src/client.tsx | 64 ++++++++++++++++++++++++++++++++++++++++---------- src/core.ts | 39 ------------------------------ 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/client.tsx b/src/client.tsx index 54c02a0..0ba7cd8 100644 --- a/src/client.tsx +++ b/src/client.tsx @@ -1,4 +1,4 @@ -import { CheckResult, PushyOptions, ProgressData } from './type'; +import { CheckResult, PushyOptions, ProgressData, EventType } from './type'; import { assertRelease, log } from './utils'; import { EmitterSubscription, @@ -12,9 +12,9 @@ import { pushyNativeEventEmitter, currentVersion, packageVersion, - report, rolledBackVersion, setLocalHashInfo, + isRolledBack, } from './core'; const defaultServer = { @@ -25,6 +25,8 @@ const defaultServer = { }; const empty = {}; +const noop = () => {}; + export class Pushy { options: PushyOptions = { appKey: '', @@ -32,6 +34,7 @@ export class Pushy { autoMarkSuccess: true, useAlert: true, strategy: 'both', + logger: noop, }; lastChecking: number; @@ -55,10 +58,45 @@ export class Pushy { for (const [key, value] of Object.entries(options)) { if (value !== undefined) { this.options[key] = value; + if (key === 'logger') { + if (isRolledBack) { + this.report({ + type: 'rollback', + data: { + rolledBackVersion, + }, + }); + } + } } } }; + report = ({ + type, + message = '', + data = {}, + }: { + type: EventType; + message?: string; + data?: Record; + }) => { + log(type + ' ' + message); + const { logger = noop, appKey } = this.options; + logger({ + type, + data: { + appKey, + currentVersion, + cInfo, + packageVersion, + buildTime, + message, + ...data, + }, + }); + }; + getCheckUrl = (endpoint: string = this.options.server!.main) => { return `${endpoint}/checkUpdate/${this.options.appKey}`; }; @@ -79,7 +117,7 @@ export class Pushy { } this.marked = true; PushyModule.markSuccess(); - report({ type: 'markSuccess' }); + this.report({ type: 'markSuccess' }); }; switchVersion = (hash: string) => { assertRelease(); @@ -108,7 +146,7 @@ export class Pushy { return this.lastResult; } this.lastChecking = now; - report({ type: 'checking' }); + this.report({ type: 'checking' }); const fetchPayload = { method: 'POST', headers: { @@ -126,7 +164,7 @@ export class Pushy { try { resp = await fetch(this.getCheckUrl(), fetchPayload); } catch (e) { - report({ + this.report({ type: 'errorChecking', message: 'Can not connect to update server. Trying backup endpoints.', }); @@ -142,7 +180,7 @@ export class Pushy { } } if (!resp) { - report({ + this.report({ type: 'errorChecking', message: 'Can not connect to update server. Please check your network.', }); @@ -153,7 +191,7 @@ export class Pushy { this.lastResult = result; if (resp.status !== 200) { - report({ + this.report({ type: 'errorChecking', message: result.message, }); @@ -214,7 +252,7 @@ export class Pushy { ); } let succeeded = false; - report({ type: 'downloading' }); + this.report({ type: 'downloading' }); if (diffUrl) { log('downloading diff'); try { @@ -257,7 +295,7 @@ export class Pushy { delete this.progressHandlers[hash]; } if (!succeeded) { - return report({ + return this.report({ type: 'errorUpdate', data: { newVersion: hash }, }); @@ -278,17 +316,17 @@ export class Pushy { if (Platform.OS !== 'android') { return; } - report({ type: 'downloadingApk' }); + this.report({ type: 'downloadingApk' }); if (Platform.Version <= 23) { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, ); if (granted !== PermissionsAndroid.RESULTS.GRANTED) { - return report({ type: 'rejectStoragePermission' }); + return this.report({ type: 'rejectStoragePermission' }); } } catch (err) { - return report({ type: 'errorStoragePermission' }); + return this.report({ type: 'errorStoragePermission' }); } } const progressKey = 'downloadingApk'; @@ -307,7 +345,7 @@ export class Pushy { target: 'update.apk', hash: progressKey, }).catch(() => { - report({ type: 'errowDownloadAndInstallApk' }); + this.report({ type: 'errowDownloadAndInstallApk' }); }); if (this.progressHandlers[progressKey]) { this.progressHandlers[progressKey].remove(); diff --git a/src/core.ts b/src/core.ts index b1f81fa..be73f74 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,5 +1,4 @@ import { NativeEventEmitter, NativeModules, Platform } from 'react-native'; -import { EventType, UpdateEventsLogger } from './type'; import { log } from './utils'; const { version: v, @@ -58,44 +57,6 @@ if (!uuid) { PushyModule.setUuid(uuid); } -const noop = () => {}; -let reporter: UpdateEventsLogger = noop; - -export function onPushyEvents(customReporter: UpdateEventsLogger) { - reporter = customReporter; - if (isRolledBack) { - report({ - type: 'rollback', - data: { - rolledBackVersion, - }, - }); - } -} - -export function report({ - type, - message = '', - data = {}, -}: { - type: EventType; - message?: string; - data?: Record; -}) { - log(type + ' ' + message); - reporter({ - type, - data: { - currentVersion, - cInfo, - packageVersion, - buildTime, - message, - ...data, - }, - }); -} - log('uuid: ' + uuid); export const cInfo = {