@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.
- package/CHANGELOG.md +51 -0
- package/LICENSE +21 -0
- package/README.md +319 -0
- package/cjs/development/index.js +395 -0
- package/cjs/development/index.js.map +1 -0
- package/cjs/development/lib.js +64 -0
- package/cjs/development/lib.js.map +1 -0
- package/cjs/development/test-utils.js +1075 -0
- package/cjs/development/test-utils.js.map +1 -0
- package/cjs/production/index.js +1 -0
- package/cjs/production/index.js.map +1 -0
- package/cjs/production/lib.js +1 -0
- package/cjs/production/lib.js.map +1 -0
- package/cjs/production/test-utils.js +1 -0
- package/cjs/production/test-utils.js.map +1 -0
- package/esm/development/wirestate-core/bind/bind-constant.js +19 -0
- package/esm/development/wirestate-core/bind/bind-constant.js.map +1 -0
- package/esm/development/wirestate-core/bind/bind-dynamic-value.js +28 -0
- package/esm/development/wirestate-core/bind/bind-dynamic-value.js.map +1 -0
- package/esm/development/wirestate-core/bind/bind-entry.js +39 -0
- package/esm/development/wirestate-core/bind/bind-entry.js.map +1 -0
- package/esm/development/wirestate-core/bind/bind-service.js +253 -0
- package/esm/development/wirestate-core/bind/bind-service.js.map +1 -0
- package/esm/development/wirestate-core/bind/get-entry-token.js +14 -0
- package/esm/development/wirestate-core/bind/get-entry-token.js.map +1 -0
- package/esm/development/wirestate-core/commands/command-bus.js +112 -0
- package/esm/development/wirestate-core/commands/command-bus.js.map +1 -0
- package/esm/development/wirestate-core/commands/get-command-handler-metadata.js +27 -0
- package/esm/development/wirestate-core/commands/get-command-handler-metadata.js.map +1 -0
- package/esm/development/wirestate-core/container/create-ioc-container.js +35 -0
- package/esm/development/wirestate-core/container/create-ioc-container.js.map +1 -0
- package/esm/development/wirestate-core/container/wire-scope.js +151 -0
- package/esm/development/wirestate-core/container/wire-scope.js.map +1 -0
- package/esm/development/wirestate-core/error/error-code.js +10 -0
- package/esm/development/wirestate-core/error/error-code.js.map +1 -0
- package/esm/development/wirestate-core/error/wirestate-error.js +29 -0
- package/esm/development/wirestate-core/error/wirestate-error.js.map +1 -0
- package/esm/development/wirestate-core/events/build-event-dispatcher.js +39 -0
- package/esm/development/wirestate-core/events/build-event-dispatcher.js.map +1 -0
- package/esm/development/wirestate-core/events/event-bus.js +52 -0
- package/esm/development/wirestate-core/events/event-bus.js.map +1 -0
- package/esm/development/wirestate-core/events/get-event-handler-metadata.js +27 -0
- package/esm/development/wirestate-core/events/get-event-handler-metadata.js.map +1 -0
- package/esm/development/wirestate-core/queries/get-query-handler-metadata.js +27 -0
- package/esm/development/wirestate-core/queries/get-query-handler-metadata.js.map +1 -0
- package/esm/development/wirestate-core/queries/query-bus.js +99 -0
- package/esm/development/wirestate-core/queries/query-bus.js.map +1 -0
- package/esm/development/wirestate-core/registry.js +56 -0
- package/esm/development/wirestate-core/registry.js.map +1 -0
- package/esm/development/wirestate-core/service/get-activated-handler-metadata.js +27 -0
- package/esm/development/wirestate-core/service/get-activated-handler-metadata.js.map +1 -0
- package/esm/development/wirestate-core/service/get-deactivation-handler-metadata.js +27 -0
- package/esm/development/wirestate-core/service/get-deactivation-handler-metadata.js.map +1 -0
- package/esm/development/wirestate-core/test-utils/mock-bind-entry.js +24 -0
- package/esm/development/wirestate-core/test-utils/mock-bind-entry.js.map +1 -0
- package/esm/development/wirestate-core/test-utils/mock-container.js +52 -0
- package/esm/development/wirestate-core/test-utils/mock-container.js.map +1 -0
- package/esm/development/wirestate-core/types/commands.js +12 -0
- package/esm/development/wirestate-core/types/commands.js.map +1 -0
- package/esm/development/wirestate-react/commands/use-command-caller.js +18 -0
- package/esm/development/wirestate-react/commands/use-command-caller.js.map +1 -0
- package/esm/development/wirestate-react/commands/use-command-handler.js +28 -0
- package/esm/development/wirestate-react/commands/use-command-handler.js.map +1 -0
- package/esm/development/wirestate-react/commands/use-optional-command-caller.js +19 -0
- package/esm/development/wirestate-react/commands/use-optional-command-caller.js.map +1 -0
- package/esm/development/wirestate-react/error/error-code.js +6 -0
- package/esm/development/wirestate-react/error/error-code.js.map +1 -0
- package/esm/development/wirestate-react/events/use-event-emitter.js +22 -0
- package/esm/development/wirestate-react/events/use-event-emitter.js.map +1 -0
- package/esm/development/wirestate-react/events/use-event.js +30 -0
- package/esm/development/wirestate-react/events/use-event.js.map +1 -0
- package/esm/development/wirestate-react/events/use-events-handler.js +25 -0
- package/esm/development/wirestate-react/events/use-events-handler.js.map +1 -0
- package/esm/development/wirestate-react/events/use-events.js +31 -0
- package/esm/development/wirestate-react/events/use-events.js.map +1 -0
- package/esm/development/wirestate-react/index.js +19 -0
- package/esm/development/wirestate-react/index.js.map +1 -0
- package/esm/development/wirestate-react/provision/create-injectables-provider.js +101 -0
- package/esm/development/wirestate-react/provision/create-injectables-provider.js.map +1 -0
- package/esm/development/wirestate-react/provision/ioc-context.js +11 -0
- package/esm/development/wirestate-react/provision/ioc-context.js.map +1 -0
- package/esm/development/wirestate-react/provision/ioc-provider.js +50 -0
- package/esm/development/wirestate-react/provision/ioc-provider.js.map +1 -0
- package/esm/development/wirestate-react/provision/use-container-revision.js +13 -0
- package/esm/development/wirestate-react/provision/use-container-revision.js.map +1 -0
- package/esm/development/wirestate-react/provision/use-container.js +13 -0
- package/esm/development/wirestate-react/provision/use-container.js.map +1 -0
- package/esm/development/wirestate-react/provision/use-injection.js +22 -0
- package/esm/development/wirestate-react/provision/use-injection.js.map +1 -0
- package/esm/development/wirestate-react/provision/use-ioc-context.js +21 -0
- package/esm/development/wirestate-react/provision/use-ioc-context.js.map +1 -0
- package/esm/development/wirestate-react/provision/use-optional-injection.js +29 -0
- package/esm/development/wirestate-react/provision/use-optional-injection.js.map +1 -0
- package/esm/development/wirestate-react/queries/use-optional-query-caller.js +19 -0
- package/esm/development/wirestate-react/queries/use-optional-query-caller.js.map +1 -0
- package/esm/development/wirestate-react/queries/use-optional-sync-query-caller.js +19 -0
- package/esm/development/wirestate-react/queries/use-optional-sync-query-caller.js.map +1 -0
- package/esm/development/wirestate-react/queries/use-query-caller.js +18 -0
- package/esm/development/wirestate-react/queries/use-query-caller.js.map +1 -0
- package/esm/development/wirestate-react/queries/use-query-handler.js +27 -0
- package/esm/development/wirestate-react/queries/use-query-handler.js.map +1 -0
- package/esm/development/wirestate-react/queries/use-sync-query-caller.js +20 -0
- package/esm/development/wirestate-react/queries/use-sync-query-caller.js.map +1 -0
- package/esm/development/wirestate-react/test-utils/with-ioc-provider.js +30 -0
- package/esm/development/wirestate-react/test-utils/with-ioc-provider.js.map +1 -0
- package/esm/development/wirestate-react/test-utils.js +2 -0
- package/esm/development/wirestate-react/test-utils.js.map +1 -0
- package/esm/production/wirestate-core/bind/bind-constant.js +1 -0
- package/esm/production/wirestate-core/bind/bind-constant.js.map +1 -0
- package/esm/production/wirestate-core/bind/bind-dynamic-value.js +1 -0
- package/esm/production/wirestate-core/bind/bind-dynamic-value.js.map +1 -0
- package/esm/production/wirestate-core/bind/bind-entry.js +1 -0
- package/esm/production/wirestate-core/bind/bind-entry.js.map +1 -0
- package/esm/production/wirestate-core/bind/bind-service.js +1 -0
- package/esm/production/wirestate-core/bind/bind-service.js.map +1 -0
- package/esm/production/wirestate-core/bind/get-entry-token.js +1 -0
- package/esm/production/wirestate-core/bind/get-entry-token.js.map +1 -0
- package/esm/production/wirestate-core/commands/command-bus.js +1 -0
- package/esm/production/wirestate-core/commands/command-bus.js.map +1 -0
- package/esm/production/wirestate-core/commands/get-command-handler-metadata.js +1 -0
- package/esm/production/wirestate-core/commands/get-command-handler-metadata.js.map +1 -0
- package/esm/production/wirestate-core/container/create-ioc-container.js +1 -0
- package/esm/production/wirestate-core/container/create-ioc-container.js.map +1 -0
- package/esm/production/wirestate-core/container/wire-scope.js +1 -0
- package/esm/production/wirestate-core/container/wire-scope.js.map +1 -0
- package/esm/production/wirestate-core/error/error-code.js +1 -0
- package/esm/production/wirestate-core/error/error-code.js.map +1 -0
- package/esm/production/wirestate-core/error/wirestate-error.js +1 -0
- package/esm/production/wirestate-core/error/wirestate-error.js.map +1 -0
- package/esm/production/wirestate-core/events/build-event-dispatcher.js +1 -0
- package/esm/production/wirestate-core/events/build-event-dispatcher.js.map +1 -0
- package/esm/production/wirestate-core/events/event-bus.js +1 -0
- package/esm/production/wirestate-core/events/event-bus.js.map +1 -0
- package/esm/production/wirestate-core/events/get-event-handler-metadata.js +1 -0
- package/esm/production/wirestate-core/events/get-event-handler-metadata.js.map +1 -0
- package/esm/production/wirestate-core/queries/get-query-handler-metadata.js +1 -0
- package/esm/production/wirestate-core/queries/get-query-handler-metadata.js.map +1 -0
- package/esm/production/wirestate-core/queries/query-bus.js +1 -0
- package/esm/production/wirestate-core/queries/query-bus.js.map +1 -0
- package/esm/production/wirestate-core/registry.js +1 -0
- package/esm/production/wirestate-core/registry.js.map +1 -0
- package/esm/production/wirestate-core/service/get-activated-handler-metadata.js +1 -0
- package/esm/production/wirestate-core/service/get-activated-handler-metadata.js.map +1 -0
- package/esm/production/wirestate-core/service/get-deactivation-handler-metadata.js +1 -0
- package/esm/production/wirestate-core/service/get-deactivation-handler-metadata.js.map +1 -0
- package/esm/production/wirestate-core/test-utils/mock-bind-entry.js +1 -0
- package/esm/production/wirestate-core/test-utils/mock-bind-entry.js.map +1 -0
- package/esm/production/wirestate-core/test-utils/mock-container.js +1 -0
- package/esm/production/wirestate-core/test-utils/mock-container.js.map +1 -0
- package/esm/production/wirestate-core/types/commands.js +1 -0
- package/esm/production/wirestate-core/types/commands.js.map +1 -0
- package/esm/production/wirestate-react/commands/use-command-caller.js +1 -0
- package/esm/production/wirestate-react/commands/use-command-caller.js.map +1 -0
- package/esm/production/wirestate-react/commands/use-command-handler.js +1 -0
- package/esm/production/wirestate-react/commands/use-command-handler.js.map +1 -0
- package/esm/production/wirestate-react/commands/use-optional-command-caller.js +1 -0
- package/esm/production/wirestate-react/commands/use-optional-command-caller.js.map +1 -0
- package/esm/production/wirestate-react/error/error-code.js +1 -0
- package/esm/production/wirestate-react/error/error-code.js.map +1 -0
- package/esm/production/wirestate-react/events/use-event-emitter.js +1 -0
- package/esm/production/wirestate-react/events/use-event-emitter.js.map +1 -0
- package/esm/production/wirestate-react/events/use-event.js +1 -0
- package/esm/production/wirestate-react/events/use-event.js.map +1 -0
- package/esm/production/wirestate-react/events/use-events-handler.js +1 -0
- package/esm/production/wirestate-react/events/use-events-handler.js.map +1 -0
- package/esm/production/wirestate-react/events/use-events.js +1 -0
- package/esm/production/wirestate-react/events/use-events.js.map +1 -0
- package/esm/production/wirestate-react/index.js +1 -0
- package/esm/production/wirestate-react/index.js.map +1 -0
- package/esm/production/wirestate-react/provision/create-injectables-provider.js +1 -0
- package/esm/production/wirestate-react/provision/create-injectables-provider.js.map +1 -0
- package/esm/production/wirestate-react/provision/ioc-context.js +1 -0
- package/esm/production/wirestate-react/provision/ioc-context.js.map +1 -0
- package/esm/production/wirestate-react/provision/ioc-provider.js +1 -0
- package/esm/production/wirestate-react/provision/ioc-provider.js.map +1 -0
- package/esm/production/wirestate-react/provision/use-container-revision.js +1 -0
- package/esm/production/wirestate-react/provision/use-container-revision.js.map +1 -0
- package/esm/production/wirestate-react/provision/use-container.js +1 -0
- package/esm/production/wirestate-react/provision/use-container.js.map +1 -0
- package/esm/production/wirestate-react/provision/use-injection.js +1 -0
- package/esm/production/wirestate-react/provision/use-injection.js.map +1 -0
- package/esm/production/wirestate-react/provision/use-ioc-context.js +1 -0
- package/esm/production/wirestate-react/provision/use-ioc-context.js.map +1 -0
- package/esm/production/wirestate-react/provision/use-optional-injection.js +1 -0
- package/esm/production/wirestate-react/provision/use-optional-injection.js.map +1 -0
- package/esm/production/wirestate-react/queries/use-optional-query-caller.js +1 -0
- package/esm/production/wirestate-react/queries/use-optional-query-caller.js.map +1 -0
- package/esm/production/wirestate-react/queries/use-optional-sync-query-caller.js +1 -0
- package/esm/production/wirestate-react/queries/use-optional-sync-query-caller.js.map +1 -0
- package/esm/production/wirestate-react/queries/use-query-caller.js +1 -0
- package/esm/production/wirestate-react/queries/use-query-caller.js.map +1 -0
- package/esm/production/wirestate-react/queries/use-query-handler.js +1 -0
- package/esm/production/wirestate-react/queries/use-query-handler.js.map +1 -0
- package/esm/production/wirestate-react/queries/use-sync-query-caller.js +1 -0
- package/esm/production/wirestate-react/queries/use-sync-query-caller.js.map +1 -0
- package/esm/production/wirestate-react/test-utils/with-ioc-provider.js +1 -0
- package/esm/production/wirestate-react/test-utils/with-ioc-provider.js.map +1 -0
- package/esm/production/wirestate-react/test-utils.js +1 -0
- package/esm/production/wirestate-react/test-utils.js.map +1 -0
- package/index.d.ts +218 -0
- package/package.json +62 -0
- package/test-utils.d.ts +18 -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
|
+
[](https://www.npmjs.com/package/wirestate)
|
|
4
|
+
[](https://github.com/Neloreck/wirestate/search?l=typescript)
|
|
5
|
+
[](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
|