Skip to content

Commit 6215876

Browse files
committed
feat: add reset command
1 parent d6b1642 commit 6215876

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class NodeJS extends CordovaPlugin {
5050

5151
private static final String SHARED_PREFS = "NODEJS_MOBILE_PREFS";
5252
private static final String LAST_UPDATED_TIME = "NODEJS_MOBILE_APK_LastUpdateTime";
53+
private static final String FORCE_RESET = "NODEJS_MOBILE_RESET";
5354
private long lastUpdateTime = 1;
5455
private long previousLastUpdateTime = 0;
5556

@@ -107,7 +108,7 @@ public void pluginInitialize() {
107108
}
108109

109110
private void asyncInit() {
110-
if (wasAPKUpdated() || isEmptyNodeModules()) {
111+
if (wasAPKUpdated() || isReset()) {
111112
try {
112113
initSemaphore.acquire();
113114
new Thread(new Runnable() {
@@ -151,6 +152,8 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
151152
String scriptBody = data.getString(0);
152153
JSONObject startOptions = data.getJSONObject(1);
153154
this.startEngineWithScript(scriptBody, startOptions, callbackContext);
155+
} else if (action.equals("reset")) {
156+
this.setReset(callbackContext);
154157
} else {
155158
Log.e(LOGTAG, "Invalid action: " + action);
156159
return false;
@@ -367,6 +370,28 @@ private boolean isEmptyNodeModules(){
367370
return !nodejsModulesFolder.exists();
368371
}
369372

373+
private void setReset(CallbackContext callbackContext) {
374+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
375+
SharedPreferences.Editor editor = prefs.edit();
376+
editor.putBoolean(FORCE_RESET, true);
377+
editor.commit();
378+
sendResult(true, "Reset done.", callbackContext);
379+
}
380+
381+
private void clearReset() {
382+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
383+
SharedPreferences.Editor editor = prefs.edit();
384+
editor.remove(FORCE_RESET);
385+
editor.commit();
386+
}
387+
388+
private void isReset() {
389+
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
390+
boolean result = prefs.getBoolean(FORCE_RESET, false);
391+
clearReset();
392+
return result;
393+
}
394+
370395
private boolean wasAPKUpdated() {
371396
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
372397
this.previousLastUpdateTime = prefs.getLong(LAST_UPDATED_TIME, 0);

www/nodejs_apis.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ function startWithScript(script, callback, options) {
142142
startEngine('startEngineWithScript', [script, options], callback);
143143
};
144144

145+
function reset(callback) {
146+
startEngine('reset', [], callback);
147+
};
148+
145149
const eventChannel = new EventChannel(EVENT_CHANNEL);
146150
registerChannel(eventChannel);
147151

148152
module.exports = exports = {
149153
start,
150154
startWithScript,
155+
reset,
151156
channel: eventChannel
152157
};

0 commit comments

Comments
 (0)