@@ -44,6 +44,7 @@ interface Settings {
44
44
articleTemplate : string
45
45
highlightTemplate : string
46
46
loading : boolean
47
+ syncJobId : number
47
48
}
48
49
49
50
const siteNameFromUrl = ( originalArticleUrl : string ) : string => {
@@ -402,13 +403,11 @@ const fetchOmnivore = async (inBackground = false) => {
402
403
}
403
404
}
404
405
405
- const syncOmnivore = ( ) : number => {
406
+ const startSyncJob = ( ) => {
406
407
const settings = logseq . settings as Settings
407
-
408
- let intervalID = 0
409
408
// sync every frequency minutes
410
409
if ( settings . frequency > 0 ) {
411
- intervalID = setInterval (
410
+ const intervalId = setInterval (
412
411
async ( ) => {
413
412
if ( await isValidCurrentGraph ( ) ) {
414
413
await fetchOmnivore ( true )
@@ -417,9 +416,8 @@ const syncOmnivore = (): number => {
417
416
settings . frequency * 1000 * 60 ,
418
417
settings . syncAt
419
418
)
419
+ logseq . updateSettings ( { syncJobId : intervalId } )
420
420
}
421
-
422
- return intervalID
423
421
}
424
422
425
423
const resetLoadingState = ( ) => {
@@ -428,9 +426,16 @@ const resetLoadingState = () => {
428
426
settings . loading && logseq . updateSettings ( { loading : false } )
429
427
}
430
428
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 = ( ) => {
432
437
resetLoadingState ( )
433
- return Promise . resolve ( )
438
+ resetSyncJob ( )
434
439
}
435
440
436
441
/**
@@ -446,7 +451,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
446
451
title : 'Enter your Omnivore Api Key' ,
447
452
description :
448
453
'You can create an API key at https://omnivore.app/settings/api' ,
449
- default : logseq . settings ?. [ 'api key' ] as string ,
454
+ default : '' ,
450
455
} ,
451
456
{
452
457
key : 'filter' ,
@@ -488,9 +493,7 @@ const main = async (baseInfo: LSPluginBaseInfo) => {
488
493
title : 'Last Sync' ,
489
494
description :
490
495
'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 : '' ,
494
497
inputAs : 'datetime-local' ,
495
498
} ,
496
499
{
@@ -542,21 +545,13 @@ date-published:: {{{datePublished}}}
542
545
]
543
546
logseq . useSettingsSchema ( settingsSchema )
544
547
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 ) {
552
551
// 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 ( )
560
555
}
561
556
} )
562
557
@@ -620,19 +615,23 @@ date-published:: {{{datePublished}}}
620
615
}
621
616
` )
622
617
618
+ // reset loading state on startup
619
+ resetState ( )
620
+
623
621
// fetch articles on startup
624
622
if ( await isValidCurrentGraph ( ) ) {
625
623
await fetchOmnivore ( true )
626
624
}
627
625
628
- // sync every frequency minutes
629
- intervalID = syncOmnivore ( )
626
+ // start the sync job
627
+ startSyncJob ( )
630
628
}
631
629
632
630
// reset loading state before plugin unload
633
631
logseq . beforeunload ( async ( ) => {
634
632
console . log ( 'beforeunload' )
635
- await resetLoadingStateAsync ( )
633
+ resetState ( )
634
+ return Promise . resolve ( )
636
635
} )
637
636
638
637
// bootstrap
0 commit comments