functional-models 1.0.27 → 1.0.28
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/package.json +2 -1
- package/src/lazy.js +14 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functional-models",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.28",
|
|
4
4
|
"description": "A library for creating JavaScript function based models.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"sinon": "^11.1.2"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"async-lock": "^1.3.0",
|
|
52
53
|
"get-random-values": "^1.2.2",
|
|
53
54
|
"lodash": "^4.17.21"
|
|
54
55
|
}
|
package/src/lazy.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
+
const AsyncLock = require('async-lock')
|
|
2
|
+
const { createUuid } = require('./utils')
|
|
3
|
+
|
|
1
4
|
const lazyValue = method => {
|
|
5
|
+
const key = createUuid()
|
|
6
|
+
const lock = new AsyncLock()
|
|
2
7
|
/* eslint-disable functional/no-let */
|
|
3
8
|
let value = undefined
|
|
4
9
|
let called = false
|
|
5
10
|
return async (...args) => {
|
|
6
|
-
|
|
7
|
-
called
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
return lock.acquire(key, async () => {
|
|
12
|
+
if (!called) {
|
|
13
|
+
called = true
|
|
14
|
+
value = await method(...args)
|
|
15
|
+
// eslint-disable-next-line require-atomic-updates
|
|
16
|
+
}
|
|
17
|
+
}).then(() => {
|
|
18
|
+
return value
|
|
19
|
+
})
|
|
13
20
|
}
|
|
14
21
|
/* eslint-enable functional/no-let */
|
|
15
22
|
}
|