@figliolia/galena 2.3.4 → 3.0.0
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/README.md +172 -476
- package/dist/Galena.cjs +102 -0
- package/dist/Galena.d.cts +80 -0
- package/dist/Galena.d.cts.map +1 -0
- package/dist/Galena.d.mts +80 -0
- package/dist/Galena.d.mts.map +1 -0
- package/dist/Galena.mjs +103 -0
- package/dist/Galena.mjs.map +1 -0
- package/dist/GalenaProvider.cjs +50 -0
- package/dist/GalenaProvider.d.cts +35 -0
- package/dist/GalenaProvider.d.cts.map +1 -0
- package/dist/GalenaProvider.d.mts +35 -0
- package/dist/GalenaProvider.d.mts.map +1 -0
- package/dist/GalenaProvider.mjs +52 -0
- package/dist/GalenaProvider.mjs.map +1 -0
- package/dist/Logger.cjs +46 -0
- package/dist/Logger.d.cts +35 -0
- package/dist/Logger.d.cts.map +1 -0
- package/dist/Logger.d.mts +35 -0
- package/dist/Logger.d.mts.map +1 -0
- package/dist/Logger.mjs +48 -0
- package/dist/Logger.mjs.map +1 -0
- package/dist/Middleware.cjs +62 -0
- package/dist/Middleware.d.cts +65 -0
- package/dist/Middleware.d.cts.map +1 -0
- package/dist/Middleware.d.mts +65 -0
- package/dist/Middleware.d.mts.map +1 -0
- package/dist/Middleware.mjs +64 -0
- package/dist/Middleware.mjs.map +1 -0
- package/dist/Profiler.cjs +41 -0
- package/dist/Profiler.d.cts +31 -0
- package/dist/Profiler.d.cts.map +1 -0
- package/dist/Profiler.d.mts +31 -0
- package/dist/Profiler.d.mts.map +1 -0
- package/dist/Profiler.mjs +43 -0
- package/dist/Profiler.mjs.map +1 -0
- package/dist/State.cjs +147 -0
- package/dist/State.d.cts +114 -0
- package/dist/State.d.cts.map +1 -0
- package/dist/State.d.mts +114 -0
- package/dist/State.d.mts.map +1 -0
- package/dist/State.mjs +148 -0
- package/dist/State.mjs.map +1 -0
- package/dist/commonHooks.cjs +17 -0
- package/dist/commonHooks.mjs +18 -0
- package/dist/commonHooks.mjs.map +1 -0
- package/dist/connect-multi.cjs +42 -0
- package/dist/connect-multi.d.cts +10 -0
- package/dist/connect-multi.d.cts.map +1 -0
- package/dist/connect-multi.d.mts +10 -0
- package/dist/connect-multi.d.mts.map +1 -0
- package/dist/connect-multi.mjs +44 -0
- package/dist/connect-multi.mjs.map +1 -0
- package/dist/connect.cjs +41 -0
- package/dist/connect.d.cts +9 -0
- package/dist/connect.d.cts.map +1 -0
- package/dist/connect.d.mts +9 -0
- package/dist/connect.d.mts.map +1 -0
- package/dist/connect.mjs +43 -0
- package/dist/connect.mjs.map +1 -0
- package/dist/createUseState.cjs +47 -0
- package/dist/createUseState.d.cts +46 -0
- package/dist/createUseState.d.cts.map +1 -0
- package/dist/createUseState.d.mts +46 -0
- package/dist/createUseState.d.mts.map +1 -0
- package/dist/createUseState.mjs +49 -0
- package/dist/createUseState.mjs.map +1 -0
- package/dist/index.cjs +23 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +11 -0
- package/dist/types.d.cts +23 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +23 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/useState.cjs +45 -0
- package/dist/useState.d.cts +43 -0
- package/dist/useState.d.cts.map +1 -0
- package/dist/useState.d.mts +43 -0
- package/dist/useState.d.mts.map +1 -0
- package/dist/useState.mjs +47 -0
- package/dist/useState.mjs.map +1 -0
- package/media/Logging.png +0 -0
- package/media/Profiling.png +0 -0
- package/package.json +38 -59
- package/src/Galena.ts +120 -0
- package/src/{Middlewares/Logger.ts → Logger.ts} +15 -14
- package/src/Middleware.ts +62 -0
- package/src/Profiler.ts +53 -0
- package/src/State.ts +167 -0
- package/src/index.ts +6 -3
- package/src/types.ts +28 -0
- package/dist/cjs/Galena/Galena.js +0 -223
- package/dist/cjs/Galena/Guards.js +0 -40
- package/dist/cjs/Galena/Scheduler.js +0 -84
- package/dist/cjs/Galena/State.js +0 -314
- package/dist/cjs/Galena/index.js +0 -22
- package/dist/cjs/Galena/types.js +0 -9
- package/dist/cjs/Middleware/Middleware.js +0 -46
- package/dist/cjs/Middleware/index.js +0 -20
- package/dist/cjs/Middleware/types.js +0 -8
- package/dist/cjs/Middlewares/Logger.js +0 -51
- package/dist/cjs/Middlewares/Profiler.js +0 -38
- package/dist/cjs/Middlewares/index.js +0 -7
- package/dist/cjs/index.js +0 -19
- package/dist/cjs/package.json +0 -3
- package/dist/mjs/Galena/Galena.js +0 -218
- package/dist/mjs/Galena/Guards.js +0 -36
- package/dist/mjs/Galena/Scheduler.js +0 -79
- package/dist/mjs/Galena/State.js +0 -313
- package/dist/mjs/Galena/index.js +0 -3
- package/dist/mjs/Galena/types.js +0 -6
- package/dist/mjs/Middleware/Middleware.js +0 -42
- package/dist/mjs/Middleware/index.js +0 -2
- package/dist/mjs/Middleware/types.js +0 -5
- package/dist/mjs/Middlewares/Logger.js +0 -44
- package/dist/mjs/Middlewares/Profiler.js +0 -35
- package/dist/mjs/Middlewares/index.js +0 -2
- package/dist/mjs/index.js +0 -3
- package/dist/mjs/package.json +0 -4
- package/dist/types/Galena/Galena.d.ts +0 -160
- package/dist/types/Galena/Guards.d.ts +0 -29
- package/dist/types/Galena/Scheduler.d.ts +0 -51
- package/dist/types/Galena/State.d.ts +0 -235
- package/dist/types/Galena/index.d.ts +0 -3
- package/dist/types/Galena/types.d.ts +0 -13
- package/dist/types/Middleware/Middleware.d.ts +0 -43
- package/dist/types/Middleware/index.d.ts +0 -2
- package/dist/types/Middleware/types.d.ts +0 -4
- package/dist/types/Middlewares/Logger.d.ts +0 -27
- package/dist/types/Middlewares/Profiler.d.ts +0 -22
- package/dist/types/Middlewares/index.d.ts +0 -2
- package/dist/types/index.d.ts +0 -3
- package/src/Galena/Galena.ts +0 -252
- package/src/Galena/Guards.ts +0 -49
- package/src/Galena/Scheduler.ts +0 -85
- package/src/Galena/State.ts +0 -344
- package/src/Galena/index.ts +0 -3
- package/src/Galena/types.ts +0 -18
- package/src/Middleware/Middleware.ts +0 -45
- package/src/Middleware/index.ts +0 -2
- package/src/Middleware/types.ts +0 -4
- package/src/Middlewares/Profiler.ts +0 -41
- package/src/Middlewares/index.ts +0 -2
package/dist/connect.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Component } from "react";
|
|
2
|
+
//#region src/connect.tsx
|
|
3
|
+
const connect = (state) => {
|
|
4
|
+
return (selection) => {
|
|
5
|
+
return (WrappedComponent) => {
|
|
6
|
+
return class GalenaComponent extends Component {
|
|
7
|
+
state;
|
|
8
|
+
listener = null;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
super(props);
|
|
11
|
+
this.update = this.update.bind(this);
|
|
12
|
+
this.state = selection(state.getSnapshot(), this.props);
|
|
13
|
+
}
|
|
14
|
+
static displayName = `GalenaComponent(${(WrappedComponent.displayName ?? WrappedComponent.name) || "Component"})`;
|
|
15
|
+
componentDidMount() {
|
|
16
|
+
this.listener = state.subscribe((state) => this.update(state));
|
|
17
|
+
}
|
|
18
|
+
UNSAFE_componentWillReceiveProps(nextProps) {
|
|
19
|
+
if (nextProps !== this.props) this.update(state.getSnapshot(), nextProps);
|
|
20
|
+
}
|
|
21
|
+
shouldComponentUpdate(nextProps, nextState) {
|
|
22
|
+
return nextState !== this.state || nextProps !== this.props;
|
|
23
|
+
}
|
|
24
|
+
componentWillUnmount() {
|
|
25
|
+
if (this.listener) {
|
|
26
|
+
this.listener();
|
|
27
|
+
this.listener = null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
update(nextState, ownProps = this.props) {
|
|
31
|
+
this.setState(selection(nextState, ownProps));
|
|
32
|
+
}
|
|
33
|
+
render() {
|
|
34
|
+
return <WrappedComponent {...this.props} {...this.state} />;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { connect };
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=connect.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.mjs","names":[],"sources":["../src/connect.tsx"],"sourcesContent":["import { Component, type ComponentType } from \"react\";\nimport type { State } from \"./State\";\nimport type { Subtract } from \"./types\";\n\nexport const connect = <StateInstance extends State<any>>(\n state: StateInstance,\n) => {\n type ScopedState = ReturnType<StateInstance[\"getSnapshot\"]>;\n return <\n SelectorFunction extends (\n state: ScopedState,\n ownProps: any,\n ) => Record<string, any>,\n >(\n selection: SelectorFunction,\n ) => {\n type Selection = ReturnType<SelectorFunction>;\n return <ComponentProps extends Selection>(\n WrappedComponent: ComponentType<ComponentProps>,\n ): ComponentType<Subtract<ComponentProps, Selection>> => {\n type OwnProps = Subtract<ComponentProps, Selection>;\n return class GalenaComponent extends Component<OwnProps, Selection> {\n override state: any;\n private listener: (() => void) | null = null;\n constructor(props: OwnProps) {\n super(props);\n this.update = this.update.bind(this);\n this.state = selection(state.getSnapshot(), this.props);\n }\n\n static displayName = `GalenaComponent(${\n (WrappedComponent.displayName ?? WrappedComponent.name) || \"Component\"\n })`;\n\n public override componentDidMount() {\n this.listener = state.subscribe(state => this.update(state));\n }\n\n public override UNSAFE_componentWillReceiveProps(nextProps: OwnProps) {\n if (nextProps !== this.props) {\n this.update(state.getSnapshot(), nextProps);\n }\n }\n\n public override shouldComponentUpdate(\n nextProps: OwnProps,\n nextState: Selection,\n ) {\n return nextState !== this.state || nextProps !== this.props;\n }\n\n public override componentWillUnmount() {\n if (this.listener) {\n this.listener();\n this.listener = null;\n }\n }\n\n private update(nextState: ScopedState, ownProps = this.props) {\n this.setState(selection(nextState, ownProps));\n }\n\n public override render() {\n return <WrappedComponent {...this.props} {...this.state} />;\n }\n };\n };\n };\n};\n"],"mappings":";;AAIA,MAAa,WACX,UACG;AAEH,SAME,cACG;AAEH,UACE,qBACuD;AAEvD,UAAO,MAAM,wBAAwB,UAA+B;IAClE;IACA,WAAwC;IACxC,YAAY,OAAiB;AAC3B,WAAM,MAAM;AACZ,UAAK,SAAS,KAAK,OAAO,KAAK,KAAK;AACpC,UAAK,QAAQ,UAAU,MAAM,aAAa,EAAE,KAAK,MAAM;;IAGzD,OAAO,cAAc,oBAClB,iBAAiB,eAAe,iBAAiB,SAAS,YAC5D;IAED,oBAAoC;AAClC,UAAK,WAAW,MAAM,WAAU,UAAS,KAAK,OAAO,MAAM,CAAC;;IAG9D,iCAAiD,WAAqB;AACpE,SAAI,cAAc,KAAK,MACrB,MAAK,OAAO,MAAM,aAAa,EAAE,UAAU;;IAI/C,sBACE,WACA,WACA;AACA,YAAO,cAAc,KAAK,SAAS,cAAc,KAAK;;IAGxD,uBAAuC;AACrC,SAAI,KAAK,UAAU;AACjB,WAAK,UAAU;AACf,WAAK,WAAW;;;IAIpB,OAAe,WAAwB,WAAW,KAAK,OAAO;AAC5D,UAAK,SAAS,UAAU,WAAW,SAAS,CAAC;;IAG/C,SAAyB;AACvB,YAAO,CAAC,qBAAqB,KAAK,WAAW,KAAK"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const require_commonHooks = require("./commonHooks.cjs");
|
|
2
|
+
//#region src/createUseState.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create Use State
|
|
5
|
+
*
|
|
6
|
+
* A reuseable `State` consumer/creator for your react components.
|
|
7
|
+
* This hook can accept an exising instance of `State` and return
|
|
8
|
+
* a hook for deriving reactive state and updaters fn's for your components
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
12
|
+
*
|
|
13
|
+
* const myState = new State("<some-value>");
|
|
14
|
+
*
|
|
15
|
+
* export const useMyState = createUseState(myState);
|
|
16
|
+
*
|
|
17
|
+
* // In your components
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useMyState(
|
|
20
|
+
* // optional selector function
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can accept an optional selector function for deriving
|
|
30
|
+
* new values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
* const useMyState = createUseState(myState);
|
|
37
|
+
*
|
|
38
|
+
* const eight = useMyState(myState, state => deeply.nested.data * 2);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
const createUseState = (value) => {
|
|
42
|
+
return (selector = ((value) => value)) => {
|
|
43
|
+
return require_commonHooks.useStateHookAPI(value, require_commonHooks.useStableSelector(selector));
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.createUseState = createUseState;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { NonFunction, Setter } from "./types.cjs";
|
|
2
|
+
import { State } from "./State.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/createUseState.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create Use State
|
|
7
|
+
*
|
|
8
|
+
* A reuseable `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` and return
|
|
10
|
+
* a hook for deriving reactive state and updaters fn's for your components
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
14
|
+
*
|
|
15
|
+
* const myState = new State("<some-value>");
|
|
16
|
+
*
|
|
17
|
+
* export const useMyState = createUseState(myState);
|
|
18
|
+
*
|
|
19
|
+
* // In your components
|
|
20
|
+
* export const MyComponent = () => {
|
|
21
|
+
* const [state, setState] = useMyState(
|
|
22
|
+
* // optional selector function
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* // your jsx
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* `useState` can accept an optional selector function for deriving
|
|
32
|
+
* new values from a unit of state
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
36
|
+
*
|
|
37
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
38
|
+
* const useMyState = createUseState(myState);
|
|
39
|
+
*
|
|
40
|
+
* const eight = useMyState(myState, state => deeply.nested.data * 2);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare const createUseState: <T>(value: State<T>) => <U = T>(selector?: (value: NonFunction<T>) => U) => readonly [U, (setter: Setter<T>) => void];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createUseState };
|
|
46
|
+
//# sourceMappingURL=createUseState.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createUseState.d.cts","names":[],"sources":["../src/createUseState.ts"],"mappings":";;;;;;AA0CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,cAAA,MAAqB,KAAA,EAAO,KAAA,CAAM,CAAA,WACjC,CAAA,EACV,QAAA,IACE,KAAA,EAAO,WAAA,CAAY,CAAA,MAChB,CAAA,eAAC,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { NonFunction, Setter } from "./types.mjs";
|
|
2
|
+
import { State } from "./State.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/createUseState.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create Use State
|
|
7
|
+
*
|
|
8
|
+
* A reuseable `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` and return
|
|
10
|
+
* a hook for deriving reactive state and updaters fn's for your components
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
14
|
+
*
|
|
15
|
+
* const myState = new State("<some-value>");
|
|
16
|
+
*
|
|
17
|
+
* export const useMyState = createUseState(myState);
|
|
18
|
+
*
|
|
19
|
+
* // In your components
|
|
20
|
+
* export const MyComponent = () => {
|
|
21
|
+
* const [state, setState] = useMyState(
|
|
22
|
+
* // optional selector function
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* // your jsx
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* `useState` can accept an optional selector function for deriving
|
|
32
|
+
* new values from a unit of state
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
36
|
+
*
|
|
37
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
38
|
+
* const useMyState = createUseState(myState);
|
|
39
|
+
*
|
|
40
|
+
* const eight = useMyState(myState, state => deeply.nested.data * 2);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare const createUseState: <T>(value: State<T>) => <U = T>(selector?: (value: NonFunction<T>) => U) => readonly [U, (setter: Setter<T>) => void];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createUseState };
|
|
46
|
+
//# sourceMappingURL=createUseState.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createUseState.d.mts","names":[],"sources":["../src/createUseState.ts"],"mappings":";;;;;;AA0CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,cAAA,MAAqB,KAAA,EAAO,KAAA,CAAM,CAAA,WACjC,CAAA,EACV,QAAA,IACE,KAAA,EAAO,WAAA,CAAY,CAAA,MAChB,CAAA,eAAC,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useStableSelector, useStateHookAPI } from "./commonHooks.mjs";
|
|
2
|
+
//#region src/createUseState.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create Use State
|
|
5
|
+
*
|
|
6
|
+
* A reuseable `State` consumer/creator for your react components.
|
|
7
|
+
* This hook can accept an exising instance of `State` and return
|
|
8
|
+
* a hook for deriving reactive state and updaters fn's for your components
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
12
|
+
*
|
|
13
|
+
* const myState = new State("<some-value>");
|
|
14
|
+
*
|
|
15
|
+
* export const useMyState = createUseState(myState);
|
|
16
|
+
*
|
|
17
|
+
* // In your components
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useMyState(
|
|
20
|
+
* // optional selector function
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can accept an optional selector function for deriving
|
|
30
|
+
* new values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, createUseState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
* const useMyState = createUseState(myState);
|
|
37
|
+
*
|
|
38
|
+
* const eight = useMyState(myState, state => deeply.nested.data * 2);
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
const createUseState = (value) => {
|
|
42
|
+
return (selector = ((value) => value)) => {
|
|
43
|
+
return useStateHookAPI(value, useStableSelector(selector));
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { createUseState };
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=createUseState.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createUseState.mjs","names":[],"sources":["../src/createUseState.ts"],"sourcesContent":["import { useStableSelector, useStateHookAPI } from \"./commonHooks\";\nimport type { State } from \"./State\";\nimport type { NonFunction } from \"./types\";\n\n/**\n * Create Use State\n *\n * A reuseable `State` consumer/creator for your react components.\n * This hook can accept an exising instance of `State` and return\n * a hook for deriving reactive state and updaters fn's for your components\n *\n * ```typescript\n * import { State, createUseState } from \"@figliolia/galena\";\n *\n * const myState = new State(\"<some-value>\");\n *\n * export const useMyState = createUseState(myState);\n *\n * // In your components\n * export const MyComponent = () => {\n * const [state, setState] = useMyState(\n * // optional selector function\n * );\n *\n * return (\n * // your jsx\n * );\n * }\n * ```\n *\n * `useState` can accept an optional selector function for deriving\n * new values from a unit of state\n *\n * ```typescript\n * import { State, createUseState } from \"@figliolia/galena\";\n *\n * const myState = new State({ deeply: { nested: data: 4 } });\n * const useMyState = createUseState(myState);\n *\n * const eight = useMyState(myState, state => deeply.nested.data * 2);\n * ```\n */\nexport const createUseState = <T>(value: State<T>) => {\n return <U = T>(\n selector = ((value: NonFunction<T>) => value) as (\n value: NonFunction<T>,\n ) => U,\n ) => {\n const stableSelector = useStableSelector(selector);\n return useStateHookAPI(value, stableSelector);\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,MAAa,kBAAqB,UAAoB;AACpD,SACE,aAAa,UAA0B,WAGpC;AAEH,SAAO,gBAAgB,OADA,kBAAkB,SAAS,CACL"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_connect = require("./connect.cjs");
|
|
3
|
+
const require_connect_multi = require("./connect-multi.cjs");
|
|
4
|
+
const require_createUseState = require("./createUseState.cjs");
|
|
5
|
+
const require_Galena = require("./Galena.cjs");
|
|
6
|
+
const require_GalenaProvider = require("./GalenaProvider.cjs");
|
|
7
|
+
const require_Middleware = require("./Middleware.cjs");
|
|
8
|
+
const require_Logger = require("./Logger.cjs");
|
|
9
|
+
const require_Profiler = require("./Profiler.cjs");
|
|
10
|
+
const require_State = require("./State.cjs");
|
|
11
|
+
const require_useState = require("./useState.cjs");
|
|
12
|
+
exports.Galena = require_Galena.Galena;
|
|
13
|
+
exports.Logger = require_Logger.Logger;
|
|
14
|
+
exports.Middleware = require_Middleware.Middleware;
|
|
15
|
+
exports.Profiler = require_Profiler.Profiler;
|
|
16
|
+
exports.State = require_State.State;
|
|
17
|
+
exports.connect = require_connect.connect;
|
|
18
|
+
exports.connectMulti = require_connect_multi.connectMulti;
|
|
19
|
+
exports.createGalena = require_Galena.createGalena;
|
|
20
|
+
exports.createGalenaProvider = require_GalenaProvider.createGalenaProvider;
|
|
21
|
+
exports.createState = require_State.createState;
|
|
22
|
+
exports.createUseState = require_createUseState.createUseState;
|
|
23
|
+
exports.useState = require_useState.useState;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Middleware } from "./Middleware.cjs";
|
|
2
|
+
import { AppSubscriber, DerivedArguments, DerivedSelector, GalenaSnapshot, NonFunction, PartialSupport, SetComplement, SetDifference, Setter, StateTypes, Subscriber, Subtract } from "./types.cjs";
|
|
3
|
+
import { State, createState } from "./State.cjs";
|
|
4
|
+
import { connect } from "./connect.cjs";
|
|
5
|
+
import { ReactiveStates, connectMulti } from "./connect-multi.cjs";
|
|
6
|
+
import { createUseState } from "./createUseState.cjs";
|
|
7
|
+
import { Galena, createGalena } from "./Galena.cjs";
|
|
8
|
+
import { createGalenaProvider } from "./GalenaProvider.cjs";
|
|
9
|
+
import { Logger } from "./Logger.cjs";
|
|
10
|
+
import { Profiler } from "./Profiler.cjs";
|
|
11
|
+
import { useState } from "./useState.cjs";
|
|
12
|
+
export { AppSubscriber, DerivedArguments, DerivedSelector, Galena, GalenaSnapshot, Logger, Middleware, NonFunction, PartialSupport, Profiler, ReactiveStates, SetComplement, SetDifference, Setter, State, StateTypes, Subscriber, Subtract, connect, connectMulti, createGalena, createGalenaProvider, createState, createUseState, useState };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Middleware } from "./Middleware.mjs";
|
|
2
|
+
import { AppSubscriber, DerivedArguments, DerivedSelector, GalenaSnapshot, NonFunction, PartialSupport, SetComplement, SetDifference, Setter, StateTypes, Subscriber, Subtract } from "./types.mjs";
|
|
3
|
+
import { State, createState } from "./State.mjs";
|
|
4
|
+
import { connect } from "./connect.mjs";
|
|
5
|
+
import { ReactiveStates, connectMulti } from "./connect-multi.mjs";
|
|
6
|
+
import { createUseState } from "./createUseState.mjs";
|
|
7
|
+
import { Galena, createGalena } from "./Galena.mjs";
|
|
8
|
+
import { createGalenaProvider } from "./GalenaProvider.mjs";
|
|
9
|
+
import { Logger } from "./Logger.mjs";
|
|
10
|
+
import { Profiler } from "./Profiler.mjs";
|
|
11
|
+
import { useState } from "./useState.mjs";
|
|
12
|
+
export { AppSubscriber, DerivedArguments, DerivedSelector, Galena, GalenaSnapshot, Logger, Middleware, NonFunction, PartialSupport, Profiler, ReactiveStates, SetComplement, SetDifference, Setter, State, StateTypes, Subscriber, Subtract, connect, connectMulti, createGalena, createGalenaProvider, createState, createUseState, useState };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { connect } from "./connect.mjs";
|
|
2
|
+
import { connectMulti } from "./connect-multi.mjs";
|
|
3
|
+
import { createUseState } from "./createUseState.mjs";
|
|
4
|
+
import { Galena, createGalena } from "./Galena.mjs";
|
|
5
|
+
import { createGalenaProvider } from "./GalenaProvider.mjs";
|
|
6
|
+
import { Middleware } from "./Middleware.mjs";
|
|
7
|
+
import { Logger } from "./Logger.mjs";
|
|
8
|
+
import { Profiler } from "./Profiler.mjs";
|
|
9
|
+
import { State, createState } from "./State.mjs";
|
|
10
|
+
import { useState } from "./useState.mjs";
|
|
11
|
+
export { Galena, Logger, Middleware, Profiler, State, connect, connectMulti, createGalena, createGalenaProvider, createState, createUseState, useState };
|
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { State } from "./State.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type NonFunction<T> = T extends ((...args: any[]) => any) ? never : T;
|
|
5
|
+
type PartialSupport<T> = T extends Record<string, any> ? Partial<T> : T;
|
|
6
|
+
type Setter<T> = NonFunction<T> | ((prevState: NonFunction<T>) => NonFunction<T> | Promise<NonFunction<T>>);
|
|
7
|
+
type Subscriber<T> = ((state: NonFunction<T>) => void) | (() => void);
|
|
8
|
+
interface GalenaSnapshot<T extends Record<string, State<any>>, K extends Extract<keyof T, string> = Extract<keyof T, string>> {
|
|
9
|
+
updated: T[K];
|
|
10
|
+
state: T;
|
|
11
|
+
}
|
|
12
|
+
type AppSubscriber<T extends Record<string, State<any>>, K extends Extract<keyof T, string> = Extract<keyof T, string>> = ((payload: GalenaSnapshot<T, K>) => void) | (() => void);
|
|
13
|
+
type StateTypes<T extends Record<string, State<any>>> = ReturnType<T[keyof T]["getSnapshot"]>;
|
|
14
|
+
type SetDifference<A, B> = A extends B ? never : A;
|
|
15
|
+
type SetComplement<A, A1 extends A> = SetDifference<A, A1>;
|
|
16
|
+
type Subtract<T extends T1, T1 extends object> = Pick<T, SetComplement<keyof T, keyof T1>>;
|
|
17
|
+
type DerivedArguments<StateInstances extends State<any>[]> = { [I in keyof StateInstances]: ReturnType<StateInstances[I]["getSnapshot"]> } & {
|
|
18
|
+
length: number;
|
|
19
|
+
};
|
|
20
|
+
type DerivedSelector<StateInstances extends State<any>[]> = (state: DerivedArguments<StateInstances>, ownProps: any) => Record<string, any>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { AppSubscriber, DerivedArguments, DerivedSelector, GalenaSnapshot, NonFunction, PartialSupport, SetComplement, SetDifference, Setter, StateTypes, Subscriber, Subtract };
|
|
23
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;KAEY,WAAA,MAAiB,CAAA,cAAc,IAAA,2BAA8B,CAAA;AAAA,KAE7D,cAAA,MAAoB,CAAA,SAAU,MAAA,gBAAsB,OAAA,CAAQ,CAAA,IAAK,CAAA;AAAA,KAEjE,MAAA,MACR,WAAA,CAAY,CAAA,MACV,SAAA,EAAW,WAAA,CAAY,CAAA,MAAO,WAAA,CAAY,CAAA,IAAK,OAAA,CAAQ,WAAA,CAAY,CAAA;AAAA,KAE7D,UAAA,QAAkB,KAAA,EAAO,WAAA,CAAY,CAAA;AAAA,UAEhC,cAAA,WACL,MAAA,SAAe,KAAA,kBACf,OAAA,OAAc,CAAA,YAAa,OAAA,OAAc,CAAA;EAEnD,OAAA,EAAS,CAAA,CAAE,CAAA;EACX,KAAA,EAAO,CAAA;AAAA;AAAA,KAGG,aAAA,WACA,MAAA,SAAe,KAAA,kBACf,OAAA,OAAc,CAAA,YAAa,OAAA,OAAc,CAAA,eAC/C,OAAA,EAAS,cAAA,CAAe,CAAA,EAAG,CAAA;AAAA,KAErB,UAAA,WAAqB,MAAA,SAAe,KAAA,UAAe,UAAA,CAC7D,CAAA,OAAQ,CAAA;AAAA,KAGE,aAAA,SAAsB,CAAA,SAAU,CAAA,WAAY,CAAA;AAAA,KAE5C,aAAA,eAA4B,CAAA,IAAK,aAAA,CAAc,CAAA,EAAG,EAAA;AAAA,KAElD,QAAA,WAAmB,EAAA,uBAAyB,IAAA,CACtD,CAAA,EACA,aAAA,OAAoB,CAAA,QAAS,EAAA;AAAA,KAGnB,gBAAA,wBAAwC,KAAA,yBACtC,cAAA,GAAiB,UAAA,CAAW,cAAA,CAAe,CAAA;EACnD,MAAA;AAAA;AAAA,KAEM,eAAA,wBAAuC,KAAA,YACjD,KAAA,EAAO,gBAAA,CAAiB,cAAA,GACxB,QAAA,UACG,MAAA"}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { State } from "./State.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type NonFunction<T> = T extends ((...args: any[]) => any) ? never : T;
|
|
5
|
+
type PartialSupport<T> = T extends Record<string, any> ? Partial<T> : T;
|
|
6
|
+
type Setter<T> = NonFunction<T> | ((prevState: NonFunction<T>) => NonFunction<T> | Promise<NonFunction<T>>);
|
|
7
|
+
type Subscriber<T> = ((state: NonFunction<T>) => void) | (() => void);
|
|
8
|
+
interface GalenaSnapshot<T extends Record<string, State<any>>, K extends Extract<keyof T, string> = Extract<keyof T, string>> {
|
|
9
|
+
updated: T[K];
|
|
10
|
+
state: T;
|
|
11
|
+
}
|
|
12
|
+
type AppSubscriber<T extends Record<string, State<any>>, K extends Extract<keyof T, string> = Extract<keyof T, string>> = ((payload: GalenaSnapshot<T, K>) => void) | (() => void);
|
|
13
|
+
type StateTypes<T extends Record<string, State<any>>> = ReturnType<T[keyof T]["getSnapshot"]>;
|
|
14
|
+
type SetDifference<A, B> = A extends B ? never : A;
|
|
15
|
+
type SetComplement<A, A1 extends A> = SetDifference<A, A1>;
|
|
16
|
+
type Subtract<T extends T1, T1 extends object> = Pick<T, SetComplement<keyof T, keyof T1>>;
|
|
17
|
+
type DerivedArguments<StateInstances extends State<any>[]> = { [I in keyof StateInstances]: ReturnType<StateInstances[I]["getSnapshot"]> } & {
|
|
18
|
+
length: number;
|
|
19
|
+
};
|
|
20
|
+
type DerivedSelector<StateInstances extends State<any>[]> = (state: DerivedArguments<StateInstances>, ownProps: any) => Record<string, any>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { AppSubscriber, DerivedArguments, DerivedSelector, GalenaSnapshot, NonFunction, PartialSupport, SetComplement, SetDifference, Setter, StateTypes, Subscriber, Subtract };
|
|
23
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;KAEY,WAAA,MAAiB,CAAA,cAAc,IAAA,2BAA8B,CAAA;AAAA,KAE7D,cAAA,MAAoB,CAAA,SAAU,MAAA,gBAAsB,OAAA,CAAQ,CAAA,IAAK,CAAA;AAAA,KAEjE,MAAA,MACR,WAAA,CAAY,CAAA,MACV,SAAA,EAAW,WAAA,CAAY,CAAA,MAAO,WAAA,CAAY,CAAA,IAAK,OAAA,CAAQ,WAAA,CAAY,CAAA;AAAA,KAE7D,UAAA,QAAkB,KAAA,EAAO,WAAA,CAAY,CAAA;AAAA,UAEhC,cAAA,WACL,MAAA,SAAe,KAAA,kBACf,OAAA,OAAc,CAAA,YAAa,OAAA,OAAc,CAAA;EAEnD,OAAA,EAAS,CAAA,CAAE,CAAA;EACX,KAAA,EAAO,CAAA;AAAA;AAAA,KAGG,aAAA,WACA,MAAA,SAAe,KAAA,kBACf,OAAA,OAAc,CAAA,YAAa,OAAA,OAAc,CAAA,eAC/C,OAAA,EAAS,cAAA,CAAe,CAAA,EAAG,CAAA;AAAA,KAErB,UAAA,WAAqB,MAAA,SAAe,KAAA,UAAe,UAAA,CAC7D,CAAA,OAAQ,CAAA;AAAA,KAGE,aAAA,SAAsB,CAAA,SAAU,CAAA,WAAY,CAAA;AAAA,KAE5C,aAAA,eAA4B,CAAA,IAAK,aAAA,CAAc,CAAA,EAAG,EAAA;AAAA,KAElD,QAAA,WAAmB,EAAA,uBAAyB,IAAA,CACtD,CAAA,EACA,aAAA,OAAoB,CAAA,QAAS,EAAA;AAAA,KAGnB,gBAAA,wBAAwC,KAAA,yBACtC,cAAA,GAAiB,UAAA,CAAW,cAAA,CAAe,CAAA;EACnD,MAAA;AAAA;AAAA,KAEM,eAAA,wBAAuC,KAAA,YACjD,KAAA,EAAO,gBAAA,CAAiB,cAAA,GACxB,QAAA,UACG,MAAA"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const require_commonHooks = require("./commonHooks.cjs");
|
|
2
|
+
const require_State = require("./State.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/useState.ts
|
|
5
|
+
/**
|
|
6
|
+
* Use State
|
|
7
|
+
*
|
|
8
|
+
* A `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` or
|
|
10
|
+
* a stateful value and return reactive state and updaters
|
|
11
|
+
* for your components
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { State, useState } from "@figliolia/galena";
|
|
15
|
+
*
|
|
16
|
+
* const myState = new State("<some-value>");
|
|
17
|
+
*
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useState(myState);
|
|
20
|
+
* // or useState can be used a standalone replacement for React's useState
|
|
21
|
+
* const [state, setState] = useState("<some-value>");
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can also accept an optional selector function for deriving
|
|
30
|
+
* values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, useState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
*
|
|
37
|
+
* const eight = useState(myState, state => deeply.nested.data * 2);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const useState = (value, selector = ((value) => value)) => {
|
|
41
|
+
const stableSelector = require_commonHooks.useStableSelector(selector);
|
|
42
|
+
return require_commonHooks.useStateHookAPI((0, react.useMemo)(() => value instanceof require_State.State ? value : new require_State.State(value), [value]), stableSelector);
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.useState = useState;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { NonFunction, Setter } from "./types.cjs";
|
|
2
|
+
import { State } from "./State.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/useState.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Use State
|
|
7
|
+
*
|
|
8
|
+
* A `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` or
|
|
10
|
+
* a stateful value and return reactive state and updaters
|
|
11
|
+
* for your components
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { State, useState } from "@figliolia/galena";
|
|
15
|
+
*
|
|
16
|
+
* const myState = new State("<some-value>");
|
|
17
|
+
*
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useState(myState);
|
|
20
|
+
* // or useState can be used a standalone replacement for React's useState
|
|
21
|
+
* const [state, setState] = useState("<some-value>");
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can also accept an optional selector function for deriving
|
|
30
|
+
* values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, useState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
*
|
|
37
|
+
* const eight = useState(myState, state => deeply.nested.data * 2);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare const useState: <T, U = T>(value: NonFunction<T> | State<T>, selector?: (value: NonFunction<T>) => U) => readonly [U, (setter: Setter<T>) => void];
|
|
41
|
+
//#endregion
|
|
42
|
+
export { useState };
|
|
43
|
+
//# sourceMappingURL=useState.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useState.d.cts","names":[],"sources":["../src/useState.ts"],"mappings":";;;;;;AAwCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,QAAA,UAAmB,CAAA,EAC9B,KAAA,EAAO,WAAA,CAAY,CAAA,IAAK,KAAA,CAAM,CAAA,GAC9B,QAAA,IAAkD,KAAA,EAAO,WAAA,CAAY,CAAA,MAAO,CAAA,eAAC,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { NonFunction, Setter } from "./types.mjs";
|
|
2
|
+
import { State } from "./State.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/useState.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Use State
|
|
7
|
+
*
|
|
8
|
+
* A `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` or
|
|
10
|
+
* a stateful value and return reactive state and updaters
|
|
11
|
+
* for your components
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { State, useState } from "@figliolia/galena";
|
|
15
|
+
*
|
|
16
|
+
* const myState = new State("<some-value>");
|
|
17
|
+
*
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useState(myState);
|
|
20
|
+
* // or useState can be used a standalone replacement for React's useState
|
|
21
|
+
* const [state, setState] = useState("<some-value>");
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can also accept an optional selector function for deriving
|
|
30
|
+
* values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, useState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
*
|
|
37
|
+
* const eight = useState(myState, state => deeply.nested.data * 2);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare const useState: <T, U = T>(value: NonFunction<T> | State<T>, selector?: (value: NonFunction<T>) => U) => readonly [U, (setter: Setter<T>) => void];
|
|
41
|
+
//#endregion
|
|
42
|
+
export { useState };
|
|
43
|
+
//# sourceMappingURL=useState.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useState.d.mts","names":[],"sources":["../src/useState.ts"],"mappings":";;;;;;AAwCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,QAAA,UAAmB,CAAA,EAC9B,KAAA,EAAO,WAAA,CAAY,CAAA,IAAK,KAAA,CAAM,CAAA,GAC9B,QAAA,IAAkD,KAAA,EAAO,WAAA,CAAY,CAAA,MAAO,CAAA,eAAC,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,CAAA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useStableSelector, useStateHookAPI } from "./commonHooks.mjs";
|
|
2
|
+
import { State } from "./State.mjs";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
//#region src/useState.ts
|
|
5
|
+
/**
|
|
6
|
+
* Use State
|
|
7
|
+
*
|
|
8
|
+
* A `State` consumer/creator for your react components.
|
|
9
|
+
* This hook can accept an exising instance of `State` or
|
|
10
|
+
* a stateful value and return reactive state and updaters
|
|
11
|
+
* for your components
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { State, useState } from "@figliolia/galena";
|
|
15
|
+
*
|
|
16
|
+
* const myState = new State("<some-value>");
|
|
17
|
+
*
|
|
18
|
+
* export const MyComponent = () => {
|
|
19
|
+
* const [state, setState] = useState(myState);
|
|
20
|
+
* // or useState can be used a standalone replacement for React's useState
|
|
21
|
+
* const [state, setState] = useState("<some-value>");
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* // your jsx
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* `useState` can also accept an optional selector function for deriving
|
|
30
|
+
* values from a unit of state
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { State, useState } from "@figliolia/galena";
|
|
34
|
+
*
|
|
35
|
+
* const myState = new State({ deeply: { nested: data: 4 } });
|
|
36
|
+
*
|
|
37
|
+
* const eight = useState(myState, state => deeply.nested.data * 2);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const useState = (value, selector = ((value) => value)) => {
|
|
41
|
+
const stableSelector = useStableSelector(selector);
|
|
42
|
+
return useStateHookAPI(useMemo(() => value instanceof State ? value : new State(value), [value]), stableSelector);
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { useState };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=useState.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useState.mjs","names":[],"sources":["../src/useState.ts"],"sourcesContent":["import { useMemo } from \"react\";\nimport { useStableSelector, useStateHookAPI } from \"./commonHooks\";\nimport { State } from \"./State\";\nimport type { NonFunction } from \"./types\";\n\n/**\n * Use State\n *\n * A `State` consumer/creator for your react components.\n * This hook can accept an exising instance of `State` or\n * a stateful value and return reactive state and updaters\n * for your components\n *\n * ```typescript\n * import { State, useState } from \"@figliolia/galena\";\n *\n * const myState = new State(\"<some-value>\");\n *\n * export const MyComponent = () => {\n * const [state, setState] = useState(myState);\n * // or useState can be used a standalone replacement for React's useState\n * const [state, setState] = useState(\"<some-value>\");\n *\n * return (\n * // your jsx\n * );\n * }\n * ```\n *\n * `useState` can also accept an optional selector function for deriving\n * values from a unit of state\n *\n * ```typescript\n * import { State, useState } from \"@figliolia/galena\";\n *\n * const myState = new State({ deeply: { nested: data: 4 } });\n *\n * const eight = useState(myState, state => deeply.nested.data * 2);\n * ```\n */\nexport const useState = <T, U = T>(\n value: NonFunction<T> | State<T>,\n selector = ((value: NonFunction<T>) => value) as (value: NonFunction<T>) => U,\n) => {\n const stableSelector = useStableSelector(selector);\n const readable = useMemo(\n () => (value instanceof State ? value : new State(value)),\n [value],\n );\n return useStateHookAPI(readable, stableSelector);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,MAAa,YACX,OACA,aAAa,UAA0B,WACpC;CACH,MAAM,iBAAiB,kBAAkB,SAAS;AAKlD,QAAO,gBAJU,cACR,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,MAAM,EACxD,CAAC,MAAM,CACR,EACgC,eAAe"}
|
|
Binary file
|
|
Binary file
|