Skip to content

Commit

Permalink
Merge pull request #48 from rolandbernard/devel
Browse files Browse the repository at this point in the history
Improved refresh logic and increased version number
  • Loading branch information
rolandbernard committed Aug 16, 2021
2 parents 4d575f1 + 407749d commit cb5314d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/common/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Module<ModuleResult extends Result> {
deinit?: () => Promise<void>;

search?: (query: Query) => Promise<ModuleResult[]>;
refresh?: () => Promise<unknown>;
rebuild?: (query: Query, result: ModuleResult) => Promise<ModuleResult | undefined>;
execute?: (result: ModuleResult) => Promise<void>;
executeAny?: (result: Result) => Promise<void>;
Expand Down
7 changes: 4 additions & 3 deletions src/main/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function filterAndSortQueryResults(results: Result[]): Result[] {

export async function searchQuery(query: Query, callback?: (results: Result[]) => unknown): Promise<Result[]> {
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) {
Expand All @@ -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 */ });
}

17 changes: 8 additions & 9 deletions src/main/modules/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ipcMain.on(IpcChannels.REFRESH_APPLICATIONS, () => {
export class ApplicationsModule implements Module<ApplicationsResult> {
readonly configs = ApplicationsConfig;

refresh?: NodeJS.Timer;
last_load?: number;

get config() {
return moduleConfig<ApplicationsConfig>(MODULE_ID);
Expand All @@ -60,20 +60,19 @@ export class ApplicationsModule implements Module<ApplicationsResult> {

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, string>): string | undefined {
Expand Down Expand Up @@ -115,7 +114,7 @@ export class ApplicationsModule implements Module<ApplicationsResult> {
return [];
}
}

async rebuild(query: Query, result: ApplicationsResult): Promise<ApplicationsResult | undefined> {
const application = (await getAllApplications()).find(app => app.id === result.app_id);
return application && this.itemForApplication(query, application);
Expand Down
23 changes: 20 additions & 3 deletions src/main/modules/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -45,12 +49,25 @@ export class BookmarkModule implements Module<BookmarkResult> {
get config() {
return moduleConfig<BookmarkConfig>(MODULE_ID);
}

async init() {
if (this.config.active) {
this.refresh();
}
}

async search(query: Query): Promise<BookmarkResult[]> {
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<BookmarkResult[]> {
if (query.text.length > 0) {
const match = query.matchAny(getAllTranslations('bookmarks'), getTranslation('bookmarks', config));
return (await getAllBookmarks()).map(bookmark => ({
Expand Down
15 changes: 12 additions & 3 deletions src/main/modules/currency-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ export class CurrencyConverterModule implements Module<SimpleResult> {
return moduleConfig<CurrencyConfig>(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();
}
}

Expand All @@ -97,7 +107,6 @@ export class CurrencyConverterModule implements Module<SimpleResult> {
async itemForQuery(query: Query, text: string, raw?: string): Promise<SimpleResult | undefined> {
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 {
Expand Down
29 changes: 23 additions & 6 deletions src/main/modules/windows.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,6 +17,9 @@ interface WindowsResult extends SimpleResult {
}

class WindowsConfig extends ModuleConfig {
@configKind('time')
refresh_interval_min = time(1, TimeUnit.SECOND);

constructor() {
super(true);
}
Expand All @@ -29,6 +33,23 @@ export class WindowsModule implements Module<WindowsResult> {

get config() {
return moduleConfig<WindowsConfig>(MODULE_ID);
}

async init() {
if (this.config.active) {
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 {
Expand All @@ -49,10 +70,6 @@ export class WindowsModule implements Module<WindowsResult> {
}

async search(query: Query): Promise<WindowsResult[]> {
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 {
Expand Down

0 comments on commit cb5314d

Please sign in to comment.