mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
feat: enhance local storage and webview APIs with improved error handling and resource management
This commit is contained in:
parent
f4306ad1c3
commit
bb0afa5a0c
@ -13,7 +13,7 @@ class PluginLocalStorageApi {
|
||||
required this.pluginName,
|
||||
}) {
|
||||
runtime.onMessage("LocalStorage.getItem", (args) {
|
||||
final key = args[0];
|
||||
final key = args[0]["key"];
|
||||
final value = getItem(key);
|
||||
runtime.evaluate(
|
||||
"""
|
||||
|
@ -70,4 +70,11 @@ class PluginSetIntervalApi {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
for (var timer in _timers.values) {
|
||||
timer.cancel();
|
||||
}
|
||||
_timers.clear();
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ class PluginTotpGenerator {
|
||||
|
||||
runtime.evaluate(
|
||||
"""
|
||||
eventEmitter.emit('Totp.generate', '$otp');
|
||||
eventEmitter.emit('TotpGenerator.generate', '$otp');
|
||||
""",
|
||||
);
|
||||
});
|
||||
|
@ -49,13 +49,17 @@ class PluginWebViewApi {
|
||||
}
|
||||
showWebView(
|
||||
url: args[0]["url"] as String,
|
||||
initialSettings: WebviewInitialSettings.fromJson(
|
||||
initialSettings: args[0]["initialSettings"] != null
|
||||
? WebviewInitialSettings.fromJson(
|
||||
args[0]["initialSettings"],
|
||||
),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Webview? webviewWindow;
|
||||
|
||||
Future showWebView({
|
||||
required String url,
|
||||
WebviewInitialSettings? initialSettings,
|
||||
@ -117,6 +121,8 @@ class PluginWebViewApi {
|
||||
),
|
||||
);
|
||||
|
||||
webviewWindow = webview;
|
||||
|
||||
runtime.onMessage("WebView.close", (args) {
|
||||
webview.close();
|
||||
});
|
||||
@ -134,7 +140,7 @@ class PluginWebViewApi {
|
||||
"domain": e.domain,
|
||||
"path": e.path,
|
||||
};
|
||||
});
|
||||
}).toList();
|
||||
|
||||
runtime.evaluate(
|
||||
"""
|
||||
@ -157,4 +163,9 @@ class PluginWebViewApi {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
webviewWindow?.close();
|
||||
webviewWindow = null;
|
||||
}
|
||||
}
|
||||
|
@ -61,19 +61,6 @@ class MetadataApiSignature {
|
||||
},
|
||||
);
|
||||
|
||||
final res = runtime.evaluate(
|
||||
"""
|
||||
;$libraryCode;
|
||||
const metadataApi = new MetadataApi();
|
||||
""",
|
||||
);
|
||||
|
||||
if (res.isError) {
|
||||
AppLogger.reportError(
|
||||
"Error evaluating code: $libraryCode\n${res.rawResult}",
|
||||
);
|
||||
}
|
||||
|
||||
// Create all the PluginAPIs after library code is evaluated
|
||||
final localStorageApi = PluginLocalStorageApi(
|
||||
runtime: runtime,
|
||||
@ -93,12 +80,26 @@ class MetadataApiSignature {
|
||||
setIntervalApi,
|
||||
);
|
||||
|
||||
final res = runtime.evaluate(
|
||||
"""
|
||||
;$libraryCode;
|
||||
const metadataApi = new MetadataApi();
|
||||
""",
|
||||
);
|
||||
metadataApi._signatureFlags = await metadataApi._getSignatureFlags();
|
||||
|
||||
if (res.isError) {
|
||||
AppLogger.reportError(
|
||||
"Error evaluating code: $libraryCode\n${res.rawResult}",
|
||||
);
|
||||
}
|
||||
|
||||
return metadataApi;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
setIntervalApi.dispose();
|
||||
webViewApi.dispose();
|
||||
runtime.dispose();
|
||||
}
|
||||
|
||||
@ -119,7 +120,7 @@ class MetadataApiSignature {
|
||||
$method(...${args != null ? jsonEncode(args) : "[]"})
|
||||
.then((res) => {
|
||||
try {
|
||||
sendMessage("$method", JSON.stringify(res));
|
||||
sendMessage("$method", res ? JSON.stringify(res) : "[]");
|
||||
} catch (e) {
|
||||
console.error("Failed to send message in $method.then: ", `\${e.toString()}\n\${e.stack.toString()}`);
|
||||
}
|
||||
@ -497,6 +498,12 @@ class MetadataApiSignature {
|
||||
}
|
||||
|
||||
// ----- User ------
|
||||
Future<SpotubeUserObject> getMe() async {
|
||||
final res = await invoke("metadataApi.getMe");
|
||||
|
||||
return SpotubeUserObject.fromJson(res);
|
||||
}
|
||||
|
||||
Future<void> followArtist(String userId, String artistId) async {
|
||||
await invoke("metadataApi.followArtist", [userId, artistId]);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user