Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm committed Feb 24, 2024
1 parent 4f9e149 commit f01716b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
64 changes: 51 additions & 13 deletions src/client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckResult, PushyOptions, ProgressData } from './type';
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { assertRelease, log } from './utils';
import {
EmitterSubscription,
Expand All @@ -12,9 +12,9 @@ import {
pushyNativeEventEmitter,
currentVersion,
packageVersion,
report,
rolledBackVersion,
setLocalHashInfo,
isRolledBack,
} from './core';

const defaultServer = {
Expand All @@ -25,13 +25,16 @@ const defaultServer = {
};

const empty = {};
const noop = () => {};

export class Pushy {
options: PushyOptions = {
appKey: '',
server: defaultServer,
autoMarkSuccess: true,
useAlert: true,
strategy: 'both',
logger: noop,
};

lastChecking: number;
Expand All @@ -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<string, string | number>;
}) => {
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}`;
};
Expand All @@ -79,7 +117,7 @@ export class Pushy {
}
this.marked = true;
PushyModule.markSuccess();
report({ type: 'markSuccess' });
this.report({ type: 'markSuccess' });
};
switchVersion = (hash: string) => {
assertRelease();
Expand Down Expand Up @@ -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: {
Expand All @@ -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.',
});
Expand All @@ -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.',
});
Expand All @@ -153,7 +191,7 @@ export class Pushy {
this.lastResult = result;

if (resp.status !== 200) {
report({
this.report({
type: 'errorChecking',
message: result.message,
});
Expand Down Expand Up @@ -214,7 +252,7 @@ export class Pushy {
);
}
let succeeded = false;
report({ type: 'downloading' });
this.report({ type: 'downloading' });
if (diffUrl) {
log('downloading diff');
try {
Expand Down Expand Up @@ -257,7 +295,7 @@ export class Pushy {
delete this.progressHandlers[hash];
}
if (!succeeded) {
return report({
return this.report({
type: 'errorUpdate',
data: { newVersion: hash },
});
Expand All @@ -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';
Expand All @@ -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();
Expand Down
39 changes: 0 additions & 39 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { EventType, UpdateEventsLogger } from './type';
import { log } from './utils';
const {
version: v,
Expand Down Expand Up @@ -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<string, string | number>;
}) {
log(type + ' ' + message);
reporter({
type,
data: {
currentVersion,
cInfo,
packageVersion,
buildTime,
message,
...data,
},
});
}

log('uuid: ' + uuid);

export const cInfo = {
Expand Down

0 comments on commit f01716b

Please sign in to comment.