1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-22 15:36:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Refactor UpdateContext to implement singleton pattern and ensure synchronous SharedPreferences writes. Update UpdateModule constructors to utilize the singleton instance.

This commit is contained in:
sunnylqm
2025-11-19 18:58:00 +08:00
parent e151c9c618
commit dc9b5d722a
4 changed files with 30 additions and 8 deletions

View File

@@ -22,6 +22,10 @@ public class UpdateContext {
public static boolean DEBUG = false;
private static ReactInstanceManager mReactInstanceManager;
private static boolean isUsingBundleUrl = false;
// Singleton instance
private static UpdateContext sInstance;
private static final Object sLock = new Object();
public UpdateContext(Context context) {
this.context = context;
@@ -54,13 +58,17 @@ public class UpdateContext {
boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
if (packageVersionChanged || buildTimeChanged) {
// Execute cleanUp before clearing SharedPreferences to avoid race condition
this.cleanUp();
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.putString("packageVersion", packageVersion);
editor.putString("buildTime", buildTime);
editor.apply();
this.cleanUp();
// Use commit() instead of apply() to ensure synchronous write completion
// This prevents race condition where getBundleUrl() might read null values
// if called before apply() completes
editor.commit();
}
}
@@ -227,12 +235,26 @@ public class UpdateContext {
return mReactInstanceManager;
}
/**
* Get singleton instance of UpdateContext
*/
public static UpdateContext getInstance(Context context) {
if (sInstance == null) {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new UpdateContext(context.getApplicationContext());
}
}
}
return sInstance;
}
public static String getBundleUrl(Context context) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl();
return getInstance(context).getBundleUrl();
}
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl);
return getInstance(context).getBundleUrl(defaultAssetsUrl);
}
public String getBundleUrl() {

View File

@@ -27,7 +27,7 @@ public class UpdateModule extends NativePushySpec {
}
public UpdateModule(ReactApplicationContext reactContext) {
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
this(reactContext, UpdateContext.getInstance(reactContext));
}
@Override

View File

@@ -40,7 +40,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
}
public UpdateModule(ReactApplicationContext reactContext) {
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
this(reactContext, UpdateContext.getInstance(reactContext));
}
@Override

View File

@@ -37,10 +37,10 @@ export class UpdateContext {
this.preferences.putSync('packageVersion', packageVersion);
this.preferences.flush();
} else if (storedVersion && packageVersion !== storedVersion) {
this.cleanUp();
this.preferences.clear();
this.preferences.putSync('packageVersion', packageVersion);
this.preferences.flush();
this.cleanUp();
}
} catch (e) {
console.error('Failed to init preferences:', e);