forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageLock.js
41 lines (29 loc) · 895 Bytes
/
storageLock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Debug = require('debug');
/* eslint-disable no-unused-vars */
const log = Debug('storageLock:log');
const error = Debug('storageLock:error');
/* eslint-enable no-unused-vars */
const debug = Debug('storageLock:debug');
module.exports = {};
const storageLockExports = module.exports;
let StorageLocked = false;
const timeout = async ms => new Promise(resolve => setTimeout(resolve, ms));
storageLockExports.lockStorage = async () => {
let count = 0;
// sleep for 1 second at a time until
// the storage is unlocked
while (StorageLocked) {
count += 1;
if (count > 5) {
debug('Storage locked... waiting 1 second');
}
/* eslint-disable-next-line no-await-in-loop */
await timeout(1000);
}
debug('Storage locked.');
StorageLocked = true;
};
storageLockExports.unlockStorage = () => {
debug('Storage unlocked.');
StorageLocked = false;
};