From 5db759150cc2bcdca14654d5494f3451cfa9fad9 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Wed, 29 Nov 2023 01:27:51 +0900 Subject: [PATCH] feat: use '.call' instead of '.bind' --- src/index.ts | 2 +- src/renderer.ts | 6 +++--- src/utils/index.ts | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 31f443af..414c20f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,7 +165,7 @@ const initHook = (win: BrowserWindow) => { const mainPlugin = getAllLoadedMainPlugins()[id]; if (mainPlugin) { if (config.enabled && typeof mainPlugin.backend !== 'function') { - mainPlugin.backend?.onConfigChange?.bind(mainPlugin.backend)?.(config); + mainPlugin.backend?.onConfigChange?.call(mainPlugin.backend, config); } } diff --git a/src/renderer.ts b/src/renderer.ts index c70db501..84e74bdc 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -55,7 +55,7 @@ async function onApiLoaded() { for (const [id, plugin] of Object.entries(getAllLoadedRendererPlugins())) { if (typeof plugin.renderer !== 'function') { - await plugin.renderer?.onPlayerApiReady?.bind(plugin.renderer)?.(api!, createContext(id)); + await plugin.renderer?.onPlayerApiReady?.call(plugin.renderer, api!, createContext(id)); } } @@ -142,7 +142,7 @@ async function onApiLoaded() { if (api) { const plugin = getLoadedRendererPlugin(id); if (plugin && typeof plugin.renderer !== 'function') { - plugin.renderer?.onPlayerApiReady?.bind(plugin.renderer)?.(api, createContext(id)); + plugin.renderer?.onPlayerApiReady?.call(plugin.renderer, api, createContext(id)); } } }, @@ -153,7 +153,7 @@ async function onApiLoaded() { (_event, id: string, newConfig: PluginConfig) => { const plugin = getAllLoadedRendererPlugins()[id]; if (plugin && typeof plugin.renderer !== 'function') { - plugin.renderer?.onConfigChange?.bind(plugin.renderer)?.(newConfig); + plugin.renderer?.onConfigChange?.call(plugin.renderer, newConfig); } }, ); diff --git a/src/utils/index.ts b/src/utils/index.ts index f3456c6e..7d9ee5cb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -85,8 +85,6 @@ export const startPlugin = ( > )?.start; - if (!lifecycle) return null; - try { // HACK: for bind 'this' to context const defContext = def[options.ctx]; @@ -100,7 +98,9 @@ export const startPlugin = ( } const start = performance.now(); - lifecycle.bind(def[options.ctx])( + + lifecycle?.call( + defContext, options.context as Config & typeof options.context, ); @@ -110,7 +110,7 @@ export const startPlugin = ( } ms`, ); - return true; + return lifecycle ? true : null; } catch (err) { console.error(LoggerPrefix, `Failed to start ${id}::${options.ctx}`); console.trace(err); @@ -132,7 +132,8 @@ export const stopPlugin = ( try { const stop = defCtx.stop; const start = performance.now(); - stop.bind(def[options.ctx])( + stop.call( + def[options.ctx], options.context as Config & typeof options.context, );