@bemedev/app-reactjs 1.8.0 → 1.9.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 +7 -7
- package/lib/useService.cjs +6 -5
- package/lib/useService.cjs.map +1 -1
- package/lib/useService.d.ts +7 -3
- package/lib/useService.d.ts.map +1 -1
- package/lib/useService.js +6 -5
- package/lib/useService.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -32,10 +32,10 @@ service.start();
|
|
|
32
32
|
|
|
33
33
|
function MyComponent() {
|
|
34
34
|
// 1. Select a specific slice of context
|
|
35
|
-
const count = useService(service, s => s.context.count);
|
|
35
|
+
const count = useService(service, { selector: s => s.context.count });
|
|
36
36
|
|
|
37
37
|
// 2. Or track the active state value
|
|
38
|
-
const stateValue = useService(service, s => s.value);
|
|
38
|
+
const stateValue = useService(service, { selector: s => s.value });
|
|
39
39
|
|
|
40
40
|
// 3. Or track the whole state object
|
|
41
41
|
const state = useService(service);
|
|
@@ -53,15 +53,15 @@ function MyComponent() {
|
|
|
53
53
|
|
|
54
54
|
## API Reference
|
|
55
55
|
|
|
56
|
-
### `useService(service,
|
|
56
|
+
### `useService(service, options?)`
|
|
57
57
|
|
|
58
58
|
Creates a React state hook that updates whenever the interpreter
|
|
59
59
|
transitions, using deep comparison to prevent unnecessary renders.
|
|
60
60
|
|
|
61
|
-
| Parameter
|
|
62
|
-
|
|
|
63
|
-
| `service`
|
|
64
|
-
| `
|
|
61
|
+
| Parameter | Type | Description |
|
|
62
|
+
| --------- | ------------------- | ----------------------------------------------------------------------------------- |
|
|
63
|
+
| `service` | `Interpreter` | The `@bemedev/app` interpreter service instance. |
|
|
64
|
+
| `options` | `UseServiceOptions` | Optional object containing `selector` and/or custom `equality` comparison function. |
|
|
65
65
|
|
|
66
66
|
Returns the selected state or slice.
|
|
67
67
|
|
package/lib/useService.cjs
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let
|
|
2
|
+
let _bemedev_app = require("@bemedev/app");
|
|
3
3
|
let react = require("react");
|
|
4
4
|
//#region src/useService.ts
|
|
5
5
|
/**
|
|
6
6
|
* A hook that creates a React state from a service with a subscribe function.
|
|
7
7
|
*
|
|
8
8
|
* @param service - The service containing the state and subscribe method.
|
|
9
|
-
* @param
|
|
9
|
+
* @param options - Optional configuration options for selection and equality comparison.
|
|
10
10
|
* @returns The selected state.
|
|
11
11
|
*/
|
|
12
|
-
function useService(service,
|
|
12
|
+
function useService(service, options) {
|
|
13
|
+
const { selector = (s) => s, equality = (first, next) => (0, _bemedev_app.deepEqual)(first, next) } = options ?? {};
|
|
13
14
|
const [state, setState] = (0, react.useState)(() => selector(service.state));
|
|
14
15
|
const sub = service.subscribe((nextState) => setState(selector(nextState)), { equals: (first, next) => {
|
|
15
16
|
const _first = selector(first);
|
|
16
17
|
const _next = selector(next);
|
|
17
|
-
return (
|
|
18
|
+
return equality(_first, _next);
|
|
18
19
|
} });
|
|
19
|
-
(0, react.useEffect)(() => sub.unsubscribe, [
|
|
20
|
+
(0, react.useEffect)(() => sub.unsubscribe, []);
|
|
20
21
|
return state;
|
|
21
22
|
}
|
|
22
23
|
//#endregion
|
package/lib/useService.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useService.cjs","names":["
|
|
1
|
+
{"version":3,"file":"useService.cjs","names":["deepEqual","useState"],"sources":["../src/useService.ts"],"sourcesContent":["import type {\n AddSubscriber_F,\n EventObject,\n PrimitiveObject,\n State,\n} from '@bemedev/app';\nimport { deepEqual } from '@bemedev/app';\nimport { useEffect, useState } from 'react';\n\nexport type UseServiceOptions<\n Tc extends PrimitiveObject,\n Ta extends string,\n Eo extends EventObject,\n T = State<Eo, Tc, Ta>,\n> = {\n selector?: (state: State<Eo, Tc, Ta>) => T;\n equality?: (first: T, next: T) => boolean;\n};\n\n/**\n * A hook that creates a React state from a service with a subscribe function.\n *\n * @param service - The service containing the state and subscribe method.\n * @param options - Optional configuration options for selection and equality comparison.\n * @returns The selected state.\n */\nexport function useService<\n Tc extends PrimitiveObject,\n Ta extends string,\n Eo extends EventObject,\n T = State<Eo, Tc, Ta>,\n>(\n service: {\n subscribe: AddSubscriber_F<Tc, Ta, Eo>;\n state: State<Eo, Tc, Ta>;\n },\n options?: UseServiceOptions<Tc, Ta, Eo, T>,\n): T {\n const {\n selector = (s: State<Eo, Tc, Ta>) => s as T,\n equality = (first: T, next: T) => deepEqual(first, next),\n } = options ?? {};\n\n const [state, setState] = useState<T>(() => selector(service.state));\n\n const sub = service.subscribe(\n nextState => setState(selector(nextState)),\n {\n equals: (first, next) => {\n const _first = selector(first);\n const _next = selector(next);\n return equality(_first, _next);\n },\n },\n );\n\n useEffect(() => sub.unsubscribe, []);\n return state;\n}\n"],"mappings":";;;;;;;;;;;AA0BA,SAAgB,WAMd,SAIA,SACG;CACH,MAAM,EACJ,YAAY,MAAyB,GACrC,YAAY,OAAU,UAAA,GAAYA,aAAAA,UAAAA,CAAU,OAAO,IAAI,MACrD,WAAW,CAAC;CAEhB,MAAM,CAAC,OAAO,aAAA,GAAYC,MAAAA,SAAAA,OAAkB,SAAS,QAAQ,KAAK,CAAC;CAEnE,MAAM,MAAM,QAAQ,WAClB,cAAa,SAAS,SAAS,SAAS,CAAC,GACzC,EACE,SAAS,OAAO,SAAS;EACvB,MAAM,SAAS,SAAS,KAAK;EAC7B,MAAM,QAAQ,SAAS,IAAI;EAC3B,OAAO,SAAS,QAAQ,KAAK;CAC/B,EACF,CACF;CAEA,CAAA,GAAA,MAAA,UAAA,OAAgB,IAAI,aAAa,CAAC,CAAC;CACnC,OAAO;AACT"}
|
package/lib/useService.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import type { AddSubscriber_F, EventObject, PrimitiveObject, State } from '@bemedev/app
|
|
1
|
+
import type { AddSubscriber_F, EventObject, PrimitiveObject, State } from '@bemedev/app';
|
|
2
|
+
export type UseServiceOptions<Tc extends PrimitiveObject, Ta extends string, Eo extends EventObject, T = State<Eo, Tc, Ta>> = {
|
|
3
|
+
selector?: (state: State<Eo, Tc, Ta>) => T;
|
|
4
|
+
equality?: (first: T, next: T) => boolean;
|
|
5
|
+
};
|
|
2
6
|
/**
|
|
3
7
|
* A hook that creates a React state from a service with a subscribe function.
|
|
4
8
|
*
|
|
5
9
|
* @param service - The service containing the state and subscribe method.
|
|
6
|
-
* @param
|
|
10
|
+
* @param options - Optional configuration options for selection and equality comparison.
|
|
7
11
|
* @returns The selected state.
|
|
8
12
|
*/
|
|
9
13
|
export declare function useService<Tc extends PrimitiveObject, Ta extends string, Eo extends EventObject, T = State<Eo, Tc, Ta>>(service: {
|
|
10
14
|
subscribe: AddSubscriber_F<Tc, Ta, Eo>;
|
|
11
15
|
state: State<Eo, Tc, Ta>;
|
|
12
|
-
},
|
|
16
|
+
}, options?: UseServiceOptions<Tc, Ta, Eo, T>): T;
|
|
13
17
|
//# sourceMappingURL=useService.d.ts.map
|
package/lib/useService.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useService.d.ts","sourceRoot":"","sources":["../src/useService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,WAAW,EACX,eAAe,EACf,KAAK,EACN,MAAM,
|
|
1
|
+
{"version":3,"file":"useService.d.ts","sourceRoot":"","sources":["../src/useService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,WAAW,EACX,eAAe,EACf,KAAK,EACN,MAAM,cAAc,CAAC;AAItB,MAAM,MAAM,iBAAiB,CAC3B,EAAE,SAAS,eAAe,EAC1B,EAAE,SAAS,MAAM,EACjB,EAAE,SAAS,WAAW,EACtB,CAAC,GAAG,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,IACnB;IACF,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IAC3C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC;CAC3C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,EAAE,SAAS,eAAe,EAC1B,EAAE,SAAS,MAAM,EACjB,EAAE,SAAS,WAAW,EACtB,CAAC,GAAG,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAErB,OAAO,EAAE;IACP,SAAS,EAAE,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACvC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;CAC1B,EACD,OAAO,CAAC,EAAE,iBAAiB,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,GACzC,CAAC,CAqBH"}
|
package/lib/useService.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import { deepEqual } from "@bemedev/app
|
|
1
|
+
import { deepEqual } from "@bemedev/app";
|
|
2
2
|
import { useEffect, useState } from "react";
|
|
3
3
|
//#region src/useService.ts
|
|
4
4
|
/**
|
|
5
5
|
* A hook that creates a React state from a service with a subscribe function.
|
|
6
6
|
*
|
|
7
7
|
* @param service - The service containing the state and subscribe method.
|
|
8
|
-
* @param
|
|
8
|
+
* @param options - Optional configuration options for selection and equality comparison.
|
|
9
9
|
* @returns The selected state.
|
|
10
10
|
*/
|
|
11
|
-
function useService(service,
|
|
11
|
+
function useService(service, options) {
|
|
12
|
+
const { selector = (s) => s, equality = (first, next) => deepEqual(first, next) } = options ?? {};
|
|
12
13
|
const [state, setState] = useState(() => selector(service.state));
|
|
13
14
|
const sub = service.subscribe((nextState) => setState(selector(nextState)), { equals: (first, next) => {
|
|
14
15
|
const _first = selector(first);
|
|
15
16
|
const _next = selector(next);
|
|
16
|
-
return
|
|
17
|
+
return equality(_first, _next);
|
|
17
18
|
} });
|
|
18
|
-
useEffect(() => sub.unsubscribe, [
|
|
19
|
+
useEffect(() => sub.unsubscribe, []);
|
|
19
20
|
return state;
|
|
20
21
|
}
|
|
21
22
|
//#endregion
|
package/lib/useService.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useService.js","names":[],"sources":["../src/useService.ts"],"sourcesContent":["import type {\n AddSubscriber_F,\n EventObject,\n PrimitiveObject,\n State,\n} from '@bemedev/app
|
|
1
|
+
{"version":3,"file":"useService.js","names":[],"sources":["../src/useService.ts"],"sourcesContent":["import type {\n AddSubscriber_F,\n EventObject,\n PrimitiveObject,\n State,\n} from '@bemedev/app';\nimport { deepEqual } from '@bemedev/app';\nimport { useEffect, useState } from 'react';\n\nexport type UseServiceOptions<\n Tc extends PrimitiveObject,\n Ta extends string,\n Eo extends EventObject,\n T = State<Eo, Tc, Ta>,\n> = {\n selector?: (state: State<Eo, Tc, Ta>) => T;\n equality?: (first: T, next: T) => boolean;\n};\n\n/**\n * A hook that creates a React state from a service with a subscribe function.\n *\n * @param service - The service containing the state and subscribe method.\n * @param options - Optional configuration options for selection and equality comparison.\n * @returns The selected state.\n */\nexport function useService<\n Tc extends PrimitiveObject,\n Ta extends string,\n Eo extends EventObject,\n T = State<Eo, Tc, Ta>,\n>(\n service: {\n subscribe: AddSubscriber_F<Tc, Ta, Eo>;\n state: State<Eo, Tc, Ta>;\n },\n options?: UseServiceOptions<Tc, Ta, Eo, T>,\n): T {\n const {\n selector = (s: State<Eo, Tc, Ta>) => s as T,\n equality = (first: T, next: T) => deepEqual(first, next),\n } = options ?? {};\n\n const [state, setState] = useState<T>(() => selector(service.state));\n\n const sub = service.subscribe(\n nextState => setState(selector(nextState)),\n {\n equals: (first, next) => {\n const _first = selector(first);\n const _next = selector(next);\n return equality(_first, _next);\n },\n },\n );\n\n useEffect(() => sub.unsubscribe, []);\n return state;\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAgB,WAMd,SAIA,SACG;CACH,MAAM,EACJ,YAAY,MAAyB,GACrC,YAAY,OAAU,SAAY,UAAU,OAAO,IAAI,MACrD,WAAW,CAAC;CAEhB,MAAM,CAAC,OAAO,YAAY,eAAkB,SAAS,QAAQ,KAAK,CAAC;CAEnE,MAAM,MAAM,QAAQ,WAClB,cAAa,SAAS,SAAS,SAAS,CAAC,GACzC,EACE,SAAS,OAAO,SAAS;EACvB,MAAM,SAAS,SAAS,KAAK;EAC7B,MAAM,QAAQ,SAAS,IAAI;EAC3B,OAAO,SAAS,QAAQ,KAAK;CAC/B,EACF,CACF;CAEA,gBAAgB,IAAI,aAAa,CAAC,CAAC;CACnC,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemedev/app-reactjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Middleware between @bemedev/app and reactjs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -74,12 +74,12 @@
|
|
|
74
74
|
"tsx": "^4.23.1",
|
|
75
75
|
"typescript": "^6.0.3",
|
|
76
76
|
"vitest": "^4.1.10",
|
|
77
|
-
"@bemedev/app": "^1.
|
|
78
|
-
"@bemedev/app-vitest": "^1.
|
|
77
|
+
"@bemedev/app": "^1.9.0",
|
|
78
|
+
"@bemedev/app-vitest": "^1.9.0"
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
81
|
"react": "^19.2.8",
|
|
82
|
-
"@bemedev/app": "^1.
|
|
82
|
+
"@bemedev/app": "^1.9.0"
|
|
83
83
|
},
|
|
84
84
|
"size-limit": [
|
|
85
85
|
{
|