From 183d580788b6dd8d195311daabe7bd2c8c83e561 Mon Sep 17 00:00:00 2001 From: Roland Bernard Date: Sun, 15 Aug 2021 18:19:56 +0100 Subject: [PATCH 1/3] Changed async refresh logic --- src/common/module.ts | 1 + src/main/executor.ts | 7 ++++--- src/main/modules/applications.ts | 17 +++++++-------- src/main/modules/bookmarks.ts | 23 +++++++++++++++++--- src/main/modules/currency-converter.ts | 15 ++++++++++--- src/main/modules/windows.ts | 29 ++++++++++++++++++++------ 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/common/module.ts b/src/common/module.ts index 4d86f0e..2018d0b 100644 --- a/src/common/module.ts +++ b/src/common/module.ts @@ -11,6 +11,7 @@ export interface Module { deinit?: () => Promise; search?: (query: Query) => Promise; + refresh?: () => Promise; rebuild?: (query: Query, result: ModuleResult) => Promise; execute?: (result: ModuleResult) => Promise; executeAny?: (result: Result) => Promise; diff --git a/src/main/executor.ts b/src/main/executor.ts index 6783951..814c694 100644 --- a/src/main/executor.ts +++ b/src/main/executor.ts @@ -63,9 +63,9 @@ function filterAndSortQueryResults(results: Result[]): Result[] { export async function searchQuery(query: Query, callback?: (results: Result[]) => unknown): Promise { let results: Result[] = []; - const modules = findPossibleModules(query); + const possible_modules = findPossibleModules(query); await Promise.all( - modules.map(async id => { + possible_modules.map(async id => { const result = await searchQueryInModule(id, query); results.push(...result); if (callback) { @@ -74,14 +74,15 @@ export async function searchQuery(query: Query, callback?: (results: Result[]) = } }) ); + possible_modules.map(id => modules[id].refresh?.().catch(() => { /* Ignore errors */ })); return filterAndSortQueryResults(results); } export async function executeResult(result: Result) { + await moduleForId(result.module)?.execute?.(result).catch(() => { /* Ignore errors */ }); await Promise.all( Object.values(modules) .map(module => module.executeAny?.(result).catch(() => { /* Ignore errors */ })) ); - await moduleForId(result.module)?.execute?.(result).catch(() => { /* Ignore errors */ }); } diff --git a/src/main/modules/applications.ts b/src/main/modules/applications.ts index 6b28b5a..3900e07 100644 --- a/src/main/modules/applications.ts +++ b/src/main/modules/applications.ts @@ -48,7 +48,7 @@ ipcMain.on(IpcChannels.REFRESH_APPLICATIONS, () => { export class ApplicationsModule implements Module { readonly configs = ApplicationsConfig; - refresh?: NodeJS.Timer; + last_load?: number; get config() { return moduleConfig(MODULE_ID); @@ -60,20 +60,19 @@ export class ApplicationsModule implements Module { async init() { if (this.config.active) { - this.refreshApplications(); - this.refresh = setInterval(() => { - this.refreshApplications(); - }, this.config.refresh_interval_min); + this.refresh(); } } async update() { - await this.deinit(); await this.init(); } - async deinit() { - clearInterval(this.refresh!); + async refresh() { + if (!this.last_load || (Date.now() - this.last_load) > this.config.refresh_interval_min) { + this.last_load = Date.now(); + await this.refreshApplications(); + } } forLanguage(names?: Record): string | undefined { @@ -115,7 +114,7 @@ export class ApplicationsModule implements Module { return []; } } - + async rebuild(query: Query, result: ApplicationsResult): Promise { const application = (await getAllApplications()).find(app => app.id === result.app_id); return application && this.itemForApplication(query, application); diff --git a/src/main/modules/bookmarks.ts b/src/main/modules/bookmarks.ts index 34fe00c..1bb7e8b 100644 --- a/src/main/modules/bookmarks.ts +++ b/src/main/modules/bookmarks.ts @@ -4,6 +4,7 @@ import { Query } from 'common/query'; import { SimpleResult } from 'common/result'; import { Module } from 'common/module'; import { getAllTranslations, getTranslation } from 'common/local/locale'; +import { time, TimeUnit } from 'common/time'; import { module } from 'main/modules'; import { config, moduleConfig } from 'main/config'; @@ -19,6 +20,9 @@ interface BookmarkResult extends SimpleResult { } class BookmarkConfig extends ModuleConfig { + @configKind('time') + refresh_interval_min = time(1, TimeUnit.SECOND); + @configKind('quality') default_quality = 0; @@ -45,12 +49,25 @@ export class BookmarkModule implements Module { get config() { return moduleConfig(MODULE_ID); } + + async init() { + if (this.config.active) { + await this.refresh(); + } + } - async search(query: Query): Promise { - if (!this.last_load || (Date.now() - this.last_load) > 1000) { - updateBookmarkCache(this.config.chromium_directories, this.config.firefox_directories); + async update() { + await this.init(); + } + + async refresh() { + if (!this.last_load || (Date.now() - this.last_load) > this.config.refresh_interval_min) { this.last_load = Date.now(); + await updateBookmarkCache(this.config.chromium_directories, this.config.firefox_directories); } + } + + async search(query: Query): Promise { if (query.text.length > 0) { const match = query.matchAny(getAllTranslations('bookmarks'), getTranslation('bookmarks', config)); return (await getAllBookmarks()).map(bookmark => ({ diff --git a/src/main/modules/currency-converter.ts b/src/main/modules/currency-converter.ts index c83ce31..6313c65 100644 --- a/src/main/modules/currency-converter.ts +++ b/src/main/modules/currency-converter.ts @@ -69,12 +69,22 @@ export class CurrencyConverterModule implements Module { return moduleConfig(MODULE_ID); } - async loadCurrencyRates() { + async init() { + if (this.config.active) { + this.refresh(); + } + } + + async update() { + await this.init(); + } + + async refresh() { if (!this.last_rate || !this.rates || Date.now() - this.last_rate > this.config.refresh_interval_min) { + this.last_rate = Date.now(); const response = await fetch(`${API_ROOT}/latest`); const json = await response.json(); this.rates = json.rates; - this.last_rate = Date.now(); } } @@ -97,7 +107,6 @@ export class CurrencyConverterModule implements Module { async itemForQuery(query: Query, text: string, raw?: string): Promise { const { value, from, to } = this.parseQuery(text); if (value !== undefined && from && to) { - await this.loadCurrencyRates(); const round = Math.pow(10, this.config.round_to); const result = Math.round(value / this.rates![from] * this.rates![to] * round) / round; return { diff --git a/src/main/modules/windows.ts b/src/main/modules/windows.ts index 9c343db..a87193a 100644 --- a/src/main/modules/windows.ts +++ b/src/main/modules/windows.ts @@ -1,9 +1,10 @@ -import { ModuleConfig } from 'common/config'; +import { ModuleConfig, configKind } from 'common/config'; import { Query } from 'common/query'; import { SimpleResult } from 'common/result'; -import { Module } from 'common/module'; +import { time, TimeUnit } from 'common/time'; +import { Module } from 'common/module'; import { moduleConfig } from 'main/config'; import { module } from 'main/modules'; import { focusWindow, openWindows, updateWindowCache, Window } from 'main/adapters/windows'; @@ -16,6 +17,9 @@ interface WindowsResult extends SimpleResult { } class WindowsConfig extends ModuleConfig { + @configKind('time') + refresh_interval_min = time(1, TimeUnit.SECOND); + constructor() { super(true); } @@ -29,6 +33,23 @@ export class WindowsModule implements Module { get config() { return moduleConfig(MODULE_ID); + } + + async init() { + if (this.config.active) { + await this.refresh(); + } + } + + async update() { + await this.init(); + } + + async refresh() { + if (!this.last_load || (Date.now() - this.last_load) > this.config.refresh_interval_min) { + this.last_load = Date.now(); + await updateWindowCache(); + } } itemForWindow(query: Query, window: Window): WindowsResult { @@ -49,10 +70,6 @@ export class WindowsModule implements Module { } async search(query: Query): Promise { - if (!this.last_load || (Date.now() - this.last_load) > 1000) { - updateWindowCache(); - this.last_load = Date.now(); - } if (query.text.length > 0) { return (await openWindows()).map(window => this.itemForWindow(query, window)); } else { From 85d3cd67c499b4ca3fc6496f541c2b49830471e1 Mon Sep 17 00:00:00 2001 From: Roland Bernard Date: Mon, 16 Aug 2021 19:53:28 +0200 Subject: [PATCH 2/3] Increased version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a9789c9..9667db7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "marvin", - "version": "0.3.0", + "version": "0.4.0", "description": "This is a keystroke launcher for Linux", "repository": "https://github.com/rolandbernard/marvin/", "author": "Roland Bernard", From 407749dac7ab3dfad6d6838c40de8e4c1844f297 Mon Sep 17 00:00:00 2001 From: Roland Bernard Date: Mon, 16 Aug 2021 19:58:00 +0200 Subject: [PATCH 3/3] Removed init refresh awaiting --- src/main/modules/bookmarks.ts | 2 +- src/main/modules/windows.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/modules/bookmarks.ts b/src/main/modules/bookmarks.ts index 1bb7e8b..c3daabe 100644 --- a/src/main/modules/bookmarks.ts +++ b/src/main/modules/bookmarks.ts @@ -52,7 +52,7 @@ export class BookmarkModule implements Module { async init() { if (this.config.active) { - await this.refresh(); + this.refresh(); } } diff --git a/src/main/modules/windows.ts b/src/main/modules/windows.ts index a87193a..24b0949 100644 --- a/src/main/modules/windows.ts +++ b/src/main/modules/windows.ts @@ -37,7 +37,7 @@ export class WindowsModule implements Module { async init() { if (this.config.active) { - await this.refresh(); + this.refresh(); } }