@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.
Files changed (46) hide show
  1. package/README.md +98 -126
  2. package/dist/StateContainerRegistry-Brmferpf.js +588 -0
  3. package/dist/StateContainerRegistry-Brmferpf.js.map +1 -0
  4. package/dist/StateContainerRegistry-CVPTMaHn.cjs +629 -0
  5. package/dist/StateContainerRegistry-CVPTMaHn.cjs.map +1 -0
  6. package/dist/branded-DndTHjv2.cjs +18 -0
  7. package/dist/branded-DndTHjv2.cjs.map +1 -0
  8. package/dist/branded-mwGzzPF1.js +13 -0
  9. package/dist/branded-mwGzzPF1.js.map +1 -0
  10. package/dist/config-BInS_9WT.cjs +101 -0
  11. package/dist/config-BInS_9WT.cjs.map +1 -0
  12. package/dist/config-BmFnPVM0.js +42 -0
  13. package/dist/config-BmFnPVM0.js.map +1 -0
  14. package/dist/constants.d.cts +81 -0
  15. package/dist/core/StateContainer.d.ts +51 -1
  16. package/dist/debug.cjs +12 -0
  17. package/dist/debug.d.cts +35 -0
  18. package/dist/debug.js +3 -0
  19. package/dist/index.cjs +169 -1515
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +1 -2
  22. package/dist/index.d.ts +1 -2
  23. package/dist/index.js +136 -1466
  24. package/dist/index.js.map +1 -1
  25. package/dist/plugin/BlacPlugin.d.ts +34 -1
  26. package/dist/plugins.cjs +4 -0
  27. package/dist/plugins.d.cts +25 -0
  28. package/dist/plugins.js +2 -0
  29. package/dist/resolve-dependencies-B22YjFGk.cjs +773 -0
  30. package/dist/resolve-dependencies-B22YjFGk.cjs.map +1 -0
  31. package/dist/resolve-dependencies-Cvq0glJF.js +648 -0
  32. package/dist/resolve-dependencies-Cvq0glJF.js.map +1 -0
  33. package/dist/tracked-B_MUXIpe.cjs +216 -0
  34. package/dist/tracked-B_MUXIpe.cjs.map +1 -0
  35. package/dist/tracked-I_q9buYg.js +181 -0
  36. package/dist/tracked-I_q9buYg.js.map +1 -0
  37. package/dist/tracking.cjs +18 -0
  38. package/dist/tracking.d.cts +9 -0
  39. package/dist/tracking.js +2 -0
  40. package/dist/types.cjs +3 -0
  41. package/dist/types.d.cts +8 -0
  42. package/dist/types.js +2 -0
  43. package/dist/watch-entry.cjs +7 -0
  44. package/dist/watch-entry.d.cts +26 -0
  45. package/dist/watch-entry.js +2 -0
  46. 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 library implementing the BloC pattern for TypeScript applications.
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
- ### Cubit
19
+ ### StateContainer
20
20
 
21
- Simple state container with direct state emission. Use when you need straightforward state updates.
21
+ `StateContainer` is the abstract base class. It provides state storage, subscriptions, lifecycle events, and registry integration.
22
22
 
23
- ```typescript
24
- import { Cubit } from '@blac/core';
23
+ ```ts
24
+ import { StateContainer } from '@blac/core';
25
25
 
26
- class CounterCubit extends Cubit<{ count: number }> {
26
+ class CounterContainer extends StateContainer<{ count: number }> {
27
27
  constructor() {
28
28
  super({ count: 0 });
29
29
  }
30
30
 
31
31
  increment() {
32
- this.emit({ count: this.state.count + 1 });
32
+ this.update((state) => ({ count: state.count + 1 }));
33
33
  }
34
+ }
35
+ ```
34
36
 
35
- decrement() {
36
- this.update((state) => ({ count: state.count - 1 }));
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
- reset() {
40
- this.patch({ count: 0 });
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
- Manage state container instances with the registry functions:
73
+ The registry manages instance lifecycles and ref counting.
48
74
 
49
- ```typescript
50
- import { acquire, release, borrow, hasInstance, clear } from '@blac/core';
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
- // Acquire an instance (creates if needed, increments ref count)
53
- const counter = acquire(CounterCubit);
90
+ const counter = acquire(CounterCubit); // increments ref count
54
91
 
55
- // Release when done (decrements ref count, disposes when 0)
56
- release(CounterCubit);
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
- // Borrow without affecting ref count
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
- // Clear a specific class
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
- Use the `@blac` decorator to configure container behavior:
109
+ Configure container behavior with `@blac`.
76
110
 
77
- ```typescript
111
+ ```ts
78
112
  import { Cubit, blac } from '@blac/core';
79
113
 
80
- @blac({ isolated: true }) // Each consumer gets its own instance
114
+ @blac({ isolated: true })
81
115
  class FormCubit extends Cubit<FormState> {}
82
116
 
83
- @blac({ keepAlive: true }) // Never auto-dispose
117
+ @blac({ keepAlive: true })
84
118
  class AuthCubit extends Cubit<AuthState> {}
85
119
 
86
- @blac({ excludeFromDevTools: true }) // Hide from DevTools
120
+ @blac({ excludeFromDevTools: true })
87
121
  class InternalCubit extends Cubit<State> {}
88
122
  ```
89
123
 
90
- ## Utilities
124
+ `BlacOptions` is a union type, so only one option can be specified at a time.
91
125
 
92
- ### waitUntil
126
+ ## Watch and Tracking
93
127
 
94
- Wait for a specific state condition:
128
+ ### watch
95
129
 
96
- ```typescript
97
- import { waitUntil } from '@blac/core';
130
+ ```ts
131
+ import { watch, instance } from '@blac/core';
98
132
 
99
- const counter = acquire(CounterCubit);
133
+ const stop = watch(CounterCubit, (counter) => {
134
+ console.log(counter.state.count);
100
135
 
101
- // Wait until count reaches 10
102
- await waitUntil(counter, (state) => state.count >= 10);
136
+ if (counter.state.count >= 10) {
137
+ return watch.STOP; // stop from inside the callback
138
+ }
139
+ });
103
140
 
104
- // With timeout
105
- await waitUntil(counter, (state) => state.count >= 10, {
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
- ### watch
111
-
112
- Create computed values that react to state changes:
146
+ ### tracked
113
147
 
114
- ```typescript
115
- import { watch, instance } from '@blac/core';
148
+ ```ts
149
+ import { tracked } from '@blac/core';
116
150
 
117
- class DashboardCubit extends Cubit<DashboardState> {
118
- constructor() {
119
- super({ items: [] });
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
- Extend functionality with plugins:
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
- onStateChange: (container, prevState, newState) => {
141
- console.log(`[${container.constructor.name}]`, prevState, '->', newState);
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().register(loggingPlugin);
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
- ## API Reference
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
- | Function | Description |
171
- | -------------------------------- | ------------------------------------------- |
172
- | `acquire(Class, key?, options?)` | Get or create instance, increment ref count |
173
- | `release(Class, key?)` | Decrement ref count, dispose when 0 |
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