utilitas 2001.1.100 → 2001.1.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "2001.1.100",
4
+ "version": "2001.1.102",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/utilitas.mjs CHANGED
@@ -8,7 +8,7 @@ import { assertPath, decodeBase64DataURL, readJson } from './storage.mjs';
8
8
 
9
9
  const call = (f, ...a) => promisify(Array.isArray(f) ? f[0].bind(f[1]) : f)(...a);
10
10
  const invalidTime = 'Invalid time.';
11
- const [Chrome, Edge] = ['Chrome', 'Edge'];
11
+ const [Chrome, Edge, LOCKS] = ['Chrome', 'Edge', {}];
12
12
  const getDateByUnixTimestamp = timestamp => new Date(~~timestamp * 1000);
13
13
  const getUnixTimestampByDate = date => Math.round(date.getTime() / 1000);
14
14
  const getRandomInt = max => Math.floor(Math.random() * Math.floor(max));
@@ -46,6 +46,7 @@ const modules = {};
46
46
  const defaultName = filename => String(filename || __filename);
47
47
  const basename = f => _basename(defaultName(f)).replace(/\.[^\.]*$/, '').trim();
48
48
  const checkChance = chance => Math.random() < 1 / (~~chance || 100);
49
+ const unlock = (key) => delete LOCKS[getLockKey(key)];
49
50
 
50
51
  const timeEmojis = {
51
52
  '00:00': '🕛', '00:30': '🕧', '01:00': '🕐', '01:30': '🕜', '02:00': '🕑',
@@ -843,6 +844,20 @@ const reverseKeyValues = obj => Object.fromEntries(
843
844
  Object.entries(obj).map(x => x.reverse())
844
845
  );
845
846
 
847
+ const getLockKey = key => {
848
+ assert(key, 'Invalid lock key.', 500);
849
+ return `LOCK_${key}`;
850
+ };
851
+
852
+ const lock = async (key, options) => {
853
+ key = getLockKey(key);
854
+ while (new Date().getTime() - parseInt(LOCKS?.[key] || 0)
855
+ < (options?.timeout || 1000)) {
856
+ await timeout(options?.checkInterval || 1000);
857
+ }
858
+ return (LOCKS[key] = new Date().getTime());
859
+ };
860
+
846
861
  export {
847
862
  __,
848
863
  analyzeModule,
@@ -915,6 +930,7 @@ export {
915
930
  lastItem,
916
931
  lineSplit,
917
932
  locate,
933
+ lock,
918
934
  log,
919
935
  makeStringByLength,
920
936
  mapKeys,
@@ -948,6 +964,7 @@ export {
948
964
  trim,
949
965
  tryUntil,
950
966
  uniqueArray,
967
+ unlock,
951
968
  verifyEmail,
952
969
  verifyPhone,
953
970
  verifyUrl,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "2001.1.100",
4
+ "version": "2001.1.102",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",