@wirestate/react 0.6.3 → 0.7.0-experimental.2
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 +27 -1
- package/README.md +109 -25
- package/cjs/development/index.js +462 -222
- package/cjs/development/index.js.map +1 -1
- package/cjs/development/lib.js +167 -45
- package/cjs/development/lib.js.map +1 -1
- package/cjs/development/test-utils.js +22 -11
- package/cjs/development/test-utils.js.map +1 -1
- package/cjs/production/index.js +1 -1
- package/cjs/production/index.js.map +1 -1
- package/cjs/production/lib.js +1 -1
- package/cjs/production/lib.js.map +1 -1
- package/cjs/production/test-utils.js +1 -1
- package/cjs/production/test-utils.js.map +1 -1
- package/esm/development/commands/use-command-caller.js +16 -3
- package/esm/development/commands/use-command-caller.js.map +1 -1
- package/esm/development/commands/use-command-handler.js +20 -4
- package/esm/development/commands/use-command-handler.js.map +1 -1
- package/esm/development/commands/use-optional-command-caller.js +20 -4
- package/esm/development/commands/use-optional-command-caller.js.map +1 -1
- package/esm/development/context/container-context.js +16 -0
- package/esm/development/context/container-context.js.map +1 -0
- package/esm/development/context/use-container.js +33 -0
- package/esm/development/context/use-container.js.map +1 -0
- package/esm/development/context/use-root-container.js +35 -0
- package/esm/development/context/use-root-container.js.map +1 -0
- package/esm/development/context/use-scope.js +31 -0
- package/esm/development/context/use-scope.js.map +1 -0
- package/esm/development/error/error-code.js +1 -3
- package/esm/development/error/error-code.js.map +1 -1
- package/esm/development/events/use-event-emitter.js +20 -4
- package/esm/development/events/use-event-emitter.js.map +1 -1
- package/esm/development/events/use-event.js +18 -4
- package/esm/development/events/use-event.js.map +1 -1
- package/esm/development/events/use-events-handler.js +18 -3
- package/esm/development/events/use-events-handler.js.map +1 -1
- package/esm/development/events/use-events.js +18 -4
- package/esm/development/events/use-events.js.map +1 -1
- package/esm/development/index.js +12 -10
- package/esm/development/index.js.map +1 -1
- package/esm/development/injection/use-injection.js +36 -0
- package/esm/development/injection/use-injection.js.map +1 -0
- package/esm/development/injection/use-optional-injection.js +40 -0
- package/esm/development/injection/use-optional-injection.js.map +1 -0
- package/esm/development/provision/container-activator.js +33 -0
- package/esm/development/provision/container-activator.js.map +1 -0
- package/esm/development/provision/container-provider.js +74 -0
- package/esm/development/provision/container-provider.js.map +1 -0
- package/esm/development/provision/sub-container-provider.js +69 -0
- package/esm/development/provision/sub-container-provider.js.map +1 -0
- package/esm/development/provision/use-container-provision-state.js +76 -0
- package/esm/development/provision/use-container-provision-state.js.map +1 -0
- package/esm/development/queries/use-optional-query-caller.js +16 -4
- package/esm/development/queries/use-optional-query-caller.js.map +1 -1
- package/esm/development/queries/use-optional-sync-query-caller.js +14 -3
- package/esm/development/queries/use-optional-sync-query-caller.js.map +1 -1
- package/esm/development/queries/use-query-caller.js +15 -3
- package/esm/development/queries/use-query-caller.js.map +1 -1
- package/esm/development/queries/use-query-handler.js +21 -5
- package/esm/development/queries/use-query-handler.js.map +1 -1
- package/esm/development/queries/use-sync-query-caller.js +15 -3
- package/esm/development/queries/use-sync-query-caller.js.map +1 -1
- package/esm/development/test-utils/with-container-provider.js +35 -0
- package/esm/development/test-utils/with-container-provider.js.map +1 -0
- package/esm/development/test-utils.js +1 -1
- package/esm/development/utils/shallow-equal-arrays.js +28 -0
- package/esm/development/utils/shallow-equal-arrays.js.map +1 -0
- package/esm/production/commands/use-command-caller.js +1 -1
- package/esm/production/commands/use-command-caller.js.map +1 -1
- package/esm/production/commands/use-command-handler.js +1 -1
- package/esm/production/commands/use-command-handler.js.map +1 -1
- package/esm/production/commands/use-optional-command-caller.js +1 -1
- package/esm/production/commands/use-optional-command-caller.js.map +1 -1
- package/esm/production/context/container-context.js +1 -0
- package/esm/production/context/container-context.js.map +1 -0
- package/esm/production/context/use-container.js +1 -0
- package/esm/production/context/use-container.js.map +1 -0
- package/esm/production/context/use-root-container.js +1 -0
- package/esm/production/context/use-root-container.js.map +1 -0
- package/esm/production/context/use-scope.js +1 -0
- package/esm/production/context/use-scope.js.map +1 -0
- package/esm/production/error/error-code.js +1 -1
- package/esm/production/error/error-code.js.map +1 -1
- package/esm/production/events/use-event-emitter.js +1 -1
- package/esm/production/events/use-event-emitter.js.map +1 -1
- package/esm/production/events/use-event.js +1 -1
- package/esm/production/events/use-event.js.map +1 -1
- package/esm/production/events/use-events-handler.js +1 -1
- package/esm/production/events/use-events-handler.js.map +1 -1
- package/esm/production/events/use-events.js +1 -1
- package/esm/production/events/use-events.js.map +1 -1
- package/esm/production/index.js +1 -1
- package/esm/production/injection/use-injection.js +1 -0
- package/esm/production/injection/use-injection.js.map +1 -0
- package/esm/production/injection/use-optional-injection.js +1 -0
- package/esm/production/injection/use-optional-injection.js.map +1 -0
- package/esm/production/provision/container-activator.js +1 -0
- package/esm/production/provision/container-activator.js.map +1 -0
- package/esm/production/provision/container-provider.js +1 -0
- package/esm/production/provision/container-provider.js.map +1 -0
- package/esm/production/provision/sub-container-provider.js +1 -0
- package/esm/production/provision/sub-container-provider.js.map +1 -0
- package/esm/production/provision/use-container-provision-state.js +1 -0
- package/esm/production/provision/use-container-provision-state.js.map +1 -0
- package/esm/production/queries/use-optional-query-caller.js +1 -1
- package/esm/production/queries/use-optional-query-caller.js.map +1 -1
- package/esm/production/queries/use-optional-sync-query-caller.js +1 -1
- package/esm/production/queries/use-optional-sync-query-caller.js.map +1 -1
- package/esm/production/queries/use-query-caller.js +1 -1
- package/esm/production/queries/use-query-caller.js.map +1 -1
- package/esm/production/queries/use-query-handler.js +1 -1
- package/esm/production/queries/use-query-handler.js.map +1 -1
- package/esm/production/queries/use-sync-query-caller.js +1 -1
- package/esm/production/queries/use-sync-query-caller.js.map +1 -1
- package/esm/production/test-utils/with-container-provider.js +1 -0
- package/esm/production/test-utils/with-container-provider.js.map +1 -0
- package/esm/production/test-utils.js +1 -1
- package/esm/production/utils/shallow-equal-arrays.js +1 -0
- package/esm/production/utils/shallow-equal-arrays.js.map +1 -0
- package/index.d.ts +554 -137
- package/package.json +2 -2
- package/test-utils.d.ts +19 -8
- package/esm/development/provision/create-injectables-provider.js +0 -101
- package/esm/development/provision/create-injectables-provider.js.map +0 -1
- package/esm/development/provision/ioc-context.js +0 -11
- package/esm/development/provision/ioc-context.js.map +0 -1
- package/esm/development/provision/ioc-provider.js +0 -50
- package/esm/development/provision/ioc-provider.js.map +0 -1
- package/esm/development/provision/use-container-revision.js +0 -13
- package/esm/development/provision/use-container-revision.js.map +0 -1
- package/esm/development/provision/use-container.js +0 -13
- package/esm/development/provision/use-container.js.map +0 -1
- package/esm/development/provision/use-injection.js +0 -22
- package/esm/development/provision/use-injection.js.map +0 -1
- package/esm/development/provision/use-ioc-context.js +0 -21
- package/esm/development/provision/use-ioc-context.js.map +0 -1
- package/esm/development/provision/use-optional-injection.js +0 -29
- package/esm/development/provision/use-optional-injection.js.map +0 -1
- package/esm/development/test-utils/with-ioc-provider.js +0 -24
- package/esm/development/test-utils/with-ioc-provider.js.map +0 -1
- package/esm/production/provision/create-injectables-provider.js +0 -1
- package/esm/production/provision/create-injectables-provider.js.map +0 -1
- package/esm/production/provision/ioc-context.js +0 -1
- package/esm/production/provision/ioc-context.js.map +0 -1
- package/esm/production/provision/ioc-provider.js +0 -1
- package/esm/production/provision/ioc-provider.js.map +0 -1
- package/esm/production/provision/use-container-revision.js +0 -1
- package/esm/production/provision/use-container-revision.js.map +0 -1
- package/esm/production/provision/use-container.js +0 -1
- package/esm/production/provision/use-container.js.map +0 -1
- package/esm/production/provision/use-injection.js +0 -1
- package/esm/production/provision/use-injection.js.map +0 -1
- package/esm/production/provision/use-ioc-context.js +0 -1
- package/esm/production/provision/use-ioc-context.js.map +0 -1
- package/esm/production/provision/use-optional-injection.js +0 -1
- package/esm/production/provision/use-optional-injection.js.map +0 -1
- package/esm/production/test-utils/with-ioc-provider.js +0 -1
- package/esm/production/test-utils/with-ioc-provider.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
## Unreleased
|
|
2
|
+
|
|
3
|
+
- Add `useScope` in `@wirestate/react`
|
|
4
|
+
- New lit elements modules - `@wirestate/lit` and `@wirestate/lit-signals`
|
|
5
|
+
- `EventBus`: add `unsubscribe` method for explicit handler removal by reference
|
|
6
|
+
- `QueryBus`: add `unregister` method for explicit handler removal by type and reference
|
|
7
|
+
- `CommandBus`: add `unregister` method for explicit handler removal by type and reference
|
|
8
|
+
- `WireScope`: add new event/command/query subscribe-unsubscribe methods
|
|
9
|
+
- Export more alias / methods from `@wirestate/core`
|
|
10
|
+
- Export more alias / methods from `@wirestate/react-mobx`
|
|
11
|
+
- Export missing methods typing for `@wirestate/core`
|
|
12
|
+
- Extensive JSDoc coverage for wirestate packages
|
|
13
|
+
- `createIocContainer`: Removed in favor of `createContainer`
|
|
14
|
+
- `createContainer`: Added ability to instantly provide and activate entries, targeted seeds
|
|
15
|
+
- `createInjectablesProvider`: Removed.
|
|
16
|
+
- `IocProvider`: Removed
|
|
17
|
+
- `useRootContainer`: Added separate hook for store management in React tree
|
|
18
|
+
- `ContainerProvider`: Simpler provider for containers.
|
|
19
|
+
- `SubContainerProvider`: Added component solving problems of removed `createInjectablesProvider`
|
|
20
|
+
- `ContainerActivator`: Added separate activation component
|
|
21
|
+
- Replace IoC-context provision APIs with `ContainerContext`, `containerProvide`, `ContainerProvider`, and `useContainerProvision`
|
|
22
|
+
- Replace injectables-provider APIs with `subContainerProvide`, `SubContainerProvider`, and `useSubContainerProvider`
|
|
23
|
+
- Provide plain `Container` values through Lit context instead of wrapper objects
|
|
24
|
+
- Recreate managed child containers when the parent container context changes
|
|
25
|
+
- Add `useContainer` and `useScope` consumers in `@wirestate/lit`
|
|
26
|
+
|
|
1
27
|
## 0.6.3
|
|
2
28
|
|
|
3
29
|
- Update readme files for each module
|
|
@@ -59,6 +85,6 @@
|
|
|
59
85
|
- useService -> useInjection
|
|
60
86
|
- AbstractService::getService -> AbstractService::resolve
|
|
61
87
|
|
|
62
|
-
## 0.1.
|
|
88
|
+
## 0.1.0
|
|
63
89
|
|
|
64
90
|
- Initial release
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @wirestate/react
|
|
1
|
+
# @wirestate/react [[monorepo](https://github.com/Neloreck/wirestate)] [[docs](https://neloreck.github.io/wirestate/)]
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@wirestate/react)
|
|
4
4
|
[](https://github.com/Neloreck/wirestate/blob/master/LICENSE)
|
|
@@ -13,18 +13,79 @@ npm install @wirestate/core @wirestate/react reflect-metadata
|
|
|
13
13
|
|
|
14
14
|
## Providers
|
|
15
15
|
|
|
16
|
-
### `
|
|
16
|
+
### `ContainerProvider`
|
|
17
17
|
|
|
18
18
|
Root provider. Creates the top-level IoC container. Place once near the root of your application.
|
|
19
19
|
|
|
20
|
+
With globally declared container:
|
|
21
|
+
|
|
20
22
|
```tsx
|
|
21
|
-
import {
|
|
23
|
+
import { createContainer, Container } from "@wirestate/core";
|
|
24
|
+
import { ContainerProvider } from "@wirestate/react";
|
|
25
|
+
import { CounterService, LoggerService } from "./services";
|
|
26
|
+
|
|
27
|
+
const container: Container = createContainer({
|
|
28
|
+
entries: [CounterService, LoggerService],
|
|
29
|
+
activate: [LoggerService],
|
|
30
|
+
});
|
|
22
31
|
|
|
23
|
-
export function
|
|
32
|
+
export function Application() {
|
|
24
33
|
return (
|
|
25
|
-
<
|
|
34
|
+
<ContainerProvider container={container}>
|
|
26
35
|
<SomeComponent />
|
|
27
|
-
</
|
|
36
|
+
</ContainerProvider>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
With locally declared container:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { createContainer, Container } from "@wirestate/core";
|
|
45
|
+
import { ContaierProvider, ContainerActivator, useRootContainer } from "@wirestate/react";
|
|
46
|
+
import { CounterService, LoggerService } from "./services";
|
|
47
|
+
|
|
48
|
+
export function Application() {
|
|
49
|
+
const container: Container = useRootContainer(
|
|
50
|
+
() =>
|
|
51
|
+
createContainer({
|
|
52
|
+
entries: [CounterService, LoggerService],
|
|
53
|
+
}),
|
|
54
|
+
[]
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<ContaierProvider container={container}>
|
|
59
|
+
<ContainerActivator activate={[LoggerService]}>
|
|
60
|
+
<SomeComponent />
|
|
61
|
+
</ContainerActivator>
|
|
62
|
+
</ContaierProvider>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `useRootContainer(factory, deps)`
|
|
68
|
+
|
|
69
|
+
Creates and memoizes a root container for the component instance.
|
|
70
|
+
The factory runs again only when `deps` change or on dev mode HMR refreshment.
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import { Container, createContainer } from "@wirestate/core";
|
|
74
|
+
import { useRootContainer, ContainerProvider } from "@wirestate/react";
|
|
75
|
+
|
|
76
|
+
function Root() {
|
|
77
|
+
const container: Container = useRootContainer(
|
|
78
|
+
() =>
|
|
79
|
+
createContainer({
|
|
80
|
+
entries: [CounterService, LoggerService],
|
|
81
|
+
}),
|
|
82
|
+
[]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<ContainerProvider container={container}>
|
|
87
|
+
<SomeComponent />
|
|
88
|
+
</ContainerProvider>
|
|
28
89
|
);
|
|
29
90
|
}
|
|
30
91
|
```
|
|
@@ -32,11 +93,11 @@ export function App() {
|
|
|
32
93
|
### `createInjectablesProvider`
|
|
33
94
|
|
|
34
95
|
Creates a component that binds a set of services into a child container scoped to the component's lifetime.
|
|
35
|
-
Services are activated on mount and deactivated on unmount. Expects to be under `
|
|
96
|
+
Services are activated on mount and deactivated on unmount. Expects to be under `ContainerProvider` tree.
|
|
36
97
|
|
|
37
98
|
```tsx
|
|
38
|
-
import { createInjectablesProvider } from
|
|
39
|
-
import { CounterService, LoggerService } from
|
|
99
|
+
import { createInjectablesProvider } from "@wirestate/react";
|
|
100
|
+
import { CounterService, LoggerService } from "./services";
|
|
40
101
|
|
|
41
102
|
const InjectionProvider = createInjectablesProvider([CounterService, LoggerService]);
|
|
42
103
|
|
|
@@ -51,12 +112,31 @@ export function CounterPage() {
|
|
|
51
112
|
|
|
52
113
|
**Props:**
|
|
53
114
|
|
|
54
|
-
| Prop
|
|
55
|
-
|
|
56
|
-
| `seed` | `object` | Shared seed injected into all services via `@Inject(SEED)` |
|
|
115
|
+
| Prop | Type | Description |
|
|
116
|
+
| ------- | ------------- | ----------------------------------------------------------- |
|
|
57
117
|
| `seeds` | `SeedEntries` | Per-service seeds, e.g. `[[CounterService, { count: 10 }]]` |
|
|
58
118
|
|
|
59
|
-
|
|
119
|
+
`seeds` are applied on first render only.
|
|
120
|
+
|
|
121
|
+
### `ContainerActivator`
|
|
122
|
+
|
|
123
|
+
Resolves a list of already bound services from the current container before rendering children.
|
|
124
|
+
Useful when services should be eagerly activated for a subtree.
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { ContainerActivator } from "@wirestate/react";
|
|
128
|
+
import { CounterService, LoggerService } from "./services";
|
|
129
|
+
|
|
130
|
+
export function CounterPage() {
|
|
131
|
+
return (
|
|
132
|
+
<ContainerActivator activate={[CounterService, LoggerService]}>
|
|
133
|
+
<CounterView />
|
|
134
|
+
</ContainerActivator>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`ContainerActivator` runs activation once per container instance. If the container changes, activation runs again.
|
|
60
140
|
|
|
61
141
|
## Injection hooks
|
|
62
142
|
|
|
@@ -65,8 +145,8 @@ Both `seed` and `seeds` are applied on first render only.
|
|
|
65
145
|
Resolves a value from the nearest container. Re-resolves when the container resets.
|
|
66
146
|
|
|
67
147
|
```tsx
|
|
68
|
-
import { useInjection } from
|
|
69
|
-
import { CounterService } from
|
|
148
|
+
import { useInjection } from "@wirestate/react";
|
|
149
|
+
import { CounterService } from "./services";
|
|
70
150
|
|
|
71
151
|
function CounterView() {
|
|
72
152
|
const counter = useInjection(CounterService);
|
|
@@ -87,8 +167,8 @@ Returns a function that emits an event into the current container's event bus.
|
|
|
87
167
|
```tsx
|
|
88
168
|
const emit = useEventEmitter();
|
|
89
169
|
|
|
90
|
-
emit(
|
|
91
|
-
emit(
|
|
170
|
+
emit("RESET");
|
|
171
|
+
emit("ADD", { amount: 5 });
|
|
92
172
|
```
|
|
93
173
|
|
|
94
174
|
### `useEvent(type, handler)`
|
|
@@ -96,7 +176,7 @@ emit('ADD', { amount: 5 });
|
|
|
96
176
|
Subscribes a handler to a single event type for the lifetime of the component.
|
|
97
177
|
|
|
98
178
|
```tsx
|
|
99
|
-
useEvent(
|
|
179
|
+
useEvent("RESET", (event) => {
|
|
100
180
|
console.log(event.type, event.payload);
|
|
101
181
|
});
|
|
102
182
|
```
|
|
@@ -106,7 +186,7 @@ useEvent('RESET', (event) => {
|
|
|
106
186
|
Subscribes to multiple event types with a single handler.
|
|
107
187
|
|
|
108
188
|
```tsx
|
|
109
|
-
useEvents([
|
|
189
|
+
useEvents(["RESET", "CLEAR"], (event) => {
|
|
110
190
|
console.log(event.type, event.payload);
|
|
111
191
|
});
|
|
112
192
|
```
|
|
@@ -131,7 +211,7 @@ Returns a function that dispatches a command and waits for its result.
|
|
|
131
211
|
const call = useCommandCaller();
|
|
132
212
|
|
|
133
213
|
async function handleClick() {
|
|
134
|
-
await call(
|
|
214
|
+
await call("LOGIN", { username: "alice" }).task;
|
|
135
215
|
}
|
|
136
216
|
```
|
|
137
217
|
|
|
@@ -144,7 +224,7 @@ Same as `useCommandCaller` but returns `null` if no handler is registered.
|
|
|
144
224
|
Registers a command handler from a component for the lifetime of the component.
|
|
145
225
|
|
|
146
226
|
```tsx
|
|
147
|
-
useCommandHandler(
|
|
227
|
+
useCommandHandler("SCROLL_TOP", () => {
|
|
148
228
|
window.scrollTo(0, 0);
|
|
149
229
|
});
|
|
150
230
|
```
|
|
@@ -157,7 +237,7 @@ Returns an async function that calls a query handler and resolves its return val
|
|
|
157
237
|
|
|
158
238
|
```tsx
|
|
159
239
|
const query = useQueryCaller();
|
|
160
|
-
const items = await query(
|
|
240
|
+
const items = await query("GET_ITEMS");
|
|
161
241
|
```
|
|
162
242
|
|
|
163
243
|
### `useSyncQueryCaller()`
|
|
@@ -173,7 +253,7 @@ Return `null` when no handler is registered instead of throwing.
|
|
|
173
253
|
Registers a query handler from a component.
|
|
174
254
|
|
|
175
255
|
```tsx
|
|
176
|
-
useQueryHandler(
|
|
256
|
+
useQueryHandler("GET_SCROLL_POS", () => window.scrollY);
|
|
177
257
|
```
|
|
178
258
|
|
|
179
259
|
## Container hooks
|
|
@@ -186,13 +266,17 @@ Returns the nearest `Container` instance. Useful for advanced manual resolution.
|
|
|
186
266
|
|
|
187
267
|
Returns a counter that increments each time the container is reset. Use to trigger effects when the container changes.
|
|
188
268
|
|
|
269
|
+
### `useScope()`
|
|
270
|
+
|
|
271
|
+
Returns the current `WireScope` linked to the nearest container.
|
|
272
|
+
|
|
189
273
|
## Test utilities
|
|
190
274
|
|
|
191
275
|
```ts
|
|
192
|
-
import {
|
|
276
|
+
import { withContainerProvider } from "@wirestate/react/test-utils";
|
|
193
277
|
```
|
|
194
278
|
|
|
195
|
-
`
|
|
279
|
+
`withContainerProvider(children, container?, seed?)` — wraps a React tree with an `ContainerProvider` for use in tests.
|
|
196
280
|
|
|
197
281
|
## License
|
|
198
282
|
|