Skip to content

Commit b917b07

Browse files
committed
fix: reset background sync job id after plugin reload
1 parent efcfd19 commit b917b07

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/index.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface Settings {
4444
articleTemplate: string
4545
highlightTemplate: string
4646
loading: boolean
47+
syncJobId: number
4748
}
4849

4950
const siteNameFromUrl = (originalArticleUrl: string): string => {
@@ -402,13 +403,11 @@ const fetchOmnivore = async (inBackground = false) => {
402403
}
403404
}
404405

405-
const syncOmnivore = (): number => {
406+
const startSyncJob = () => {
406407
const settings = logseq.settings as Settings
407-
408-
let intervalID = 0
409408
// sync every frequency minutes
410409
if (settings.frequency > 0) {
411-
intervalID = setInterval(
410+
const intervalId = setInterval(
412411
async () => {
413412
if (await isValidCurrentGraph()) {
414413
await fetchOmnivore(true)
@@ -417,9 +416,8 @@ const syncOmnivore = (): number => {
417416
settings.frequency * 1000 * 60,
418417
settings.syncAt
419418
)
419+
logseq.updateSettings({ syncJobId: intervalId })
420420
}
421-
422-
return intervalID
423421
}
424422

425423
const resetLoadingState = () => {
@@ -428,9 +426,16 @@ const resetLoadingState = () => {
428426
settings.loading && logseq.updateSettings({ loading: false })
429427
}
430428

431-
const resetLoadingStateAsync = async () => {
429+
const resetSyncJob = () => {
430+
console.log('reset sync job')
431+
const settings = logseq.settings as Settings
432+
settings.syncJobId > 0 && clearInterval(settings.syncJobId)
433+
logseq.updateSettings({ syncJobId: 0 })
434+
}
435+
436+
const resetState = () => {
432437
resetLoadingState()
433-
return Promise.resolve()
438+
resetSyncJob()
434439
}
435440

436441
/**
@@ -446,7 +451,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
446451
title: 'Enter your Omnivore Api Key',
447452
description:
448453
'You can create an API key at https://omnivore.app/settings/api',
449-
default: logseq.settings?.['api key'] as string,
454+
default: '',
450455
},
451456
{
452457
key: 'filter',
@@ -488,9 +493,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
488493
title: 'Last Sync',
489494
description:
490495
'The last time Omnivore was synced. Clear this value to completely refresh the sync.',
491-
default: DateTime.fromISO(logseq.settings?.['synced at'] as string)
492-
.toLocal()
493-
.toFormat(DATE_FORMAT),
496+
default: '',
494497
inputAs: 'datetime-local',
495498
},
496499
{
@@ -542,21 +545,13 @@ date-published:: {{{datePublished}}}
542545
]
543546
logseq.useSettingsSchema(settingsSchema)
544547

545-
let frequency = logseq.settings?.frequency as number
546-
let intervalID: number
547-
548-
logseq.onSettingsChanged(() => {
549-
const settings = logseq.settings as Settings
550-
const newFrequency = settings.frequency
551-
if (newFrequency !== frequency) {
548+
logseq.onSettingsChanged((newSettings: Settings, oldSettings: Settings) => {
549+
const newFrequency = newSettings.frequency
550+
if (newFrequency !== oldSettings.frequency) {
552551
// remove existing scheduled task and create new one
553-
if (intervalID) {
554-
clearInterval(intervalID)
555-
}
556-
if (newFrequency > 0) {
557-
intervalID = syncOmnivore()
558-
}
559-
frequency = newFrequency
552+
oldSettings.syncJobId > 0 && clearInterval(oldSettings.syncJobId)
553+
logseq.updateSettings({ syncJobId: 0 })
554+
newFrequency > 0 && startSyncJob()
560555
}
561556
})
562557

@@ -620,19 +615,23 @@ date-published:: {{{datePublished}}}
620615
}
621616
`)
622617

618+
// reset loading state on startup
619+
resetState()
620+
623621
// fetch articles on startup
624622
if (await isValidCurrentGraph()) {
625623
await fetchOmnivore(true)
626624
}
627625

628-
// sync every frequency minutes
629-
intervalID = syncOmnivore()
626+
// start the sync job
627+
startSyncJob()
630628
}
631629

632630
// reset loading state before plugin unload
633631
logseq.beforeunload(async () => {
634632
console.log('beforeunload')
635-
await resetLoadingStateAsync()
633+
resetState()
634+
return Promise.resolve()
636635
})
637636

638637
// bootstrap

0 commit comments

Comments
 (0)