@wirestate/core 0.6.0 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +173 -230
- package/cjs/development/index.js +1 -1
- package/cjs/development/index.js.map +1 -1
- package/cjs/development/lib.js +9 -9
- package/cjs/development/lib.js.map +1 -1
- package/cjs/development/test-utils.js.map +1 -1
- package/cjs/production/index.js.map +1 -1
- package/cjs/production/lib.js.map +1 -1
- package/cjs/production/test-utils.js.map +1 -1
- package/esm/development/bind/bind-constant.js.map +1 -1
- package/esm/development/bind/bind-dynamic-value.js.map +1 -1
- package/esm/development/bind/bind-entry.js.map +1 -1
- package/esm/development/bind/bind-service.js.map +1 -1
- package/esm/development/bind/get-entry-token.js.map +1 -1
- package/esm/development/commands/command-bus.js +4 -4
- package/esm/development/commands/command-bus.js.map +1 -1
- package/esm/development/commands/command-optional.js.map +1 -1
- package/esm/development/commands/command.js.map +1 -1
- package/esm/development/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/development/commands/on-command.js.map +1 -1
- package/esm/development/container/create-ioc-container.js +1 -1
- package/esm/development/container/create-ioc-container.js.map +1 -1
- package/esm/development/container/wire-scope.js.map +1 -1
- package/esm/development/events/build-event-dispatcher.js.map +1 -1
- package/esm/development/events/emit-event.js.map +1 -1
- package/esm/development/events/event-bus.js.map +1 -1
- package/esm/development/events/get-event-handler-metadata.js.map +1 -1
- package/esm/development/events/on-event.js.map +1 -1
- package/esm/development/index.js +1 -1
- package/esm/development/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/development/queries/on-query.js.map +1 -1
- package/esm/development/queries/query-bus.js.map +1 -1
- package/esm/development/queries/query-optional.js.map +1 -1
- package/esm/development/queries/query.js.map +1 -1
- package/esm/development/registry.js.map +1 -1
- package/esm/development/seeds/apply-seeds.js.map +1 -1
- package/esm/development/seeds/apply-shared-seed.js.map +1 -1
- package/esm/development/seeds/unapply-seeds.js.map +1 -1
- package/esm/development/test-utils/mock-bind-entry.js.map +1 -1
- package/esm/development/test-utils/mock-bind-service.js.map +1 -1
- package/esm/development/test-utils/mock-container.js.map +1 -1
- package/esm/development/test-utils/mock-service.js.map +1 -1
- package/esm/development/types/commands.js +7 -7
- package/esm/development/types/commands.js.map +1 -1
- package/esm/production/bind/bind-entry.js.map +1 -1
- package/esm/production/bind/bind-service.js.map +1 -1
- package/esm/production/commands/command-bus.js +1 -1
- package/esm/production/commands/command-bus.js.map +1 -1
- package/esm/production/commands/command-optional.js.map +1 -1
- package/esm/production/commands/command.js.map +1 -1
- package/esm/production/commands/get-command-handler-metadata.js.map +1 -1
- package/esm/production/commands/on-command.js.map +1 -1
- package/esm/production/container/create-ioc-container.js +1 -1
- package/esm/production/container/create-ioc-container.js.map +1 -1
- package/esm/production/container/wire-scope.js.map +1 -1
- package/esm/production/events/build-event-dispatcher.js.map +1 -1
- package/esm/production/events/emit-event.js.map +1 -1
- package/esm/production/events/event-bus.js.map +1 -1
- package/esm/production/events/get-event-handler-metadata.js.map +1 -1
- package/esm/production/events/on-event.js.map +1 -1
- package/esm/production/index.js +1 -1
- package/esm/production/queries/get-query-handler-metadata.js.map +1 -1
- package/esm/production/queries/on-query.js.map +1 -1
- package/esm/production/queries/query-bus.js.map +1 -1
- package/esm/production/queries/query-optional.js.map +1 -1
- package/esm/production/queries/query.js.map +1 -1
- package/esm/production/registry.js.map +1 -1
- package/esm/production/seeds/apply-seeds.js.map +1 -1
- package/esm/production/seeds/apply-shared-seed.js.map +1 -1
- package/esm/production/seeds/unapply-seeds.js.map +1 -1
- package/esm/production/test-utils/mock-container.js.map +1 -1
- package/esm/production/types/commands.js +1 -1
- package/esm/production/types/commands.js.map +1 -1
- package/index.d.ts +56 -88
- package/lib.d.ts +6 -6
- package/package.json +1 -1
- package/test-utils.d.ts +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
## 0.6.3
|
|
2
|
+
|
|
3
|
+
- Update readme files for each module
|
|
4
|
+
|
|
5
|
+
## 0.6.2
|
|
6
|
+
|
|
7
|
+
- Corrected build system, react package structure
|
|
8
|
+
|
|
9
|
+
## 0.6.1
|
|
10
|
+
|
|
11
|
+
- React related types moved to react lib
|
|
12
|
+
- Avoid prefixed I/T types
|
|
13
|
+
|
|
1
14
|
## 0.6.0
|
|
2
15
|
|
|
3
16
|
- Split wirestate as separate @wirestate packages
|
package/README.md
CHANGED
|
@@ -1,317 +1,260 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wirestate/core
|
|
2
2
|
|
|
3
|
-
[](https://github.com/Neloreck/wirestate/search?l=typescript)
|
|
3
|
+
[](https://www.npmjs.com/package/@wirestate/core)
|
|
5
4
|
[](https://github.com/Neloreck/wirestate/blob/master/LICENSE)
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
Core package for wirestate.
|
|
7
|
+
Provides the DI container, service primitives, and event/command/query buses.
|
|
8
|
+
React integration is in [`@wirestate/react`](https://www.npmjs.com/package/@wirestate/react).
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
```bash
|
|
13
|
+
npm install @wirestate/core reflect-metadata
|
|
14
|
+
```
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
Import `reflect-metadata` once at your application entry point, before any wirestate imports:
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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.
|
|
18
|
+
```ts
|
|
19
|
+
import 'reflect-metadata';
|
|
20
|
+
```
|
|
23
21
|
|
|
24
|
-
##
|
|
22
|
+
## Services
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
- `reflect-metadata` (must be imported at application entry)
|
|
24
|
+
Services are plain classes decorated with `@Injectable`. Each service may inject a `WireScope` which provides access to the event, command, and query buses and to other services in the container.
|
|
28
25
|
|
|
29
|
-
|
|
26
|
+
```ts
|
|
27
|
+
import { Injectable, Inject, WireScope } from '@wirestate/core';
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
@Injectable()
|
|
30
|
+
export class CounterService {
|
|
31
|
+
public count = 0;
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
public constructor(
|
|
34
|
+
@Inject(WireScope) private scope: WireScope
|
|
35
|
+
) {}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
public increment(): void {
|
|
38
|
+
this.count++;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
39
41
|
```
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
## Container
|
|
42
44
|
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
npm install --save-dev @preact/signals-react-transform
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Quick Start with mobx
|
|
45
|
+
```ts
|
|
46
|
+
import { createIocContainer, bindService } from '@wirestate/core';
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
const container = createIocContainer();
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
bindService(container, CounterService);
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
import { makeObservable, Observable, Action } from '@wirestate/react-mobx';
|
|
52
|
+
const service = container.get(CounterService);
|
|
53
|
+
```
|
|
57
54
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
@Observable()
|
|
61
|
-
public count: number = 0;
|
|
55
|
+
`bindService` binds a class in singleton scope by default.
|
|
56
|
+
Use `bindConstant` to bind a value, `bindEntry` to bind under a custom token.
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
@Inject(WireScope)
|
|
65
|
-
private scope: WireScope
|
|
66
|
-
) {
|
|
67
|
-
makeObservable(this);
|
|
68
|
-
}
|
|
58
|
+
## Events
|
|
69
59
|
|
|
70
|
-
|
|
71
|
-
public increment(amount: number = 1): void {
|
|
72
|
-
this.count += amount;
|
|
73
|
-
}
|
|
60
|
+
Events are fire-and-forget messages. Any service can emit or subscribe.
|
|
74
61
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.count = 0;
|
|
78
|
-
}
|
|
62
|
+
```ts
|
|
63
|
+
import { OnEvent, WireScope, Inject } from '@wirestate/core';
|
|
79
64
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
65
|
+
@Injectable()
|
|
66
|
+
export class SenderService {
|
|
67
|
+
public constructor(@Inject(WireScope) private scope: WireScope) {}
|
|
84
68
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return this.count;
|
|
69
|
+
public notify(): void {
|
|
70
|
+
this.scope.emitEvent('USER_LOGGED_OUT');
|
|
88
71
|
}
|
|
72
|
+
}
|
|
89
73
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
74
|
+
@Injectable()
|
|
75
|
+
export class ReceiverService {
|
|
76
|
+
@OnEvent('USER_LOGGED_OUT')
|
|
77
|
+
public onLogout(): void {
|
|
78
|
+
// handle logout
|
|
93
79
|
}
|
|
94
80
|
}
|
|
95
81
|
```
|
|
96
82
|
|
|
97
|
-
|
|
83
|
+
`@OnEvent()` with no argument subscribes to all events.
|
|
98
84
|
|
|
99
|
-
|
|
100
|
-
Lifetimes are managed automatically.
|
|
85
|
+
## Commands
|
|
101
86
|
|
|
102
|
-
|
|
103
|
-
import { IocProvider, createInjectablesProvider } from '@wirestate/react';
|
|
104
|
-
import { CounterService } from './CounterService';
|
|
87
|
+
Commands are write operations dispatched by token. A single handler is expected per command type.
|
|
105
88
|
|
|
106
|
-
|
|
89
|
+
```ts
|
|
90
|
+
import { OnCommand, WireScope, Inject } from '@wirestate/core';
|
|
107
91
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
</IocProvider>
|
|
115
|
-
);
|
|
92
|
+
@Injectable()
|
|
93
|
+
export class AuthService {
|
|
94
|
+
@OnCommand('LOGIN')
|
|
95
|
+
public async onLogin(payload: { username: string }): Promise<void> {
|
|
96
|
+
// perform login
|
|
97
|
+
}
|
|
116
98
|
}
|
|
117
|
-
```
|
|
118
99
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
});
|
|
100
|
+
@Injectable()
|
|
101
|
+
export class AnotherService {
|
|
102
|
+
public constructor(@Inject(WireScope) private scope: WireScope) {}
|
|
103
|
+
|
|
104
|
+
public async login(): Promise<void> {
|
|
105
|
+
await this.scope.executeCommand('LOGIN').task;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
144
108
|
```
|
|
145
109
|
|
|
146
|
-
|
|
110
|
+
Use `commandOptional` when a handler may not be registered — returns `null` instead of throwing.
|
|
147
111
|
|
|
148
|
-
|
|
112
|
+
## Queries
|
|
149
113
|
|
|
150
|
-
|
|
114
|
+
Queries are request-response operations. A single handler is expected per query type.
|
|
151
115
|
|
|
152
|
-
```
|
|
153
|
-
import {
|
|
154
|
-
import { signal, computed, Signal, ReadonlySignal } from '@wirestate/react-signals';
|
|
116
|
+
```ts
|
|
117
|
+
import { OnQuery, WireScope, Inject } from '@wirestate/core';
|
|
155
118
|
|
|
156
119
|
@Injectable()
|
|
157
|
-
export class
|
|
158
|
-
|
|
159
|
-
public readonly isEven: ReadonlySignal<boolean> = computed(() => this.count.value % 2 === 0);
|
|
120
|
+
export class StoreService {
|
|
121
|
+
private items: Array<string> = [];
|
|
160
122
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
) {}
|
|
165
|
-
|
|
166
|
-
public increment(amount: number = 1): void {
|
|
167
|
-
this.count.value += amount;
|
|
123
|
+
@OnQuery('STORE_ITEMS')
|
|
124
|
+
public onGetItems(): Array<string> {
|
|
125
|
+
return this.items;
|
|
168
126
|
}
|
|
127
|
+
}
|
|
169
128
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
129
|
+
@Injectable()
|
|
130
|
+
export class AnotherService {
|
|
131
|
+
public constructor(@Inject(WireScope) private scope: WireScope) {}
|
|
173
132
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
this.
|
|
133
|
+
public async someActionRequiringItems(): Promise<void> {
|
|
134
|
+
const asyncItems: Array<string> = await this.scope.queryData('STORE_ITEMS');
|
|
135
|
+
const syncItems: Array<string> = await this.scope.queryData('STORE_ITEMS');
|
|
177
136
|
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
178
139
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
140
|
+
## Seeds
|
|
141
|
+
|
|
142
|
+
Seeds pass initial data to services when they are activated.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { SEED, Injectable, Inject } from '@wirestate/core';
|
|
183
146
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
147
|
+
// Shared seed — same object injected into all services in the tree:
|
|
148
|
+
@Injectable()
|
|
149
|
+
export class MyService {
|
|
150
|
+
public constructor(@Inject(SEED) private seed: { theme: string }) {}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Per-service seed — each service gets its own seed value:
|
|
154
|
+
@Injectable()
|
|
155
|
+
export class OtherService {
|
|
156
|
+
public constructor(@Inject(WireScope) scope: WireScope) {
|
|
157
|
+
const { count } = scope.getSeed(OtherService) as { count: number };
|
|
187
158
|
}
|
|
188
159
|
}
|
|
189
160
|
```
|
|
190
161
|
|
|
191
|
-
|
|
162
|
+
Seeds are applied via `applySeeds` / `applySharedSeed` and removed via `unapplySeeds`.
|
|
163
|
+
In React, pass them as `seed` or `seeds` props to the provider — see `@wirestate/react`.
|
|
192
164
|
|
|
193
|
-
|
|
165
|
+
## Lifecycle
|
|
194
166
|
|
|
195
|
-
```
|
|
196
|
-
import {
|
|
197
|
-
import { CounterService } from './CounterService';
|
|
167
|
+
```ts
|
|
168
|
+
import { OnActivated, OnDeactivation } from '@wirestate/core';
|
|
198
169
|
|
|
199
|
-
|
|
170
|
+
@Injectable()
|
|
171
|
+
export class PollingService {
|
|
172
|
+
private timer?: ReturnType<typeof setInterval>;
|
|
200
173
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
<CounterView />
|
|
206
|
-
</MainProvider>
|
|
207
|
-
</IocProvider>
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
```
|
|
174
|
+
@OnActivated()
|
|
175
|
+
public onActivated(): void {
|
|
176
|
+
this.timer = setInterval(() => console.info('interval execution'), 5000);
|
|
177
|
+
}
|
|
211
178
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
);
|
|
179
|
+
@OnDeactivation()
|
|
180
|
+
public onDeactivation(): void {
|
|
181
|
+
clearInterval(this.timer);
|
|
182
|
+
}
|
|
235
183
|
}
|
|
236
184
|
```
|
|
237
185
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
### Seeding Shared Initial State
|
|
186
|
+
`@OnActivated` runs after the service is bound and all dependencies are resolved.
|
|
187
|
+
`@OnDeactivation` runs when the container scope is disposed.
|
|
241
188
|
|
|
242
|
-
|
|
189
|
+
## WireScope API
|
|
243
190
|
|
|
244
|
-
|
|
245
|
-
const MainProvider = createInjectablesProvider([CounterService]);
|
|
191
|
+
`WireScope` is injected per-service and exposes:
|
|
246
192
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
193
|
+
| Method | Description |
|
|
194
|
+
|---|---|
|
|
195
|
+
| `getContainer()` | Access the raw IoC container |
|
|
196
|
+
| `resolve(token)` | Resolve a service or value by token |
|
|
197
|
+
| `getSeed(token)` | Get the per-service seed for a given token |
|
|
198
|
+
| `emitEvent(type, payload?)` | Emit an event |
|
|
199
|
+
| `command(type)` | Get a caller for a command |
|
|
200
|
+
| `query(type)` | Get a caller for a query |
|
|
253
201
|
|
|
254
|
-
|
|
202
|
+
## Test utilities
|
|
255
203
|
|
|
256
|
-
|
|
257
|
-
import { Injectable, Inject, SEED } from 'wirestate';
|
|
204
|
+
Available via `@wirestate/core/test-utils`:
|
|
258
205
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
}
|
|
206
|
+
```ts
|
|
207
|
+
import {
|
|
208
|
+
mockContainer,
|
|
209
|
+
mockService,
|
|
210
|
+
mockBindService,
|
|
211
|
+
mockBindEntry,
|
|
212
|
+
mockUnbindService,
|
|
213
|
+
} from '@wirestate/core/test-utils';
|
|
269
214
|
```
|
|
270
215
|
|
|
271
|
-
###
|
|
216
|
+
### `mockContainer(options?)`
|
|
272
217
|
|
|
273
|
-
|
|
218
|
+
Creates a configured IoC container for testing. Accepts an optional object:
|
|
274
219
|
|
|
275
|
-
|
|
276
|
-
|
|
220
|
+
| Option | Type | Description |
|
|
221
|
+
|---|---|---|
|
|
222
|
+
| `entries` | `Array<Newable \| InjectableDescriptor>` | Services or descriptors to bind |
|
|
223
|
+
| `activate` | `Array<ServiceIdentifier>` | Tokens to resolve immediately after binding |
|
|
224
|
+
| `skipLifecycle` | `boolean` | Skip `@OnActivated` / `@OnDeactivation` hooks |
|
|
277
225
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
226
|
+
```ts
|
|
227
|
+
const container = mockContainer({
|
|
228
|
+
entries: [CounterService, LoggerService],
|
|
229
|
+
activate: [CounterService],
|
|
230
|
+
});
|
|
283
231
|
```
|
|
284
232
|
|
|
285
|
-
|
|
233
|
+
### `mockService(ServiceClass, container?, options?)`
|
|
286
234
|
|
|
287
|
-
|
|
288
|
-
import { Injectable, Inject, WireScope } from 'wirestate';
|
|
235
|
+
Binds a service class to a container and returns its instance. Creates a new `mockContainer` if none is provided.
|
|
289
236
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
this.count = scope.getSeed(CounterService).count;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
237
|
+
```ts
|
|
238
|
+
const counter = mockService(CounterService);
|
|
239
|
+
counter.increment();
|
|
240
|
+
expect(counter.count).toBe(1);
|
|
297
241
|
```
|
|
298
242
|
|
|
299
|
-
###
|
|
243
|
+
### `mockBindService(container, ServiceClass, options?)`
|
|
300
244
|
|
|
301
|
-
|
|
245
|
+
Binds a service class to an existing container. Accepts `{ skipLifecycle?: boolean }`.
|
|
302
246
|
|
|
303
|
-
|
|
304
|
-
import { OnActivated, OnDeactivation } from 'wirestate';
|
|
247
|
+
### `mockBindEntry(container, entry, options?)`
|
|
305
248
|
|
|
306
|
-
|
|
307
|
-
public onActivated(): void {
|
|
308
|
-
// Start polling, fetch initial data, etc.
|
|
309
|
-
}
|
|
249
|
+
Binds a service class or `InjectableDescriptor` to an existing container. Accepts `{ skipLifecycle?: boolean }`.
|
|
310
250
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
251
|
+
### `mockUnbindService(container, ServiceClass)`
|
|
252
|
+
|
|
253
|
+
Removes a service binding from the container. Useful for overriding registrations between tests.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
mockUnbindService(container, CounterService);
|
|
257
|
+
mockBindEntry(container, { token: CounterService, useValue: fakeCounter });
|
|
315
258
|
```
|
|
316
259
|
|
|
317
260
|
## License
|
package/cjs/development/index.js
CHANGED
|
@@ -271,7 +271,7 @@ Object.defineProperty(exports, "Tagged", {
|
|
|
271
271
|
exports.CommandBus = createIocContainer.CommandBus;
|
|
272
272
|
Object.defineProperty(exports, "CommandStatus", {
|
|
273
273
|
enumerable: true,
|
|
274
|
-
get: function () { return createIocContainer.
|
|
274
|
+
get: function () { return createIocContainer.CommandStatus; }
|
|
275
275
|
});
|
|
276
276
|
exports.EventBus = createIocContainer.EventBus;
|
|
277
277
|
exports.QueryBus = createIocContainer.QueryBus;
|
|
@@ -1 +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,+BAAqB,CAACD,OAAO,CAAC;AAC3C;;
|
|
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,+BAAqB,CAACD,OAAO,CAAC;AAC3C;;ACfA;;;;;;;AAOG;SACaE,OAAOA,CACrBC,SAAoB,EACpBC,IAAO,EACPC,IAAQ,EAAA;AAIR,EAAA,OAAOF,SAAS,CAACG,GAAG,CAACC,6BAAU,CAAC,CAACL,OAAO,CAAOE,IAAI,EAAEC,IAAI,CAAC;AAC5D;;ACfA;;;;;;;AAOG;SACaG,eAAeA,CAC7BL,SAAoB,EACpBC,IAAO,EACPC,IAAQ,EAAA;AAIR,EAAA,OAAOF,SAAS,CAACG,GAAG,CAACC,6BAAU,CAAC,CAACC,eAAe,CAAOJ,IAAI,EAAEC,IAAI,CAAC;AACpE;;ACnBA;;;;;A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AE;AACTwC,MAAAA,6CAA0B,CAACtC,GAAG,CAACH,WAAW,EAAEC,IAAI,CAAC;AACnD,IAAA;AAEAA,IAAAA,IAAI,CAACG,IAAI,CAACL,WAAW,CAAC;EACxB,CAAC;AACH;;ACzBA;;;;AAIG;SACa2C,cAAcA,GAAA;AAC5B,EAAA,OAAO,UAAC5C,MAAM,EAAEC,WAAW,EAAA;AAQzB,IAAA,IAAMC,WAAW,GAAIF,MAAiB,CAACE,WAAW;AAElD,IAAA,IAAIC,IAAI,GAAkC0C,gDAA6B,CAACjD,GAAG,CAACM,WAAW,CAAC;IAExF,IAAI,CAACC,IAAI,EAAE;AACTA,MAAAA,IAAI,GAAG,EAAE;AACT0C,MAAAA,gDAA6B,CAACxC,GAAG,CAACH,WAAW,EAAEC,IAAI,CAAC;AACtD,IAAA;AAEAA,IAAAA,IAAI,CAACG,IAAI,CAACL,WAAW,CAAC;EACxB,CAAC;AACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/cjs/development/lib.js
CHANGED
|
@@ -75,12 +75,12 @@ function bindDynamicValue(container, entry) {
|
|
|
75
75
|
/**
|
|
76
76
|
* Command execution status.
|
|
77
77
|
*/
|
|
78
|
-
exports.
|
|
79
|
-
(function (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
})(exports.
|
|
78
|
+
exports.CommandStatus = void 0;
|
|
79
|
+
(function (CommandStatus) {
|
|
80
|
+
CommandStatus["PENDING"] = "pending";
|
|
81
|
+
CommandStatus["SETTLED"] = "settled";
|
|
82
|
+
CommandStatus["ERROR"] = "error";
|
|
83
|
+
})(exports.CommandStatus || (exports.CommandStatus = {}));
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* Dispatches commands to handlers.
|
|
@@ -145,15 +145,15 @@ var CommandBus = /** @class */function () {
|
|
|
145
145
|
var handler = stack[stack.length - 1];
|
|
146
146
|
var descriptor = {
|
|
147
147
|
task: null,
|
|
148
|
-
status: exports.
|
|
148
|
+
status: exports.CommandStatus.PENDING
|
|
149
149
|
};
|
|
150
150
|
descriptor.task = Promise.resolve().then(function () {
|
|
151
151
|
return handler(data);
|
|
152
152
|
}).then(function (result) {
|
|
153
|
-
descriptor.status = exports.
|
|
153
|
+
descriptor.status = exports.CommandStatus.SETTLED;
|
|
154
154
|
return result;
|
|
155
155
|
}).catch(function (error) {
|
|
156
|
-
descriptor.status = exports.
|
|
156
|
+
descriptor.status = exports.CommandStatus.ERROR;
|
|
157
157
|
throw error;
|
|
158
158
|
});
|
|
159
159
|
return descriptor;
|