From f4c62cc3d1bc8f4ca5f025b9fbf5bbf61af300e5 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Thu, 29 Jul 2021 00:50:48 +0800 Subject: [PATCH] Prevent some repeated actions --- .../modules/update/UpdateContext.java | 2 +- lib/index.js | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java index b7f76b5..fcf8191 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -154,7 +154,7 @@ public class UpdateContext { String lastVersion = getCurrentVersion(); SharedPreferences.Editor editor = sp.edit(); editor.putString("currentVersion", hash); - if (lastVersion != null) { + if (lastVersion != null && lastVersion != hash) { editor.putString("lastVersion", lastVersion); } editor.putBoolean("firstTime", true); diff --git a/lib/index.js b/lib/index.js index 6c549a5..336d8d2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -85,8 +85,17 @@ function assertRelease() { } } +let checkingThrottling = false; export async function checkUpdate(APPKEY, isRetry) { assertRelease(); + if (checkingThrottling) { + logger('repeated checking, ignored'); + return; + } + checkingThrottling = true; + setTimeout(() => { + checkingThrottling = false; + }, 3000); if (blockUpdate && blockUpdate.until > Date.now() / 1000) { throw new Error( `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date( @@ -145,11 +154,25 @@ function checkOperation(op) { }); } +let downloadingThrottling = false; +let downloadedHash; export async function downloadUpdate(options, eventListeners) { assertRelease(); if (!options.update) { return; } + if (downloadedHash === options.hash) { + logger(`duplicated downloaded hash ${downloadedHash}, ignored`); + return; + } + if (downloadingThrottling) { + logger('repeated downloading, ignored'); + return; + } + downloadingThrottling = true; + setTimeout(() => { + downloadingThrottling = false; + }, 3000); let progressHandler; if (eventListeners) { if (eventListeners.onDownloadProgress) { @@ -188,7 +211,7 @@ export async function downloadUpdate(options, eventListeners) { }); } progressHandler && progressHandler.remove(); - await new Promise((resolve) => setTimeout(resolve, 100)); + downloadedHash = options.hash; return options.hash; } @@ -198,14 +221,26 @@ export function switchVersion(hash) { Pushy.reloadUpdate({ hash }); } +let readyHash; export function switchVersionLater(hash) { assertRelease(); + if (readyHash === hash) { + logger(`duplicated ready hash ${readyHash}, ignored`); + return; + } + readyHash = hash; logger('switchVersionLater'); Pushy.setNeedUpdate({ hash }); } +let marked = false; export function markSuccess() { assertRelease(); + if (marked) { + logger('repeated markSuccess, ignored'); + return; + } + marked = true; logger('markSuccess'); Pushy.markSuccess(); }