@blac/core 2.0.5 → 2.0.6
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/README.md +98 -126
- package/dist/StateContainerRegistry-Brmferpf.js +588 -0
- package/dist/StateContainerRegistry-Brmferpf.js.map +1 -0
- package/dist/StateContainerRegistry-CVPTMaHn.cjs +629 -0
- package/dist/StateContainerRegistry-CVPTMaHn.cjs.map +1 -0
- package/dist/branded-DndTHjv2.cjs +18 -0
- package/dist/branded-DndTHjv2.cjs.map +1 -0
- package/dist/branded-mwGzzPF1.js +13 -0
- package/dist/branded-mwGzzPF1.js.map +1 -0
- package/dist/config-BInS_9WT.cjs +101 -0
- package/dist/config-BInS_9WT.cjs.map +1 -0
- package/dist/config-BmFnPVM0.js +42 -0
- package/dist/config-BmFnPVM0.js.map +1 -0
- package/dist/constants.d.cts +81 -0
- package/dist/core/StateContainer.d.ts +51 -1
- package/dist/debug.cjs +12 -0
- package/dist/debug.d.cts +35 -0
- package/dist/debug.js +3 -0
- package/dist/index.cjs +169 -1515
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +136 -1466
- package/dist/index.js.map +1 -1
- package/dist/plugin/BlacPlugin.d.ts +34 -1
- package/dist/plugins.cjs +4 -0
- package/dist/plugins.d.cts +25 -0
- package/dist/plugins.js +2 -0
- package/dist/resolve-dependencies-B22YjFGk.cjs +773 -0
- package/dist/resolve-dependencies-B22YjFGk.cjs.map +1 -0
- package/dist/resolve-dependencies-Cvq0glJF.js +648 -0
- package/dist/resolve-dependencies-Cvq0glJF.js.map +1 -0
- package/dist/tracked-B_MUXIpe.cjs +216 -0
- package/dist/tracked-B_MUXIpe.cjs.map +1 -0
- package/dist/tracked-I_q9buYg.js +181 -0
- package/dist/tracked-I_q9buYg.js.map +1 -0
- package/dist/tracking.cjs +18 -0
- package/dist/tracking.d.cts +9 -0
- package/dist/tracking.js +2 -0
- package/dist/types.cjs +3 -0
- package/dist/types.d.cts +8 -0
- package/dist/types.js +2 -0
- package/dist/watch-entry.cjs +7 -0
- package/dist/watch-entry.d.cts +26 -0
- package/dist/watch-entry.js +2 -0
- package/package.json +57 -3
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> ⚠️ **Warning:** This project is currently under active development. The API may change in future releases. Use with caution in production environments.
|
|
4
4
|
|
|
5
|
-
Core state management
|
|
5
|
+
Core state management primitives for BlaC: state containers, registry, plugins, and tracking utilities.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -16,198 +16,170 @@ yarn add @blac/core
|
|
|
16
16
|
|
|
17
17
|
## Core Concepts
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### StateContainer
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
`StateContainer` is the abstract base class. It provides state storage, subscriptions, lifecycle events, and registry integration.
|
|
22
22
|
|
|
23
|
-
```
|
|
24
|
-
import {
|
|
23
|
+
```ts
|
|
24
|
+
import { StateContainer } from '@blac/core';
|
|
25
25
|
|
|
26
|
-
class
|
|
26
|
+
class CounterContainer extends StateContainer<{ count: number }> {
|
|
27
27
|
constructor() {
|
|
28
28
|
super({ count: 0 });
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
increment() {
|
|
32
|
-
this.
|
|
32
|
+
this.update((state) => ({ count: state.count + 1 }));
|
|
33
33
|
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
Key public API:
|
|
38
|
+
|
|
39
|
+
- `state` (readonly)
|
|
40
|
+
- `subscribe(listener)` -> `unsubscribe`
|
|
41
|
+
- `dispose()`
|
|
42
|
+
- `isDisposed`, `name`, `debug`, `instanceId`, `createdAt`, `lastUpdateTimestamp`
|
|
43
|
+
|
|
44
|
+
Protected API for subclasses:
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
- `emit(newState)`
|
|
47
|
+
- `update(fn)`
|
|
48
|
+
- `onSystemEvent(event, handler)` for `stateChanged` and `dispose`
|
|
49
|
+
- `depend(BlocClass, instanceKey?)` for cross-bloc dependencies
|
|
50
|
+
|
|
51
|
+
### Cubit
|
|
52
|
+
|
|
53
|
+
`Cubit` extends `StateContainer` with public mutation methods.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { Cubit } from '@blac/core';
|
|
57
|
+
|
|
58
|
+
class CounterCubit extends Cubit<{ count: number }> {
|
|
59
|
+
constructor() {
|
|
60
|
+
super({ count: 0 });
|
|
41
61
|
}
|
|
62
|
+
|
|
63
|
+
increment = () => this.emit({ count: this.state.count + 1 });
|
|
64
|
+
decrement = () => this.update((state) => ({ count: state.count - 1 }));
|
|
65
|
+
reset = () => this.patch({ count: 0 });
|
|
42
66
|
}
|
|
43
67
|
```
|
|
44
68
|
|
|
69
|
+
`patch()` performs a shallow merge for object state and is only available when `S extends object`.
|
|
70
|
+
|
|
45
71
|
## Registry API
|
|
46
72
|
|
|
47
|
-
|
|
73
|
+
The registry manages instance lifecycles and ref counting.
|
|
48
74
|
|
|
49
|
-
```
|
|
50
|
-
import {
|
|
75
|
+
```ts
|
|
76
|
+
import {
|
|
77
|
+
acquire,
|
|
78
|
+
ensure,
|
|
79
|
+
borrow,
|
|
80
|
+
borrowSafe,
|
|
81
|
+
release,
|
|
82
|
+
hasInstance,
|
|
83
|
+
getRefCount,
|
|
84
|
+
getAll,
|
|
85
|
+
forEach,
|
|
86
|
+
clear,
|
|
87
|
+
clearAll,
|
|
88
|
+
} from '@blac/core';
|
|
51
89
|
|
|
52
|
-
|
|
53
|
-
const counter = acquire(CounterCubit);
|
|
90
|
+
const counter = acquire(CounterCubit); // increments ref count
|
|
54
91
|
|
|
55
|
-
|
|
56
|
-
|
|
92
|
+
const shared = ensure(CounterCubit); // no ref count increment
|
|
93
|
+
const existing = borrow(CounterCubit); // no create, no ref count
|
|
94
|
+
const maybe = borrowSafe(CounterCubit); // { error, instance }
|
|
57
95
|
|
|
58
|
-
//
|
|
59
|
-
const instance = borrow(CounterCubit);
|
|
96
|
+
release(CounterCubit); // decrements ref count, auto-dispose at 0 unless keepAlive
|
|
60
97
|
|
|
61
|
-
// Check if instance exists
|
|
62
98
|
if (hasInstance(CounterCubit)) {
|
|
63
|
-
|
|
99
|
+
console.log(getRefCount(CounterCubit));
|
|
64
100
|
}
|
|
65
101
|
|
|
66
|
-
|
|
102
|
+
forEach(CounterCubit, (inst) => console.log(inst.state));
|
|
67
103
|
clear(CounterCubit);
|
|
68
|
-
|
|
69
|
-
// Clear all instances
|
|
70
104
|
clearAll();
|
|
71
105
|
```
|
|
72
106
|
|
|
73
107
|
## Decorators
|
|
74
108
|
|
|
75
|
-
|
|
109
|
+
Configure container behavior with `@blac`.
|
|
76
110
|
|
|
77
|
-
```
|
|
111
|
+
```ts
|
|
78
112
|
import { Cubit, blac } from '@blac/core';
|
|
79
113
|
|
|
80
|
-
@blac({ isolated: true })
|
|
114
|
+
@blac({ isolated: true })
|
|
81
115
|
class FormCubit extends Cubit<FormState> {}
|
|
82
116
|
|
|
83
|
-
@blac({ keepAlive: true })
|
|
117
|
+
@blac({ keepAlive: true })
|
|
84
118
|
class AuthCubit extends Cubit<AuthState> {}
|
|
85
119
|
|
|
86
|
-
@blac({ excludeFromDevTools: true })
|
|
120
|
+
@blac({ excludeFromDevTools: true })
|
|
87
121
|
class InternalCubit extends Cubit<State> {}
|
|
88
122
|
```
|
|
89
123
|
|
|
90
|
-
|
|
124
|
+
`BlacOptions` is a union type, so only one option can be specified at a time.
|
|
91
125
|
|
|
92
|
-
|
|
126
|
+
## Watch and Tracking
|
|
93
127
|
|
|
94
|
-
|
|
128
|
+
### watch
|
|
95
129
|
|
|
96
|
-
```
|
|
97
|
-
import {
|
|
130
|
+
```ts
|
|
131
|
+
import { watch, instance } from '@blac/core';
|
|
98
132
|
|
|
99
|
-
const
|
|
133
|
+
const stop = watch(CounterCubit, (counter) => {
|
|
134
|
+
console.log(counter.state.count);
|
|
100
135
|
|
|
101
|
-
|
|
102
|
-
|
|
136
|
+
if (counter.state.count >= 10) {
|
|
137
|
+
return watch.STOP; // stop from inside the callback
|
|
138
|
+
}
|
|
139
|
+
});
|
|
103
140
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
timeout: 5000,
|
|
141
|
+
const stopSpecific = watch(instance(CounterCubit, 'counter-1'), (counter) => {
|
|
142
|
+
console.log(counter.state.count);
|
|
107
143
|
});
|
|
108
144
|
```
|
|
109
145
|
|
|
110
|
-
###
|
|
111
|
-
|
|
112
|
-
Create computed values that react to state changes:
|
|
146
|
+
### tracked
|
|
113
147
|
|
|
114
|
-
```
|
|
115
|
-
import {
|
|
148
|
+
```ts
|
|
149
|
+
import { tracked } from '@blac/core';
|
|
116
150
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// Watch another bloc's state
|
|
122
|
-
watch(
|
|
123
|
-
instance(UserCubit),
|
|
124
|
-
(userState) => userState.preferences,
|
|
125
|
-
(preferences) => this.onPreferencesChanged(preferences),
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
151
|
+
const { result, dependencies } = tracked(() => {
|
|
152
|
+
const user = ensure(UserCubit);
|
|
153
|
+
return user.state.name;
|
|
154
|
+
});
|
|
129
155
|
```
|
|
130
156
|
|
|
131
157
|
## Plugins
|
|
132
158
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
159
|
+
```ts
|
|
136
160
|
import { getPluginManager, type BlacPlugin } from '@blac/core';
|
|
137
161
|
|
|
138
162
|
const loggingPlugin: BlacPlugin = {
|
|
139
163
|
name: 'logging',
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
version: '1.0.0',
|
|
165
|
+
onStateChanged(instance, previousState, currentState, callstack) {
|
|
166
|
+
console.log(instance.constructor.name, previousState, currentState);
|
|
167
|
+
if (callstack) console.log(callstack);
|
|
142
168
|
},
|
|
143
169
|
};
|
|
144
170
|
|
|
145
|
-
getPluginManager().
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
## Configuration
|
|
149
|
-
|
|
150
|
-
Configure global behavior:
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
import { configureBlac } from '@blac/core';
|
|
154
|
-
|
|
155
|
-
configureBlac({
|
|
156
|
-
devMode: import.meta.env.DEV,
|
|
171
|
+
getPluginManager().install(loggingPlugin, {
|
|
172
|
+
enabled: true,
|
|
173
|
+
environment: 'development',
|
|
157
174
|
});
|
|
158
175
|
```
|
|
159
176
|
|
|
160
|
-
##
|
|
161
|
-
|
|
162
|
-
### State Containers
|
|
163
|
-
|
|
164
|
-
| Class | Description |
|
|
165
|
-
| ------------- | ----------------------------------------------------------- |
|
|
166
|
-
| `Cubit<S, P>` | Simple state container with `emit()`, `update()`, `patch()` |
|
|
167
|
-
|
|
168
|
-
### Registry Functions
|
|
177
|
+
## Subpath Exports
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
| `borrow(Class, key?)` | Get instance without affecting ref count |
|
|
175
|
-
| `borrowSafe(Class, key?)` | Borrow or return undefined |
|
|
176
|
-
| `ensure(Class, key?, options?)` | Acquire without incrementing ref count |
|
|
177
|
-
| `hasInstance(Class, key?)` | Check if instance exists |
|
|
178
|
-
| `getRefCount(Class, key?)` | Get current reference count |
|
|
179
|
-
| `clear(Class)` | Remove all instances of a class |
|
|
180
|
-
| `clearAll()` | Remove all instances |
|
|
181
|
-
|
|
182
|
-
### Exports
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
// Core classes
|
|
186
|
-
export { Cubit } from '@blac/core';
|
|
187
|
-
|
|
188
|
-
// Registry
|
|
189
|
-
export {
|
|
190
|
-
acquire,
|
|
191
|
-
release,
|
|
192
|
-
borrow,
|
|
193
|
-
ensure,
|
|
194
|
-
hasInstance,
|
|
195
|
-
clear,
|
|
196
|
-
clearAll,
|
|
197
|
-
} from '@blac/core';
|
|
198
|
-
|
|
199
|
-
// Utilities
|
|
200
|
-
export { waitUntil, watch, instance } from '@blac/core';
|
|
201
|
-
|
|
202
|
-
// Decorators
|
|
203
|
-
export { blac } from '@blac/core';
|
|
204
|
-
|
|
205
|
-
// Plugin system
|
|
206
|
-
export { getPluginManager, type BlacPlugin } from '@blac/core';
|
|
207
|
-
|
|
208
|
-
// Configuration
|
|
209
|
-
export { configureBlac, getBlacConfig, isDevMode } from '@blac/core';
|
|
210
|
-
```
|
|
179
|
+
- `@blac/core/watch` -> `watch`, `instance`, `tracked` utilities
|
|
180
|
+
- `@blac/core/tracking` -> dependency tracking utilities for framework adapters
|
|
181
|
+
- `@blac/core/plugins` -> plugin system exports
|
|
182
|
+
- `@blac/core/debug` -> advanced registry introspection helpers
|
|
211
183
|
|
|
212
184
|
## License
|
|
213
185
|
|