@blac/core 2.0.0-rc-14 → 2.0.0-rc.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.
Files changed (162) hide show
  1. package/dist/.tsbuildinfo +1 -0
  2. package/dist/Blac.d.ts +246 -0
  3. package/dist/Blac.d.ts.map +1 -0
  4. package/dist/Blac.js +625 -0
  5. package/dist/Blac.js.map +1 -0
  6. package/dist/Bloc.d.ts +41 -0
  7. package/dist/Bloc.d.ts.map +1 -0
  8. package/dist/Bloc.js +140 -0
  9. package/dist/Bloc.js.map +1 -0
  10. package/dist/BlocBase.d.ts +110 -0
  11. package/dist/BlocBase.d.ts.map +1 -0
  12. package/dist/BlocBase.js +287 -0
  13. package/dist/BlocBase.js.map +1 -0
  14. package/dist/Cubit.d.ts +25 -0
  15. package/dist/Cubit.d.ts.map +1 -0
  16. package/dist/Cubit.js +64 -0
  17. package/dist/Cubit.js.map +1 -0
  18. package/dist/adapter/BlacAdapter.d.ts +71 -0
  19. package/dist/adapter/BlacAdapter.d.ts.map +1 -0
  20. package/dist/adapter/BlacAdapter.js +373 -0
  21. package/dist/adapter/BlacAdapter.js.map +1 -0
  22. package/dist/adapter/ProxyFactory.d.ts +50 -0
  23. package/dist/adapter/ProxyFactory.d.ts.map +1 -0
  24. package/dist/adapter/ProxyFactory.js +150 -0
  25. package/dist/adapter/ProxyFactory.js.map +1 -0
  26. package/{src/adapter/index.ts → dist/adapter/index.d.ts} +1 -0
  27. package/dist/adapter/index.d.ts.map +1 -0
  28. package/dist/adapter/index.js +19 -0
  29. package/dist/adapter/index.js.map +1 -0
  30. package/dist/errors/BlacError.d.ts +38 -0
  31. package/dist/errors/BlacError.d.ts.map +1 -0
  32. package/dist/errors/BlacError.js +55 -0
  33. package/dist/errors/BlacError.js.map +1 -0
  34. package/dist/errors/ErrorManager.d.ts +39 -0
  35. package/dist/errors/ErrorManager.d.ts.map +1 -0
  36. package/dist/errors/ErrorManager.js +91 -0
  37. package/dist/errors/ErrorManager.js.map +1 -0
  38. package/dist/errors/handleError.d.ts +24 -0
  39. package/dist/errors/handleError.d.ts.map +1 -0
  40. package/dist/errors/handleError.js +62 -0
  41. package/dist/errors/handleError.js.map +1 -0
  42. package/dist/errors/index.d.ts +5 -0
  43. package/dist/errors/index.d.ts.map +1 -0
  44. package/dist/errors/index.js +13 -0
  45. package/dist/errors/index.js.map +1 -0
  46. package/{src/events.ts → dist/events.d.ts} +4 -2
  47. package/dist/events.d.ts.map +1 -0
  48. package/dist/events.js +17 -0
  49. package/dist/events.js.map +1 -0
  50. package/dist/index.cjs +38 -0
  51. package/dist/index.d.ts +17 -1303
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +38 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/lifecycle/BlocLifecycle.d.ts +51 -0
  56. package/dist/lifecycle/BlocLifecycle.d.ts.map +1 -0
  57. package/dist/lifecycle/BlocLifecycle.js +123 -0
  58. package/dist/lifecycle/BlocLifecycle.js.map +1 -0
  59. package/dist/lifecycle/index.d.ts +2 -0
  60. package/dist/lifecycle/index.d.ts.map +1 -0
  61. package/dist/lifecycle/index.js +18 -0
  62. package/dist/lifecycle/index.js.map +1 -0
  63. package/dist/plugins/BlocPluginRegistry.d.ts +58 -0
  64. package/dist/plugins/BlocPluginRegistry.d.ts.map +1 -0
  65. package/dist/plugins/BlocPluginRegistry.js +201 -0
  66. package/dist/plugins/BlocPluginRegistry.js.map +1 -0
  67. package/dist/plugins/SystemPluginRegistry.d.ts +79 -0
  68. package/dist/plugins/SystemPluginRegistry.d.ts.map +1 -0
  69. package/dist/plugins/SystemPluginRegistry.js +184 -0
  70. package/dist/plugins/SystemPluginRegistry.js.map +1 -0
  71. package/{src/plugins/index.ts → dist/plugins/index.d.ts} +1 -5
  72. package/dist/plugins/index.d.ts.map +1 -0
  73. package/dist/plugins/index.js +26 -0
  74. package/dist/plugins/index.js.map +1 -0
  75. package/dist/plugins/types.d.ts +103 -0
  76. package/dist/plugins/types.d.ts.map +1 -0
  77. package/dist/plugins/types.js +3 -0
  78. package/dist/plugins/types.js.map +1 -0
  79. package/dist/subscription/SubscriptionManager.d.ts +67 -0
  80. package/dist/subscription/SubscriptionManager.d.ts.map +1 -0
  81. package/dist/subscription/SubscriptionManager.js +317 -0
  82. package/dist/subscription/SubscriptionManager.js.map +1 -0
  83. package/{src/subscription/index.ts → dist/subscription/index.d.ts} +1 -0
  84. package/dist/subscription/index.d.ts.map +1 -0
  85. package/dist/subscription/index.js +19 -0
  86. package/dist/subscription/index.js.map +1 -0
  87. package/dist/subscription/types.d.ts +65 -0
  88. package/dist/subscription/types.d.ts.map +1 -0
  89. package/dist/subscription/types.js +3 -0
  90. package/dist/subscription/types.js.map +1 -0
  91. package/dist/testing.d.ts +85 -0
  92. package/dist/testing.d.ts.map +1 -0
  93. package/dist/testing.js +186 -0
  94. package/dist/testing.js.map +1 -0
  95. package/{src/types.ts → dist/types.d.ts} +11 -28
  96. package/dist/types.d.ts.map +1 -0
  97. package/dist/types.js +3 -0
  98. package/dist/types.js.map +1 -0
  99. package/dist/utils/BatchingManager.d.ts +28 -0
  100. package/dist/utils/BatchingManager.d.ts.map +1 -0
  101. package/dist/utils/BatchingManager.js +48 -0
  102. package/dist/utils/BatchingManager.js.map +1 -0
  103. package/dist/utils/RerenderLogger.d.ts +60 -0
  104. package/dist/utils/RerenderLogger.d.ts.map +1 -0
  105. package/dist/utils/RerenderLogger.js +205 -0
  106. package/dist/utils/RerenderLogger.js.map +1 -0
  107. package/dist/utils/generateInstanceId.d.ts +11 -0
  108. package/dist/utils/generateInstanceId.d.ts.map +1 -0
  109. package/dist/utils/generateInstanceId.js +50 -0
  110. package/dist/utils/generateInstanceId.js.map +1 -0
  111. package/dist/utils/shallowEqual.d.ts +9 -0
  112. package/dist/utils/shallowEqual.d.ts.map +1 -0
  113. package/dist/utils/shallowEqual.js +35 -0
  114. package/dist/utils/shallowEqual.js.map +1 -0
  115. package/dist/utils/uuid.d.ts +10 -0
  116. package/dist/utils/uuid.d.ts.map +1 -0
  117. package/dist/utils/uuid.js +24 -0
  118. package/dist/utils/uuid.js.map +1 -0
  119. package/package.json +17 -10
  120. package/dist/index.cjs.js +0 -11
  121. package/dist/index.cjs.js.map +0 -1
  122. package/dist/index.es.js +0 -2275
  123. package/dist/index.es.js.map +0 -1
  124. package/src/Blac.ts +0 -802
  125. package/src/Bloc.ts +0 -180
  126. package/src/BlocBase.ts +0 -398
  127. package/src/Cubit.ts +0 -68
  128. package/src/__tests__/Blac.config.test.ts +0 -79
  129. package/src/__tests__/Bloc.event.test.ts +0 -394
  130. package/src/__tests__/BlocBase.subscription.test.ts +0 -349
  131. package/src/__tests__/Cubit.test.ts +0 -462
  132. package/src/__tests__/memory-leaks.test.ts +0 -383
  133. package/src/__tests__/plugins.test.ts +0 -464
  134. package/src/adapter/BlacAdapter.ts +0 -467
  135. package/src/adapter/ProxyFactory.ts +0 -249
  136. package/src/adapter/__tests__/BlacAdapter.test.ts +0 -555
  137. package/src/adapter/__tests__/ProxyFactory.test.ts +0 -655
  138. package/src/errors/BlacError.ts +0 -69
  139. package/src/errors/ErrorManager.ts +0 -101
  140. package/src/errors/handleError.ts +0 -114
  141. package/src/errors/index.ts +0 -13
  142. package/src/index.ts +0 -26
  143. package/src/lifecycle/BlocLifecycle.ts +0 -156
  144. package/src/lifecycle/index.ts +0 -1
  145. package/src/plugins/BlocPluginRegistry.ts +0 -240
  146. package/src/plugins/SystemPluginRegistry.ts +0 -242
  147. package/src/plugins/__tests__/BlocPluginRegistry.test.ts +0 -450
  148. package/src/plugins/__tests__/SystemPluginRegistry.test.ts +0 -404
  149. package/src/plugins/types.ts +0 -129
  150. package/src/subscription/README.md +0 -132
  151. package/src/subscription/SubscriptionManager.ts +0 -395
  152. package/src/subscription/__tests__/SubscriptionManager.test.ts +0 -355
  153. package/src/subscription/types.ts +0 -90
  154. package/src/testing.ts +0 -246
  155. package/src/utils/BatchingManager.ts +0 -59
  156. package/src/utils/RerenderLogger.ts +0 -265
  157. package/src/utils/__tests__/generateInstanceId.test.ts +0 -104
  158. package/src/utils/__tests__/shallowEqual.test.ts +0 -171
  159. package/src/utils/__tests__/uuid.test.ts +0 -144
  160. package/src/utils/generateInstanceId.ts +0 -54
  161. package/src/utils/shallowEqual.ts +0 -40
  162. package/src/utils/uuid.ts +0 -22
