avoid reloading accounts just after saving them

Massimo Melina committed Feb 9, 2022 at 11:22 UTC 4d67432b7d7eacb207cff433acc0299a72f29962
1 file changed +8 -4
src/perm.ts
+8 -4
@@ -81,21 +81,25 @@ export async function updateAccount(account: Account, changer?:Changer) {
81 }
82
83 let saving = false
84 +let justSaved = false
85 const saveAccountsAsap = _.debounce(() => {
86 saving = true
87 fs.writeFile(path, yaml.stringify({ accounts }, { lineWidth:1000 })) // we don't want big numbers to be folded
87 - .catch(err => console.error('Failed at saving accounts file, please ensure it is writable.', String(err)))
88 + .then(() => justSaved = true,
89 + err => console.error('Failed at saving accounts file, please ensure it is writable.', String(err)))
90 .finally(()=> saving = false)
89 -})
91 +}, 200) // group burst of requests
92
93 let watcher: undefined | (()=>void)
94 subscribeConfig({ k:'accounts', defaultValue:'accounts.yaml' }, v => {
95 watcher?.()
96 if (!v)
97 return applyAccounts({})
96 - if (typeof v !== 'string')
97 - return console.error('bad type for accounts')
98 watcher = watchLoad(path = v, async data => {
99 + if (justSaved) {
100 + justSaved = false
101 + return
102 + }
103 if (saving) return
104 const a = data?.accounts
105 if (!a)