vast 1.0.2 → 1.0.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/CHANGELOG.md +29 -0
- package/README.md +1 -1
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/vast.development.js +81 -0
- package/dist/cjs/vast.production.js +1 -0
- package/dist/es/package.json +1 -0
- package/dist/es/vast.development.js +77 -0
- package/dist/es/vast.production.js +1 -0
- package/dist/umd/vast.development.js +87 -0
- package/dist/umd/vast.production.js +1 -0
- package/package.json +36 -50
- package/tsconfig.json +8 -0
- package/types/vast.d.ts +13 -0
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -8
- package/dist/vast.cjs.development.js +0 -81
- package/dist/vast.cjs.development.js.map +0 -1
- package/dist/vast.cjs.production.min.js +0 -2
- package/dist/vast.cjs.production.min.js.map +0 -1
- package/dist/vast.esm.js +0 -77
- package/dist/vast.esm.js.map +0 -1
- package/src/index.ts +0 -90
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# vast - Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## 1.0.4 - 2021-12-24
|
|
8
|
+
### Fixed and improved
|
|
9
|
+
- 7e8d48d Update changelog (ealush)
|
|
10
|
+
|
|
11
|
+
## 2.0.0 - 2021-12-24
|
|
12
|
+
|
|
13
|
+
- Use named exports
|
|
14
|
+
|
|
15
|
+
## 1.0.11 - 2021-07-02
|
|
16
|
+
|
|
17
|
+
### Fixed and improved
|
|
18
|
+
|
|
19
|
+
- 34e0414 improved conditions (undefined)
|
|
20
|
+
- 33f4e46 release (undefined)
|
|
21
|
+
- 6fe40c7 better bundle (undefined)
|
|
22
|
+
- c6387ab before ts settings (undefined)
|
|
23
|
+
- c0e9708 generate correct d.ts file (undefined)
|
|
24
|
+
- 8e01b8e x (undefined)
|
|
25
|
+
- afb3960 x (undefined)
|
|
26
|
+
- e0a8463 add changelog support (undefined)
|
|
27
|
+
- cc46c38 current (undefined)
|
|
28
|
+
- b6db1c6 transform any to unknowns (ealush)
|
|
29
|
+
- 81aad51 fix most tests (ealush)
|
package/README.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function isFunction(value) {
|
|
6
|
+
return typeof value === 'function';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function optionalFunctionValue(value) {
|
|
10
|
+
var args = [];
|
|
11
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
12
|
+
args[_i - 1] = arguments[_i];
|
|
13
|
+
}
|
|
14
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line max-lines-per-function
|
|
18
|
+
function createState(onStateChange) {
|
|
19
|
+
var state = {
|
|
20
|
+
references: []
|
|
21
|
+
};
|
|
22
|
+
var registrations = [];
|
|
23
|
+
return {
|
|
24
|
+
registerStateKey: registerStateKey,
|
|
25
|
+
reset: reset
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* const useColor = state.registerStateKey("blue");
|
|
33
|
+
*
|
|
34
|
+
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
35
|
+
*
|
|
36
|
+
* setColor("green");
|
|
37
|
+
*
|
|
38
|
+
* useColor()[0]; -> "green"
|
|
39
|
+
*/
|
|
40
|
+
function registerStateKey(initialState, onUpdate) {
|
|
41
|
+
var key = registrations.length;
|
|
42
|
+
registrations.push([initialState, onUpdate]);
|
|
43
|
+
return initKey(key, initialState);
|
|
44
|
+
}
|
|
45
|
+
function reset() {
|
|
46
|
+
var prev = current();
|
|
47
|
+
state.references = [];
|
|
48
|
+
registrations.forEach(function (_a, index) {
|
|
49
|
+
var initialValue = _a[0];
|
|
50
|
+
return initKey(index, initialValue, prev[index]);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function initKey(key, initialState, prevState) {
|
|
54
|
+
current().push();
|
|
55
|
+
set(key, optionalFunctionValue(initialState, prevState));
|
|
56
|
+
return function useStateKey() {
|
|
57
|
+
return [
|
|
58
|
+
current()[key],
|
|
59
|
+
function (nextState) {
|
|
60
|
+
return set(key, optionalFunctionValue(nextState, current()[key]));
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function current() {
|
|
66
|
+
return state.references;
|
|
67
|
+
}
|
|
68
|
+
function set(index, value) {
|
|
69
|
+
var prevValue = state.references[index];
|
|
70
|
+
state.references[index] = value;
|
|
71
|
+
var _a = registrations[index], onUpdate = _a[1];
|
|
72
|
+
if (isFunction(onUpdate)) {
|
|
73
|
+
onUpdate(value, prevValue);
|
|
74
|
+
}
|
|
75
|
+
if (isFunction(onStateChange)) {
|
|
76
|
+
onStateChange();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.createState = createState;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e){return"function"==typeof e}function r(r){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return e(r)?r.apply(void 0,n):r}Object.defineProperty(exports,"__esModule",{value:!0}),exports.createState=function(n){function t(e,n,t){return c.references.push(),u(e,r(n,t)),function(){return[c.references[e],function(n){return u(e,r(n,c.references[e]))}]}}function u(r,t){var u=c.references[r];c.references[r]=t,e(r=f[r][1])&&r(t,u),e(n)&&n()}var c={references:[]},f=[];return{registerStateKey:function(e,r){var n=f.length;return f.push([e,r]),t(n,e)},reset:function(){var e=c.references;c.references=[],f.forEach((function(r,n){return t(n,r[0],e[n])}))}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function isFunction(value) {
|
|
2
|
+
return typeof value === 'function';
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function optionalFunctionValue(value) {
|
|
6
|
+
var args = [];
|
|
7
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
8
|
+
args[_i - 1] = arguments[_i];
|
|
9
|
+
}
|
|
10
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line max-lines-per-function
|
|
14
|
+
function createState(onStateChange) {
|
|
15
|
+
var state = {
|
|
16
|
+
references: []
|
|
17
|
+
};
|
|
18
|
+
var registrations = [];
|
|
19
|
+
return {
|
|
20
|
+
registerStateKey: registerStateKey,
|
|
21
|
+
reset: reset
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* const useColor = state.registerStateKey("blue");
|
|
29
|
+
*
|
|
30
|
+
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
31
|
+
*
|
|
32
|
+
* setColor("green");
|
|
33
|
+
*
|
|
34
|
+
* useColor()[0]; -> "green"
|
|
35
|
+
*/
|
|
36
|
+
function registerStateKey(initialState, onUpdate) {
|
|
37
|
+
var key = registrations.length;
|
|
38
|
+
registrations.push([initialState, onUpdate]);
|
|
39
|
+
return initKey(key, initialState);
|
|
40
|
+
}
|
|
41
|
+
function reset() {
|
|
42
|
+
var prev = current();
|
|
43
|
+
state.references = [];
|
|
44
|
+
registrations.forEach(function (_a, index) {
|
|
45
|
+
var initialValue = _a[0];
|
|
46
|
+
return initKey(index, initialValue, prev[index]);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function initKey(key, initialState, prevState) {
|
|
50
|
+
current().push();
|
|
51
|
+
set(key, optionalFunctionValue(initialState, prevState));
|
|
52
|
+
return function useStateKey() {
|
|
53
|
+
return [
|
|
54
|
+
current()[key],
|
|
55
|
+
function (nextState) {
|
|
56
|
+
return set(key, optionalFunctionValue(nextState, current()[key]));
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function current() {
|
|
62
|
+
return state.references;
|
|
63
|
+
}
|
|
64
|
+
function set(index, value) {
|
|
65
|
+
var prevValue = state.references[index];
|
|
66
|
+
state.references[index] = value;
|
|
67
|
+
var _a = registrations[index], onUpdate = _a[1];
|
|
68
|
+
if (isFunction(onUpdate)) {
|
|
69
|
+
onUpdate(value, prevValue);
|
|
70
|
+
}
|
|
71
|
+
if (isFunction(onStateChange)) {
|
|
72
|
+
onStateChange();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { createState };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return"function"==typeof e}function r(r){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return e(r)?r.apply(void 0,n):r}export function createState(n){function t(e,n,t){return c.references.push(),f(e,r(n,t)),function(){return[c.references[e],function(n){return f(e,r(n,c.references[e]))}]}}function f(r,t){var f=c.references[r];c.references[r]=t,e(r=u[r][1])&&r(t,f),e(n)&&n()}var c={references:[]},u=[];return{registerStateKey:function(e,r){var n=u.length;return u.push([e,r]),t(n,e)},reset:function(){var e=c.references;c.references=[],u.forEach((function(r,n){return t(n,r[0],e[n])}))}}}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vast = {}));
|
|
5
|
+
}(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function isFunction(value) {
|
|
8
|
+
return typeof value === 'function';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function optionalFunctionValue(value) {
|
|
12
|
+
var args = [];
|
|
13
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
14
|
+
args[_i - 1] = arguments[_i];
|
|
15
|
+
}
|
|
16
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// eslint-disable-next-line max-lines-per-function
|
|
20
|
+
function createState(onStateChange) {
|
|
21
|
+
var state = {
|
|
22
|
+
references: []
|
|
23
|
+
};
|
|
24
|
+
var registrations = [];
|
|
25
|
+
return {
|
|
26
|
+
registerStateKey: registerStateKey,
|
|
27
|
+
reset: reset
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* const useColor = state.registerStateKey("blue");
|
|
35
|
+
*
|
|
36
|
+
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
37
|
+
*
|
|
38
|
+
* setColor("green");
|
|
39
|
+
*
|
|
40
|
+
* useColor()[0]; -> "green"
|
|
41
|
+
*/
|
|
42
|
+
function registerStateKey(initialState, onUpdate) {
|
|
43
|
+
var key = registrations.length;
|
|
44
|
+
registrations.push([initialState, onUpdate]);
|
|
45
|
+
return initKey(key, initialState);
|
|
46
|
+
}
|
|
47
|
+
function reset() {
|
|
48
|
+
var prev = current();
|
|
49
|
+
state.references = [];
|
|
50
|
+
registrations.forEach(function (_a, index) {
|
|
51
|
+
var initialValue = _a[0];
|
|
52
|
+
return initKey(index, initialValue, prev[index]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function initKey(key, initialState, prevState) {
|
|
56
|
+
current().push();
|
|
57
|
+
set(key, optionalFunctionValue(initialState, prevState));
|
|
58
|
+
return function useStateKey() {
|
|
59
|
+
return [
|
|
60
|
+
current()[key],
|
|
61
|
+
function (nextState) {
|
|
62
|
+
return set(key, optionalFunctionValue(nextState, current()[key]));
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function current() {
|
|
68
|
+
return state.references;
|
|
69
|
+
}
|
|
70
|
+
function set(index, value) {
|
|
71
|
+
var prevValue = state.references[index];
|
|
72
|
+
state.references[index] = value;
|
|
73
|
+
var _a = registrations[index], onUpdate = _a[1];
|
|
74
|
+
if (isFunction(onUpdate)) {
|
|
75
|
+
onUpdate(value, prevValue);
|
|
76
|
+
}
|
|
77
|
+
if (isFunction(onStateChange)) {
|
|
78
|
+
onStateChange();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exports.createState = createState;
|
|
84
|
+
|
|
85
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
86
|
+
|
|
87
|
+
})));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).vast={})}(this,(function(e){function n(e){return"function"==typeof e}function r(e){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];return n(e)?e.apply(void 0,r):e}e.createState=function(e){function t(e,n,t){return o.references.push(),f(e,r(n,t)),function(){return[o.references[e],function(n){return f(e,r(n,o.references[e]))}]}}function f(r,t){var f=o.references[r];o.references[r]=t,n(r=u[r][1])&&r(t,f),n(e)&&e()}var o={references:[]},u=[];return{registerStateKey:function(e,n){var r=u.length;return u.push([e,n]),t(r,e)},reset:function(){var e=o.references;o.references=[],u.forEach((function(n,r){return t(r,n[0],e[r])}))}}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,59 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.0.
|
|
2
|
+
"version": "1.0.4",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"main": "dist/index.js",
|
|
9
|
-
"typings": "dist/index.d.ts",
|
|
10
|
-
"files": [
|
|
11
|
-
"dist",
|
|
12
|
-
"src"
|
|
13
|
-
],
|
|
14
|
-
"engines": {
|
|
15
|
-
"node": ">=10"
|
|
16
|
-
},
|
|
4
|
+
"main": "./dist/cjs/vast.js",
|
|
5
|
+
"types": "./types/vast.d.ts",
|
|
6
|
+
"name": "vast",
|
|
7
|
+
"author": "ealush",
|
|
17
8
|
"scripts": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"test": "tsdx test",
|
|
21
|
-
"lint": "tsdx lint",
|
|
22
|
-
"prepare": "tsdx build",
|
|
23
|
-
"size": "size-limit",
|
|
24
|
-
"analyze": "size-limit --why"
|
|
9
|
+
"test": "vx test",
|
|
10
|
+
"release": "vx release"
|
|
25
11
|
},
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
12
|
+
"module": "./dist/es/vast.production.js",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/ealush/vest.git",
|
|
16
|
+
"directory": "packages/vast"
|
|
31
17
|
},
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"semi": true,
|
|
35
|
-
"singleQuote": true,
|
|
36
|
-
"trailingComma": "es5"
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/ealush/vest.git/issues"
|
|
37
20
|
},
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"development": {
|
|
24
|
+
"types": "./types/vast.d.ts",
|
|
25
|
+
"browser": "./dist/es/vast.development.js",
|
|
26
|
+
"umd": "./dist/umd/vast.development.js",
|
|
27
|
+
"import": "./dist/es/vast.development.js",
|
|
28
|
+
"require": "./dist/cjs/vast.development.js",
|
|
29
|
+
"node": "./dist/cjs/vast.development.js",
|
|
30
|
+
"module": "./dist/es/vast.development.js",
|
|
31
|
+
"default": "./dist/cjs/vast.development.js"
|
|
32
|
+
},
|
|
33
|
+
"types": "./types/vast.d.ts",
|
|
34
|
+
"browser": "./dist/es/vast.production.js",
|
|
35
|
+
"umd": "./dist/umd/vast.production.js",
|
|
36
|
+
"import": "./dist/es/vast.production.js",
|
|
37
|
+
"require": "./dist/cjs/vast.production.js",
|
|
38
|
+
"node": "./dist/cjs/vast.production.js",
|
|
39
|
+
"module": "./dist/es/vast.production.js",
|
|
40
|
+
"default": "./dist/cjs/vast.production.js"
|
|
45
41
|
},
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"limit": "10 KB"
|
|
49
|
-
}
|
|
50
|
-
],
|
|
51
|
-
"devDependencies": {
|
|
52
|
-
"@size-limit/preset-small-lib": "^4.10.2",
|
|
53
|
-
"husky": "^6.0.0",
|
|
54
|
-
"size-limit": "^4.10.2",
|
|
55
|
-
"tsdx": "^0.14.1",
|
|
56
|
-
"tslib": "^2.1.0",
|
|
57
|
-
"typescript": "^3.8.0"
|
|
42
|
+
"./package.json": "./package.json",
|
|
43
|
+
"./": "./"
|
|
58
44
|
}
|
|
59
45
|
}
|
package/tsconfig.json
ADDED
package/types/vast.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare function createState(onStateChange?: (...args: unknown[]) => unknown): TCreateStateReturn;
|
|
2
|
+
type TStateInput<S> = S | ((prevState?: S) => S);
|
|
3
|
+
type TSetStateInput<S> = S | ((prevState: S) => S);
|
|
4
|
+
type TState = ReturnType<typeof createState>;
|
|
5
|
+
type TStateHandlerReturn<S> = [
|
|
6
|
+
S,
|
|
7
|
+
(nextState: TSetStateInput<S>) => void
|
|
8
|
+
];
|
|
9
|
+
type TCreateStateReturn = {
|
|
10
|
+
reset: () => void;
|
|
11
|
+
registerStateKey: <S>(initialState?: TStateInput<S> | undefined, onUpdate?: (() => void) | undefined) => () => TStateHandlerReturn<S>;
|
|
12
|
+
};
|
|
13
|
+
export { createState, TState, TStateHandlerReturn };
|
package/dist/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
declare type TSetStateInput<S> = S | ((prevState: S) => S);
|
|
2
|
-
declare type TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];
|
|
3
|
-
export default function createState(onStateChange?: Function): {
|
|
4
|
-
registerStateKey: <S>(initialState?: S | (() => S) | undefined, onUpdate?: (() => void) | undefined) => () => TStateHandlerReturn<S>;
|
|
5
|
-
reset: () => void;
|
|
6
|
-
};
|
|
7
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function createState(onStateChange) {
|
|
6
|
-
var state = {
|
|
7
|
-
references: []
|
|
8
|
-
};
|
|
9
|
-
var registrations = [];
|
|
10
|
-
return {
|
|
11
|
-
registerStateKey: registerStateKey,
|
|
12
|
-
reset: reset
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
*
|
|
19
|
-
* const useColor = state.registerStateKey("blue");
|
|
20
|
-
*
|
|
21
|
-
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
22
|
-
*
|
|
23
|
-
* setColor("green");
|
|
24
|
-
*
|
|
25
|
-
* useColor()[0]; -> "green"
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
function registerStateKey(initialState, onUpdate) {
|
|
29
|
-
var key = registrations.length;
|
|
30
|
-
registrations.push([initialState, onUpdate]);
|
|
31
|
-
return initKey(key, initialState);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function reset() {
|
|
35
|
-
state.references = [];
|
|
36
|
-
registrations.forEach(function (_ref, index) {
|
|
37
|
-
var initialValue = _ref[0];
|
|
38
|
-
return initKey(index, initialValue);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function initKey(key, initialState) {
|
|
43
|
-
current().push();
|
|
44
|
-
set(key, optionalFunctionValue(initialState));
|
|
45
|
-
return function useStateKey() {
|
|
46
|
-
return [current()[key], function (nextState) {
|
|
47
|
-
return set(key, optionalFunctionValue(nextState, [current()[key]]));
|
|
48
|
-
}];
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function current() {
|
|
53
|
-
return state.references;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function set(key, value) {
|
|
57
|
-
var prevValue = state.references[key];
|
|
58
|
-
state.references[key] = value;
|
|
59
|
-
var _registrations$key = registrations[key],
|
|
60
|
-
onUpdate = _registrations$key[1];
|
|
61
|
-
|
|
62
|
-
if (isFunction(onUpdate)) {
|
|
63
|
-
onUpdate(value, prevValue);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (isFunction(onStateChange)) {
|
|
67
|
-
onStateChange();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function isFunction(f) {
|
|
73
|
-
return typeof f === 'function';
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function optionalFunctionValue(value, args) {
|
|
77
|
-
return isFunction(value) ? value.apply(null, args) : value;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
exports.default = createState;
|
|
81
|
-
//# sourceMappingURL=vast.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vast.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["type TStateInput<S> = S | (() => S);\ntype TSetStateInput<S> = S | ((prevState: S) => S);\ntype TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];\n\nexport default function createState(onStateChange?: Function) {\n const state: {\n references: any[];\n } = {\n references: [],\n };\n\n const registrations: [\n any,\n (<S>(currentState: S, prevState: S) => void)?\n ][] = [];\n\n return {\n registerStateKey,\n reset,\n };\n\n /**\n * Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.\n *\n * @example\n *\n * const useColor = state.registerStateKey(\"blue\");\n *\n * let [color, setColor] = useColor(); // -> [\"blue\", Function]\n *\n * setColor(\"green\");\n *\n * useColor()[0]; -> \"green\"\n */\n function registerStateKey<S>(\n initialState?: TStateInput<S>,\n onUpdate?: () => void\n ): () => TStateHandlerReturn<S> {\n const key = registrations.length;\n registrations.push([initialState, onUpdate]);\n return initKey(key, initialState);\n }\n\n function reset(): void {\n state.references = [];\n registrations.forEach(([initialValue], index) =>\n initKey(index, initialValue)\n );\n }\n\n function initKey<S>(key: number, initialState?: TStateInput<S>) {\n current().push();\n set(key, optionalFunctionValue(initialState));\n\n return function useStateKey(): TStateHandlerReturn<S> {\n return [\n current()[key],\n (nextState: TSetStateInput<S>) =>\n set(key, optionalFunctionValue(nextState, [current()[key]])),\n ];\n };\n }\n\n function current(): any[] {\n return state.references;\n }\n\n function set(key: number, value: any): void {\n const prevValue = state.references[key];\n state.references[key] = value;\n\n const [, onUpdate] = registrations[key];\n\n if (isFunction(onUpdate)) {\n onUpdate(value, prevValue);\n }\n\n if (isFunction(onStateChange)) {\n onStateChange();\n }\n }\n}\n\nfunction isFunction(f: any): f is Function {\n return typeof f === 'function';\n}\n\nfunction optionalFunctionValue(value: any, args?: any[]): any {\n return isFunction(value) ? value.apply(null, args) : value;\n}\n"],"names":["createState","onStateChange","state","references","registrations","registerStateKey","reset","initialState","onUpdate","key","length","push","initKey","forEach","index","initialValue","current","set","optionalFunctionValue","useStateKey","nextState","value","prevValue","isFunction","f","args","apply"],"mappings":";;;;SAIwBA,YAAYC;AAClC,MAAMC,KAAK,GAEP;AACFC,IAAAA,UAAU,EAAE;AADV,GAFJ;AAMA,MAAMC,aAAa,GAGb,EAHN;AAKA,SAAO;AACLC,IAAAA,gBAAgB,EAAhBA,gBADK;AAELC,IAAAA,KAAK,EAALA;AAFK,GAAP;AAKA;;;;;;;;;;;;;;AAaA,WAASD,gBAAT,CACEE,YADF,EAEEC,QAFF;AAIE,QAAMC,GAAG,GAAGL,aAAa,CAACM,MAA1B;AACAN,IAAAA,aAAa,CAACO,IAAd,CAAmB,CAACJ,YAAD,EAAeC,QAAf,CAAnB;AACA,WAAOI,OAAO,CAACH,GAAD,EAAMF,YAAN,CAAd;AACD;;AAED,WAASD,KAAT;AACEJ,IAAAA,KAAK,CAACC,UAAN,GAAmB,EAAnB;AACAC,IAAAA,aAAa,CAACS,OAAd,CAAsB,gBAAiBC,KAAjB;AAAA,UAAEC,YAAF;AAAA,aACpBH,OAAO,CAACE,KAAD,EAAQC,YAAR,CADa;AAAA,KAAtB;AAGD;;AAED,WAASH,OAAT,CAAoBH,GAApB,EAAiCF,YAAjC;AACES,IAAAA,OAAO,GAAGL,IAAV;AACAM,IAAAA,GAAG,CAACR,GAAD,EAAMS,qBAAqB,CAACX,YAAD,CAA3B,CAAH;AAEA,WAAO,SAASY,WAAT;AACL,aAAO,CACLH,OAAO,GAAGP,GAAH,CADF,EAEL,UAACW,SAAD;AAAA,eACEH,GAAG,CAACR,GAAD,EAAMS,qBAAqB,CAACE,SAAD,EAAY,CAACJ,OAAO,GAAGP,GAAH,CAAR,CAAZ,CAA3B,CADL;AAAA,OAFK,CAAP;AAKD,KAND;AAOD;;AAED,WAASO,OAAT;AACE,WAAOd,KAAK,CAACC,UAAb;AACD;;AAED,WAASc,GAAT,CAAaR,GAAb,EAA0BY,KAA1B;AACE,QAAMC,SAAS,GAAGpB,KAAK,CAACC,UAAN,CAAiBM,GAAjB,CAAlB;AACAP,IAAAA,KAAK,CAACC,UAAN,CAAiBM,GAAjB,IAAwBY,KAAxB;6BAEqBjB,aAAa,CAACK,GAAD;QAAzBD;;AAET,QAAIe,UAAU,CAACf,QAAD,CAAd,EAA0B;AACxBA,MAAAA,QAAQ,CAACa,KAAD,EAAQC,SAAR,CAAR;AACD;;AAED,QAAIC,UAAU,CAACtB,aAAD,CAAd,EAA+B;AAC7BA,MAAAA,aAAa;AACd;AACF;AACF;;AAED,SAASsB,UAAT,CAAoBC,CAApB;AACE,SAAO,OAAOA,CAAP,KAAa,UAApB;AACD;;AAED,SAASN,qBAAT,CAA+BG,KAA/B,EAA2CI,IAA3C;AACE,SAAOF,UAAU,CAACF,KAAD,CAAV,GAAoBA,KAAK,CAACK,KAAN,CAAY,IAAZ,EAAkBD,IAAlB,CAApB,GAA8CJ,KAArD;AACD;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";function e(e){return"function"==typeof e}function n(n,r){return e(n)?n.apply(null,r):n}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(r){var t={references:[]},u=[];return{registerStateKey:function(e,n){var r=u.length;return u.push([e,n]),f(r,e)},reset:function(){t.references=[],u.forEach((function(e,n){return f(n,e[0])}))}};function f(e,r){return c().push(),o(e,n(r)),function(){return[c()[e],function(r){return o(e,n(r,[c()[e]]))}]}}function c(){return t.references}function o(n,f){var c=t.references[n];t.references[n]=f;var o=u[n][1];e(o)&&o(f,c),e(r)&&r()}};
|
|
2
|
-
//# sourceMappingURL=vast.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vast.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["type TStateInput<S> = S | (() => S);\ntype TSetStateInput<S> = S | ((prevState: S) => S);\ntype TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];\n\nexport default function createState(onStateChange?: Function) {\n const state: {\n references: any[];\n } = {\n references: [],\n };\n\n const registrations: [\n any,\n (<S>(currentState: S, prevState: S) => void)?\n ][] = [];\n\n return {\n registerStateKey,\n reset,\n };\n\n /**\n * Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.\n *\n * @example\n *\n * const useColor = state.registerStateKey(\"blue\");\n *\n * let [color, setColor] = useColor(); // -> [\"blue\", Function]\n *\n * setColor(\"green\");\n *\n * useColor()[0]; -> \"green\"\n */\n function registerStateKey<S>(\n initialState?: TStateInput<S>,\n onUpdate?: () => void\n ): () => TStateHandlerReturn<S> {\n const key = registrations.length;\n registrations.push([initialState, onUpdate]);\n return initKey(key, initialState);\n }\n\n function reset(): void {\n state.references = [];\n registrations.forEach(([initialValue], index) =>\n initKey(index, initialValue)\n );\n }\n\n function initKey<S>(key: number, initialState?: TStateInput<S>) {\n current().push();\n set(key, optionalFunctionValue(initialState));\n\n return function useStateKey(): TStateHandlerReturn<S> {\n return [\n current()[key],\n (nextState: TSetStateInput<S>) =>\n set(key, optionalFunctionValue(nextState, [current()[key]])),\n ];\n };\n }\n\n function current(): any[] {\n return state.references;\n }\n\n function set(key: number, value: any): void {\n const prevValue = state.references[key];\n state.references[key] = value;\n\n const [, onUpdate] = registrations[key];\n\n if (isFunction(onUpdate)) {\n onUpdate(value, prevValue);\n }\n\n if (isFunction(onStateChange)) {\n onStateChange();\n }\n }\n}\n\nfunction isFunction(f: any): f is Function {\n return typeof f === 'function';\n}\n\nfunction optionalFunctionValue(value: any, args?: any[]): any {\n return isFunction(value) ? value.apply(null, args) : value;\n}\n"],"names":["isFunction","f","optionalFunctionValue","value","args","apply","onStateChange","state","references","registrations","registerStateKey","initialState","onUpdate","key","length","push","initKey","reset","forEach","index","current","set","nextState","prevValue"],"mappings":"aAmFA,SAASA,EAAWC,SACE,mBAANA,EAGhB,SAASC,EAAsBC,EAAYC,UAClCJ,EAAWG,GAASA,EAAME,MAAM,KAAMD,GAAQD,kFApFnBG,OAC5BC,EAEF,CACFC,WAAY,IAGRC,EAGA,SAEC,CACLC,0BAkBAC,EACAC,OAEMC,EAAMJ,EAAcK,cAC1BL,EAAcM,KAAK,CAACJ,EAAcC,IAC3BI,EAAQH,EAAKF,IAtBpBM,iBA0BAV,EAAMC,WAAa,GACnBC,EAAcS,SAAQ,WAAiBC,UACrCH,EAAQG,sBAIHH,EAAWH,EAAaF,UAC/BS,IAAUL,OACVM,EAAIR,EAAKX,EAAsBS,IAExB,iBACE,CACLS,IAAUP,GACV,SAACS,UACCD,EAAIR,EAAKX,EAAsBoB,EAAW,CAACF,IAAUP,kBAKpDO,WACAb,EAAMC,oBAGNa,EAAIR,EAAaV,OAClBoB,EAAYhB,EAAMC,WAAWK,GACnCN,EAAMC,WAAWK,GAAOV,MAEfS,EAAYH,EAAcI,MAE/Bb,EAAWY,IACbA,EAAST,EAAOoB,GAGdvB,EAAWM,IACbA"}
|
package/dist/vast.esm.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
function createState(onStateChange) {
|
|
2
|
-
var state = {
|
|
3
|
-
references: []
|
|
4
|
-
};
|
|
5
|
-
var registrations = [];
|
|
6
|
-
return {
|
|
7
|
-
registerStateKey: registerStateKey,
|
|
8
|
-
reset: reset
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
*
|
|
15
|
-
* const useColor = state.registerStateKey("blue");
|
|
16
|
-
*
|
|
17
|
-
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
18
|
-
*
|
|
19
|
-
* setColor("green");
|
|
20
|
-
*
|
|
21
|
-
* useColor()[0]; -> "green"
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
function registerStateKey(initialState, onUpdate) {
|
|
25
|
-
var key = registrations.length;
|
|
26
|
-
registrations.push([initialState, onUpdate]);
|
|
27
|
-
return initKey(key, initialState);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function reset() {
|
|
31
|
-
state.references = [];
|
|
32
|
-
registrations.forEach(function (_ref, index) {
|
|
33
|
-
var initialValue = _ref[0];
|
|
34
|
-
return initKey(index, initialValue);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function initKey(key, initialState) {
|
|
39
|
-
current().push();
|
|
40
|
-
set(key, optionalFunctionValue(initialState));
|
|
41
|
-
return function useStateKey() {
|
|
42
|
-
return [current()[key], function (nextState) {
|
|
43
|
-
return set(key, optionalFunctionValue(nextState, [current()[key]]));
|
|
44
|
-
}];
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function current() {
|
|
49
|
-
return state.references;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function set(key, value) {
|
|
53
|
-
var prevValue = state.references[key];
|
|
54
|
-
state.references[key] = value;
|
|
55
|
-
var _registrations$key = registrations[key],
|
|
56
|
-
onUpdate = _registrations$key[1];
|
|
57
|
-
|
|
58
|
-
if (isFunction(onUpdate)) {
|
|
59
|
-
onUpdate(value, prevValue);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (isFunction(onStateChange)) {
|
|
63
|
-
onStateChange();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function isFunction(f) {
|
|
69
|
-
return typeof f === 'function';
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function optionalFunctionValue(value, args) {
|
|
73
|
-
return isFunction(value) ? value.apply(null, args) : value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export default createState;
|
|
77
|
-
//# sourceMappingURL=vast.esm.js.map
|
package/dist/vast.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vast.esm.js","sources":["../src/index.ts"],"sourcesContent":["type TStateInput<S> = S | (() => S);\ntype TSetStateInput<S> = S | ((prevState: S) => S);\ntype TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];\n\nexport default function createState(onStateChange?: Function) {\n const state: {\n references: any[];\n } = {\n references: [],\n };\n\n const registrations: [\n any,\n (<S>(currentState: S, prevState: S) => void)?\n ][] = [];\n\n return {\n registerStateKey,\n reset,\n };\n\n /**\n * Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.\n *\n * @example\n *\n * const useColor = state.registerStateKey(\"blue\");\n *\n * let [color, setColor] = useColor(); // -> [\"blue\", Function]\n *\n * setColor(\"green\");\n *\n * useColor()[0]; -> \"green\"\n */\n function registerStateKey<S>(\n initialState?: TStateInput<S>,\n onUpdate?: () => void\n ): () => TStateHandlerReturn<S> {\n const key = registrations.length;\n registrations.push([initialState, onUpdate]);\n return initKey(key, initialState);\n }\n\n function reset(): void {\n state.references = [];\n registrations.forEach(([initialValue], index) =>\n initKey(index, initialValue)\n );\n }\n\n function initKey<S>(key: number, initialState?: TStateInput<S>) {\n current().push();\n set(key, optionalFunctionValue(initialState));\n\n return function useStateKey(): TStateHandlerReturn<S> {\n return [\n current()[key],\n (nextState: TSetStateInput<S>) =>\n set(key, optionalFunctionValue(nextState, [current()[key]])),\n ];\n };\n }\n\n function current(): any[] {\n return state.references;\n }\n\n function set(key: number, value: any): void {\n const prevValue = state.references[key];\n state.references[key] = value;\n\n const [, onUpdate] = registrations[key];\n\n if (isFunction(onUpdate)) {\n onUpdate(value, prevValue);\n }\n\n if (isFunction(onStateChange)) {\n onStateChange();\n }\n }\n}\n\nfunction isFunction(f: any): f is Function {\n return typeof f === 'function';\n}\n\nfunction optionalFunctionValue(value: any, args?: any[]): any {\n return isFunction(value) ? value.apply(null, args) : value;\n}\n"],"names":["createState","onStateChange","state","references","registrations","registerStateKey","reset","initialState","onUpdate","key","length","push","initKey","forEach","index","initialValue","current","set","optionalFunctionValue","useStateKey","nextState","value","prevValue","isFunction","f","args","apply"],"mappings":"SAIwBA,YAAYC;AAClC,MAAMC,KAAK,GAEP;AACFC,IAAAA,UAAU,EAAE;AADV,GAFJ;AAMA,MAAMC,aAAa,GAGb,EAHN;AAKA,SAAO;AACLC,IAAAA,gBAAgB,EAAhBA,gBADK;AAELC,IAAAA,KAAK,EAALA;AAFK,GAAP;AAKA;;;;;;;;;;;;;;AAaA,WAASD,gBAAT,CACEE,YADF,EAEEC,QAFF;AAIE,QAAMC,GAAG,GAAGL,aAAa,CAACM,MAA1B;AACAN,IAAAA,aAAa,CAACO,IAAd,CAAmB,CAACJ,YAAD,EAAeC,QAAf,CAAnB;AACA,WAAOI,OAAO,CAACH,GAAD,EAAMF,YAAN,CAAd;AACD;;AAED,WAASD,KAAT;AACEJ,IAAAA,KAAK,CAACC,UAAN,GAAmB,EAAnB;AACAC,IAAAA,aAAa,CAACS,OAAd,CAAsB,gBAAiBC,KAAjB;AAAA,UAAEC,YAAF;AAAA,aACpBH,OAAO,CAACE,KAAD,EAAQC,YAAR,CADa;AAAA,KAAtB;AAGD;;AAED,WAASH,OAAT,CAAoBH,GAApB,EAAiCF,YAAjC;AACES,IAAAA,OAAO,GAAGL,IAAV;AACAM,IAAAA,GAAG,CAACR,GAAD,EAAMS,qBAAqB,CAACX,YAAD,CAA3B,CAAH;AAEA,WAAO,SAASY,WAAT;AACL,aAAO,CACLH,OAAO,GAAGP,GAAH,CADF,EAEL,UAACW,SAAD;AAAA,eACEH,GAAG,CAACR,GAAD,EAAMS,qBAAqB,CAACE,SAAD,EAAY,CAACJ,OAAO,GAAGP,GAAH,CAAR,CAAZ,CAA3B,CADL;AAAA,OAFK,CAAP;AAKD,KAND;AAOD;;AAED,WAASO,OAAT;AACE,WAAOd,KAAK,CAACC,UAAb;AACD;;AAED,WAASc,GAAT,CAAaR,GAAb,EAA0BY,KAA1B;AACE,QAAMC,SAAS,GAAGpB,KAAK,CAACC,UAAN,CAAiBM,GAAjB,CAAlB;AACAP,IAAAA,KAAK,CAACC,UAAN,CAAiBM,GAAjB,IAAwBY,KAAxB;6BAEqBjB,aAAa,CAACK,GAAD;QAAzBD;;AAET,QAAIe,UAAU,CAACf,QAAD,CAAd,EAA0B;AACxBA,MAAAA,QAAQ,CAACa,KAAD,EAAQC,SAAR,CAAR;AACD;;AAED,QAAIC,UAAU,CAACtB,aAAD,CAAd,EAA+B;AAC7BA,MAAAA,aAAa;AACd;AACF;AACF;;AAED,SAASsB,UAAT,CAAoBC,CAApB;AACE,SAAO,OAAOA,CAAP,KAAa,UAApB;AACD;;AAED,SAASN,qBAAT,CAA+BG,KAA/B,EAA2CI,IAA3C;AACE,SAAOF,UAAU,CAACF,KAAD,CAAV,GAAoBA,KAAK,CAACK,KAAN,CAAY,IAAZ,EAAkBD,IAAlB,CAApB,GAA8CJ,KAArD;AACD;;;;"}
|
package/src/index.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
type TStateInput<S> = S | (() => S);
|
|
2
|
-
type TSetStateInput<S> = S | ((prevState: S) => S);
|
|
3
|
-
type TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];
|
|
4
|
-
|
|
5
|
-
export default function createState(onStateChange?: Function) {
|
|
6
|
-
const state: {
|
|
7
|
-
references: any[];
|
|
8
|
-
} = {
|
|
9
|
-
references: [],
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const registrations: [
|
|
13
|
-
any,
|
|
14
|
-
(<S>(currentState: S, prevState: S) => void)?
|
|
15
|
-
][] = [];
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
registerStateKey,
|
|
19
|
-
reset,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Registers a new key in the state, takes the initial value (may be a function that returns the initial value), returns a function.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
*
|
|
27
|
-
* const useColor = state.registerStateKey("blue");
|
|
28
|
-
*
|
|
29
|
-
* let [color, setColor] = useColor(); // -> ["blue", Function]
|
|
30
|
-
*
|
|
31
|
-
* setColor("green");
|
|
32
|
-
*
|
|
33
|
-
* useColor()[0]; -> "green"
|
|
34
|
-
*/
|
|
35
|
-
function registerStateKey<S>(
|
|
36
|
-
initialState?: TStateInput<S>,
|
|
37
|
-
onUpdate?: () => void
|
|
38
|
-
): () => TStateHandlerReturn<S> {
|
|
39
|
-
const key = registrations.length;
|
|
40
|
-
registrations.push([initialState, onUpdate]);
|
|
41
|
-
return initKey(key, initialState);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function reset(): void {
|
|
45
|
-
state.references = [];
|
|
46
|
-
registrations.forEach(([initialValue], index) =>
|
|
47
|
-
initKey(index, initialValue)
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function initKey<S>(key: number, initialState?: TStateInput<S>) {
|
|
52
|
-
current().push();
|
|
53
|
-
set(key, optionalFunctionValue(initialState));
|
|
54
|
-
|
|
55
|
-
return function useStateKey(): TStateHandlerReturn<S> {
|
|
56
|
-
return [
|
|
57
|
-
current()[key],
|
|
58
|
-
(nextState: TSetStateInput<S>) =>
|
|
59
|
-
set(key, optionalFunctionValue(nextState, [current()[key]])),
|
|
60
|
-
];
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function current(): any[] {
|
|
65
|
-
return state.references;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function set(key: number, value: any): void {
|
|
69
|
-
const prevValue = state.references[key];
|
|
70
|
-
state.references[key] = value;
|
|
71
|
-
|
|
72
|
-
const [, onUpdate] = registrations[key];
|
|
73
|
-
|
|
74
|
-
if (isFunction(onUpdate)) {
|
|
75
|
-
onUpdate(value, prevValue);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (isFunction(onStateChange)) {
|
|
79
|
-
onStateChange();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function isFunction(f: any): f is Function {
|
|
85
|
-
return typeof f === 'function';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function optionalFunctionValue(value: any, args?: any[]): any {
|
|
89
|
-
return isFunction(value) ? value.apply(null, args) : value;
|
|
90
|
-
}
|