@wirestate/core 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 (183) 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 +301 -0
  5. package/cjs/development/index.js.map +1 -0
  6. package/cjs/development/lib.js +1007 -0
  7. package/cjs/development/lib.js.map +1 -0
  8. package/cjs/development/test-utils.js +128 -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/alias.js +9 -0
  17. package/esm/development/alias.js.map +1 -0
  18. package/esm/development/bind/bind-constant.js +19 -0
  19. package/esm/development/bind/bind-constant.js.map +1 -0
  20. package/esm/development/bind/bind-dynamic-value.js +28 -0
  21. package/esm/development/bind/bind-dynamic-value.js.map +1 -0
  22. package/esm/development/bind/bind-entry.js +39 -0
  23. package/esm/development/bind/bind-entry.js.map +1 -0
  24. package/esm/development/bind/bind-service.js +253 -0
  25. package/esm/development/bind/bind-service.js.map +1 -0
  26. package/esm/development/bind/get-entry-token.js +14 -0
  27. package/esm/development/bind/get-entry-token.js.map +1 -0
  28. package/esm/development/commands/command-bus.js +112 -0
  29. package/esm/development/commands/command-bus.js.map +1 -0
  30. package/esm/development/commands/command-optional.js +16 -0
  31. package/esm/development/commands/command-optional.js.map +1 -0
  32. package/esm/development/commands/command.js +16 -0
  33. package/esm/development/commands/command.js.map +1 -0
  34. package/esm/development/commands/get-command-handler-metadata.js +27 -0
  35. package/esm/development/commands/get-command-handler-metadata.js.map +1 -0
  36. package/esm/development/commands/on-command.js +26 -0
  37. package/esm/development/commands/on-command.js.map +1 -0
  38. package/esm/development/container/create-ioc-container.js +35 -0
  39. package/esm/development/container/create-ioc-container.js.map +1 -0
  40. package/esm/development/container/wire-scope.js +151 -0
  41. package/esm/development/container/wire-scope.js.map +1 -0
  42. package/esm/development/error/error-code.js +10 -0
  43. package/esm/development/error/error-code.js.map +1 -0
  44. package/esm/development/error/wirestate-error.js +29 -0
  45. package/esm/development/error/wirestate-error.js.map +1 -0
  46. package/esm/development/events/build-event-dispatcher.js +39 -0
  47. package/esm/development/events/build-event-dispatcher.js.map +1 -0
  48. package/esm/development/events/emit-event.js +20 -0
  49. package/esm/development/events/emit-event.js.map +1 -0
  50. package/esm/development/events/event-bus.js +52 -0
  51. package/esm/development/events/event-bus.js.map +1 -0
  52. package/esm/development/events/get-event-handler-metadata.js +27 -0
  53. package/esm/development/events/get-event-handler-metadata.js.map +1 -0
  54. package/esm/development/events/on-event.js +29 -0
  55. package/esm/development/events/on-event.js.map +1 -0
  56. package/esm/development/index.js +28 -0
  57. package/esm/development/index.js.map +1 -0
  58. package/esm/development/queries/get-query-handler-metadata.js +27 -0
  59. package/esm/development/queries/get-query-handler-metadata.js.map +1 -0
  60. package/esm/development/queries/on-query.js +26 -0
  61. package/esm/development/queries/on-query.js.map +1 -0
  62. package/esm/development/queries/query-bus.js +99 -0
  63. package/esm/development/queries/query-bus.js.map +1 -0
  64. package/esm/development/queries/query-optional.js +16 -0
  65. package/esm/development/queries/query-optional.js.map +1 -0
  66. package/esm/development/queries/query.js +16 -0
  67. package/esm/development/queries/query.js.map +1 -0
  68. package/esm/development/registry.js +56 -0
  69. package/esm/development/registry.js.map +1 -0
  70. package/esm/development/seeds/apply-seeds.js +21 -0
  71. package/esm/development/seeds/apply-seeds.js.map +1 -0
  72. package/esm/development/seeds/apply-shared-seed.js +14 -0
  73. package/esm/development/seeds/apply-shared-seed.js.map +1 -0
  74. package/esm/development/seeds/unapply-seeds.js +19 -0
  75. package/esm/development/seeds/unapply-seeds.js.map +1 -0
  76. package/esm/development/service/get-activated-handler-metadata.js +27 -0
  77. package/esm/development/service/get-activated-handler-metadata.js.map +1 -0
  78. package/esm/development/service/get-deactivation-handler-metadata.js +27 -0
  79. package/esm/development/service/get-deactivation-handler-metadata.js.map +1 -0
  80. package/esm/development/service/on-activated.js +21 -0
  81. package/esm/development/service/on-activated.js.map +1 -0
  82. package/esm/development/service/on-deactivation.js +21 -0
  83. package/esm/development/service/on-deactivation.js.map +1 -0
  84. package/esm/development/test-utils/mock-bind-entry.js +24 -0
  85. package/esm/development/test-utils/mock-bind-entry.js.map +1 -0
  86. package/esm/development/test-utils/mock-bind-service.js +24 -0
  87. package/esm/development/test-utils/mock-bind-service.js.map +1 -0
  88. package/esm/development/test-utils/mock-container.js +52 -0
  89. package/esm/development/test-utils/mock-container.js.map +1 -0
  90. package/esm/development/test-utils/mock-service.js +26 -0
  91. package/esm/development/test-utils/mock-service.js.map +1 -0
  92. package/esm/development/test-utils/mock-unbind-service.js +13 -0
  93. package/esm/development/test-utils/mock-unbind-service.js.map +1 -0
  94. package/esm/development/test-utils.js +6 -0
  95. package/esm/development/test-utils.js.map +1 -0
  96. package/esm/development/types/commands.js +12 -0
  97. package/esm/development/types/commands.js.map +1 -0
  98. package/esm/production/alias.js +1 -0
  99. package/esm/production/alias.js.map +1 -0
  100. package/esm/production/bind/bind-constant.js +1 -0
  101. package/esm/production/bind/bind-constant.js.map +1 -0
  102. package/esm/production/bind/bind-dynamic-value.js +1 -0
  103. package/esm/production/bind/bind-dynamic-value.js.map +1 -0
  104. package/esm/production/bind/bind-entry.js +1 -0
  105. package/esm/production/bind/bind-entry.js.map +1 -0
  106. package/esm/production/bind/bind-service.js +1 -0
  107. package/esm/production/bind/bind-service.js.map +1 -0
  108. package/esm/production/bind/get-entry-token.js +1 -0
  109. package/esm/production/bind/get-entry-token.js.map +1 -0
  110. package/esm/production/commands/command-bus.js +1 -0
  111. package/esm/production/commands/command-bus.js.map +1 -0
  112. package/esm/production/commands/command-optional.js +1 -0
  113. package/esm/production/commands/command-optional.js.map +1 -0
  114. package/esm/production/commands/command.js +1 -0
  115. package/esm/production/commands/command.js.map +1 -0
  116. package/esm/production/commands/get-command-handler-metadata.js +1 -0
  117. package/esm/production/commands/get-command-handler-metadata.js.map +1 -0
  118. package/esm/production/commands/on-command.js +1 -0
  119. package/esm/production/commands/on-command.js.map +1 -0
  120. package/esm/production/container/create-ioc-container.js +1 -0
  121. package/esm/production/container/create-ioc-container.js.map +1 -0
  122. package/esm/production/container/wire-scope.js +1 -0
  123. package/esm/production/container/wire-scope.js.map +1 -0
  124. package/esm/production/error/error-code.js +1 -0
  125. package/esm/production/error/error-code.js.map +1 -0
  126. package/esm/production/error/wirestate-error.js +1 -0
  127. package/esm/production/error/wirestate-error.js.map +1 -0
  128. package/esm/production/events/build-event-dispatcher.js +1 -0
  129. package/esm/production/events/build-event-dispatcher.js.map +1 -0
  130. package/esm/production/events/emit-event.js +1 -0
  131. package/esm/production/events/emit-event.js.map +1 -0
  132. package/esm/production/events/event-bus.js +1 -0
  133. package/esm/production/events/event-bus.js.map +1 -0
  134. package/esm/production/events/get-event-handler-metadata.js +1 -0
  135. package/esm/production/events/get-event-handler-metadata.js.map +1 -0
  136. package/esm/production/events/on-event.js +1 -0
  137. package/esm/production/events/on-event.js.map +1 -0
  138. package/esm/production/index.js +1 -0
  139. package/esm/production/index.js.map +1 -0
  140. package/esm/production/queries/get-query-handler-metadata.js +1 -0
  141. package/esm/production/queries/get-query-handler-metadata.js.map +1 -0
  142. package/esm/production/queries/on-query.js +1 -0
  143. package/esm/production/queries/on-query.js.map +1 -0
  144. package/esm/production/queries/query-bus.js +1 -0
  145. package/esm/production/queries/query-bus.js.map +1 -0
  146. package/esm/production/queries/query-optional.js +1 -0
  147. package/esm/production/queries/query-optional.js.map +1 -0
  148. package/esm/production/queries/query.js +1 -0
  149. package/esm/production/queries/query.js.map +1 -0
  150. package/esm/production/registry.js +1 -0
  151. package/esm/production/registry.js.map +1 -0
  152. package/esm/production/seeds/apply-seeds.js +1 -0
  153. package/esm/production/seeds/apply-seeds.js.map +1 -0
  154. package/esm/production/seeds/apply-shared-seed.js +1 -0
  155. package/esm/production/seeds/apply-shared-seed.js.map +1 -0
  156. package/esm/production/seeds/unapply-seeds.js +1 -0
  157. package/esm/production/seeds/unapply-seeds.js.map +1 -0
  158. package/esm/production/service/get-activated-handler-metadata.js +1 -0
  159. package/esm/production/service/get-activated-handler-metadata.js.map +1 -0
  160. package/esm/production/service/get-deactivation-handler-metadata.js +1 -0
  161. package/esm/production/service/get-deactivation-handler-metadata.js.map +1 -0
  162. package/esm/production/service/on-activated.js +1 -0
  163. package/esm/production/service/on-activated.js.map +1 -0
  164. package/esm/production/service/on-deactivation.js +1 -0
  165. package/esm/production/service/on-deactivation.js.map +1 -0
  166. package/esm/production/test-utils/mock-bind-entry.js +1 -0
  167. package/esm/production/test-utils/mock-bind-entry.js.map +1 -0
  168. package/esm/production/test-utils/mock-bind-service.js +1 -0
  169. package/esm/production/test-utils/mock-bind-service.js.map +1 -0
  170. package/esm/production/test-utils/mock-container.js +1 -0
  171. package/esm/production/test-utils/mock-container.js.map +1 -0
  172. package/esm/production/test-utils/mock-service.js +1 -0
  173. package/esm/production/test-utils/mock-service.js.map +1 -0
  174. package/esm/production/test-utils/mock-unbind-service.js +1 -0
  175. package/esm/production/test-utils/mock-unbind-service.js.map +1 -0
  176. package/esm/production/test-utils.js +1 -0
  177. package/esm/production/test-utils.js.map +1 -0
  178. package/esm/production/types/commands.js +1 -0
  179. package/esm/production/types/commands.js.map +1 -0
  180. package/index.d.ts +566 -0
  181. package/lib.d.ts +17 -0
  182. package/package.json +61 -0
  183. package/test-utils.d.ts +108 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,51 @@
