@wirestate/react 0.6.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.
Files changed (202) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/LICENSE +21 -0
  3. package/README.md +319 -0
  4. package/cjs/development/index.js +395 -0
  5. package/cjs/development/index.js.map +1 -0
  6. package/cjs/development/lib.js +64 -0
  7. package/cjs/development/lib.js.map +1 -0
  8. package/cjs/development/test-utils.js +1075 -0
  9. package/cjs/development/test-utils.js.map +1 -0
  10. package/cjs/production/index.js +1 -0
  11. package/cjs/production/index.js.map +1 -0
  12. package/cjs/production/lib.js +1 -0
  13. package/cjs/production/lib.js.map +1 -0
  14. package/cjs/production/test-utils.js +1 -0
  15. package/cjs/production/test-utils.js.map +1 -0
  16. package/esm/development/wirestate-core/bind/bind-constant.js +19 -0
  17. package/esm/development/wirestate-core/bind/bind-constant.js.map +1 -0
  18. package/esm/development/wirestate-core/bind/bind-dynamic-value.js +28 -0
  19. package/esm/development/wirestate-core/bind/bind-dynamic-value.js.map +1 -0
  20. package/esm/development/wirestate-core/bind/bind-entry.js +39 -0
  21. package/esm/development/wirestate-core/bind/bind-entry.js.map +1 -0
  22. package/esm/development/wirestate-core/bind/bind-service.js +253 -0
  23. package/esm/development/wirestate-core/bind/bind-service.js.map +1 -0
  24. package/esm/development/wirestate-core/bind/get-entry-token.js +14 -0
  25. package/esm/development/wirestate-core/bind/get-entry-token.js.map +1 -0
  26. package/esm/development/wirestate-core/commands/command-bus.js +112 -0
  27. package/esm/development/wirestate-core/commands/command-bus.js.map +1 -0
  28. package/esm/development/wirestate-core/commands/get-command-handler-metadata.js +27 -0
  29. package/esm/development/wirestate-core/commands/get-command-handler-metadata.js.map +1 -0
  30. package/esm/development/wirestate-core/container/create-ioc-container.js +35 -0
  31. package/esm/development/wirestate-core/container/create-ioc-container.js.map +1 -0
  32. package/esm/development/wirestate-core/container/wire-scope.js +151 -0
  33. package/esm/development/wirestate-core/container/wire-scope.js.map +1 -0
  34. package/esm/development/wirestate-core/error/error-code.js +10 -0
  35. package/esm/development/wirestate-core/error/error-code.js.map +1 -0
  36. package/esm/development/wirestate-core/error/wirestate-error.js +29 -0
  37. package/esm/development/wirestate-core/error/wirestate-error.js.map +1 -0
  38. package/esm/development/wirestate-core/events/build-event-dispatcher.js +39 -0
  39. package/esm/development/wirestate-core/events/build-event-dispatcher.js.map +1 -0
  40. package/esm/development/wirestate-core/events/event-bus.js +52 -0
  41. package/esm/development/wirestate-core/events/event-bus.js.map +1 -0
  42. package/esm/development/wirestate-core/events/get-event-handler-metadata.js +27 -0
  43. package/esm/development/wirestate-core/events/get-event-handler-metadata.js.map +1 -0
  44. package/esm/development/wirestate-core/queries/get-query-handler-metadata.js +27 -0
  45. package/esm/development/wirestate-core/queries/get-query-handler-metadata.js.map +1 -0
  46. package/esm/development/wirestate-core/queries/query-bus.js +99 -0
  47. package/esm/development/wirestate-core/queries/query-bus.js.map +1 -0
  48. package/esm/development/wirestate-core/registry.js +56 -0
  49. package/esm/development/wirestate-core/registry.js.map +1 -0
  50. package/esm/development/wirestate-core/service/get-activated-handler-metadata.js +27 -0
  51. package/esm/development/wirestate-core/service/get-activated-handler-metadata.js.map +1 -0
  52. package/esm/development/wirestate-core/service/get-deactivation-handler-metadata.js +27 -0
  53. package/esm/development/wirestate-core/service/get-deactivation-handler-metadata.js.map +1 -0
  54. package/esm/development/wirestate-core/test-utils/mock-bind-entry.js +24 -0
  55. package/esm/development/wirestate-core/test-utils/mock-bind-entry.js.map +1 -0
  56. package/esm/development/wirestate-core/test-utils/mock-container.js +52 -0
  57. package/esm/development/wirestate-core/test-utils/mock-container.js.map +1 -0
  58. package/esm/development/wirestate-core/types/commands.js +12 -0
  59. package/esm/development/wirestate-core/types/commands.js.map +1 -0
  60. package/esm/development/wirestate-react/commands/use-command-caller.js +18 -0
  61. package/esm/development/wirestate-react/commands/use-command-caller.js.map +1 -0
  62. package/esm/development/wirestate-react/commands/use-command-handler.js +28 -0
  63. package/esm/development/wirestate-react/commands/use-command-handler.js.map +1 -0
  64. package/esm/development/wirestate-react/commands/use-optional-command-caller.js +19 -0
  65. package/esm/development/wirestate-react/commands/use-optional-command-caller.js.map +1 -0
  66. package/esm/development/wirestate-react/error/error-code.js +6 -0
  67. package/esm/development/wirestate-react/error/error-code.js.map +1 -0
  68. package/esm/development/wirestate-react/events/use-event-emitter.js +22 -0
  69. package/esm/development/wirestate-react/events/use-event-emitter.js.map +1 -0
  70. package/esm/development/wirestate-react/events/use-event.js +30 -0
  71. package/esm/development/wirestate-react/events/use-event.js.map +1 -0
  72. package/esm/development/wirestate-react/events/use-events-handler.js +25 -0
  73. package/esm/development/wirestate-react/events/use-events-handler.js.map +1 -0
  74. package/esm/development/wirestate-react/events/use-events.js +31 -0
  75. package/esm/development/wirestate-react/events/use-events.js.map +1 -0
  76. package/esm/development/wirestate-react/index.js +19 -0
  77. package/esm/development/wirestate-react/index.js.map +1 -0
  78. package/esm/development/wirestate-react/provision/create-injectables-provider.js +101 -0
  79. package/esm/development/wirestate-react/provision/create-injectables-provider.js.map +1 -0
  80. package/esm/development/wirestate-react/provision/ioc-context.js +11 -0
  81. package/esm/development/wirestate-react/provision/ioc-context.js.map +1 -0
  82. package/esm/development/wirestate-react/provision/ioc-provider.js +50 -0
  83. package/esm/development/wirestate-react/provision/ioc-provider.js.map +1 -0
  84. package/esm/development/wirestate-react/provision/use-container-revision.js +13 -0
  85. package/esm/development/wirestate-react/provision/use-container-revision.js.map +1 -0
  86. package/esm/development/wirestate-react/provision/use-container.js +13 -0
  87. package/esm/development/wirestate-react/provision/use-container.js.map +1 -0
  88. package/esm/development/wirestate-react/provision/use-injection.js +22 -0
  89. package/esm/development/wirestate-react/provision/use-injection.js.map +1 -0
  90. package/esm/development/wirestate-react/provision/use-ioc-context.js +21 -0
  91. package/esm/development/wirestate-react/provision/use-ioc-context.js.map +1 -0
  92. package/esm/development/wirestate-react/provision/use-optional-injection.js +29 -0
  93. package/esm/development/wirestate-react/provision/use-optional-injection.js.map +1 -0
  94. package/esm/development/wirestate-react/queries/use-optional-query-caller.js +19 -0
  95. package/esm/development/wirestate-react/queries/use-optional-query-caller.js.map +1 -0
  96. package/esm/development/wirestate-react/queries/use-optional-sync-query-caller.js +19 -0
  97. package/esm/development/wirestate-react/queries/use-optional-sync-query-caller.js.map +1 -0
  98. package/esm/development/wirestate-react/queries/use-query-caller.js +18 -0
  99. package/esm/development/wirestate-react/queries/use-query-caller.js.map +1 -0
  100. package/esm/development/wirestate-react/queries/use-query-handler.js +27 -0
  101. package/esm/development/wirestate-react/queries/use-query-handler.js.map +1 -0
  102. package/esm/development/wirestate-react/queries/use-sync-query-caller.js +20 -0
  103. package/esm/development/wirestate-react/queries/use-sync-query-caller.js.map +1 -0
  104. package/esm/development/wirestate-react/test-utils/with-ioc-provider.js +30 -0
  105. package/esm/development/wirestate-react/test-utils/with-ioc-provider.js.map +1 -0
  106. package/esm/development/wirestate-react/test-utils.js +2 -0
  107. package/esm/development/wirestate-react/test-utils.js.map +1 -0
  108. package/esm/production/wirestate-core/bind/bind-constant.js +1 -0
  109. package/esm/production/wirestate-core/bind/bind-constant.js.map +1 -0
  110. package/esm/production/wirestate-core/bind/bind-dynamic-value.js +1 -0
  111. package/esm/production/wirestate-core/bind/bind-dynamic-value.js.map +1 -0
  112. package/esm/production/wirestate-core/bind/bind-entry.js +1 -0
  113. package/esm/production/wirestate-core/bind/bind-entry.js.map +1 -0
  114. package/esm/production/wirestate-core/bind/bind-service.js +1 -0
  115. package/esm/production/wirestate-core/bind/bind-service.js.map +1 -0
  116. package/esm/production/wirestate-core/bind/get-entry-token.js +1 -0
  117. package/esm/production/wirestate-core/bind/get-entry-token.js.map +1 -0
  118. package/esm/production/wirestate-core/commands/command-bus.js +1 -0
  119. package/esm/production/wirestate-core/commands/command-bus.js.map +1 -0
  120. package/esm/production/wirestate-core/commands/get-command-handler-metadata.js +1 -0
  121. package/esm/production/wirestate-core/commands/get-command-handler-metadata.js.map +1 -0
  122. package/esm/production/wirestate-core/container/create-ioc-container.js +1 -0
  123. package/esm/production/wirestate-core/container/create-ioc-container.js.map +1 -0
  124. package/esm/production/wirestate-core/container/wire-scope.js +1 -0
  125. package/esm/production/wirestate-core/container/wire-scope.js.map +1 -0
  126. package/esm/production/wirestate-core/error/error-code.js +1 -0
  127. package/esm/production/wirestate-core/error/error-code.js.map +1 -0
  128. package/esm/production/wirestate-core/error/wirestate-error.js +1 -0
  129. package/esm/production/wirestate-core/error/wirestate-error.js.map +1 -0
  130. package/esm/production/wirestate-core/events/build-event-dispatcher.js +1 -0
  131. package/esm/production/wirestate-core/events/build-event-dispatcher.js.map +1 -0
  132. package/esm/production/wirestate-core/events/event-bus.js +1 -0
  133. package/esm/production/wirestate-core/events/event-bus.js.map +1 -0
  134. package/esm/production/wirestate-core/events/get-event-handler-metadata.js +1 -0
  135. package/esm/production/wirestate-core/events/get-event-handler-metadata.js.map +1 -0
  136. package/esm/production/wirestate-core/queries/get-query-handler-metadata.js +1 -0
  137. package/esm/production/wirestate-core/queries/get-query-handler-metadata.js.map +1 -0
  138. package/esm/production/wirestate-core/queries/query-bus.js +1 -0
  139. package/esm/production/wirestate-core/queries/query-bus.js.map +1 -0
  140. package/esm/production/wirestate-core/registry.js +1 -0
  141. package/esm/production/wirestate-core/registry.js.map +1 -0
  142. package/esm/production/wirestate-core/service/get-activated-handler-metadata.js +1 -0
  143. package/esm/production/wirestate-core/service/get-activated-handler-metadata.js.map +1 -0
  144. package/esm/production/wirestate-core/service/get-deactivation-handler-metadata.js +1 -0
  145. package/esm/production/wirestate-core/service/get-deactivation-handler-metadata.js.map +1 -0
  146. package/esm/production/wirestate-core/test-utils/mock-bind-entry.js +1 -0
  147. package/esm/production/wirestate-core/test-utils/mock-bind-entry.js.map +1 -0
  148. package/esm/production/wirestate-core/test-utils/mock-container.js +1 -0
  149. package/esm/production/wirestate-core/test-utils/mock-container.js.map +1 -0
  150. package/esm/production/wirestate-core/types/commands.js +1 -0
  151. package/esm/production/wirestate-core/types/commands.js.map +1 -0
  152. package/esm/production/wirestate-react/commands/use-command-caller.js +1 -0
  153. package/esm/production/wirestate-react/commands/use-command-caller.js.map +1 -0
  154. package/esm/production/wirestate-react/commands/use-command-handler.js +1 -0
  155. package/esm/production/wirestate-react/commands/use-command-handler.js.map +1 -0
  156. package/esm/production/wirestate-react/commands/use-optional-command-caller.js +1 -0
  157. package/esm/production/wirestate-react/commands/use-optional-command-caller.js.map +1 -0
  158. package/esm/production/wirestate-react/error/error-code.js +1 -0
  159. package/esm/production/wirestate-react/error/error-code.js.map +1 -0
  160. package/esm/production/wirestate-react/events/use-event-emitter.js +1 -0
  161. package/esm/production/wirestate-react/events/use-event-emitter.js.map +1 -0
  162. package/esm/production/wirestate-react/events/use-event.js +1 -0
  163. package/esm/production/wirestate-react/events/use-event.js.map +1 -0
  164. package/esm/production/wirestate-react/events/use-events-handler.js +1 -0
  165. package/esm/production/wirestate-react/events/use-events-handler.js.map +1 -0
  166. package/esm/production/wirestate-react/events/use-events.js +1 -0
  167. package/esm/production/wirestate-react/events/use-events.js.map +1 -0
  168. package/esm/production/wirestate-react/index.js +1 -0
  169. package/esm/production/wirestate-react/index.js.map +1 -0
  170. package/esm/production/wirestate-react/provision/create-injectables-provider.js +1 -0
  171. package/esm/production/wirestate-react/provision/create-injectables-provider.js.map +1 -0
  172. package/esm/production/wirestate-react/provision/ioc-context.js +1 -0
  173. package/esm/production/wirestate-react/provision/ioc-context.js.map +1 -0
  174. package/esm/production/wirestate-react/provision/ioc-provider.js +1 -0
  175. package/esm/production/wirestate-react/provision/ioc-provider.js.map +1 -0
  176. package/esm/production/wirestate-react/provision/use-container-revision.js +1 -0
  177. package/esm/production/wirestate-react/provision/use-container-revision.js.map +1 -0
  178. package/esm/production/wirestate-react/provision/use-container.js +1 -0
  179. package/esm/production/wirestate-react/provision/use-container.js.map +1 -0
  180. package/esm/production/wirestate-react/provision/use-injection.js +1 -0
  181. package/esm/production/wirestate-react/provision/use-injection.js.map +1 -0
  182. package/esm/production/wirestate-react/provision/use-ioc-context.js +1 -0
  183. package/esm/production/wirestate-react/provision/use-ioc-context.js.map +1 -0
  184. package/esm/production/wirestate-react/provision/use-optional-injection.js +1 -0
  185. package/esm/production/wirestate-react/provision/use-optional-injection.js.map +1 -0
  186. package/esm/production/wirestate-react/queries/use-optional-query-caller.js +1 -0
  187. package/esm/production/wirestate-react/queries/use-optional-query-caller.js.map +1 -0
  188. package/esm/production/wirestate-react/queries/use-optional-sync-query-caller.js +1 -0
  189. package/esm/production/wirestate-react/queries/use-optional-sync-query-caller.js.map +1 -0
  190. package/esm/production/wirestate-react/queries/use-query-caller.js +1 -0
  191. package/esm/production/wirestate-react/queries/use-query-caller.js.map +1 -0
  192. package/esm/production/wirestate-react/queries/use-query-handler.js +1 -0
  193. package/esm/production/wirestate-react/queries/use-query-handler.js.map +1 -0
  194. package/esm/production/wirestate-react/queries/use-sync-query-caller.js +1 -0
  195. package/esm/production/wirestate-react/queries/use-sync-query-caller.js.map +1 -0
  196. package/esm/production/wirestate-react/test-utils/with-ioc-provider.js +1 -0
  197. package/esm/production/wirestate-react/test-utils/with-ioc-provider.js.map +1 -0
  198. package/esm/production/wirestate-react/test-utils.js +1 -0
  199. package/esm/production/wirestate-react/test-utils.js.map +1 -0
  200. package/index.d.ts +218 -0
  201. package/package.json +62 -0
  202. package/test-utils.d.ts +18 -0
