Skip to content

Commit

Permalink
New Async UpdateConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
chen08209 committed May 4, 2024
1 parent ecd1bca commit 74c3d0a
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 55 deletions.
6 changes: 1 addition & 5 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ func patchConfig(general *config.General) {
resolver.DisableIPv6 = !general.IPv6
}

func applyConfig(isPatch bool) bool {
if currentConfig == nil {
return false
}
func applyConfig(isPatch bool) {
cfg, err := config.ParseRawConfig(currentConfig)
if err != nil {
cfg, _ = config.ParseRawConfig(config.DefaultRawConfig())
Expand All @@ -177,5 +174,4 @@ func applyConfig(isPatch bool) bool {
} else {
executor.ApplyConfig(cfg, true)
}
return true
}
2 changes: 1 addition & 1 deletion core/dart-bridge/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func InitDartApi(api unsafe.Pointer) {
}
}

func sendToPort(port int64, msg string) {
func SendToPort(port int64, msg string) {
var obj C.Dart_CObject
obj._type = C.Dart_CObject_kString
msgString := C.CString(msg)
Expand Down
2 changes: 1 addition & 1 deletion core/dart-bridge/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (message *Message) toJson() string {
}

func SendMessage(message Message) {
sendToPort(*Port, message.toJson())
SendToPort(*Port, message.toJson())
}
34 changes: 19 additions & 15 deletions core/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,25 @@ func validateConfig(s *C.char) bool {
}

//export updateConfig
func updateConfig(s *C.char) bool {
paramsString := C.GoString(s)
var params = &GenerateConfigParams{}
err := json.Unmarshal([]byte(paramsString), params)
if err != nil {
log.Errorln("generateConfig Unmarshal error %v", err)
return false
}
prof := decorationConfig(params.ProfilePath, *params.Config)
currentConfig = prof
if *params.IsPatch {
return applyConfig(true)
} else {
return applyConfig(false)
}
func updateConfig(s *C.char, port C.longlong) {
i := int64(port)
go func() {
paramsString := C.GoString(s)
var params = &GenerateConfigParams{}
err := json.Unmarshal([]byte(paramsString), params)
if err != nil {
bridge.SendToPort(i, err.Error())
return
}
prof := decorationConfig(params.ProfilePath, *params.Config)
currentConfig = prof
if *params.IsPatch {
applyConfig(true)
} else {
applyConfig(false)
}
bridge.SendToPort(i, "")
}()
}

//export getProxies
Expand Down
1 change: 0 additions & 1 deletion lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class ApplicationState extends State<Application> {
primaryColor: config.primaryColor,
),
builder: (_, state, child) {
debugPrint("[Application] update===>");
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
_updateSystemColorSchemes(lightDynamic, darkDynamic);
Expand Down
20 changes: 14 additions & 6 deletions lib/clash/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ClashCore {
late final DynamicLibrary lib;

DynamicLibrary _getClashLib() {
debugPrint("OpenClash");
if (Platform.isWindows) {
return DynamicLibrary.open("libclash.dll");
}
Expand Down Expand Up @@ -67,12 +66,21 @@ class ClashCore {
1;
}

bool updateConfig(UpdateConfigParams updateConfigParams) {
Future<String> updateConfig(UpdateConfigParams updateConfigParams) {
final completer = Completer<String>();
final receiver = ReceivePort();
receiver.listen((message) {
if(!completer.isCompleted){
completer.complete(message);
receiver.close();
}
});
final params = json.encode(updateConfigParams);
return clashFFI.updateConfig(
params.toNativeUtf8().cast(),
) ==
1;
clashFFI.updateConfig(
params.toNativeUtf8().cast(),
receiver.sendPort.nativePort,
);
return completer.future;
}

Future<List<Group>> getProxiesGroups() {
Expand Down
13 changes: 8 additions & 5 deletions lib/clash/generated/clash_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -907,19 +907,22 @@ class ClashFFI {
late final _validateConfig =
_validateConfigPtr.asFunction<int Function(ffi.Pointer<ffi.Char>)>();

int updateConfig(
void updateConfig(
ffi.Pointer<ffi.Char> s,
int port,
) {
return _updateConfig(
s,
port,
);
}

late final _updateConfigPtr =
_lookup<ffi.NativeFunction<GoUint8 Function(ffi.Pointer<ffi.Char>)>>(
'updateConfig');
late final _updateConfigPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(
ffi.Pointer<ffi.Char>, ffi.LongLong)>>('updateConfig');
late final _updateConfig =
_updateConfigPtr.asFunction<int Function(ffi.Pointer<ffi.Char>)>();
_updateConfigPtr.asFunction<void Function(ffi.Pointer<ffi.Char>, int)>();

ffi.Pointer<ffi.Char> getProxies() {
return _getProxies();
Expand Down
14 changes: 6 additions & 8 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ class AppController {
}
}

Future<bool> updateClashConfig({bool isPatch = true}) async {
Future<String> updateClashConfig({bool isPatch = true}) async {
return await globalState.updateClashConfig(
clashConfig: clashConfig,
config: config,
isPatch: isPatch,
);
}

applyProfile() {
globalState.applyProfile(
applyProfile() async {
await globalState.applyProfile(
appState: appState,
config: config,
clashConfig: clashConfig,
Expand All @@ -139,11 +139,9 @@ class AppController {
if (profileId == config.currentProfileId) return;
config.currentProfileId = profileId;
_changeProfileDebounce ??= debounce<Function(String?)>((profileId) async {
if (context.mounted) {
await applyProfile();
appState.delayMap = {};
saveConfigPreferences();
}
await applyProfile();
appState.delayMap = {};
saveConfigPreferences();
});
_changeProfileDebounce!([profileId]);
}
Expand Down
1 change: 0 additions & 1 deletion lib/fragments/dashboard/core_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class CoreInfo extends StatelessWidget {
return Selector<AppState, VersionInfo?>(
selector: (_, appState) => appState.versionInfo,
builder: (_, versionInfo, __) {
debugPrint("[CoreInfo] update===>");
return CommonCard(
info: Info(
label: appLocalizations.coreInfo,
Expand Down
2 changes: 0 additions & 2 deletions lib/fragments/dashboard/network_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class _NetworkDetectionState extends State<NetworkDetection> {
);
},
builder: (_, state, __) {
debugPrint("[UpdateCurrentDelay] update===>");
_updateCurrentDelay(
state.currentProxyName,
state.delay,
Expand Down Expand Up @@ -145,7 +144,6 @@ class _NetworkDetectionState extends State<NetworkDetection> {
);
},
builder: (_, state, __) {
debugPrint("[NetworkDetection] update===>");
return Container(
padding: const EdgeInsets.all(16).copyWith(top: 0),
child: Column(
Expand Down
1 change: 0 additions & 1 deletion lib/fragments/dashboard/start_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class _StartButtonState extends State<StartButton>
hasProfile: config.profiles.isNotEmpty,
),
builder: (_, state, child) {
debugPrint("[StartButton] update===>");
if (!state.isInit || !state.hasProfile) {
return Container();
}
Expand Down
1 change: 0 additions & 1 deletion lib/fragments/profiles/profiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class _ProfilesFragmentState extends State<ProfilesFragment> {
currentProfileId: config.currentProfileId,
),
builder: (context, state, child) {
debugPrint("[Profiles] update===>");
if (state.profiles.isEmpty) {
return NullStatus(
label: appLocalizations.nullProfileDesc,
Expand Down
1 change: 0 additions & 1 deletion lib/fragments/proxies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class _ProxiesFragmentState extends State<ProxiesFragment>
vsync: this,
initialIndex: state.currentIndex,
);
debugPrint("[Proxies] update===>");
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down
1 change: 0 additions & 1 deletion lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class HomePage extends StatelessWidget {
child: Selector<AppState, List<NavigationItem>>(
selector: (_, appState) => appState.navigationItems,
builder: (_, navigationItems, __) {
debugPrint("[Home] update===>");
final desktopNavigationItems = navigationItems
.where(
(element) =>
Expand Down
5 changes: 3 additions & 2 deletions lib/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GlobalState {
timer?.cancel();
}

Future<bool> updateClashConfig({
Future<String> updateClashConfig({
required ClashConfig clashConfig,
required Config config,
bool isPatch = true,
Expand Down Expand Up @@ -92,11 +92,12 @@ class GlobalState {
required Config config,
required ClashConfig clashConfig,
}) async {
await updateClashConfig(
final res = await updateClashConfig(
clashConfig: clashConfig,
config: config,
isPatch: false,
);
if (res.isNotEmpty) return Result.error(message: res);
await updateGroups(appState);
changeProxy(
appState: appState,
Expand Down
2 changes: 0 additions & 2 deletions lib/widgets/app_state_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class AppStateContainer extends StatelessWidget {
return Selector<Config, bool>(
selector: (_, config) => config.autoLaunch,
builder: (_, isAutoLaunch, child) {
debugPrint("[autoLaunchContainer] update===>");
autoLaunch?.updateStatus(isAutoLaunch);
return child!;
},
Expand All @@ -35,7 +34,6 @@ class AppStateContainer extends StatelessWidget {
);
},
builder: (context, state, child) {
debugPrint("[NavigationsContainer] update===>");
WidgetsBinding.instance.addPostFrameCallback(
(_) {
context.appController.appState.navigationItems =
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/open_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ class _OpenContainerRoute<T> extends ModalRoute<T> {
return Selector<Config, ThemeMode>(
selector: (_, config) => config.themeMode,
builder: (_, __, ___) {
debugPrint("[OpenContainerTheme] update===>");
_colorTween = _getColorTween(
transitionType: transitionType,
closedColor: Theme.of(context).colorScheme.background,
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/tray_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ class _TrayContainerState extends State<TrayContainer> with TrayListener {
locale: config.locale,
),
builder: (_, state, child) {
debugPrint("[TrayContainer] update===>");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updateMenu(state);
});
Expand Down

0 comments on commit 74c3d0a

Please sign in to comment.