@@ -1,349 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { BlocBase } from '../BlocBase';
3
- import { Blac } from '../Blac';
4
-
5
- // Test implementation of BlocBase
6
- class TestBloc extends BlocBase<{ count: number; message: string }> {
7
- constructor(initialState = { count: 0, message: 'initial' }) {
8
- super(initialState);
9
- }
10
-
11
- increment() {
12
- this.emit({ ...this.state, count: this.state.count + 1 });
13
- }
14
-
15
- updateMessage(message: string) {
16
- this.emit({ ...this.state, message });
17
- }
18
-
19
- updateBoth(count: number, message: string) {
20
- this.emit({ count, message });
21
- }
22
- }
23
-
24
- // Test bloc with static properties
25
- class KeepAliveBloc extends BlocBase<string> {
26
- static keepAlive = true;
27
-
28
- constructor() {
29
- super('initial');
30
- }
31
-
32
- update(value: string) {
33
- this.emit(value);
34
- }
35
- }
36
-
37
- describe('BlocBase Subscription Model', () => {
38
- let blac: Blac;
39
-
40
- beforeEach(() => {
41
- blac = new Blac({ __unsafe_ignore_singleton: true });
42
- vi.useFakeTimers();
43
- });
44
-
45
- afterEach(() => {
46
- vi.useRealTimers();
47
- vi.clearAllMocks();
48
- });
49
-
50
- describe('Basic Subscriptions', () => {
51
- it('should subscribe to all state changes', () => {
52
- const bloc = new TestBloc();
53
- const callback = vi.fn();
54
-
55
- const unsubscribe = bloc.subscribe(callback);
56
-
57
- bloc.increment();
58
- expect(callback).toHaveBeenCalledWith({ count: 1, message: 'initial' });
59
-
60
- bloc.updateMessage('updated');
61
- expect(callback).toHaveBeenCalledWith({ count: 1, message: 'updated' });
62
-
63
- expect(callback).toHaveBeenCalledTimes(2);
64
-
65
- unsubscribe();
66
- bloc.increment();
67
- expect(callback).toHaveBeenCalledTimes(2); // No new calls
68
- });
69
-
70
- it('should track subscription count', () => {
71
- const bloc = new TestBloc();
72
-
73
- expect(bloc.subscriptionCount).toBe(0);
74
-
75
- const unsub1 = bloc.subscribe(() => {});
76
- expect(bloc.subscriptionCount).toBe(1);
77
-
78
- const unsub2 = bloc.subscribe(() => {});
79
- expect(bloc.subscriptionCount).toBe(2);
80
-
81
- unsub1();
82
- expect(bloc.subscriptionCount).toBe(1);
83
-
84
- unsub2();
85
- expect(bloc.subscriptionCount).toBe(0);
86
- });
87
- });
88
-
89
- describe('Selector-based Subscriptions', () => {
90
- it('should only notify when selected value changes', () => {
91
- const bloc = new TestBloc();
92
- const callback = vi.fn();
93
-
94
- const unsubscribe = bloc.subscribeWithSelector(
95
- (state) => state.count,
96
- callback,
97
- );
98
-
99
- // Should not be called on subscription
100
- expect(callback).not.toHaveBeenCalled();
101
-
102
- bloc.increment();
103
- expect(callback).toHaveBeenCalledWith(1);
104
-
105
- // Message update should not trigger callback
106
- bloc.updateMessage('updated');
107
- expect(callback).toHaveBeenCalledTimes(1);
108
-
109
- bloc.increment();
110
- expect(callback).toHaveBeenCalledWith(2);
111
- expect(callback).toHaveBeenCalledTimes(2);
112
-
113
- unsubscribe();
114
- });
115
-
116
- it('should support custom equality function', () => {
117
- const bloc = new TestBloc();
118
- const callback = vi.fn();
119
-
120
- // Only notify when count changes by more than 2
121
- const unsubscribe = bloc.subscribeWithSelector(
122
- (state) => state.count,
123
- callback,
124
- (a, b) => Math.abs(a - b) <= 2,
125
- );
126
-
127
- bloc.increment(); // 0 -> 1 (diff = 1, not notified)
128
- expect(callback).not.toHaveBeenCalled();
129
-
130
- bloc.increment(); // 1 -> 2 (diff = 1, not notified)
131
- expect(callback).not.toHaveBeenCalled();
132
-
133
- bloc.updateBoth(5, 'test'); // 2 -> 5 (diff = 3, notified)
134
- expect(callback).toHaveBeenCalledWith(5);
135
- expect(callback).toHaveBeenCalledTimes(1);
136
-
137
- unsubscribe();
138
- });
139
-
140
- it('should handle complex selectors', () => {
141
- const bloc = new TestBloc();
142
- const callback = vi.fn();
143
-
144
- const unsubscribe = bloc.subscribeWithSelector(
145
- (state) => ({
146
- doubled: state.count * 2,
147
- upper: state.message.toUpperCase(),
148
- }),
149
- callback,
150
- );
151
-
152
- bloc.increment();
153
- expect(callback).toHaveBeenCalledWith({ doubled: 2, upper: 'INITIAL' });
154
-
155
- bloc.updateMessage('hello');
156
- expect(callback).toHaveBeenCalledWith({ doubled: 2, upper: 'HELLO' });
157
-
158
- unsubscribe();
159
- });
160
- });
161
-
162
- describe('Component Subscriptions', () => {
163
- it('should support weak reference subscriptions', () => {
164
- const bloc = new TestBloc();
165
- const callback = vi.fn();
166
- let component: any = { id: 'test-component' };
167
- const weakRef = new WeakRef(component);
168
-
169
- const unsubscribe = bloc.subscribeComponent(weakRef, callback);
170
-
171
- bloc.increment();
172
- expect(callback).toHaveBeenCalled();
173
-
174
- // Clear strong reference
175
- component = null;
176
-
177
- // Force garbage collection (platform-specific)
178
- if ((global as any).gc) {
179
- (global as any).gc();
180
- }
181
-
182
- unsubscribe();
183
- });
184
- });
185
-
186
- describe('Lifecycle and Disposal', () => {
187
- it('should dispose when no subscriptions remain', async () => {
188
- const bloc = new TestBloc();
189
- const disposeSpy = vi.spyOn(bloc, 'dispose');
190
-
191
- const unsub1 = bloc.subscribe(() => {});
192
- const unsub2 = bloc.subscribe(() => {});
193
-
194
- unsub1();
195
- expect(disposeSpy).not.toHaveBeenCalled();
196
-
197
- unsub2();
198
-
199
- // Disposal is scheduled with timeout
200
- await vi.runAllTimersAsync();
201
- expect(disposeSpy).toHaveBeenCalled();
202
- });
203
-
204
- it('should not dispose keep-alive blocs', async () => {
205
- const bloc = new KeepAliveBloc();
206
- const disposeSpy = vi.spyOn(bloc, 'dispose');
207
-
208
- const unsubscribe = bloc.subscribe(() => {});
209
- unsubscribe();
210
-
211
- await vi.runAllTimersAsync();
212
- expect(disposeSpy).not.toHaveBeenCalled();
213
- });
214
-
215
- it('should cancel disposal if new subscription added', async () => {
216
- const bloc = new TestBloc();
217
- const disposeSpy = vi.spyOn(bloc, 'dispose');
218
-
219
- const unsub1 = bloc.subscribe(() => {});
220
- unsub1();
221
-
222
- // Disposal scheduled
223
- expect(bloc.subscriptionCount).toBe(0);
224
-
225
- // Add new subscription before disposal
226
- const unsub2 = bloc.subscribe(() => {});
227
-
228
- await vi.runAllTimersAsync();
229
- expect(disposeSpy).not.toHaveBeenCalled();
230
-
231
- unsub2();
232
- await vi.runAllTimersAsync();
233
- expect(disposeSpy).toHaveBeenCalled();
234
- });
235
-
236
- it('should not allow subscriptions to disposed bloc', async () => {
237
- const bloc = new TestBloc();
238
- await bloc.dispose();
239
-
240
- const callback = vi.fn();
241
- const unsubscribe = bloc.subscribe(callback);
242
-
243
- bloc.increment();
244
- expect(callback).not.toHaveBeenCalled();
245
-
246
- unsubscribe();
247
- });
248
- });
249
-
250
- describe('State Updates and Batching', () => {
251
- it('should batch multiple state updates', () => {
252
- const bloc = new TestBloc();
253
- const callback = vi.fn();
254
-
255
- bloc.subscribe(callback);
256
-
257
- bloc._batchUpdates(() => {
258
- bloc.increment();
259
- bloc.increment();
260
- bloc.updateMessage('batched');
261
- });
262
-
263
- // Only one notification for the final state
264
- expect(callback).toHaveBeenCalledTimes(1);
265
- expect(callback).toHaveBeenCalledWith({ count: 2, message: 'batched' });
266
- });
267
-
268
- it('should not emit undefined states', () => {
269
- const bloc = new TestBloc();
270
- const callback = vi.fn();
271
-
272
- bloc.subscribe(callback);
273
-
274
- // Try to emit undefined
275
- (bloc as any).emit(undefined);
276
-
277
- expect(callback).not.toHaveBeenCalled();
278
- });
279
- });
280
-
281
- describe('Priority-based Notifications', () => {
282
- it('should notify subscriptions in priority order', () => {
283
- const bloc = new TestBloc();
284
- const order: number[] = [];
285
-
286
- // Direct access to subscription manager for priority testing
287
- const manager = bloc._subscriptionManager;
288
-
289
- manager.subscribe({
290
- type: 'observer',
291
- priority: 1,
292
- notify: () => order.push(1),
293
- });
294
-
295
- manager.subscribe({
296
- type: 'observer',
297
- priority: 10,
298
- notify: () => order.push(10),
299
- });
300
-
301
- manager.subscribe({
302
- type: 'observer',
303
- priority: 5,
304
- notify: () => order.push(5),
305
- });
306
-
307
- bloc.increment();
308
-
309
- expect(order).toEqual([10, 5, 1]); // Highest priority first
310
- });
311
- });
312
-
313
- describe('Error Handling', () => {
314
- it('should handle errors in subscription callbacks', () => {
315
- const bloc = new TestBloc();
316
- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
317
-
318
- const goodCallback = vi.fn();
319
- const badCallback = vi.fn(() => {
320
- throw new Error('Subscription error');
321
- });
322
-
323
- bloc.subscribe(badCallback);
324
- bloc.subscribe(goodCallback);
325
-
326
- bloc.increment();
327
-
328
- expect(goodCallback).toHaveBeenCalled();
329
- expect(badCallback).toHaveBeenCalled();
330
-
331
- errorSpy.mockRestore();
332
- });
333
-
334
- it('should handle errors in selectors', () => {
335
- const bloc = new TestBloc();
336
- const callback = vi.fn();
337
- const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
338
-
339
- bloc.subscribeWithSelector(() => {
340
- throw new Error('Selector error');
341
- }, callback);
342
-
343
- bloc.increment();
344
- expect(callback).not.toHaveBeenCalled();
345
-
346
- errorSpy.mockRestore();
347
- });
348
- });
349
- });