@wirestate/react 0.6.3 → 0.7.0-experimental.1

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.
Files changed (158) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/README.md +109 -25
  3. package/cjs/development/index.js +462 -222
  4. package/cjs/development/index.js.map +1 -1
  5. package/cjs/development/lib.js +167 -45
  6. package/cjs/development/lib.js.map +1 -1
  7. package/cjs/development/test-utils.js +22 -11
  8. package/cjs/development/test-utils.js.map +1 -1
  9. package/cjs/production/index.js +1 -1
  10. package/cjs/production/index.js.map +1 -1
  11. package/cjs/production/lib.js +1 -1
  12. package/cjs/production/lib.js.map +1 -1
  13. package/cjs/production/test-utils.js +1 -1
  14. package/cjs/production/test-utils.js.map +1 -1
  15. package/esm/development/commands/use-command-caller.js +16 -3
  16. package/esm/development/commands/use-command-caller.js.map +1 -1
  17. package/esm/development/commands/use-command-handler.js +20 -4
  18. package/esm/development/commands/use-command-handler.js.map +1 -1
  19. package/esm/development/commands/use-optional-command-caller.js +20 -4
  20. package/esm/development/commands/use-optional-command-caller.js.map +1 -1
  21. package/esm/development/context/container-context.js +16 -0
  22. package/esm/development/context/container-context.js.map +1 -0
  23. package/esm/development/context/use-container.js +33 -0
  24. package/esm/development/context/use-container.js.map +1 -0
  25. package/esm/development/context/use-root-container.js +35 -0
  26. package/esm/development/context/use-root-container.js.map +1 -0
  27. package/esm/development/context/use-scope.js +31 -0
  28. package/esm/development/context/use-scope.js.map +1 -0
  29. package/esm/development/error/error-code.js +1 -3
  30. package/esm/development/error/error-code.js.map +1 -1
  31. package/esm/development/events/use-event-emitter.js +20 -4
  32. package/esm/development/events/use-event-emitter.js.map +1 -1
  33. package/esm/development/events/use-event.js +18 -4
  34. package/esm/development/events/use-event.js.map +1 -1
  35. package/esm/development/events/use-events-handler.js +18 -3
  36. package/esm/development/events/use-events-handler.js.map +1 -1
  37. package/esm/development/events/use-events.js +18 -4
  38. package/esm/development/events/use-events.js.map +1 -1
  39. package/esm/development/index.js +12 -10
  40. package/esm/development/index.js.map +1 -1
  41. package/esm/development/injection/use-injection.js +36 -0
  42. package/esm/development/injection/use-injection.js.map +1 -0
  43. package/esm/development/injection/use-optional-injection.js +40 -0
  44. package/esm/development/injection/use-optional-injection.js.map +1 -0
  45. package/esm/development/provision/container-activator.js +33 -0
  46. package/esm/development/provision/container-activator.js.map +1 -0
  47. package/esm/development/provision/container-provider.js +74 -0
  48. package/esm/development/provision/container-provider.js.map +1 -0
  49. package/esm/development/provision/sub-container-provider.js +69 -0
  50. package/esm/development/provision/sub-container-provider.js.map +1 -0
  51. package/esm/development/provision/use-container-provision-state.js +76 -0
  52. package/esm/development/provision/use-container-provision-state.js.map +1 -0
  53. package/esm/development/queries/use-optional-query-caller.js +16 -4
  54. package/esm/development/queries/use-optional-query-caller.js.map +1 -1
  55. package/esm/development/queries/use-optional-sync-query-caller.js +14 -3
  56. package/esm/development/queries/use-optional-sync-query-caller.js.map +1 -1
  57. package/esm/development/queries/use-query-caller.js +15 -3
  58. package/esm/development/queries/use-query-caller.js.map +1 -1
  59. package/esm/development/queries/use-query-handler.js +21 -5
  60. package/esm/development/queries/use-query-handler.js.map +1 -1
  61. package/esm/development/queries/use-sync-query-caller.js +15 -3
  62. package/esm/development/queries/use-sync-query-caller.js.map +1 -1
  63. package/esm/development/test-utils/with-container-provider.js +35 -0
  64. package/esm/development/test-utils/with-container-provider.js.map +1 -0
  65. package/esm/development/test-utils.js +1 -1
  66. package/esm/development/utils/shallow-equal-arrays.js +28 -0
  67. package/esm/development/utils/shallow-equal-arrays.js.map +1 -0
  68. package/esm/production/commands/use-command-caller.js +1 -1
  69. package/esm/production/commands/use-command-caller.js.map +1 -1
  70. package/esm/production/commands/use-command-handler.js +1 -1
  71. package/esm/production/commands/use-command-handler.js.map +1 -1
  72. package/esm/production/commands/use-optional-command-caller.js +1 -1
  73. package/esm/production/commands/use-optional-command-caller.js.map +1 -1
  74. package/esm/production/context/container-context.js +1 -0
  75. package/esm/production/context/container-context.js.map +1 -0
  76. package/esm/production/context/use-container.js +1 -0
  77. package/esm/production/context/use-container.js.map +1 -0
  78. package/esm/production/context/use-root-container.js +1 -0
  79. package/esm/production/context/use-root-container.js.map +1 -0
  80. package/esm/production/context/use-scope.js +1 -0
  81. package/esm/production/context/use-scope.js.map +1 -0
  82. package/esm/production/error/error-code.js +1 -1
  83. package/esm/production/error/error-code.js.map +1 -1
  84. package/esm/production/events/use-event-emitter.js +1 -1
  85. package/esm/production/events/use-event-emitter.js.map +1 -1
  86. package/esm/production/events/use-event.js +1 -1
  87. package/esm/production/events/use-event.js.map +1 -1
  88. package/esm/production/events/use-events-handler.js +1 -1
  89. package/esm/production/events/use-events-handler.js.map +1 -1
  90. package/esm/production/events/use-events.js +1 -1
  91. package/esm/production/events/use-events.js.map +1 -1
  92. package/esm/production/index.js +1 -1
  93. package/esm/production/injection/use-injection.js +1 -0
  94. package/esm/production/injection/use-injection.js.map +1 -0
  95. package/esm/production/injection/use-optional-injection.js +1 -0
  96. package/esm/production/injection/use-optional-injection.js.map +1 -0
  97. package/esm/production/provision/container-activator.js +1 -0
  98. package/esm/production/provision/container-activator.js.map +1 -0
  99. package/esm/production/provision/container-provider.js +1 -0
  100. package/esm/production/provision/container-provider.js.map +1 -0
  101. package/esm/production/provision/sub-container-provider.js +1 -0
  102. package/esm/production/provision/sub-container-provider.js.map +1 -0
  103. package/esm/production/provision/use-container-provision-state.js +1 -0
  104. package/esm/production/provision/use-container-provision-state.js.map +1 -0
  105. package/esm/production/queries/use-optional-query-caller.js +1 -1
  106. package/esm/production/queries/use-optional-query-caller.js.map +1 -1
  107. package/esm/production/queries/use-optional-sync-query-caller.js +1 -1
  108. package/esm/production/queries/use-optional-sync-query-caller.js.map +1 -1
  109. package/esm/production/queries/use-query-caller.js +1 -1
  110. package/esm/production/queries/use-query-caller.js.map +1 -1
  111. package/esm/production/queries/use-query-handler.js +1 -1
  112. package/esm/production/queries/use-query-handler.js.map +1 -1
  113. package/esm/production/queries/use-sync-query-caller.js +1 -1
  114. package/esm/production/queries/use-sync-query-caller.js.map +1 -1
  115. package/esm/production/test-utils/with-container-provider.js +1 -0
  116. package/esm/production/test-utils/with-container-provider.js.map +1 -0
  117. package/esm/production/test-utils.js +1 -1
  118. package/esm/production/utils/shallow-equal-arrays.js +1 -0
  119. package/esm/production/utils/shallow-equal-arrays.js.map +1 -0
  120. package/index.d.ts +554 -137
  121. package/package.json +1 -1
  122. package/test-utils.d.ts +19 -8
  123. package/esm/development/provision/create-injectables-provider.js +0 -101
  124. package/esm/development/provision/create-injectables-provider.js.map +0 -1
  125. package/esm/development/provision/ioc-context.js +0 -11
  126. package/esm/development/provision/ioc-context.js.map +0 -1
  127. package/esm/development/provision/ioc-provider.js +0 -50
  128. package/esm/development/provision/ioc-provider.js.map +0 -1
  129. package/esm/development/provision/use-container-revision.js +0 -13
  130. package/esm/development/provision/use-container-revision.js.map +0 -1
  131. package/esm/development/provision/use-container.js +0 -13
  132. package/esm/development/provision/use-container.js.map +0 -1
  133. package/esm/development/provision/use-injection.js +0 -22
  134. package/esm/development/provision/use-injection.js.map +0 -1
  135. package/esm/development/provision/use-ioc-context.js +0 -21
  136. package/esm/development/provision/use-ioc-context.js.map +0 -1
  137. package/esm/development/provision/use-optional-injection.js +0 -29
  138. package/esm/development/provision/use-optional-injection.js.map +0 -1
  139. package/esm/development/test-utils/with-ioc-provider.js +0 -24
  140. package/esm/development/test-utils/with-ioc-provider.js.map +0 -1
  141. package/esm/production/provision/create-injectables-provider.js +0 -1
  142. package/esm/production/provision/create-injectables-provider.js.map +0 -1
  143. package/esm/production/provision/ioc-context.js +0 -1
  144. package/esm/production/provision/ioc-context.js.map +0 -1
  145. package/esm/production/provision/ioc-provider.js +0 -1
  146. package/esm/production/provision/ioc-provider.js.map +0 -1
  147. package/esm/production/provision/use-container-revision.js +0 -1
  148. package/esm/production/provision/use-container-revision.js.map +0 -1
  149. package/esm/production/provision/use-container.js +0 -1
  150. package/esm/production/provision/use-container.js.map +0 -1
  151. package/esm/production/provision/use-injection.js +0 -1
  152. package/esm/production/provision/use-injection.js.map +0 -1
  153. package/esm/production/provision/use-ioc-context.js +0 -1
  154. package/esm/production/provision/use-ioc-context.js.map +0 -1
  155. package/esm/production/provision/use-optional-injection.js +0 -1
  156. package/esm/production/provision/use-optional-injection.js.map +0 -1
  157. package/esm/production/test-utils/with-ioc-provider.js +0 -1
  158. package/esm/production/test-utils/with-ioc-provider.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## 0.7.0
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`: Added ability to instantly provide and activate entries, targeted seeds
14
+ - `createInjectablesProvider`: Removed.
15
+ - `IocProvider`: Removed
16
+ - `useRootContainer`: Added separate hook for store management in React tree
17
+ - `ContainerProvider`: Simpler provider for containers.
18
+ - `SubContainerProvider`: Added component solving problems of removed `createInjectablesProvider`
19
+ - `ContainerActivator`: Added separate activation component
20
+
1
21
  ## 0.6.3
2
22
 
3
23
  - Update readme files for each module
@@ -59,6 +79,6 @@
59
79
  - useService -> useInjection
60
80
  - AbstractService::getService -> AbstractService::resolve
61
81
 
62
- ## 0.1.1
82
+ ## 0.1.0
63
83
 
64
84
  - 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
  [![npm](https://img.shields.io/npm/v/@wirestate/react.svg?style=flat-square)](https://www.npmjs.com/package/@wirestate/react)
4
4
  [![license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](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
- ### `IocProvider`
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 { IocProvider } from '@wirestate/react';
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 App() {
32
+ export function Application() {
24
33
  return (
25
- <IocProvider>
34
+ <ContainerProvider container={container}>
26
35
  <SomeComponent />
27
- </IocProvider>
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 `IocProvider` tree.
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 '@wirestate/react';
39
- import { CounterService, LoggerService } from './services';
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 | Type | Description |
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
- Both `seed` and `seeds` are applied on first render only.
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 '@wirestate/react';
69
- import { CounterService } from './services';
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('RESET');
91
- emit('ADD', { amount: 5 });
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('RESET', (event) => {
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(['RESET', 'CLEAR'], (event) => {
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('LOGIN', { username: 'alice' }).task;
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('SCROLL_TOP', () => {
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('GET_ITEMS');
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('GET_SCROLL_POS', () => window.scrollY);
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 { withIocProvider } from '@wirestate/react/test-utils';
276
+ import { withContainerProvider } from "@wirestate/react/test-utils";
193
277
  ```
194
278
 
195
- `withIocProvider(children, container?, seed?)` — wraps a React tree with an `IocProvider` for use in tests.
279
+ `withContainerProvider(children, container?, seed?)` — wraps a React tree with an `ContainerProvider` for use in tests.
196
280
 
197
281
  ## License
198
282