@blac/core 2.0.5 → 2.0.7

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 (55) hide show
  1. package/README.md +98 -129
  2. package/dist/StateContainerRegistry-COxgpYAw.cjs +601 -0
  3. package/dist/StateContainerRegistry-COxgpYAw.cjs.map +1 -0
  4. package/dist/StateContainerRegistry-DS_wyKck.js +566 -0
  5. package/dist/StateContainerRegistry-DS_wyKck.js.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-BD9MkJ0k.js +42 -0
  11. package/dist/config-BD9MkJ0k.js.map +1 -0
  12. package/dist/config-DTuCbHcJ.cjs +101 -0
  13. package/dist/config-DTuCbHcJ.cjs.map +1 -0
  14. package/dist/constants.d.cts +73 -0
  15. package/dist/constants.d.ts +0 -8
  16. package/dist/core/Cubit.d.ts +0 -17
  17. package/dist/core/StateContainer.d.ts +37 -88
  18. package/dist/core/StateContainerRegistry.d.ts +2 -3
  19. package/dist/core/symbols.d.ts +2 -0
  20. package/dist/debug.cjs +12 -0
  21. package/dist/debug.d.cts +35 -0
  22. package/dist/debug.js +3 -0
  23. package/dist/decorators/blac.d.ts +3 -10
  24. package/dist/index.cjs +175 -1613
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +2 -4
  27. package/dist/index.d.ts +2 -4
  28. package/dist/index.js +142 -1564
  29. package/dist/index.js.map +1 -1
  30. package/dist/plugin/BlacPlugin.d.ts +34 -3
  31. package/dist/plugins.cjs +4 -0
  32. package/dist/plugins.d.cts +25 -0
  33. package/dist/plugins.js +2 -0
  34. package/dist/registry/management.d.ts +1 -1
  35. package/dist/resolve-dependencies-8xtWXd53.js +648 -0
  36. package/dist/resolve-dependencies-8xtWXd53.js.map +1 -0
  37. package/dist/resolve-dependencies-DJ2UBTq1.cjs +773 -0
  38. package/dist/resolve-dependencies-DJ2UBTq1.cjs.map +1 -0
  39. package/dist/tracked-12W-U7tq.js +181 -0
  40. package/dist/tracked-12W-U7tq.js.map +1 -0
  41. package/dist/tracked-DLnq137F.cjs +216 -0
  42. package/dist/tracked-DLnq137F.cjs.map +1 -0
  43. package/dist/tracking.cjs +18 -0
  44. package/dist/tracking.d.cts +9 -0
  45. package/dist/tracking.js +2 -0
  46. package/dist/types/utilities.d.ts +0 -1
  47. package/dist/types.cjs +3 -0
  48. package/dist/types.d.cts +8 -0
  49. package/dist/types.js +2 -0
  50. package/dist/utils/idGenerator.d.ts +0 -16
  51. package/dist/utils/static-props.d.ts +0 -7
  52. package/dist/watch-entry.cjs +7 -0
  53. package/dist/watch-entry.d.cts +26 -0
  54. package/dist/watch-entry.js +2 -0
  55. package/package.json +58 -3
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @blac/core
2
2
 
3
- > ⚠️ **Warning:** This project is currently under active development. The API may change in future releases. Use with caution in production environments.
3
+ Core state management primitives for BlaC: state containers, registry, plugins, and tracking utilities.
4
4
 
5
- Core state management library implementing the BloC pattern for TypeScript applications.
5
+ **[Full documentation](https://jsnanigans.github.io/blac/guide/introduction)**
6
6
 
7
7
  ## Installation
8
8
 
@@ -16,198 +16,167 @@ 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:
45
+
46
+ - `emit(newState)`
47
+ - `update(fn)`
48
+ - `onSystemEvent(event, handler)` for `stateChanged` and `dispose`
49
+ - `depend(BlocClass, instanceKey?)` for cross-bloc dependencies
38
50
 
39
- reset() {
40
- this.patch({ count: 0 });
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
81
- class FormCubit extends Cubit<FormState> {}
82
-
83
- @blac({ keepAlive: true }) // Never auto-dispose
114
+ @blac({ keepAlive: true })
84
115
  class AuthCubit extends Cubit<AuthState> {}
85
116
 
86
- @blac({ excludeFromDevTools: true }) // Hide from DevTools
117
+ @blac({ excludeFromDevTools: true })
87
118
  class InternalCubit extends Cubit<State> {}
88
119
  ```
89
120
 
90
- ## Utilities
121
+ `BlacOptions` is a union type, so only one option can be specified at a time.
91
122
 
92
- ### waitUntil
123
+ ## Watch and Tracking
93
124
 
94
- Wait for a specific state condition:
125
+ ### watch
95
126
 
96
- ```typescript
97
- import { waitUntil } from '@blac/core';
127
+ ```ts
128
+ import { watch, instance } from '@blac/core';
98
129
 
99
- const counter = acquire(CounterCubit);
130
+ const stop = watch(CounterCubit, (counter) => {
131
+ console.log(counter.state.count);
100
132
 
101
- // Wait until count reaches 10
102
- await waitUntil(counter, (state) => state.count >= 10);
133
+ if (counter.state.count >= 10) {
134
+ return watch.STOP; // stop from inside the callback
135
+ }
136
+ });
103
137
 
104
- // With timeout
105
- await waitUntil(counter, (state) => state.count >= 10, {
106
- timeout: 5000,
138
+ const stopSpecific = watch(instance(CounterCubit, 'counter-1'), (counter) => {
139
+ console.log(counter.state.count);
107
140
  });
108
141
  ```
109
142
 
110
- ### watch
111
-
112
- Create computed values that react to state changes:
143
+ ### tracked
113
144
 
114
- ```typescript
115
- import { watch, instance } from '@blac/core';
145
+ ```ts
146
+ import { tracked } from '@blac/core';
116
147
 
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
- }
148
+ const { result, dependencies } = tracked(() => {
149
+ const user = ensure(UserCubit);
150
+ return user.state.name;
151
+ });
129
152
  ```
130
153
 
131
154
  ## Plugins
132
155
 
133
- Extend functionality with plugins:
134
-
135
- ```typescript
156
+ ```ts
136
157
  import { getPluginManager, type BlacPlugin } from '@blac/core';
137
158
 
138
159
  const loggingPlugin: BlacPlugin = {
139
160
  name: 'logging',
140
- onStateChange: (container, prevState, newState) => {
141
- console.log(`[${container.constructor.name}]`, prevState, '->', newState);
161
+ version: '1.0.0',
162
+ onStateChanged(instance, previousState, currentState, callstack) {
163
+ console.log(instance.constructor.name, previousState, currentState);
164
+ if (callstack) console.log(callstack);
142
165
  },
143
166
  };
144
167
 
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,
168
+ getPluginManager().install(loggingPlugin, {
169
+ enabled: true,
170
+ environment: 'development',
157
171
  });
158
172
  ```
159
173
 
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
174
+ ## Subpath Exports
169
175
 
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
- ```
176
+ - `@blac/core/watch` -> `watch`, `instance`, `tracked` utilities
177
+ - `@blac/core/tracking` -> dependency tracking utilities for framework adapters
178
+ - `@blac/core/plugins` -> plugin system exports
179
+ - `@blac/core/debug` -> advanced registry introspection helpers
211
180
 
212
181
  ## License
213
182