Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
Switch versions explicitly (#62)
Browse files Browse the repository at this point in the history
Switch versions explicitly with call to `WebAppLocalServer.switchToPendingVersion` instead of trying to switch every time a browser reload is detected.
  • Loading branch information
wojtkowiak authored and benjamn committed Nov 15, 2018
1 parent eb19701 commit 319cd32
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/android/WebAppLocalServer.java
Expand Up @@ -42,6 +42,8 @@ public class WebAppLocalServer extends CordovaPlugin implements AssetBundleManag
private String launchUrl;
private int localServerPort;

private Boolean switchedToNewVersion = false;

private List<WebResourceHandler> resourceHandlers;

/** The asset bundle manager is responsible for managing asset bundles
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
23 changes: 22 additions & 1 deletion src/ios/WebAppLocalServer.swift
Expand Up @@ -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)"
Expand Down Expand Up @@ -197,7 +199,10 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate {
self.pendingAssetBundle = nil
}

startStartupTimer()
if (switchedToNewVersion) {
switchedToNewVersion = false;
startStartupTimer();
}
}

func startStartupTimer() {
Expand Down Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions www/webapp_local_server.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 319cd32

Please sign in to comment.