@weborigami/origami 0.2.3 → 0.2.4
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 +4 -4
- package/src/origami/once.js +18 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/origami",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Web Origami language, CLI, framework, and server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"typescript": "5.7.2"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@weborigami/async-tree": "0.2.
|
|
21
|
-
"@weborigami/language": "0.2.
|
|
22
|
-
"@weborigami/types": "0.2.
|
|
20
|
+
"@weborigami/async-tree": "0.2.4",
|
|
21
|
+
"@weborigami/language": "0.2.4",
|
|
22
|
+
"@weborigami/types": "0.2.4",
|
|
23
23
|
"exif-parser": "0.1.12",
|
|
24
24
|
"graphviz-wasm": "3.0.2",
|
|
25
25
|
"highlight.js": "11.11.0",
|
package/src/origami/once.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import assertTreeIsDefined from "../common/assertTreeIsDefined.js";
|
|
2
2
|
|
|
3
|
-
const fnPromiseMap = new
|
|
3
|
+
const fnPromiseMap = new Map();
|
|
4
|
+
const codePromiseMap = new Map();
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Evaluate the given function only once and cache the result.
|
|
@@ -11,8 +12,23 @@ const fnPromiseMap = new WeakMap();
|
|
|
11
12
|
*/
|
|
12
13
|
export default async function once(fn) {
|
|
13
14
|
assertTreeIsDefined(this, "origami:once");
|
|
15
|
+
|
|
16
|
+
const code = /** @type {any} */ (fn).code;
|
|
17
|
+
if (code) {
|
|
18
|
+
// Origami function, cache by code
|
|
19
|
+
if (!codePromiseMap.has(code)) {
|
|
20
|
+
// Don't wait for promise to resolve
|
|
21
|
+
const promise = fn.call(this);
|
|
22
|
+
codePromiseMap.set(code, promise);
|
|
23
|
+
}
|
|
24
|
+
return codePromiseMap.get(code);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Regular function, cache by function
|
|
14
28
|
if (!fnPromiseMap.has(fn)) {
|
|
15
|
-
|
|
29
|
+
// Don't wait for promise to resolve
|
|
30
|
+
const promise = fn.call(this);
|
|
31
|
+
fnPromiseMap.set(fn, promise);
|
|
16
32
|
}
|
|
17
33
|
return fnPromiseMap.get(fn);
|
|
18
34
|
}
|