From 319cd3296b5f866ed7bd492cccf645318758d91c Mon Sep 17 00:00:00 2001 From: Bartosz Wojtkowiak Date: Thu, 15 Nov 2018 19:56:22 +0100 Subject: [PATCH] Switch versions explicitly (#62) Switch versions explicitly with call to `WebAppLocalServer.switchToPendingVersion` instead of trying to switch every time a browser reload is detected. --- src/android/WebAppLocalServer.java | 31 +++++++++++++++++++----------- src/ios/WebAppLocalServer.swift | 23 +++++++++++++++++++++- www/webapp_local_server.js | 15 +++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/android/WebAppLocalServer.java b/src/android/WebAppLocalServer.java index 8dd44c9..7415a9e 100644 --- a/src/android/WebAppLocalServer.java +++ b/src/android/WebAppLocalServer.java @@ -42,6 +42,8 @@ public class WebAppLocalServer extends CordovaPlugin implements AssetBundleManag private String launchUrl; private int localServerPort; + private Boolean switchedToNewVersion = false; + private List resourceHandlers; /** The asset bundle manager is responsible for managing asset bundles @@ -179,20 +181,12 @@ public void onReset() { newVersionReadyCallbackContext = null; errorCallbackContext = null; - // If there is a pending asset bundle, we make it the current - if (pendingAssetBundle != null) { - currentAssetBundle = pendingAssetBundle; - pendingAssetBundle = null; - } - - Log.i(LOG_TAG, "Serving asset bundle with version: " + currentAssetBundle.getVersion()); - configuration.setAppId(currentAssetBundle.getAppId()); configuration.setRootUrlString(currentAssetBundle.getRootUrlString()); configuration.setCordovaCompatibilityVersion(currentAssetBundle.getCordovaCompatibilityVersion()); - // Don't start startup timer when running a test - if (testingDelegate == null) { + if (switchedToNewVersion) { + switchedToNewVersion = false; startStartupTimer(); } } @@ -217,6 +211,19 @@ private void removeStartupTimer() { } } + private void switchPendingVersion(CallbackContext callbackContext) { + // If there is a pending asset bundle, we make it the current. + if (pendingAssetBundle != null) { + Log.i(LOG_TAG, "Switching pending version " + pendingAssetBundle.getVersion() + " as current version."); + currentAssetBundle = pendingAssetBundle; + pendingAssetBundle = null; + switchedToNewVersion = true; + callbackContext.success(); + } else { + callbackContext.error("No pending version to switch to"); + } + } + //endregion //region Public plugin commands @@ -235,12 +242,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if ("startupDidComplete".equals(action)) { startupDidComplete(callbackContext); return true; + } else if ("switchPendingVersion".equals(action)) { + switchPendingVersion(callbackContext); + return true; } if (testingDelegate != null) { return testingDelegate.execute(action, args, callbackContext); } - return false; } diff --git a/src/ios/WebAppLocalServer.swift b/src/ios/WebAppLocalServer.swift index cb562f2..738e6a1 100644 --- a/src/ios/WebAppLocalServer.swift +++ b/src/ios/WebAppLocalServer.swift @@ -15,6 +15,8 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate { /// The listening port of the local web server private var localServerPort: UInt = 0 + private var switchedToNewVersion = false; + let authTokenKeyValuePair: String = { let authToken = ProcessInfo.processInfo.globallyUniqueString return "cdvToken=\(authToken)" @@ -197,7 +199,10 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate { self.pendingAssetBundle = nil } - startStartupTimer() + if (switchedToNewVersion) { + switchedToNewVersion = false; + startStartupTimer(); + } } func startStartupTimer() { @@ -238,6 +243,22 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate { self.commandDelegate?.send(result, callbackId: command.callbackId) } + open func switchPendingVersion(_ command: CDVInvokedUrlCommand) { + // If there is a pending asset bundle, we make it the current + if let pendingAssetBundle = pendingAssetBundle { + NSLog("Switching pending version \(pendingAssetBundle.version) as the current asset bundle") + currentAssetBundle = pendingAssetBundle + self.pendingAssetBundle = nil + switchedToNewVersion = true; + let result = CDVPluginResult(status: CDVCommandStatus_OK) + self.commandDelegate?.send(result, callbackId: command.callbackId) + } else { + let errorMessage = "No pending version to switch to" + let result = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: errorMessage) + commandDelegate?.send(result, callbackId: command.callbackId) + } + } + open func checkForUpdates(_ command: CDVInvokedUrlCommand) { guard let rootURL = configuration.rootURL else { let errorMessage = "checkForUpdates requires a rootURL to be configured" diff --git a/www/webapp_local_server.js b/www/webapp_local_server.js index 338d7f8..4c984f6 100644 --- a/www/webapp_local_server.js +++ b/www/webapp_local_server.js @@ -28,6 +28,21 @@ module.exports = { []); }, + switchToPendingVersion: function(callback, errorCallback) { + cordova.exec( + callback, + (error) => { + console.error(error); + if (typeof errorCallback === "function") { + errorCallback(error); + } + }, + "WebAppLocalServer", + "switchPendingVersion", + [] + ); + }, + onError: function(callback) { cordova.exec( function(errorMessage) {