From 61c262bcc696d719c7076103132a4763bcca4798 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 21 Jul 2021 17:56:03 +0800 Subject: [PATCH] refact: use DoricSingleton to hold all static or singleton objects --- .../java/pub/doric/demo/MyApplication.java | 7 +- .../main/java/pub/doric/devkit/DoricDev.java | 5 +- .../pub/doric/devkit/ui/DoricDevActivity.java | 14 +-- .../doric/src/main/java/pub/doric/Doric.java | 27 +++++- .../src/main/java/pub/doric/DoricContext.java | 4 +- .../java/pub/doric/DoricContextManager.java | 11 +-- .../java/pub/doric/DoricNativeDriver.java | 10 +- .../java/pub/doric/DoricPanelFragment.java | 2 +- .../main/java/pub/doric/DoricRegistry.java | 63 ++----------- .../main/java/pub/doric/DoricSingleton.java | 94 +++++++++++++++++++ .../doric/loader/DoricJSLoaderManager.java | 12 +-- .../performance/DoricPerformanceProfile.java | 5 +- doric-iOS/Devkit/Classes/DoricDev.m | 5 +- .../Devkit/Classes/DoricDevViewController.m | 12 +-- doric-iOS/Example/Example/AppDelegate.m | 4 +- doric-iOS/Example/Example/ViewController.m | 6 +- doric-iOS/Pod/Classes/Doric.h | 10 ++ doric-iOS/Pod/Classes/Doric.m | 23 ++++- doric-iOS/Pod/Classes/DoricContext.m | 6 +- doric-iOS/Pod/Classes/DoricContextManager.m | 8 +- doric-iOS/Pod/Classes/DoricNativeDriver.h | 1 - doric-iOS/Pod/Classes/DoricNativeDriver.m | 9 -- doric-iOS/Pod/Classes/DoricRegistry.h | 12 +-- doric-iOS/Pod/Classes/DoricRegistry.m | 68 ++------------ doric-iOS/Pod/Classes/DoricSingleton.h | 41 ++++++++ doric-iOS/Pod/Classes/DoricSingleton.m | 65 +++++++++++++ doric-iOS/Pod/Classes/DoricViewController.m | 3 +- .../Pod/Classes/Loader/DoricJSLoaderManager.h | 2 - .../Pod/Classes/Loader/DoricJSLoaderManager.m | 9 -- .../Performance/DoricPerformanceProfile.m | 3 +- 30 files changed, 320 insertions(+), 221 deletions(-) create mode 100644 doric-android/doric/src/main/java/pub/doric/DoricSingleton.java create mode 100644 doric-iOS/Pod/Classes/DoricSingleton.h create mode 100644 doric-iOS/Pod/Classes/DoricSingleton.m diff --git a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java index 3625a568..a56faf7d 100644 --- a/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java +++ b/doric-android/app/src/main/java/pub/doric/demo/MyApplication.java @@ -26,6 +26,7 @@ import java.util.Map; import pub.doric.Doric; import pub.doric.DoricRegistry; +import pub.doric.DoricSingleton; public class MyApplication extends Application { @Override @@ -41,10 +42,10 @@ public class MyApplication extends Application { Map map = new HashMap<>(); map.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage()); map.put("localeCountry", context.getResources().getConfiguration().locale.getCountry()); - DoricRegistry.setEnvironmentValue(map); + DoricSingleton.getInstance().setEnvironmentValue(map); } }, intentFilter); - DoricRegistry.enablePerformance(true); - DoricRegistry.enableRenderSnapshot(true); + Doric.enablePerformance(true); + Doric.enableRenderSnapshot(true); } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java index 03302a9e..8dfe2e84 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDev.java @@ -19,6 +19,7 @@ import pub.doric.Doric; import pub.doric.DoricContext; import pub.doric.DoricContextManager; import pub.doric.DoricNativeDriver; +import pub.doric.DoricSingleton; import pub.doric.devkit.ui.DoricDevActivity; import pub.doric.devkit.util.SimulatorUtil; import pub.doric.performance.DoricPerformanceProfile; @@ -51,8 +52,8 @@ public class DoricDev { private DoricDev() { this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application()); - DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor()); - DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook()); + DoricSingleton.getInstance().getNativeDriver().getRegistry().registerMonitor(new DoricDevMonitor()); + DoricSingleton.getInstance().getNativeDriver().getRegistry().setGlobalPerformanceAnchorHook(new DoricDevPerformanceAnchorHook()); } public static DoricDev getInstance() { diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index bde66b58..cd5f2ead 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -54,9 +54,9 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import pub.doric.Doric; import pub.doric.DoricContext; import pub.doric.DoricContextManager; -import pub.doric.DoricRegistry; import pub.doric.devkit.DoricDebugDriver; import pub.doric.devkit.DoricDev; import pub.doric.devkit.R; @@ -209,21 +209,21 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat } }); SwitchCompat snapshotSwitch = findViewById(R.id.switch_snapshot); - snapshotSwitch.setChecked(DoricRegistry.isEnableRenderSnapshot()); + snapshotSwitch.setChecked(Doric.isEnableRenderSnapshot()); snapshotSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - DoricRegistry.enableRenderSnapshot(isChecked); + Doric.enableRenderSnapshot(isChecked); } }); setSwitch(snapshotSwitch); SwitchCompat profileSwitch = findViewById(R.id.switch_profile); - profileSwitch.setChecked(DoricRegistry.isEnablePerformance()); + profileSwitch.setChecked(Doric.isEnablePerformance()); profileSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - DoricRegistry.enablePerformance(isChecked); + Doric.enablePerformance(isChecked); } }); setSwitch(profileSwitch); @@ -396,7 +396,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat }); } } - if (DoricRegistry.isEnableRenderSnapshot()) { + if (Doric.isEnableRenderSnapshot()) { actionMap.put("Snapshot", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -410,7 +410,7 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat } }); } - if (DoricRegistry.isEnablePerformance()) { + if (Doric.isEnablePerformance()) { actionMap.put("Performance", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { diff --git a/doric-android/doric/src/main/java/pub/doric/Doric.java b/doric-android/doric/src/main/java/pub/doric/Doric.java index 0993e996..70f79d01 100644 --- a/doric-android/doric/src/main/java/pub/doric/Doric.java +++ b/doric-android/doric/src/main/java/pub/doric/Doric.java @@ -20,8 +20,8 @@ import android.app.Application; import com.facebook.soloader.SoLoader; import java.io.IOException; +import java.util.Map; -import pub.doric.loader.DoricJSLoaderManager; import pub.doric.loader.IDoricJSLoader; /** @@ -56,7 +56,7 @@ public class Doric { * @param doricLibrary Which registered in global */ public static void registerLibrary(DoricLibrary doricLibrary) { - DoricRegistry.register(doricLibrary); + DoricSingleton.getInstance().registerLibrary(doricLibrary); } /** @@ -65,6 +65,27 @@ public class Doric { * @param jsLoader Which added in global */ public static void addJSLoader(IDoricJSLoader jsLoader) { - DoricJSLoaderManager.getInstance().addJSLoader(jsLoader); + DoricSingleton.getInstance().getJsLoaderManager().addJSLoader(jsLoader); } + + public void setEnvironmentValue(Map value) { + DoricSingleton.getInstance().setEnvironmentValue(value); + } + + public static void enablePerformance(boolean enable) { + DoricSingleton.getInstance().enablePerformance = enable; + } + + public static boolean isEnablePerformance() { + return DoricSingleton.getInstance().enablePerformance; + } + + public static void enableRenderSnapshot(boolean enable) { + DoricSingleton.getInstance().enableRenderSnapshot = enable; + } + + public static boolean isEnableRenderSnapshot() { + return DoricSingleton.getInstance().enableRenderSnapshot; + } + } diff --git a/doric-android/doric/src/main/java/pub/doric/DoricContext.java b/doric-android/doric/src/main/java/pub/doric/DoricContext.java index 31b31ffc..cc19a0d1 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricContext.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricContext.java @@ -135,7 +135,7 @@ public class DoricContext { } public void init(String initData) { - if (DoricRegistry.isEnableRenderSnapshot()) { + if (Doric.isEnableRenderSnapshot()) { callEntity("__enableSnapshot__"); } this.extra = initData; @@ -158,7 +158,7 @@ public class DoricContext { public IDoricDriver getDriver() { if (doricDriver == null) { - doricDriver = DoricNativeDriver.getInstance(); + doricDriver = DoricSingleton.getInstance().getNativeDriver(); } return doricDriver; } diff --git a/doric-android/doric/src/main/java/pub/doric/DoricContextManager.java b/doric-android/doric/src/main/java/pub/doric/DoricContextManager.java index a3046720..33a4426a 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricContextManager.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricContextManager.java @@ -35,17 +35,8 @@ public class DoricContextManager { private final AtomicInteger counter = new AtomicInteger(); private final Map doricContextMap = new ConcurrentHashMap<>(); - - private static class Inner { - private static final DoricContextManager sInstance = new DoricContextManager(); - } - - private DoricContextManager() { - - } - public static DoricContextManager getInstance() { - return Inner.sInstance; + return DoricSingleton.getInstance().getContextManager(); } DoricContext createContext(Context context, final String script, final String source, String extra) { diff --git a/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java b/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java index 76e8eab2..0106e705 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java @@ -43,21 +43,13 @@ public class DoricNativeDriver implements IDoricDriver { private final Handler mUIHandler; private final Handler mJSHandler; - private static class Inner { - private static final DoricNativeDriver sInstance = new DoricNativeDriver(); - } - - private DoricNativeDriver() { + public DoricNativeDriver() { doricJSEngine = new DoricJSEngine(); mBridgeExecutor = Executors.newCachedThreadPool(); mUIHandler = new Handler(Looper.getMainLooper()); mJSHandler = doricJSEngine.getJSHandler(); } - public static DoricNativeDriver getInstance() { - return Inner.sInstance; - } - @Override public AsyncResult invokeContextEntityMethod(final String contextId, final String method, final Object... args) { final AsyncResult asyncResult = new AsyncResult<>(); diff --git a/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java index 56922b49..fc76a4cb 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -198,7 +198,7 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator { final String alias = argument.getString("alias"); String source = argument.getString("source"); final String extra = argument.getString("extra"); - DoricJSLoaderManager.getInstance().loadJSBundle(source).setCallback(new AsyncResult.Callback() { + DoricSingleton.getInstance().getJsLoaderManager().loadJSBundle(source).setCallback(new AsyncResult.Callback() { @Override public void onResult(String result) { if (getActivity() == null) { diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index d2a5dfae..c6246e3d 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -19,13 +19,10 @@ import android.graphics.drawable.Drawable; import android.text.TextUtils; import java.lang.ref.WeakReference; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import pub.doric.engine.DoricJSEngine; import pub.doric.performance.DoricPerformanceProfile; @@ -71,10 +68,6 @@ import pub.doric.utils.DoricMetaInfo; * @CreateDate: 2019-07-20 */ public class DoricRegistry { - private static final Map bundles = new ConcurrentHashMap<>(); - private static final Set doricLibraries = new HashSet<>(); - private static final List> registries = new ArrayList<>(); - private static final Map envMap = new ConcurrentHashMap<>(); private final Map> pluginInfoMap = new HashMap<>(); private final Map> nodeInfoMap = new HashMap<>(); @@ -84,52 +77,13 @@ public class DoricRegistry { private Drawable defaultErrorDrawable = null; - private static boolean enablePerformance = false; - private static boolean enableRenderSnapshot = false; - - public static void enablePerformance(boolean enable) { - enablePerformance = enable; - } - - public static boolean isEnablePerformance() { - return enablePerformance; - } - - public static void enableRenderSnapshot(boolean enable) { - enableRenderSnapshot = enable; - } - - public static boolean isEnableRenderSnapshot() { - return enableRenderSnapshot; - } - - private static void initRegistry(DoricRegistry doricRegistry) { - for (DoricLibrary library : doricLibraries) { + private void initRegistry(DoricRegistry doricRegistry) { + for (DoricLibrary library : DoricSingleton.getInstance().doricLibraries) { library.load(doricRegistry); } } - public static void register(DoricLibrary doricLibrary) { - doricLibraries.add(doricLibrary); - for (WeakReference registryWeakReference : registries) { - DoricRegistry registry = registryWeakReference.get(); - if (registry != null) { - doricLibrary.load(registry); - } - } - } - - public static void setEnvironmentValue(Map value) { - envMap.putAll(value); - for (WeakReference registryWeakReference : registries) { - DoricRegistry registry = registryWeakReference.get(); - if (registry != null) { - registry.innerSetEnvironmentValue(value); - } - } - } - private final WeakReference doricJSEngineWeakReference; public DoricRegistry(DoricJSEngine doricJSEngine) { @@ -168,12 +122,12 @@ public class DoricRegistry { this.registerViewNode(SwitchNode.class); this.registerViewNode(FlexNode.class); initRegistry(this); - doricJSEngine.setEnvironmentValue(envMap); - registries.add(new WeakReference<>(this)); + doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap); + DoricSingleton.getInstance().registries.add(new WeakReference<>(this)); } public void registerJSBundle(String name, String bundle) { - bundles.put(name, bundle); + DoricSingleton.getInstance().bundles.put(name, bundle); } public void registerNativePlugin(Class pluginClass) { @@ -199,10 +153,10 @@ public class DoricRegistry { } public String acquireJSBundle(String name) { - return bundles.get(name); + return DoricSingleton.getInstance().bundles.get(name); } - private void innerSetEnvironmentValue(Map value) { + void innerSetEnvironmentValue(Map value) { DoricJSEngine doricJSEngine = doricJSEngineWeakReference.get(); if (doricJSEngine == null) { return; @@ -259,4 +213,7 @@ public class DoricRegistry { return globalPerformanceAnchorHook; } + public static void register(DoricLibrary doricLibrary) { + DoricSingleton.getInstance().registerLibrary(doricLibrary); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/DoricSingleton.java b/doric-android/doric/src/main/java/pub/doric/DoricSingleton.java new file mode 100644 index 00000000..b1c7b6e1 --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/DoricSingleton.java @@ -0,0 +1,94 @@ +/* + * Copyright [2021] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package pub.doric; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import pub.doric.loader.DoricJSLoaderManager; + +/** + * @Description: pub.doric + * @Author: pengfei.zhou + * @CreateDate: 2021/7/21 + */ +public class DoricSingleton { + + final Map bundles = new ConcurrentHashMap<>(); + final Set doricLibraries = new HashSet<>(); + final List> registries = new ArrayList<>(); + final Map envMap = new ConcurrentHashMap<>(); + + private final DoricJSLoaderManager jsLoaderManager = new DoricJSLoaderManager(); + + boolean enablePerformance = false; + + boolean enableRenderSnapshot = false; + private DoricNativeDriver nativeDriver; + private final DoricContextManager doricContextManager = new DoricContextManager(); + + private static class Inner { + private static final DoricSingleton sInstance = new DoricSingleton(); + } + + private DoricSingleton() { + } + + public static DoricSingleton getInstance() { + return Inner.sInstance; + } + + + public void registerLibrary(DoricLibrary doricLibrary) { + doricLibraries.add(doricLibrary); + for (WeakReference registryWeakReference : registries) { + DoricRegistry registry = registryWeakReference.get(); + if (registry != null) { + doricLibrary.load(registry); + } + } + } + + public void setEnvironmentValue(Map value) { + envMap.putAll(value); + for (WeakReference registryWeakReference : registries) { + DoricRegistry registry = registryWeakReference.get(); + if (registry != null) { + registry.innerSetEnvironmentValue(value); + } + } + } + + public DoricJSLoaderManager getJsLoaderManager() { + return jsLoaderManager; + } + + public DoricNativeDriver getNativeDriver() { + if (nativeDriver == null) { + nativeDriver = new DoricNativeDriver(); + } + return nativeDriver; + } + + public DoricContextManager getContextManager() { + return doricContextManager; + } +} diff --git a/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java b/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java index 666280d6..052871c3 100644 --- a/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java +++ b/doric-android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java @@ -32,17 +32,13 @@ import pub.doric.async.AsyncResult; */ public class DoricJSLoaderManager { - private Set jsLoaders = new HashSet<>(); + private final Set jsLoaders = new HashSet<>(); - private DoricJSLoaderManager() { + public DoricJSLoaderManager() { addJSLoader(new DoricAssetJSLoader()); addJSLoader(new DoricHttpJSLoader()); } - private static class Inner { - private static final DoricJSLoaderManager sInstance = new DoricJSLoaderManager(); - } - public void addJSLoader(IDoricJSLoader jsLoader) { jsLoaders.add(jsLoader); } @@ -51,10 +47,6 @@ public class DoricJSLoaderManager { return jsLoaders; } - public static DoricJSLoaderManager getInstance() { - return Inner.sInstance; - } - public AsyncResult loadJSBundle(String source) { if (!TextUtils.isEmpty(source)) { if (source.startsWith("_internal_://")) { diff --git a/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java b/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java index 25ec2358..d0b3ea50 100644 --- a/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java +++ b/doric-android/doric/src/main/java/pub/doric/performance/DoricPerformanceProfile.java @@ -17,14 +17,13 @@ package pub.doric.performance; import android.os.Handler; import android.os.HandlerThread; -import android.util.Log; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import pub.doric.DoricRegistry; +import pub.doric.Doric; /** * @Description: pub.doric.performance @@ -43,7 +42,7 @@ public class DoricPerformanceProfile { private static final String MARK_END = "end"; private final String name; - private boolean enable = DoricRegistry.isEnablePerformance(); + private boolean enable = Doric.isEnablePerformance(); private static final Handler performanceHandler; private final Set hooks = new HashSet<>(); diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 56b7cf02..24999955 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -20,6 +20,7 @@ // Created by jingpeng.wang on 2020/2/25. // #import +#import #import #import @@ -78,8 +79,8 @@ - (instancetype)init { if (self = [super init]) { _callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; - [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; - DoricNativeDriver.instance.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new]; + [DoricSingleton.instance.nativeDriver.registry registerMonitor:[DoricDevMonitor new]]; + DoricSingleton.instance.nativeDriver.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new]; } return self; } diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 8f2158b5..ec1f9531 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -125,7 +125,7 @@ - (void)onClick { [alertController addAction:startDebugging]; } } - if ([DoricRegistry isEnableRenderSnapshot]) { + if ([Doric isEnableRenderSnapshot]) { UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) { DoricSnapshotView *doricSnapshotView = [[DoricSnapshotView alloc] initWithDoricContext:self.doricContext]; doricSnapshotView.top = 50; @@ -135,7 +135,7 @@ - (void)onClick { [alertController addAction:snapshot]; } - if ([DoricRegistry isEnablePerformance]) { + if ([Doric isEnablePerformance]) { UIAlertAction *performanceAction = [UIAlertAction actionWithTitle:@"Performance" style:UIAlertActionStyleDefault @@ -290,11 +290,11 @@ - (void)viewDidLoad { } - (void)onSnapshotSwitch { - [DoricRegistry enableRenderSnapshot:self.switchSnapshot.isOn]; + [Doric enableRenderSnapshot:self.switchSnapshot.isOn]; } - (void)onPerformanceSwitch { - [DoricRegistry enablePerformance:self.switchPerformance.isOn]; + [Doric enablePerformance:self.switchPerformance.isOn]; } - (void)disconnect { @@ -385,13 +385,13 @@ - (void)initHeaders { self.switchSnapshot.left = self.tvSnapshot.right + 20; self.switchSnapshot.top = self.tvConnection.bottom + 15; self.tvSnapshot.centerY = self.switchSnapshot.centerY; - self.switchSnapshot.on = [DoricRegistry isEnableRenderSnapshot]; + self.switchSnapshot.on = [Doric isEnableRenderSnapshot]; self.tvPerformance.left = self.tvLabel.right + 20; self.switchPerformance.left = self.tvPerformance.right + 20; self.switchPerformance.top = self.switchSnapshot.bottom + 15; self.tvPerformance.centerY = self.switchPerformance.centerY; - self.switchPerformance.on = [DoricRegistry isEnablePerformance]; + self.switchPerformance.on = [Doric isEnablePerformance]; } - (void)initList { diff --git a/doric-iOS/Example/Example/AppDelegate.m b/doric-iOS/Example/Example/AppDelegate.m index 43165a66..adddec04 100644 --- a/doric-iOS/Example/Example/AppDelegate.m +++ b/doric-iOS/Example/Example/AppDelegate.m @@ -9,7 +9,7 @@ #import "AppDelegate.h" #import "NavigationController.h" #import "ViewController.h" -#import "DoricRegistry.h" +#import #if __has_include() @@ -27,7 +27,7 @@ @implementation AppDelegate - (void)localeChanged { - [DoricRegistry setEnvironmentValue:@{ + [Doric setEnvironmentValue:@{ @"localeLanguage": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode], @"localeCountry": [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode], }]; diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index 0a731f2e..7883185f 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -46,9 +46,9 @@ - (void)viewDidLoad { it.dataSource = self; it.delegate = self; }]]; - [DoricRegistry register:[DemoLibrary new]]; - [DoricRegistry enablePerformance:YES]; - [DoricRegistry enableRenderSnapshot:YES]; + [Doric registerLibrary:[DemoLibrary new]]; + [Doric enablePerformance:YES]; + [Doric enableRenderSnapshot:YES]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { diff --git a/doric-iOS/Pod/Classes/Doric.h b/doric-iOS/Pod/Classes/Doric.h index 8fbabafc..e45ebff6 100644 --- a/doric-iOS/Pod/Classes/Doric.h +++ b/doric-iOS/Pod/Classes/Doric.h @@ -49,4 +49,14 @@ * */ + (void)addJSLoader:(id )loader; + ++ (void)enablePerformance:(BOOL)enable; + ++ (BOOL)isEnablePerformance; + ++ (void)enableRenderSnapshot:(BOOL)enable; + ++ (BOOL)isEnableRenderSnapshot; + ++ (void)setEnvironmentValue:(NSDictionary *)value; @end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Doric.m b/doric-iOS/Pod/Classes/Doric.m index a3bfcace..e224d2d9 100644 --- a/doric-iOS/Pod/Classes/Doric.m +++ b/doric-iOS/Pod/Classes/Doric.m @@ -20,6 +20,7 @@ // Created by pengfei.zhou on 2020/2/28. // #import "Doric.h" +#import "DoricSingleton.h" @implementation Doric @@ -28,6 +29,26 @@ + (void)registerLibrary:(DoricLibrary *)library { } + (void)addJSLoader:(id )loader { - [[DoricJSLoaderManager instance] addJSLoader:loader]; + [DoricSingleton.instance.jsLoaderManager addJSLoader:loader]; +} + ++ (void)enablePerformance:(BOOL)enable { + DoricSingleton.instance.enablePerformance = enable; +} + ++ (BOOL)isEnablePerformance { + return DoricSingleton.instance.enablePerformance; +} + ++ (void)enableRenderSnapshot:(BOOL)enable { + DoricSingleton.instance.enableRecordSnapshot = enable; +} + ++ (BOOL)isEnableRenderSnapshot { + return DoricSingleton.instance.enableRecordSnapshot; +} + ++ (void)setEnvironmentValue:(NSDictionary *)value { + [DoricSingleton.instance setEnvironmentValue:value]; } @end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/DoricContext.m b/doric-iOS/Pod/Classes/DoricContext.m index 5d22738e..b32bb011 100644 --- a/doric-iOS/Pod/Classes/DoricContext.m +++ b/doric-iOS/Pod/Classes/DoricContext.m @@ -27,12 +27,13 @@ #import "DoricExtensions.h" #import "DoricNativeDriver.h" #import "DoricUtil.h" +#import "DoricSingleton.h" @implementation DoricContext - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extra:(NSString *)extra { if (self = [super init]) { - _driver = [DoricNativeDriver instance]; + _driver = DoricSingleton.instance.nativeDriver; _pluginInstanceMap = [NSMutableDictionary new]; _script = script; _source = source; @@ -84,7 +85,7 @@ - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray } - (void)init:(NSString *)initData { - if ([DoricRegistry isEnableRenderSnapshot]) { + if (DoricSingleton.instance.enableRecordSnapshot) { [self callEntity:@"__enableSnapshot__" withArgumentsArray:@[]]; } self.extra = initData; @@ -135,7 +136,6 @@ - (UIViewController *)vc { return _vc; } - - (void)dispatchToMainQueue:(_Nonnull dispatch_block_t)block { dispatch_async(dispatch_get_main_queue(), ^{ @try { diff --git a/doric-iOS/Pod/Classes/DoricContextManager.m b/doric-iOS/Pod/Classes/DoricContextManager.m index ba8fdc8d..dff83823 100644 --- a/doric-iOS/Pod/Classes/DoricContextManager.m +++ b/doric-iOS/Pod/Classes/DoricContextManager.m @@ -21,6 +21,7 @@ // #import "DoricContextManager.h" +#import "DoricSingleton.h" @interface DoricContextManager () @@ -43,12 +44,7 @@ - (instancetype)init { } + (instancetype)instance { - static DoricContextManager *_instance; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _instance = [[DoricContextManager alloc] init]; - }); - return _instance; + return DoricSingleton.instance.contextManager; } - (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source { diff --git a/doric-iOS/Pod/Classes/DoricNativeDriver.h b/doric-iOS/Pod/Classes/DoricNativeDriver.h index 72850603..b2c180cd 100644 --- a/doric-iOS/Pod/Classes/DoricNativeDriver.h +++ b/doric-iOS/Pod/Classes/DoricNativeDriver.h @@ -32,7 +32,6 @@ typedef NS_ENUM(NSInteger, DoricQueueMode) { NS_ASSUME_NONNULL_BEGIN @interface DoricNativeDriver : NSObject -+ (instancetype)instance; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricNativeDriver.m b/doric-iOS/Pod/Classes/DoricNativeDriver.m index ba9c42b2..1b0a186e 100644 --- a/doric-iOS/Pod/Classes/DoricNativeDriver.m +++ b/doric-iOS/Pod/Classes/DoricNativeDriver.m @@ -45,15 +45,6 @@ - (DoricRegistry *)registry { return self.jsExecutor.registry; } -+ (instancetype)instance { - static DoricNativeDriver *_instance; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _instance = [[DoricNativeDriver alloc] init]; - }); - return _instance; -} - - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args { id contextId = args.count > 0 ? args[0] : nil; DoricPerformanceProfile *profile = nil; diff --git a/doric-iOS/Pod/Classes/DoricRegistry.h b/doric-iOS/Pod/Classes/DoricRegistry.h index 1cbc1f2f..1f800976 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.h +++ b/doric-iOS/Pod/Classes/DoricRegistry.h @@ -40,7 +40,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name; - - (void)registerNativePlugin:(Class)pluginClass withName:(NSString *)name; - (Class)acquireNativePlugin:(NSString *)name; @@ -49,19 +48,12 @@ NS_ASSUME_NONNULL_BEGIN - (Class)acquireViewNode:(NSString *)name; -+ (void)setEnvironmentValue:(NSDictionary *)value; - - (void)registerMonitor:(id )monitor; +- (void)innerSetEnvironmentValue:(NSDictionary *)value; + + (void)register:(DoricLibrary *)library; -+ (void)enablePerformance:(BOOL)enable; - -+ (BOOL)isEnablePerformance; - -+ (void)enableRenderSnapshot:(BOOL)enable; - -+ (BOOL)isEnableRenderSnapshot; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 5550f9a3..06e6dfd2 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -54,39 +54,7 @@ #import "DoricFlexNode.h" #import "DoricKeyboardPlugin.h" #import "DoricJSEngine.h" - -@interface DoricLibraries : NSObject -@property(nonatomic, strong) NSMutableSet *libraries; -@property(nonatomic, strong) NSHashTable *registries; -@property(nonatomic, strong) NSMutableDictionary *envDic; -@property(nonatomic, assign) BOOL enablePerformance; -@property(nonatomic, assign) BOOL enableRecordSnapshot; - -+ (instancetype)instance; -@end - -@implementation DoricLibraries -- (instancetype)init { - if (self = [super init]) { - _libraries = [NSMutableSet new]; - _registries = [NSHashTable new]; - _envDic = [NSMutableDictionary new]; - _enablePerformance = NO; - _enableRecordSnapshot = NO; - } - return self; -} - -+ (instancetype)instance { - static DoricLibraries *_instance; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _instance = [[DoricLibraries alloc] init]; - }); - return _instance; -} - -@end +#import "DoricSingleton.h" @interface DoricRegistry () @@ -100,22 +68,14 @@ @interface DoricRegistry () @implementation DoricRegistry + (void)register:(DoricLibrary *)library { - [DoricLibraries.instance.libraries addObject:library]; - for (DoricRegistry *registry in DoricLibraries.instance.registries) { + [DoricSingleton.instance.libraries addObject:library]; + for (DoricRegistry *registry in DoricSingleton.instance.registries) { if (registry) { [library load:registry]; } } } -+ (void)setEnvironmentValue:(NSDictionary *)value { - [DoricLibraries.instance.envDic addEntriesFromDictionary:value]; - for (DoricRegistry *registry in DoricLibraries.instance.registries) { - if (registry) { - [registry innerSetEnvironmentValue:value]; - } - } -} - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine { if (self = [super init]) { @@ -125,31 +85,15 @@ - (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine { _nodes = [NSMutableDictionary new]; _monitors = [NSMutableSet new]; [self innerRegister]; - [DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { + [DoricSingleton.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [obj load:self]; }]; - [jsEngine setEnvironmentValue:DoricLibraries.instance.envDic]; - [DoricLibraries.instance.registries addObject:self]; + [jsEngine setEnvironmentValue:DoricSingleton.instance.envDic]; + [DoricSingleton.instance.registries addObject:self]; } return self; } -+ (void)enablePerformance:(BOOL)enable { - DoricLibraries.instance.enablePerformance = enable; -} - -+ (BOOL)isEnablePerformance { - return DoricLibraries.instance.enablePerformance; -} - -+ (void)enableRenderSnapshot:(BOOL)enable { - DoricLibraries.instance.enableRecordSnapshot = enable; -} - -+ (BOOL)isEnableRenderSnapshot { - return DoricLibraries.instance.enableRecordSnapshot; -} - - (void)innerRegister { [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; diff --git a/doric-iOS/Pod/Classes/DoricSingleton.h b/doric-iOS/Pod/Classes/DoricSingleton.h new file mode 100644 index 00000000..1addb973 --- /dev/null +++ b/doric-iOS/Pod/Classes/DoricSingleton.h @@ -0,0 +1,41 @@ +/* + * Copyright [2021] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2021/7/21. +// + +#import + +@class DoricLibrary; +@class DoricRegistry; +@class DoricJSLoaderManager; +@class DoricNativeDriver; +@class DoricContextManager; + +@interface DoricSingleton : NSObject +@property(nonatomic, strong) NSMutableSet *libraries; +@property(nonatomic, strong) NSHashTable *registries; +@property(nonatomic, strong) NSMutableDictionary *envDic; +@property(nonatomic, assign) BOOL enablePerformance; +@property(nonatomic, assign) BOOL enableRecordSnapshot; +@property(nonatomic, strong) DoricJSLoaderManager *jsLoaderManager; +@property(nonatomic, strong) DoricNativeDriver *nativeDriver; +@property(nonatomic, strong) DoricContextManager *contextManager; + ++ (instancetype)instance; + +- (void)setEnvironmentValue:(NSDictionary *)value; +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/DoricSingleton.m b/doric-iOS/Pod/Classes/DoricSingleton.m new file mode 100644 index 00000000..f2be2a34 --- /dev/null +++ b/doric-iOS/Pod/Classes/DoricSingleton.m @@ -0,0 +1,65 @@ +/* + * Copyright [2021] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2021/7/21. +// + +#import "DoricSingleton.h" +#import "DoricRegistry.h" +#import "DoricJSLoaderManager.h" +#import "DoricNativeDriver.h" +#import "DoricContextManager.h" + +@implementation DoricSingleton + +- (instancetype)init { + if (self = [super init]) { + _libraries = [NSMutableSet new]; + _registries = [NSHashTable new]; + _envDic = [NSMutableDictionary new]; + _enablePerformance = NO; + _enableRecordSnapshot = NO; + _jsLoaderManager = [DoricJSLoaderManager new]; + _contextManager = [DoricContextManager new]; + } + return self; +} + ++ (instancetype)instance { + static DoricSingleton *_instance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _instance = [DoricSingleton new]; + }); + return _instance; +} + +- (DoricNativeDriver *)nativeDriver { + if (!_nativeDriver) { + _nativeDriver = [DoricNativeDriver new]; + } + return _nativeDriver; +} + +- (void)setEnvironmentValue:(NSDictionary *)value { + [self.envDic addEntriesFromDictionary:value]; + for (DoricRegistry *registry in self.registries) { + if (registry) { + [registry innerSetEnvironmentValue:value]; + } + } +} +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/DoricViewController.m b/doric-iOS/Pod/Classes/DoricViewController.m index 8eeba616..35098745 100644 --- a/doric-iOS/Pod/Classes/DoricViewController.m +++ b/doric-iOS/Pod/Classes/DoricViewController.m @@ -22,6 +22,7 @@ #import "UIView+Doric.h" #import "DoricExtensions.h" #import "DoricUtil.h" +#import "DoricSingleton.h" NSString *const DORIC_MASK_RETRY = @"doric_mask_retry"; @@ -202,7 +203,7 @@ - (void)hideMask { - (void)loadJSBundle { [self showLoading]; - DoricAsyncResult *result = [DoricJSLoaderManager.instance request:self.source]; + DoricAsyncResult *result = [DoricSingleton.instance.jsLoaderManager request:self.source]; result.resultCallback = ^(NSString *result) { dispatch_async(dispatch_get_main_queue(), ^{ [self hideMask]; diff --git a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.h b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.h index 575acb94..198b73fb 100644 --- a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.h +++ b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.h @@ -25,8 +25,6 @@ #import "DoricAsyncResult.h" @interface DoricJSLoaderManager : NSObject -+ (instancetype)instance; - - (void)addJSLoader:(id )loader; - (DoricAsyncResult *)request:(NSString *)source; diff --git a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m index 0b288aa0..8e0a6df9 100644 --- a/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m +++ b/doric-iOS/Pod/Classes/Loader/DoricJSLoaderManager.m @@ -30,15 +30,6 @@ @interface DoricJSLoaderManager () @end @implementation DoricJSLoaderManager -+ (instancetype)instance { - static DoricJSLoaderManager *_instance; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - _instance = [DoricJSLoaderManager new]; - }); - return _instance; -} - - (instancetype)init { if (self = [super init]) { _loaders = [[NSSet alloc] initWithArray:@[ diff --git a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m index 14d29957..63fbd4d4 100644 --- a/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m +++ b/doric-iOS/Pod/Classes/Performance/DoricPerformanceProfile.m @@ -22,6 +22,7 @@ #import "DoricPerformanceProfile.h" #import "DoricRegistry.h" +#import "DoricSingleton.h" @interface DoricPerformanceProfile () @property(nonatomic, strong) dispatch_queue_t anchorQueue; @@ -36,7 +37,7 @@ - (instancetype)initWithName:(NSString *)name { _name = name; _anchorQueue = dispatch_queue_create("doric.performance.profile", DISPATCH_QUEUE_SERIAL); _anchorMap = [NSMutableDictionary new]; - _enable = [DoricRegistry isEnablePerformance]; + _enable = DoricSingleton.instance.enablePerformance; _hooks = [NSHashTable new]; } return self;