vast 1.0.0 → 2.0.0-dev-031d95

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 ADDED
@@ -0,0 +1,49 @@
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
+ ## 2.0.0 - 2021-12-13
8
+ ### Changed or removed
9
+ - e8652bc breaking(vest, enforce): prepare next major (ealush)
10
+
11
+ ### Fixed and improved
12
+ - 825e74b types(n4s): fix proxy type (ealush)
13
+ - 9f9b970 vx: improve package.json generation (ealush)
14
+ - 7baedf2 n4s: use named export in entry (ealush)
15
+ - e60e9a6 vx: make exports of a module use its main entry as an external (ealush)
16
+ - 94e00a8 patch(vest): Add isolate module for containing re-orders (ealush)
17
+ - bed7040 vx: add back to workspace (ealush)
18
+ - a87824b vx: add types to exports (ealush)
19
+ - f2d458d update build artifacts (ealush)
20
+ - package.json
21
+ - packages/anyone/package.json
22
+ - .eslintrc.js
23
+ - packages/anyone/package.json
24
+ - eeac20e chore: remove duplicate types (undefined)
25
+ - packages/anyone/package.json
26
+ - 4d88c04 patch: add nodejs exports (undefined)
27
+ - packages/anyone/package.json
28
+ - 26af06b chore: reduce complexity, remove all lint errors (undefined)
29
+ - packages/anyone/.npmignore
30
+ - .github/PULL_REQUEST_TEMPLATE.md
31
+ - ba68539 lint: handling lint of all packages (ealush)
32
+ - .gitignore
33
+ - 73b28a1 chore: some lint fixes (ealush)
34
+
35
+ ## 1.0.11 - 2021-07-02
36
+
37
+ ### Fixed and improved
38
+
39
+ - 34e0414 improved conditions (undefined)
40
+ - 33f4e46 release (undefined)
41
+ - 6fe40c7 better bundle (undefined)
42
+ - c6387ab before ts settings (undefined)
43
+ - c0e9708 generate correct d.ts file (undefined)
44
+ - 8e01b8e x (undefined)
45
+ - afb3960 x (undefined)
46
+ - e0a8463 add changelog support (undefined)
47
+ - cc46c38 current (undefined)
48
+ - b6db1c6 transform any to unknowns (ealush)
49
+ - 81aad51 fix most tests (ealush)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 ealush
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Vast - Simple State Utility for Libraries
2
+
3
+ Vast is a simple state utility created to use in [Vest](https://github.com/ealush/vest). It allows using a similar pattern to React's useState hook to store data.
4
+
5
+ Note that this is mostly intended to be used within libraries, and not as a consumer facing interface. When paired with a context propagation libraries such as [context](https://github.com/ealush/context), it can have pretty powerful capabilities. See Vest for a real life example.
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ npm i vast
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```js
16
+ import { createState } from 'vast';
17
+
18
+ const state = createState(); // Creates a state reference.
19
+
20
+ const useColor = state.registerStateKey('blue'); // Creates a new key in the state, and gives it an initial value
21
+ // You can also pass in a function to use as the initial state
22
+
23
+ const [color, setColor] = useColor(); // ["blue", Function]
24
+ setColor('red'); // set the color to "red"
25
+ ```
26
+
27
+ The next time you will call `useColor` the value of `color` will be `"red"`.
28
+
29
+ You can also set a computed value by passing in a function:
30
+
31
+ ```js
32
+ const [color, setColor] = useColor(); // ["blue", Function]
33
+ setColor(currentColor => (color === 'red' ? 'blue' : 'red'));
34
+ ```
35
+
36
+ ## Subscribing to changes
37
+
38
+ ### Getting notified for every change in the state
39
+
40
+ **NOTE** This will not let you know what change was made, but only that a key in the state was updated or added:
41
+
42
+ Simply add an onChange callback to your createState:
43
+
44
+ ```js
45
+ const state = createState(() => console.log('the state was updated!'));
46
+ ```
47
+
48
+ Now on every state change, this callback will run.
49
+
50
+ Alternatively, if you need more granularity, you can subscribe to specific state keys.
51
+
52
+ ### Subscribing to specific state key updates
53
+
54
+ If you need to react to specific changes in your state, you can add an `onUpdate` callback to those state key registrations:
55
+
56
+ ```js
57
+ const useColor = state.registerStateKey(
58
+ 'red',
59
+ (currentState, previousState) => {
60
+ console.log(`the color changed from ${previousState} to ${currentState}!`);
61
+ }
62
+ );
63
+ ```
64
+
65
+ Now, whenever a state update happens in that key, your callback will run, providing the previous and changed value as well.
@@ -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,7 @@
1
+ 'use strict'
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./vast.production.js');
5
+ } else {
6
+ module.exports = require('./vast.development.js');
7
+ }
@@ -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,11 +1,45 @@
1
1
  {
2
+ "version": "2.0.0-dev-031d95",
3
+ "license": "MIT",
4
+ "main": "./dist/cjs/vast.js",
5
+ "types": "./types/vast.d.ts",
2
6
  "name": "vast",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
7
+ "author": "ealush",
6
8
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
9
+ "test": "vx test",
10
+ "release": "vx release"
8
11
  },
9
- "author": "",
10
- "license": "ISC"
12
+ "module": "./dist/es/vast.production.js",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/ealush/vest.git",
16
+ "directory": "packages/vast"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/ealush/vest.git/issues"
20
+ },
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"
41
+ },
42
+ "./package.json": "./package.json",
43
+ "./": "./"
44
+ }
11
45
  }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "declarationMap": true,
5
+ "declarationDir": "./types",
6
+ "outDir": "./dist"
7
+ }
8
+ }
@@ -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 };