Skip to content

Commit 92ab9f9

Browse files
committed
Refresh Interval made configurable.
1 parent c43efc1 commit 92ab9f9

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed

src/_constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const DEFAULT_CACHE: DictionaryCache = {
4747

4848
export const DEFAULT_SETTINGS: DictionarySettings = {
4949
defaultLanguage: "en_US",
50+
defaultRefreshInterval: "60",
5051
shouldShowSynonymPopover: true,
5152
shouldShowCustomContextMenu: false,
5253
definitionApiName: "Free Dictionary API",

src/l10n/locale/de.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default {
1717

1818
//settingsTab.ts
1919
"Reset to default": "Zurücksetzen",
20-
'Dictionary Settings': 'Wörterbuch Einstellungen',
20+
'HackerNews Settings': 'Wörterbuch Einstellungen',
21+
'Refresh Interval': 'Refresh Interval',
22+
'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.': 'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.',
2123
'Language': 'Sprache',
2224
'The Language the Plugin will use to search for Definitions and Pronunciations.': 'Die Sprache, welche von dieser Erweiterung verwendet wird, um nach Definitionen zu suchen.',
2325
'Synonym Suggestions': 'Synonym Vorschläge',

src/l10n/locale/en.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default {
1717

1818
//settingsTab.ts
1919
"Reset to default": "Reset to default",
20-
'Dictionary Settings': 'Dictionary Settings',
20+
'HackerNews Settings': 'HackerNews Settings',
21+
'Refresh Interval': 'Refresh Interval',
22+
'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.': 'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.',
2123
'Language': 'Language',
2224
'The Language the Plugin will use to search for Definitions and Pronunciations.': 'The Language the Plugin will use to search for Definitions and Pronunciations.',
2325
'Synonym Suggestions': 'Synonym Suggestions',

src/l10n/locale/zh-cn.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export default {
1515
'Look up': '查询',
1616

1717
//settingsTab.ts
18-
'Dictionary Settings': '词典设置',
18+
'HackerNews Settings': '词典设置',
19+
'Refresh Interval': 'Refresh Interval',
20+
'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.': 'The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.',
1921
'Language': '语言',
2022
'The Language the Plugin will use to search for Definitions and Pronunciations.': '插件会根据该语言来搜索语义以及发音',
2123
'Synonym Suggestions': '同义词建议',

src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ export default class DictionaryPlugin extends Plugin {
120120
// Editor mode
121121
// @ts-ignore
122122
this.registerEvent(this.app.workspace.on('editor-menu', this.handleContextMenuHelper));
123+
124+
let refreshInterval = parseInt(this.settings.defaultRefreshInterval)
125+
if (Number.isNaN(refreshInterval) || refreshInterval <= 0) { refreshInterval = 60 }
126+
127+
dispatchEvent(new Event('obsidian-hackernews-fetchTopHN'))
128+
this.registerInterval(window.setInterval(() => {
129+
dispatchEvent(new Event('obsidian-hackernews-fetchTopHN'))
130+
}, refreshInterval * 1000))
131+
132+
console.log('refreshInterval', refreshInterval, 'seconds');
123133
}
124134

125135
onunload(): void {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface DictionarySettings {
1515
suffix: string;
1616
prefix: string;
1717
template: string;
18+
defaultRefreshInterval: string;
1819
}
1920

2021
export interface DictionaryCache {

src/ui/dictionary/dictionaryView.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { onDestroy } from 'svelte';
23
import type APIManager from "src/apiManager";
34
import type { DictionaryWord, HNItem } from "src/integrations/types";
45
@@ -20,7 +21,8 @@
2021
}
2122
}
2223
23-
async function fetchTopHN() {
24+
export async function fetchTopHN() {
25+
console.log('fetching top story from HackerNews');
2426
dataHN = await manager.requestTopHN();
2527
}
2628
@@ -32,15 +34,17 @@
3234
search();
3335
});
3436
35-
addEventListener("obsidian-hackernews-fetchTopHN", () => {
36-
fetchTopHN();
37-
});
37+
addEventListener("obsidian-hackernews-fetchTopHN", fetchTopHN);
3838
3939
function handleKeyDown(e: KeyboardEvent) {
4040
if (e.key === "Enter") {
4141
search();
4242
}
4343
}
44+
45+
onDestroy(() => {
46+
removeEventListener('obsidian-hackernews-fetchTopHN', fetchTopHN)
47+
})
4448
</script>
4549

4650
<div class="main">

src/ui/dictionary/dictionaryView.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type DictionaryPlugin from "src/main";
22

3-
import { ItemView, WorkspaceLeaf, debounce } from "obsidian";
3+
import { ItemView, WorkspaceLeaf } from "obsidian";
44
import { VIEW_TYPE, VIEW_DISPLAY_TEXT, VIEW_ICON } from "src/_constants";
55
import DictionaryComponent from "./dictionaryView.svelte";
66
import LanguageChooser from "src/ui/modals/languageChooser";
@@ -9,7 +9,6 @@ export default class DictionaryView extends ItemView {
99

1010
plugin: DictionaryPlugin;
1111
private _view: DictionaryComponent;
12-
clock: NodeJS.Timeout;
1312

1413
constructor(leaf: WorkspaceLeaf, plugin: DictionaryPlugin) {
1514
super(leaf);
@@ -36,7 +35,6 @@ export default class DictionaryView extends ItemView {
3635
}
3736

3837
onClose(): Promise<void> {
39-
clearInterval(this.clock)
4038
this._view.$destroy();
4139
return super.onClose();
4240
}
@@ -49,9 +47,6 @@ export default class DictionaryView extends ItemView {
4947
localDictionary: this.plugin.localDictionary,
5048
}
5149
});
52-
this.clock = setInterval(debounce(() => {
53-
dispatchEvent(new Event('obsidian-hackernews-fetchTopHN'))
54-
}, 60 * 1000), 1000, true)
5550
addEventListener('dictionary-open-language-switcher', () => {
5651
new LanguageChooser(this.app, this.plugin).open();
5752
});

src/ui/settings/settingsTab.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ export default class SettingsTab extends PluginSettingTab {
1818

1919
containerEl.empty();
2020

21-
containerEl.createEl('h2', { text: t('Dictionary Settings') });
21+
containerEl.createEl('h2', { text: t('HackerNews Settings') });
2222

23+
new Setting(containerEl)
24+
.setName(t('Refresh Interval'))
25+
.setDesc(t('The time interval in seconds after which the next top story will be fetched. Default and invalid values will be considered as 60 seconds.'))
26+
.addText(text => text
27+
.setPlaceholder('60')
28+
.setValue(plugin.settings.defaultRefreshInterval)
29+
.onChange(async (value) => {
30+
plugin.settings.defaultRefreshInterval = value;
31+
await this.save();
32+
}));
2333
new Setting(containerEl)
2434
.setName(t('Language'))
2535
.setDesc(t('The Language the Plugin will use to search for Definitions and Pronunciations.'))

0 commit comments

Comments
 (0)