@@ -0,0 +1,395 @@
1
+ 'use strict';
2
+
3
+ var core = require('@wirestate/core');
4
+ var react = require('react');
5
+ var iocProvider = require('./lib.js');
6
+
7
+ /**
8
+ * Returns the full IoC context.
9
+ *
10
+ * @returns active IoC context
11
+ * @internal
12
+ */
13
+ function useIocContext() {
14
+ var value = react.useContext(iocProvider.IocContext);
15
+ if (!value) {
16
+ throw new core.WirestateError(iocProvider.ERROR_CODE_INVALID_CONTEXT, "Trying to access IOC context from React subtree not wrapped in <IocProvider>.");
17
+ }
18
+ return value;
19
+ }
20
+
21
+ /**
22
+ * Returns the active IoC container.
23
+ *
24
+ * @returns active Inversify container
25
+ */
26
+ function useContainer() {
27
+ return useIocContext().container;
28
+ }
29
+
30
+ /**
31
+ * Returns a function to dispatch commands on the active container.
32
+ *
33
+ * @returns command dispatcher
34
+ */
35
+ function useCommandCaller() {
36
+ var container = useContainer();
37
+ return react.useCallback(function (type, data) {
38
+ return container.get(core.CommandBus).command(type, data);
39
+ }, [container]);
40
+ }
41
+
42
+ /**
43
+ * Returns a function to dispatch optional commands on the active container.
44
+ * Returns null instead of throwing when no handler is registered.
45
+ *
46
+ * @returns optional command dispatcher
47
+ */
48
+ function useOptionalCommandCaller() {
49
+ var container = useContainer();
50
+ return react.useCallback(function (type, data) {
51
+ return container.get(core.CommandBus).commandOptional(type, data);
52
+ }, [container]);
53
+ }
54
+
55
+ /**
56
+ * Registers a command handler for the component's lifetime.
57
+ * The handler is stored in a ref to avoid manual memoization.
58
+ * Only one handler is active per type; newer registrations shadow older ones.
59
+ *
60
+ * @param type - command type
61
+ * @param handler - command handler function
62
+ */
63
+ function useCommandHandler(type, handler) {
64
+ var container = useContainer();
65
+ var handlerRef = react.useRef(handler);
66
+ // Sync ref with the latest closure on every render.
67
+ react.useEffect(function () {
68
+ handlerRef.current = handler;
69
+ });
70
+ react.useEffect(function () {
71
+ return container.get(core.CommandBus).register(type, function (data) {
72
+ return handlerRef.current(data);
73
+ });
74
+ }, [container, type]);
75
+ }
76
+
77
+ /**
78
+ * Returns a function to dispatch queries on the active container.
79
+ *
80
+ * @returns query dispatcher
81
+ */
82
+ function useQueryCaller() {
83
+ var container = useContainer();
84
+ return react.useCallback(function (type, data) {
85
+ return container.get(core.QueryBus).query(type, data);
86
+ }, [container]);
87
+ }
88
+
89
+ /**
90
+ * Returns a function to dispatch optional queries on the active container.
91
+ * Returns null instead of throwing when no handler is registered.
92
+ *
93
+ * @returns optional query dispatcher
94
+ */
95
+ function useOptionalQueryCaller() {
96
+ var container = useContainer();
97
+ return react.useCallback(function (type, data) {
98
+ return container.get(core.QueryBus).queryOptional(type, data);
99
+ }, [container]);
100
+ }
101
+
102
+ /**
103
+ * Registers a query handler for the component's lifetime.
104
+ * The handler is stored in a ref to avoid manual memoization.
105
+ * Only one handler is active per type; newer registrations shadow older ones.
106
+ *
107
+ * @param type - query type
108
+ * @param handler - query handler function
109
+ */
110
+ function useQueryHandler(type, handler) {
111
+ var container = useContainer();
112
+ var handlerRef = react.useRef(handler);
113
+ react.useEffect(function () {
114
+ handlerRef.current = handler;
115
+ });
116
+ react.useEffect(function () {
117
+ return container.get(core.QueryBus).register(type, function (data) {
118
+ return handlerRef.current(data);
119
+ });
120
+ }, [container, type]);
121
+ }
122
+
123
+ /**
124
+ * Returns a stable function to dispatch synchronous queries.
125
+ * Returns the value directly from the handler.
126
+ *
127
+ * @returns sync query dispatcher
128
+ */
129
+ function useSyncQueryCaller() {
130
+ var container = useContainer();
131
+ return react.useCallback(function (type, data) {
132
+ // Access the container-scoped QueryBus and execute the query.
133
+ return container.get(core.QueryBus).query(type, data);
134
+ }, [container]);
135
+ }
136
+
137
+ /**
138
+ * Returns a stable function to dispatch synchronous optional queries.
139
+ * Returns null instead of throwing when no handler is registered.
140
+ *
141
+ * @returns optional sync query dispatcher
142
+ */
143
+ function useOptionalSyncQueryCaller() {
144
+ var container = useContainer();
145
+ return react.useCallback(function (type, data) {
146
+ return container.get(core.QueryBus).queryOptional(type, data);
147
+ }, [container]);
148
+ }
149
+
150
+ /**
151
+ * Resolves a value from the container - constant or service.
152
+ * Automatically re-resolves if the container is reset or services are rebound.
153
+ *
154
+ * @param injectionId - injection identifier
155
+ * @returns resolved value
156
+ */
157
+ function useInjection(injectionId) {
158
+ var _a = useIocContext(),
159
+ container = _a.container,
160
+ revision = _a.revision;
161
+ // Revision bump causes a container reset; force re-resolution to drop stale instances.
162
+ return react.useMemo(function () {
163
+ return container.get(injectionId);
164
+ }, [container, revision, injectionId]);
165
+ }
166
+
167
+ /**
168
+ * Resolves a value from the container if bound, returning null otherwise.
169
+ * Unlike {@link useInjection}, this hook does not throw when the token is not bound.
170
+ *
171
+ * @param injectionId - injection identifier
172
+ * @param onFallback - optional callback to handle cases when dependency was not resolved
173
+ * @returns resolved value, result of optional fallback handler or null
174
+ */
175
+ function useOptionalInjection(injectionId, onFallback) {
176
+ var _a = useIocContext(),
177
+ container = _a.container,
178
+ revision = _a.revision;
179
+ // Revision bump forces a container reset; force re-resolution to drop stale instances.
180
+ return react.useMemo(function () {
181
+ if (container.isBound(injectionId)) {
182
+ return container.get(injectionId);
183
+ } else if (onFallback) {
184
+ return onFallback(container);
185
+ } else {
186
+ return null;
187
+ }
188
+ }, [container, revision, injectionId]);
189
+ }
190
+
191
+ /**
192
+ * Creates a component that manages injectable lifetimes for its subtree.
193
+ *
194
+ * @param entries - service classes or injectable descriptors to bind
195
+ * @param options - provider configuration
196
+ * @returns injectables provider component
197
+ */
198
+ function createInjectablesProvider(entries, options) {
199
+ if (options === void 0) {
200
+ options = {};
201
+ }
202
+ var activate = options.activate;
203
+ if (activate && activate.length > 0) {
204
+ var entryTokens = entries.map(core.getEntryToken);
205
+ for (var _i = 0, activate_1 = activate; _i < activate_1.length; _i++) {
206
+ var eager = activate_1[_i];
207
+ if (!entryTokens.includes(eager)) {
208
+ throw new core.WirestateError(iocProvider.ERROR_CODE_VALIDATION_ERROR, "createInjectablesProvider: '".concat(String(eager), "' is listed in 'activate' but was not provided in 'entries'."));
209
+ }
210
+ }
211
+ }
212
+ function InjectablesProviderComponent(props) {
213
+ var iocContext = react.useContext(iocProvider.IocContext);
214
+ if (!iocContext) {
215
+ throw new core.WirestateError(iocProvider.ERROR_CODE_INVALID_CONTEXT, "<InjectablesProvider> must be rendered inside an <IocProvider> React subtree.");
216
+ }
217
+ // Snapshot props on mount to ensure binding stability.
218
+ // useState lazy initializer ensures it only runs once.
219
+ var initialPropsSnapshot = react.useState(function () {
220
+ return props;
221
+ })[0];
222
+ react.useMemo(function () {
223
+ // Seed must be applied BEFORE binding so @Inject(INITIAL_STATE_TOKEN) works during activation.
224
+ if (initialPropsSnapshot.seeds) {
225
+ core.applySeeds(iocContext.container, initialPropsSnapshot.seeds);
226
+ }
227
+ for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
228
+ var entry = entries_1[_i];
229
+ if (!iocContext.container.isBound(core.getEntryToken(entry))) {
230
+ core.bindEntry(iocContext.container, entry);
231
+ }
232
+ }
233
+ if (activate) {
234
+ for (var _a = 0, activate_2 = activate; _a < activate_2.length; _a++) {
235
+ var eager = activate_2[_a];
236
+ iocContext.container.get(eager);
237
+ }
238
+ }
239
+ }, entries);
240
+ react.useEffect(function () {
241
+ // Re-apply state and re-bind if container was reset (e.g. StrictMode remount or HMR).
242
+ var didRebind = false;
243
+ if (initialPropsSnapshot.seeds) {
244
+ core.applySeeds(iocContext.container, initialPropsSnapshot.seeds);
245
+ }
246
+ for (var _i = 0, entries_2 = entries; _i < entries_2.length; _i++) {
247
+ var entry = entries_2[_i];
248
+ if (!iocContext.container.isBound(core.getEntryToken(entry))) {
249
+ didRebind = true;
250
+ core.bindEntry(iocContext.container, entry);
251
+ }
252
+ }
253
+ if (activate) {
254
+ for (var _a = 0, activate_3 = activate; _a < activate_3.length; _a++) {
255
+ var eager = activate_3[_a];
256
+ iocContext.container.get(eager);
257
+ }
258
+ }
259
+ // Increment revision to invalidate stale injection caches.
260
+ if (didRebind) {
261
+ iocContext.setRevision(function (r) {
262
+ return r + 1;
263
+ });
264
+ }
265
+ return function () {
266
+ for (var _i = 0, entries_3 = entries; _i < entries_3.length; _i++) {
267
+ var entry = entries_3[_i];
268
+ var token = core.getEntryToken(entry);
269
+ if (iocContext.container.isBound(token)) {
270
+ iocContext.container.unbind(token);
271
+ }
272
+ }
273
+ // Remove only this provider's targeted initial state entries.
274
+ if (initialPropsSnapshot.seeds) {
275
+ core.unapplySeeds(iocContext.container, initialPropsSnapshot.seeds);
276
+ }
277
+ };
278
+ }, entries);
279
+ return props.children;
280
+ }
281
+ InjectablesProviderComponent.displayName = "InjectablesProvider";
282
+ return InjectablesProviderComponent;
283
+ }
284
+
285
+ /**
286
+ * Returns the current container revision.
287
+ *
288
+ * @returns revision number
289
+ */
290
+ function useContainerRevision() {
291
+ return useIocContext().revision;
292
+ }
293
+
294
+ /**
295
+ * Subscribes a component to events.
296
+ *
297
+ * @param type - event type to listen to
298
+ * @param handler - event handler to invoke when event is emitted
299
+ */
300
+ function useEvent(type, handler) {
301
+ var typeRef = react.useRef(type);
302
+ var handlerRef = react.useRef(handler);
303
+ var container = useContainer();
304
+ react.useEffect(function () {
305
+ typeRef.current = type;
306
+ handlerRef.current = handler;
307
+ });
308
+ react.useEffect(function () {
309
+ return container.get(core.EventBus).subscribe(function (event) {
310
+ var _a;
311
+ if (event.type === typeRef.current) {
312
+ (_a = handlerRef.current) === null || _a === void 0 ? void 0 : _a.call(handlerRef, event);
313
+ }
314
+ });
315
+ }, [container, type]);
316
+ }
317
+
318
+ /**
319
+ * Subscribes a component to multiple event types.
320
+ *
321
+ * @param types - event types to filter by
322
+ * @param handler - events handler
323
+ */
324
+ function useEvents(types, handler) {
325
+ var typesRef = react.useRef(types);
326
+ var handlerRef = react.useRef(handler);
327
+ var container = useContainer();
328
+ react.useEffect(function () {
329
+ typesRef.current = types;
330
+ handlerRef.current = handler;
331
+ });
332
+ react.useEffect(function () {
333
+ return container.get(core.EventBus).subscribe(function (event) {
334
+ var _a;
335
+ if (typesRef.current.includes(event.type)) {
336
+ (_a = handlerRef.current) === null || _a === void 0 ? void 0 : _a.call(handlerRef, event);
337
+ }
338
+ });
339
+ }, [container]);
340
+ }
341
+
342
+ /**
343
+ * Subscribes a component to all events without type filtering.
344
+ *
345
+ * @param handler - event handler invoked for every emitted event
346
+ */
347
+ function useEventsHandler(handler) {
348
+ var handlerRef = react.useRef(handler);
349
+ var container = useContainer();
350
+ react.useEffect(function () {
351
+ handlerRef.current = handler;
352
+ });
353
+ react.useEffect(function () {
354
+ return container.get(core.EventBus).subscribe(function (event) {
355
+ var _a;
356
+ (_a = handlerRef.current) === null || _a === void 0 ? void 0 : _a.call(handlerRef, event);
357
+ });
358
+ }, [container]);
359
+ }
360
+
361
+ /**
362
+ * Returns a stable function to emit events.
363
+ *
364
+ * @returns event emitter
365
+ */
366
+ function useEventEmitter() {
367
+ var container = useIocContext().container;
368
+ return react.useCallback(function (type, payload, from) {
369
+ container.get(core.EventBus).emit({
370
+ type: type,
371
+ payload: payload,
372
+ from: from
373
+ });
374
+ }, [container]);
375
+ }
376
+
377
+ exports.IocProvider = iocProvider.IocProvider;
378
+ exports.createInjectablesProvider = createInjectablesProvider;
379
+ exports.useCommandCaller = useCommandCaller;
380
+ exports.useCommandHandler = useCommandHandler;
381
+ exports.useContainer = useContainer;
382
+ exports.useContainerRevision = useContainerRevision;
383
+ exports.useEvent = useEvent;
384
+ exports.useEventEmitter = useEventEmitter;
385
+ exports.useEvents = useEvents;
386
+ exports.useEventsHandler = useEventsHandler;
387
+ exports.useInjection = useInjection;
388
+ exports.useOptionalCommandCaller = useOptionalCommandCaller;
389
+ exports.useOptionalInjection = useOptionalInjection;
390
+ exports.useOptionalQueryCaller = useOptionalQueryCaller;
391
+ exports.useOptionalSyncQueryCaller = useOptionalSyncQueryCaller;
392
+ exports.useQueryCaller = useQueryCaller;
393
+ exports.useQueryHandler = useQueryHandler;
394
+ exports.useSyncQueryCaller = useSyncQueryCaller;
395
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../../../../../../src/wirestate-react/provision/use-ioc-context.ts","../../../../../../../src/wirestate-react/provision/use-container.ts","../../../../../../../src/wirestate-react/commands/use-command-caller.ts","../../../../../../../src/wirestate-react/commands/use-optional-command-caller.ts","../../../../../../../src/wirestate-react/commands/use-command-handler.ts","../../../../../../../src/wirestate-react/queries/use-query-caller.ts","../../../../../../../src/wirestate-react/queries/use-optional-query-caller.ts","../../../../../../../src/wirestate-react/queries/use-query-handler.ts","../../../../../../../src/wirestate-react/queries/use-sync-query-caller.ts","../../../../../../../src/wirestate-react/queries/use-optional-sync-query-caller.ts","../../../../../../../src/wirestate-react/provision/use-injection.ts","../../../../../../../src/wirestate-react/provision/use-optional-injection.ts","../../../../../../../src/wirestate-react/provision/create-injectables-provider.ts","../../../../../../../src/wirestate-react/provision/use-container-revision.ts","../../../../../../../src/wirestate-react/events/use-event.ts","../../../../../../../src/wirestate-react/events/use-events.ts","../../../../../../../src/wirestate-react/events/use-events-handler.ts","../../../../../../../src/wirestate-react/events/use-event-emitter.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"names":["useIocContext","value","useContext","IocContext","WirestateError","ERROR_CODE_INVALID_CONTEXT","useContainer","container","useCommandCaller","useCallback","type","data","get","CommandBus","command","useOptionalCommandCaller","commandOptional","useCommandHandler","handler","handlerRef","useRef","useEffect","current","register","useQueryCaller","QueryBus","query","useOptionalQueryCaller","queryOptional","useQueryHandler","useSyncQueryCaller","useOptionalSyncQueryCaller","useInjection","injectionId","_a","revision","useMemo","useOptionalInjection","onFallback","isBound","createInjectablesProvider","entries","options","activate","length","entryTokens","map","getEntryToken","_i","activate_1","eager","includes","ERROR_CODE_VALIDATION_ERROR","concat","String","InjectablesProviderComponent","props","iocContext","initialPropsSnapshot","useState","seeds","applySeeds","entries_1","entry","bindEntry","activate_2","didRebind","entries_2","activate_3","setRevision","r","entries_3","token","unbind","unapplySeeds","children","displayName","useContainerRevision","useEvent","typeRef","EventBus","subscribe","event","call","useEvents","types","typesRef","useEventsHandler","useEventEmitter","payload","from","emit"],"mappings":";;;;;;AAMA;;;;;AAKG;SACaA,aAAaA,GAAA;AAC3B,EAAA,IAAMC,KAAK,GAAGC,gBAAU,CAACC,sBAAU,CAAC;EAEpC,IAAI,CAACF,KAAK,EAAE;AACV,IAAA,MAAM,IAAIG,mBAAc,CACtBC,sCAA0B,EAC1B,+EAA+E,CAChF;AACH,EAAA;AAEA,EAAA,OAAOJ,KAAK;AACd;;ACnBA;;;;AAIG;SACaK,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GAAG,CAACoE,aAAQ,CAAC,CAACC,SAAS,CAAC,UAACC,KAAK,EAAA;;MAC7C,CAAAhD,EAAA,GAAAf,UAAU,CAACG,OAAO,MAAA,IAAA,IAAAY,EAAA,KAAA,MAAA,GAAA,MAAA,GAAAA,EAAA,CAAAiD,IAAA,CAAAhE,UAAA,EAAG+D,KAAK,CAAC;AAC7B,IAAA,CAAC,CAAC;AACJ,EAAA,CAAC,EAAE,CAAC3E,SAAS,CAAC,CAAC;AACjB;;AChBA;;;;AAIG;SACaiF,eAAeA,GAAA;AAC7B,EAAA,IAAMjF,SAAS,GAAcP,aAAa,EAAE,CAACO,SAAS;EAEtD,OAAOE,iBAAW,CAChB,UAAyBC,IAAO,EAAE+E,OAAW,EAAEC,IAAc,EAAA;AAO3DnF,IAAAA,SAAS,CAACK,GAAG,CAACoE,aAAQ,CAAC,CAACW,IAAI,CAAC;AAAEjF,MAAAA,IAAI,EAAAA,IAAA;AAAE+E,MAAAA,OAAO,EAAAA,OAAA;AAAEC,MAAAA,IAAI,EAAAA;AAAA,KAAE,CAAC;AACvD,EAAA,CAAC,EACD,CAACnF,SAAS,CAAC,CACZ;AACH;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ var core = require('@wirestate/core');
4
+ var react = require('react');
5
+
6
+ var ERROR_CODE_VALIDATION_ERROR = 1050;
7
+ var ERROR_CODE_INVALID_CONTEXT = 1052;
8
+ var ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER = 1101;
9
+
10
+ /**
11
+ * React context carrying the IoC container.
12
+ * Internal. Use hooks to access services.
13
+ */
14
+ var IocContext = react.createContext(null);
15
+ IocContext.displayName = "IocContext";
16
+
17
+ /**
18
+ * Provides an IoC container to the component tree.
19
+ *
20
+ * @param props - component props
21
+ * @param props.container - external container instance
22
+ * @param props.seed - shared seed across the container
23
+ * @param props.children - components to wrap
24
+ * @returns provider element
25
+ */
26
+ function IocProvider(_a) {
27
+ var externalContainer = _a.container,
28
+ seed = _a.seed,
29
+ children = _a.children;
30
+ // Incremented on binding changes to invalidate descendant caches (e.g., useInjection).
31
+ var _b = react.useState(1),
32
+ revision = _b[0],
33
+ setRevision = _b[1];
34
+ // Lazy initialize owned container if no external container is provided.
35
+ var ownedContainer = react.useState(function () {
36
+ return externalContainer ? null : core.createIocContainer();
37
+ })[0];
38
+ var container = externalContainer !== null && externalContainer !== void 0 ? externalContainer : ownedContainer;
39
+ if (!container) {
40
+ throw new core.WirestateError(ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER, "IocProvider failed to resolve a container instance.");
41
+ }
42
+ // Context value is stable unless the container or revision changes.
43
+ var value = react.useMemo(function () {
44
+ return {
45
+ container: container,
46
+ revision: revision,
47
+ setRevision: setRevision
48
+ };
49
+ }, [container, revision]);
50
+ react.useEffect(function () {
51
+ if (seed) {
52
+ core.applySharedSeed(container, seed);
53
+ }
54
+ }, [container]);
55
+ return react.createElement(IocContext.Provider, {
56
+ value: value
57
+ }, children);
58
+ }
59
+
60
+ exports.ERROR_CODE_INVALID_CONTEXT = ERROR_CODE_INVALID_CONTEXT;
61
+ exports.ERROR_CODE_VALIDATION_ERROR = ERROR_CODE_VALIDATION_ERROR;
62
+ exports.IocContext = IocContext;
63
+ exports.IocProvider = IocProvider;
64
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sources":["../../../../../../../src/wirestate-react/error/error-code.ts","../../../../../../../src/wirestate-react/provision/ioc-context.ts","../../../../../../../src/wirestate-react/provision/ioc-provider.ts"],"sourcesContent":[null,null,null],"names":["ERROR_CODE_VALIDATION_ERROR","ERROR_CODE_INVALID_CONTEXT","ERROR_CODE_FAILED_TO_RESOLVE_QUERY_HANDLER","IocContext","createContext","displayName","IocProvider","_a","externalContainer","seed","children","_b","useState","revision","setRevision","ownedContainer","createIocContainer","container","WirestateError","value","useMemo","useEffect","applySharedSeed","createElement","Provider"],"mappings":";;;;;AA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