1
+ ## 0.6.0
2
+
3
+ - Split wirestate as separate @wirestate packages
4
+
5
+ ## 0.5.0
6
+
7
+ - Add default initial state param on creation of containers
8
+ - Explicit typing for query callers, export new types
9
+ - For wire-scope add missing optional query/command/resolve methods
10
+ - Add sourcemaps in lib bundles
11
+ - Add `signals` entry for variants of storage usage without mobx
12
+
13
+ ## 0.4.0
14
+
15
+ - Add `OnActivated` decorator
16
+ - Add `OnDeactivated` decorator
17
+ - `initialState` -> `seed`
18
+ - `type` -> `bindingType`
19
+ - `scopeType` -> `bindingScopeType`
20
+ - Emit events with two params instead of manually composed object, signal `from`
21
+ - Add commands module
22
+ - Add WireScope shared class for managing of wirestate events, queries and commands
23
+ - Remove abstract service in favor of WireScope usage
24
+ - Signals -> events
25
+ - `useOptionalInjection` -> add fallback handler
26
+
27
+ ## 0.3.0
28
+
29
+ - Rename `createServicesProvider` to `createInjectablesProvider`
30
+ - Add `useSignals`, `useSignalHandler` hooks for better signal handling ergonomics
31
+ - Add `useOptionalInjection` hook for safe resolution of optional dependencies
32
+ - Support optional queries via `queryOptional`, `useOptionalQueryCaller`, and `useOptionalSyncQueryCaller`
33
+ - Support merging of initial states when multiple providers co-exist
34
+ - Add more re-exports from `mobx` and `inversify` for easier consumer usage
35
+ - Improve test coverage, extend test utilities
36
+ - Correctly notify about `IS_DISPOSED` after deactivation of services
37
+
38
+ ## 0.2.0
39
+
40
+ - Activate and deactivate services in stack ordering
41
+ - Portable libs will not fail with react compiler builds
42
+ - Extend test-utils
43
+ - Increasing test coverage
44
+ - Error handling / custom error class
45
+ - Allow addition of constant bindings in the IOC container
46
+ - useService -> useInjection
47
+ - AbstractService::getService -> AbstractService::resolve
48
+
49
+ ## 0.1.1
50
+
51
+ - Initial release
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Syrotenko Igor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,319 @@
1
+ # <a href='https://www.npmjs.com/package/wirestate'> ⚡ wirestate </a>
2
+
3
+ [![npm version](https://img.shields.io/npm/v/wirestate.svg?style=flat-square)](https://www.npmjs.com/package/wirestate)
4
+ [![language-ts](https://img.shields.io/badge/language-typescript-blue.svg?style=flat)](https://github.com/Neloreck/wirestate/search?l=typescript)
5
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/Neloreck/wirestate/blob/master/LICENSE)
6
+
7
+ `wirestate` is a reactivity-independent state management framework for React. It integrates **InversifyJS** for robust Dependency Injection,
8
+ providing IOC/DI/indirection based architecture based on concepts of Services, Events, Commands, and Queries.
9
+
10
+ It optionally works with **MobX**, **Signals**, or other custom reactivity implementations.
11
+
12
+ ## Architecture & Core Concepts
13
+
14
+ Designed for complex applications, `wirestate` enforces a clear separation of concerns:
15
+
16
+ - **Services**: Singleton-scoped logic units that hold state and business logic.
17
+ - **Dependency Injection**: First-class InversifyJS integration for decoupled, testable code.
18
+ - **Reactivity**: Independent state tracking (optional integration with MobX, Signals, etc.).
19
+ - **Events**: Fire-and-forget communication for cross-service side effects.
20
+ - **Commands**: Encapsulated write operations with standardized execution status (pending, settled, error).
21
+ - **Queries**: Synchronous or asynchronous request-response patterns for data retrieval.
22
+ - **Lifecycle Management**: Automated services provision within react tree, activation/deactivation lifecycle.
23
+
24
+ ## Requirements
25
+
26
+ - `react >= 16.8.0`
27
+ - `reflect-metadata` (must be imported at application entry)
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ npm install --save @wirestate/core reflect-metadata
33
+ ```
34
+
35
+ ### For react-mobx
36
+
37
+ ```bash
38
+ npm install --save @wirestate/react @wirestate/react-mobx mobx mobx-react-lite
39
+ ```
40
+
41
+ ### For signals
42
+
43
+ ```bash
44
+ npm install --save @wirestate/react @wirestate/react-signals @preact/signals-react
45
+ npm install --save-dev @preact/signals-react-transform
46
+ ```
47
+
48
+ ## Quick Start with mobx
49
+
50
+ ### 1. Define a Service
51
+
52
+ Services are standard classes decorated with `@Injectable`. Use `WireScope` to interact with the framework.
53
+
54
+ ```typescript
55
+ import { Injectable, Inject, WireScope, OnEvent, OnCommand, OnQuery } from '@wirestate/core';
56
+ import { makeObservable, Observable, Action } from '@wirestate/react-mobx';
57
+
58
+ @Injectable()
59
+ export class CounterService {
60
+ @Observable()
61
+ public count: number = 0;
62
+
63
+ public constructor(
64
+ @Inject(WireScope)
65
+ private scope: WireScope
66
+ ) {
67
+ makeObservable(this);
68
+ }
69
+
70
+ @Action()
71
+ public increment(amount: number = 1): void {
72
+ this.count += amount;
73
+ }
74
+
75
+ @Action()
76
+ public reset(): void {
77
+ this.count = 0;
78
+ }
79
+
80
+ @OnCommand('INCREMENT')
81
+ public onIncrementCommand(amount: number = 1): void {
82
+ this.increment(amount);
83
+ }
84
+
85
+ @OnQuery('GET_TOTAL')
86
+ public onGetTotal(): number {
87
+ return this.count;
88
+ }
89
+
90
+ @OnEvent('RESET')
91
+ public onResetEvent(): void {
92
+ this.reset();
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### 2. Configure the Provider
98
+
99
+ Bind services at any level of the component tree.
100
+ Lifetimes are managed automatically.
101
+
102
+ ```tsx
103
+ import { IocProvider, createInjectablesProvider } from '@wirestate/react';
104
+ import { CounterService } from './CounterService';
105
+
106
+ const MainProvider = createInjectablesProvider([CounterService]);
107
+
108
+ export function Application() {
109
+ return (
110
+ <IocProvider>
111
+ <MainProvider>
112
+ <CounterView />
113
+ </MainProvider>
114
+ </IocProvider>
115
+ );
116
+ }
117
+ ```
118
+
119
+ ### 3. Consume in Components
120
+
121
+ Directly use services and rely on mobx reactivity.
122
+ Or use specialized hooks for communication without direct references.
123
+
124
+ ```tsx
125
+ import { useInjection, useCommandCaller, useEventEmitter } from '@wirestate/react';
126
+ import { observer } from '@wirestate/react-mobx';
127
+ import { CounterService } from './CounterService';
128
+
129
+ export const CounterView = observer(() => {
130
+ const service = useInjection(CounterService);
131
+ const call = useCommandCaller();
132
+ const emit = useEventEmitter();
133
+
134
+ return (
135
+ <div>
136
+ <p>Count: {service.count}</p>
137
+ <button onClick={() => service.increment(5)}>Add 1 (Method)</button>
138
+ <button onClick={() => call('INCREMENT', 5)}>Add 5 (Command)</button>
139
+ <button onClick={() => service.reset()}>Reset (Method)</button>
140
+ <button onClick={() => emit('RESET')}>Reset (Event)</button>
141
+ </div>
142
+ );
143
+ });
144
+ ```
145
+
146
+ ## Quick Start with signals
147
+
148
+ ### 1. Define a Service
149
+
150
+ Services use `signal` and `computed` for state management.
151
+
152
+ ```typescript
153
+ import { Injectable, Inject, WireScope, OnEvent, OnCommand, OnQuery } from '@wirestate/core';
154
+ import { signal, computed, Signal, ReadonlySignal } from '@wirestate/react-signals';
155
+
156
+ @Injectable()
157
+ export class CounterService {
158
+ public readonly count: Signal<number> = signal(0);
159
+ public readonly isEven: ReadonlySignal<boolean> = computed(() => this.count.value % 2 === 0);
160
+
161
+ public constructor(
162
+ @Inject(WireScope)
163
+ private scope: WireScope
164
+ ) {}
165
+
166
+ public increment(amount: number = 1): void {
167
+ this.count.value += amount;
168
+ }
169
+
170
+ public reset(): void {
171
+ this.count.value = 0;
172
+ }
173
+
174
+ @OnCommand('INCREMENT')
175
+ public onIncrementCommand(amount: number = 1): void {
176
+ this.increment(amount);
177
+ }
178
+
179
+ @OnQuery('GET_TOTAL')
180
+ public onGetTotal(): number {
181
+ return this.count.value;
182
+ }
183
+
184
+ @OnEvent('RESET')
185
+ public onResetEvent(): void {
186
+ this.reset();
187
+ }
188
+ }
189
+ ```
190
+
191
+ ### 2. Configure the Provider
192
+
193
+ The provider configuration remains the same regardless of the reactivity implementation.
194
+
195
+ ```tsx
196
+ import { IocProvider, createInjectablesProvider } from '@wirestate/react';
197
+ import { CounterService } from './CounterService';
198
+
199
+ const MainProvider = createInjectablesProvider([CounterService]);
200
+
201
+ export function Application() {
202
+ return (
203
+ <IocProvider>
204
+ <MainProvider>
205
+ <CounterView />
206
+ </MainProvider>
207
+ </IocProvider>
208
+ );
209
+ }
210
+ ```
211
+
212
+ ### 3. Consume in Components
213
+
214
+ Access signals directly in components. Reactivity is handled by the signals transform or manual subscription.
215
+
216
+ ```tsx
217
+ import { useInjection, useCommandCaller, useEventEmitter } from '@wirestate/react';
218
+ import { CounterService } from './CounterService';
219
+
220
+ export function CounterView() {
221
+ const service = useInjection(CounterService);
222
+ const call = useCommandCaller();
223
+ const emit = useEventEmitter();
224
+
225
+ return (
226
+ <div>
227
+ <p>Count: {service.count}</p>
228
+ <p>Even: {service.isEven.value ? 'Yes' : 'No'}</p>
229
+ <button onClick={() => service.increment(5)}>Add 1 (Method)</button>
230
+ <button onClick={() => call('INCREMENT', 5)}>Add 5 (Command)</button>
231
+ <button onClick={() => service.reset()}>Reset (Method)</button>
232
+ <button onClick={() => emit('RESET')}>Reset (Event)</button>
233
+ </div>
234
+ );
235
+ }
236
+ ```
237
+
238
+ ## Advanced Usage
239
+
240
+ ### Seeding Shared Initial State
241
+
242
+ `wirestate` supports providing initial data (seeds) to services during activation.
243
+
244
+ ```tsx
245
+ const MainProvider = createInjectablesProvider([CounterService]);
246
+
247
+ <IocProvider>
248
+ <MainProvider seed={{ initialCount: 100 }}>
249
+ <CounterView />
250
+ </MainProvider>
251
+ </IocProvider>
252
+ ```
253
+
254
+ In the service:
255
+
256
+ ```typescript
257
+ import { Injectable, Inject, SEED } from 'wirestate';
258
+
259
+ @Injectable()
260
+ export class CounterService {
261
+ // ...
262
+ public constructor(
263
+ @Inject(SEED)
264
+ initialState: { initialCount: number }
265
+ ) {
266
+ this.count = seed.initialCount;
267
+ }
268
+ }
269
+ ```
270
+
271
+ ### Seeding Bound Initial State
272
+
273
+ `wirestate` supports providing initial data (seeds) to services during activation.
274
+
275
+ ```tsx
276
+ const MainProvider = createInjectablesProvider([CounterService]);
277
+
278
+ <IocProvider>
279
+ <MainProvider seeds={[[CounterService, { count: 10 }]]}>
280
+ <CounterView />
281
+ </MainProvider>
282
+ </IocProvider>
283
+ ```
284
+
285
+ In the service:
286
+
287
+ ```typescript
288
+ import { Injectable, Inject, WireScope } from 'wirestate';
289
+
290
+ @Injectable()
291
+ export class CounterService {
292
+ // ...
293
+ public constructor(@Inject(WireScope) scope: WireScope) {
294
+ this.count = scope.getSeed(CounterService).count;
295
+ }
296
+ }
297
+ ```
298
+
299
+ ### Service Lifecycle
300
+
301
+ Use decorators to handle initialization and cleanup.
302
+
303
+ ```typescript
304
+ import { OnActivated, OnDeactivation } from 'wirestate';
305
+
306
+ @OnActivated()
307
+ public onActivated(): void {
308
+ // Start polling, fetch initial data, etc.
309
+ }
310
+
311
+ @OnDeactivation()
312
+ public onDeactivation(): void {
313
+ // Cleanup subscriptions
314
+ }
315
+ ```
316
+
317
+ ## License
318
+
319
+ MIT
@@ -0,0 +1,301 @@
1
+ 'use strict';
2
+
3
+ var inversify = require('inversify');
4
+ var createIocContainer = require('./lib.js');
5
+ var tslib = require('tslib');
6
+
7
+ function forwardRef(forward) {
8
+ return new inversify.LazyServiceIdentifier(forward);
9
+ }
10
+
11
+ /**
12
+ * Dispatches a command on the provided container.
13
+ *
14
+ * @param container - inversify container
15
+ * @param type - command type
16
+ * @param data - command data
17
+ * @returns command descriptor
18
+ */
19
+ function command(container, type, data) {
20
+ return container.get(createIocContainer.CommandBus).command(type, data);
21
+ }
22
+
23
+ /**
24
+ * Dispatches a command on the provided container, returning null if no handler is registered.
25
+ *
26
+ * @param container - inversify container
27
+ * @param type - command type
28
+ * @param data - command data
29
+ * @returns command descriptor or null
30
+ */
31
+ function commandOptional(container, type, data) {
32
+ return container.get(createIocContainer.CommandBus).commandOptional(type, data);
33
+ }
34
+
35
+ /**
36
+ * Decorator for service methods that handle a command.
37
+ *
38
+ * @param type - command type identifier
39
+ * @returns decorator function
40
+ */
41
+ function OnCommand(type) {
42
+ return function (target, propertyKey) {
43
+ var constructor = target.constructor;
44
+ var list = createIocContainer.COMMAND_HANDLER_METADATA.get(constructor);
45
+ if (!list) {
46
+ list = [];
47
+ createIocContainer.COMMAND_HANDLER_METADATA.set(constructor, list);
48
+ }
49
+ // Register handler metadata for prototype-based retrieval.
50
+ list.push({
51
+ methodName: propertyKey,
52
+ type: type
53
+ });
54
+ };
55
+ }
56
+
57
+ /**
58
+ * Emits events for container from outside scope.
59
+ *
60
+ * @param container - inversify container
61
+ * @param type - event type ot emit
62
+ * @param payload - event payload
63
+ * @param from - optional indicator of the event source
64
+ */
65
+ function emitEvent(container, type, payload, from) {
66
+ container.get(createIocContainer.EventBus).emit({
67
+ type: type,
68
+ payload: payload,
69
+ from: from
70
+ });
71
+ }
72
+
73
+ /**
74
+ * Decorator for service methods that respond to events.
75
+ *
76
+ * @param types - event type(s) to handle. If omitted, handles all events
77
+ * @returns decorator function
78
+ */
79
+ function OnEvent(types) {
80
+ // Normalize types to an array or null for catch-all.
81
+ var normalized = types === undefined ? null : Array.isArray(types) ? tslib.__spreadArray([], types, true) : [types];
82
+ return function (target, propertyKey) {
83
+ var constructor = target.constructor;
84
+ var list = createIocContainer.EVENT_HANDLER_METADATA.get(constructor);
85
+ if (!list) {
86
+ list = [];
87
+ createIocContainer.EVENT_HANDLER_METADATA.set(constructor, list);
88
+ }
89
+ // Register handler metadata for prototype-based retrieval.
90
+ list.push({
91
+ methodName: propertyKey,
92
+ types: normalized
93
+ });
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Dispatches a query on the provided container.
99
+ *
100
+ * @param container - inversify container
101
+ * @param type - query type
102
+ * @param data - query data
103
+ * @returns query result
104
+ */
105
+ function query(container, type, data) {
106
+ return container.get(createIocContainer.QueryBus).query(type, data);
107
+ }
108
+
109
+ /**
110
+ * Dispatches a query on the provided container, returning null if no handler is registered.
111
+ *
112
+ * @param container - inversify container
113
+ * @param type - query type
114
+ * @param data - query data
115
+ * @returns query result or null
116
+ */
117
+ function queryOptional(container, type, data) {
118
+ return container.get(createIocContainer.QueryBus).queryOptional(type, data);
119
+ }
120
+
121
+ /**
122
+ * Decorator for service methods that respond to a query.
123
+ *
124
+ * @param type - query type identifier
125
+ * @returns decorator function
126
+ */
127
+ function OnQuery(type) {
128
+ return function (target, propertyKey) {
129
+ var constructor = target.constructor;
130
+ var list = createIocContainer.QUERY_HANDLER_METADATA.get(constructor);
131
+ if (!list) {
132
+ list = [];
133
+ createIocContainer.QUERY_HANDLER_METADATA.set(constructor, list);
134
+ }
135
+ // Register handler metadata for prototype-based retrieval.
136
+ list.push({
137
+ methodName: propertyKey,
138
+ type: type
139
+ });
140
+ };
141
+ }
142
+
143
+ /**
144
+ * Applies seeds to the container into the existing instance instead of replacing it.
145
+ * This allows multiple providers to co-exist without wiping each other's seeds.
146
+ *
147
+ * @param container - target container
148
+ * @param seeds - targeted seed entries apply
149
+ */
150
+ function applySeeds(container, seeds) {
151
+ var existing = container.get(createIocContainer.SEEDS_TOKEN);
152
+ for (var _i = 0, seeds_1 = seeds; _i < seeds_1.length; _i++) {
153
+ var _a = seeds_1[_i],
154
+ key = _a[0],
155
+ state = _a[1];
156
+ existing.set(key, state);
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Applies shared seed to the container.
162
+ *
163
+ * @param container - target container
164
+ * @param seed - shared seed object
165
+ */
166
+ function applySharedSeed(container, seed) {
167
+ container.rebind(createIocContainer.SEED_TOKEN).toConstantValue(seed);
168
+ }
169
+
170
+ /**
171
+ * Removes specific seeds from the container.
172
+ * Used during provider unmounting to clean up only the entries owned by that provider.
173
+ *
174
+ * @param container - target container
175
+ * @param seeds - targeted seeds to remove
176
+ */
177
+ function unapplySeeds(container, seeds) {
178
+ var existing = container.get(createIocContainer.SEEDS_TOKEN);
179
+ for (var _i = 0, seeds_1 = seeds; _i < seeds_1.length; _i++) {
180
+ var key = seeds_1[_i][0];
181
+ existing.delete(key);
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Decorator for service methods that run after activation.
187
+ *
188
+ * @returns decorator function
189
+ */
190
+ function OnActivated() {
191
+ return function (target, propertyKey) {
192
+ var constructor = target.constructor;
193
+ var list = createIocContainer.ACTIVATED_HANDLER_METADATA.get(constructor);
194
+ if (!list) {
195
+ list = [];
196
+ createIocContainer.ACTIVATED_HANDLER_METADATA.set(constructor, list);
197
+ }
198
+ list.push(propertyKey);
199
+ };
200
+ }
201
+
202
+ /**
203
+ * Decorator for service methods that run before deactivation.
204
+ *
205
+ * @returns decorator function
206
+ */
207
+ function OnDeactivation() {
208
+ return function (target, propertyKey) {
209
+ var constructor = target.constructor;
210
+ var list = createIocContainer.DEACTIVATION_HANDLER_METADATA.get(constructor);
211
+ if (!list) {
212
+ list = [];
213
+ createIocContainer.DEACTIVATION_HANDLER_METADATA.set(constructor, list);
214
+ }
215
+ list.push(propertyKey);
216
+ };
217
+ }
218
+
219
+ Object.defineProperty(exports, "BindingType", {
220
+ enumerable: true,
221
+ get: function () { return inversify.bindingTypeValues; }
222
+ });
223
+ Object.defineProperty(exports, "Container", {
224
+ enumerable: true,
225
+ get: function () { return inversify.Container; }
226
+ });
227
+ Object.defineProperty(exports, "ContainerModule", {
228
+ enumerable: true,
229
+ get: function () { return inversify.ContainerModule; }
230
+ });
231
+ Object.defineProperty(exports, "Inject", {
232
+ enumerable: true,
233
+ get: function () { return inversify.inject; }
234
+ });
235
+ Object.defineProperty(exports, "Injectable", {
236
+ enumerable: true,
237
+ get: function () { return inversify.injectable; }
238
+ });
239
+ Object.defineProperty(exports, "LazyServiceIdentifier", {
240
+ enumerable: true,
241
+ get: function () { return inversify.LazyServiceIdentifier; }
242
+ });
243
+ Object.defineProperty(exports, "MultiInject", {
244
+ enumerable: true,
245
+ get: function () { return inversify.multiInject; }
246
+ });
247
+ Object.defineProperty(exports, "Named", {
248
+ enumerable: true,
249
+ get: function () { return inversify.named; }
250
+ });
251
+ Object.defineProperty(exports, "Optional", {
252
+ enumerable: true,
253
+ get: function () { return inversify.optional; }
254
+ });
255
+ Object.defineProperty(exports, "PostConstruct", {
256
+ enumerable: true,
257
+ get: function () { return inversify.postConstruct; }
258
+ });
259
+ Object.defineProperty(exports, "PreDestroy", {
260
+ enumerable: true,
261
+ get: function () { return inversify.preDestroy; }
262
+ });
263
+ Object.defineProperty(exports, "ScopeBindingType", {
264
+ enumerable: true,
265
+ get: function () { return inversify.bindingScopeValues; }
266
+ });
267
+ Object.defineProperty(exports, "Tagged", {
268
+ enumerable: true,
269
+ get: function () { return inversify.tagged; }
270
+ });
271
+ exports.CommandBus = createIocContainer.CommandBus;
272
+ Object.defineProperty(exports, "CommandStatus", {
273
+ enumerable: true,
274
+ get: function () { return createIocContainer.ECommandStatus; }
275
+ });
276
+ exports.EventBus = createIocContainer.EventBus;
277
+ exports.QueryBus = createIocContainer.QueryBus;
278
+ exports.SEED = createIocContainer.SEED_TOKEN;
279
+ exports.SEEDS = createIocContainer.SEEDS_TOKEN;
280
+ exports.WireScope = createIocContainer.WireScope;
281
+ exports.WirestateError = createIocContainer.WirestateError;
282
+ exports.bindConstant = createIocContainer.bindConstant;
283
+ exports.bindEntry = createIocContainer.bindEntry;
284
+ exports.bindService = createIocContainer.bindService;
285
+ exports.createIocContainer = createIocContainer.createIocContainer;
286
+ exports.getEntryToken = createIocContainer.getEntryToken;
287
+ exports.OnActivated = OnActivated;
288
+ exports.OnCommand = OnCommand;
289
+ exports.OnDeactivation = OnDeactivation;
290
+ exports.OnEvent = OnEvent;
291
+ exports.OnQuery = OnQuery;
292
+ exports.applySeeds = applySeeds;
293
+ exports.applySharedSeed = applySharedSeed;
294
+ exports.command = command;
295
+ exports.commandOptional = commandOptional;
296
+ exports.emitEvent = emitEvent;
297
+ exports.forwardRef = forwardRef;
298
+ exports.query = query;
299
+ exports.queryOptional = queryOptional;
300
+ exports.unapplySeeds = unapplySeeds;
301
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../../../../../../src/wirestate-core/alias.ts","../../../../../../../src/wirestate-core/commands/command.ts","../../../../../../../src/wirestate-core/commands/command-optional.ts","../../../../../../../src/wirestate-core/commands/on-command.ts","../../../../../../../src/wirestate-core/events/emit-event.ts","../../../../../../../src/wirestate-core/events/on-event.ts","../../../../../../../src/wirestate-core/queries/query.ts","../../../../../../../src/wirestate-core/queries/query-optional.ts","../../../../../../../src/wirestate-core/queries/on-query.ts","../../../../../../../src/wirestate-core/seeds/apply-seeds.ts","../../../../../../../src/wirestate-core/seeds/apply-shared-seed.ts","../../../../../../../src/wirestate-core/seeds/unapply-seeds.ts","../../../../../../../src/wirestate-core/service/on-activated.ts","../../../../../../../src/wirestate-core/service/on-deactivation.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null,null,null,null],"names":["forwardRef","forward","LazyServiceIdentifier","command","container","type","data","get","CommandBus","commandOptional","OnCommand","target","propertyKey","constructor","list","COMMAND_HANDLER_METADATA","set","push","methodName","emitEvent","payload","from","EventBus","emit","OnEvent","types","normalized","undefined","Array","isArray","__spreadArray","EVENT_HANDLER_METADATA","query","QueryBus","queryOptional","OnQuery","QUERY_HANDLER_METADATA","applySeeds","seeds","existing","SEEDS_TOKEN","_i","seeds_1","_a","key","state","applySharedSeed","seed","rebind","SEED_TOKEN","toConstantValue","unapplySeeds","delete","OnActivated","ACTIVATED_HANDLER_METADATA","OnDeactivation","DEACTIVATION_HANDLER_METADATA"],"mappings":";;;;;;AAoBM,SAAUA,UAAUA,CACxBC,OAA2C,EAAA;AAE3C,EAAA,OAAO,IAAIC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