Skip to content

Commit c43efc1

Browse files
committed
fetching top story periodically.
- clearing interval when view is destroyed - debouncing the API calls
1 parent 42d8730 commit c43efc1

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/apiManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ export default class APIManager {
107107
return Promise.reject(error);
108108
}
109109

110-
const itemId = itemIds[Math.floor(Math.random() * itemIds.slice(0, 15).length)]
110+
const itemId = itemIds[Math.floor(Math.random() * itemIds.slice(0, 25).length)]
111111
const itemResponse = await fetch(`https://hacker-news.firebaseio.com/v0/item/${itemId}.json?print=pretty`);
112112
const hnItem = (await itemResponse.json()) as HNItem
113-
113+
114114
return hnItem;
115115
}
116116

src/ui/dictionary/dictionaryView.svelte

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
1313
export let query: string = "";
1414
let promise: Promise<DictionaryWord>;
15-
let promiseTopHN: Promise<HNItem>;
15+
let dataHN: HNItem;
1616
1717
function search() {
1818
if (query.trim()) {
1919
promise = manager.requestDefinitions(query);
2020
}
2121
}
2222
23-
function fetchTopHN() {
24-
promiseTopHN = manager.requestTopHN();
23+
async function fetchTopHN() {
24+
dataHN = await manager.requestTopHN();
2525
}
2626
2727
function languageModal() {
@@ -44,22 +44,16 @@
4444
</script>
4545

4646
<div class="main">
47-
{#if promiseTopHN}
48-
{#await promiseTopHN}
49-
<div class="center" />
50-
{:then data}
51-
<div class="results">
52-
<div class="container">
53-
<a href="{ data.url }" target="_blank" class="hn-link">{ data.title }</a>
54-
<br />
55-
<p class="hn-read">
56-
<a href="{ data.url }" target="_blank">Read more →</a>
57-
</p>
58-
</div>
47+
{#if dataHN}
48+
<div class="results">
49+
<div class="container">
50+
<a href="{ dataHN.url }" target="_blank" class="hn-link">{ dataHN.title }</a>
51+
<br />
52+
<p class="hn-read">
53+
<a href="{ dataHN.url }" target="_blank">Read more →</a>
54+
</p>
5955
</div>
60-
{:catch error}
61-
<ErrorComponent {error} />
62-
{/await}
56+
</div>
6357
{/if}
6458
</div>
6559

src/ui/dictionary/dictionaryView.ts

Lines changed: 6 additions & 2 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 } from "obsidian";
3+
import { ItemView, WorkspaceLeaf, debounce } 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,6 +9,7 @@ export default class DictionaryView extends ItemView {
99

1010
plugin: DictionaryPlugin;
1111
private _view: DictionaryComponent;
12+
clock: NodeJS.Timeout;
1213

1314
constructor(leaf: WorkspaceLeaf, plugin: DictionaryPlugin) {
1415
super(leaf);
@@ -35,6 +36,7 @@ export default class DictionaryView extends ItemView {
3536
}
3637

3738
onClose(): Promise<void> {
39+
clearInterval(this.clock)
3840
this._view.$destroy();
3941
return super.onClose();
4042
}
@@ -47,7 +49,9 @@ export default class DictionaryView extends ItemView {
4749
localDictionary: this.plugin.localDictionary,
4850
}
4951
});
50-
dispatchEvent(new Event("obsidian-hackernews-fetchTopHN"));
52+
this.clock = setInterval(debounce(() => {
53+
dispatchEvent(new Event('obsidian-hackernews-fetchTopHN'))
54+
}, 60 * 1000), 1000, true)
5155
addEventListener('dictionary-open-language-switcher', () => {
5256
new LanguageChooser(this.app, this.plugin).open();
5357
});

0 commit comments

Comments
 (0)