@thi.ng/dynvar 0.3.46 → 0.3.48
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/index.js +80 -93
- package/package.json +8 -6
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -1,96 +1,83 @@
|
|
|
1
1
|
import { assert } from "@thi.ng/errors/assert";
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
v[v.length - 1] = val;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Executes given `body` function in a new scope which has given
|
|
81
|
-
* `val` bound to this variable. The new scope is automatically
|
|
82
|
-
* removed when the function returns or an error occurred.
|
|
83
|
-
*
|
|
84
|
-
* @param val -
|
|
85
|
-
* @param body -
|
|
86
|
-
*/
|
|
87
|
-
withBinding(val, body) {
|
|
88
|
-
this.bind(val);
|
|
89
|
-
try {
|
|
90
|
-
return body();
|
|
91
|
-
}
|
|
92
|
-
finally {
|
|
93
|
-
this.unbind();
|
|
94
|
-
}
|
|
2
|
+
const __private = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
const dynvar = (x) => new DynVar(x);
|
|
4
|
+
class DynVar {
|
|
5
|
+
constructor(val) {
|
|
6
|
+
__private.set(this, [val]);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Returns current bound value, i.e. that of the currently active
|
|
10
|
+
* dynamic scope.
|
|
11
|
+
*/
|
|
12
|
+
deref() {
|
|
13
|
+
const v = __private.get(this);
|
|
14
|
+
return v[v.length - 1];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Same as {@link DynVar.deref}, but also for `DynVar` to satisfy
|
|
18
|
+
* `Object.valueOf()` contract.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
valueOf() {
|
|
22
|
+
return this.deref();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns new `DynVar` with this var's current value as its root
|
|
26
|
+
* binding.
|
|
27
|
+
*/
|
|
28
|
+
copy() {
|
|
29
|
+
return new DynVar(this.deref());
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Starts new dynamic scope in which given `val` will be bound to
|
|
33
|
+
* this variable. In most cases, calls to `bind()` should always be
|
|
34
|
+
* eventually followed by calls to {@link DynVar.unbind} to restore
|
|
35
|
+
* this var's previously scoped value.
|
|
36
|
+
*
|
|
37
|
+
* @param val -
|
|
38
|
+
*/
|
|
39
|
+
bind(val) {
|
|
40
|
+
__private.get(this).push(val);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Attempts to end the current scope by restoring this var's bound
|
|
45
|
+
* value to that of parent scope. An error is thrown if attempting
|
|
46
|
+
* to remove the root binding.
|
|
47
|
+
*/
|
|
48
|
+
unbind() {
|
|
49
|
+
const v = __private.get(this);
|
|
50
|
+
assert(v.length > 1, `can't unbind root value`);
|
|
51
|
+
v.pop();
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Replaces current scope's value with new `val`.
|
|
56
|
+
*
|
|
57
|
+
* @param val -
|
|
58
|
+
*/
|
|
59
|
+
set(val) {
|
|
60
|
+
const v = __private.get(this);
|
|
61
|
+
v[v.length - 1] = val;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Executes given `body` function in a new scope which has given
|
|
65
|
+
* `val` bound to this variable. The new scope is automatically
|
|
66
|
+
* removed when the function returns or an error occurred.
|
|
67
|
+
*
|
|
68
|
+
* @param val -
|
|
69
|
+
* @param body -
|
|
70
|
+
*/
|
|
71
|
+
withBinding(val, body) {
|
|
72
|
+
this.bind(val);
|
|
73
|
+
try {
|
|
74
|
+
return body();
|
|
75
|
+
} finally {
|
|
76
|
+
this.unbind();
|
|
95
77
|
}
|
|
78
|
+
}
|
|
96
79
|
}
|
|
80
|
+
export {
|
|
81
|
+
DynVar,
|
|
82
|
+
dynvar
|
|
83
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/dynvar",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.48",
|
|
4
4
|
"description": "Dynamically scoped variable bindings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,12 +35,12 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
37
|
-
"@thi.ng/errors": "^2.4.
|
|
38
|
+
"@thi.ng/api": "^8.9.12",
|
|
39
|
+
"@thi.ng/errors": "^2.4.6"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@microsoft/api-extractor": "^7.38.3",
|
|
41
|
-
"
|
|
43
|
+
"esbuild": "^0.19.8",
|
|
42
44
|
"rimraf": "^5.0.5",
|
|
43
45
|
"tools": "^0.0.1",
|
|
44
46
|
"typedoc": "^0.25.4",
|
|
@@ -76,5 +78,5 @@
|
|
|
76
78
|
"year": 2016,
|
|
77
79
|
"status": "alpha"
|
|
78
80
|
},
|
|
79
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
80
82
|
}
|