@kuindji/reactive 1.0.3 → 1.0.5

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.
@@ -86,6 +86,7 @@ export declare function createEventBus<EventsMap extends BaseEventMap = DefaultE
86
86
  readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
87
87
  readonly intercept: (fn: InterceptorFunction) => void;
88
88
  readonly stopIntercepting: () => void;
89
+ readonly isIntercepting: () => boolean;
89
90
  readonly reset: () => void;
90
91
  readonly suspendAll: (withQueue?: boolean) => void;
91
92
  readonly resumeAll: () => void;
package/dist/eventBus.js CHANGED
@@ -137,6 +137,9 @@ function createEventBus(eventBusOptions) {
137
137
  const stopIntercepting = () => {
138
138
  interceptor = null;
139
139
  };
140
+ const isIntercepting = () => {
141
+ return interceptor !== null;
142
+ };
140
143
  const get = (name) => {
141
144
  return _getOrAddEvent(name);
142
145
  };
@@ -492,6 +495,7 @@ function createEventBus(eventBusOptions) {
492
495
  withTags,
493
496
  intercept,
494
497
  stopIntercepting,
498
+ isIntercepting,
495
499
  reset,
496
500
  suspendAll,
497
501
  resumeAll,
@@ -81,6 +81,7 @@ export declare function useEventBus<EventsMap extends BaseEventMap = DefaultEven
81
81
  readonly withTags: <T extends (...args: any[]) => any>(tags: string[], callback: T) => ReturnType<T>;
82
82
  readonly intercept: (fn: (name: import("../lib/types").MapKey, args: any[], tags: string[] | null, returnType: import("../lib/types").TriggerReturnType | null) => boolean) => void;
83
83
  readonly stopIntercepting: () => void;
84
+ readonly isIntercepting: () => boolean;
84
85
  readonly reset: () => void;
85
86
  readonly suspendAll: (withQueue?: boolean) => void;
86
87
  readonly resumeAll: () => void;
@@ -24,28 +24,32 @@ export declare function useStore<PropMap extends BasePropMap, Store extends Stor
24
24
  readonly reset: () => void;
25
25
  readonly onChange: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1], previousValue?: PropMap[K_1] | undefined) => void; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1], previousValue?: PropMap[K_1] | undefined) => void; }>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
26
26
  readonly removeOnChange: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1], previousValue?: PropMap[K_1] | undefined) => void; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1], previousValue?: PropMap[K_1] | undefined) => void; }>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
27
- readonly pipe: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
28
- readonly removePipe: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
29
27
  readonly control: <K extends import("..").KeyOf<import("..").GetEventsMap<{
30
- beforeChange: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
28
+ before: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
31
29
  change: (names: import("..").KeyOf<PropMap>[]) => void;
32
30
  reset: () => void;
33
31
  error: import("..").ErrorListenerSignature<any[]>;
32
+ effect: <K_1 extends import("..").KeyOf<PropMap>, V_1 extends PropMap[K_1]>(name: K_1, value: V_1) => void;
34
33
  }>>, H extends import("..").GetEventsMap<{
35
- beforeChange: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
34
+ before: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
36
35
  change: (names: import("..").KeyOf<PropMap>[]) => void;
37
36
  reset: () => void;
38
37
  error: import("..").ErrorListenerSignature<any[]>;
38
+ effect: <K_1 extends import("..").KeyOf<PropMap>, V_1 extends PropMap[K_1]>(name: K_1, value: V_1) => void;
39
39
  }>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
40
40
  readonly removeControl: <K extends import("..").KeyOf<import("..").GetEventsMap<{
41
- beforeChange: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
41
+ before: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
42
42
  change: (names: import("..").KeyOf<PropMap>[]) => void;
43
43
  reset: () => void;
44
44
  error: import("..").ErrorListenerSignature<any[]>;
45
+ effect: <K_1 extends import("..").KeyOf<PropMap>, V_1 extends PropMap[K_1]>(name: K_1, value: V_1) => void;
45
46
  }>>, H extends import("..").GetEventsMap<{
46
- beforeChange: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
47
+ before: <K_1 extends import("..").KeyOf<PropMap>, V extends PropMap[K_1]>(name: K_1, value: V) => boolean;
47
48
  change: (names: import("..").KeyOf<PropMap>[]) => void;
48
49
  reset: () => void;
49
50
  error: import("..").ErrorListenerSignature<any[]>;
51
+ effect: <K_1 extends import("..").KeyOf<PropMap>, V_1 extends PropMap[K_1]>(name: K_1, value: V_1) => void;
50
52
  }>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
53
+ readonly pipe: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>[K]["signature"]>(name: K, handler: H, options?: import("..").ListenerOptions) => void;
54
+ readonly removePipe: <K extends import("..").KeyOf<import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>>, H extends import("..").GetEventsMap<{ [K_1 in import("..").KeyOf<PropMap>]: (value: PropMap[K_1]) => PropMap[K_1]; }>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
51
55
  }>;
package/dist/store.d.ts CHANGED
@@ -3,15 +3,17 @@ import type { ApiType, ErrorListenerSignature, KeyOf, MapKey } from "./lib/types
3
3
  export interface BasePropMap {
4
4
  [key: MapKey]: any;
5
5
  }
6
- export declare const BeforeChangeEventName = "beforeChange";
6
+ export declare const BeforeChangeEventName = "before";
7
7
  export declare const ChangeEventName = "change";
8
8
  export declare const ResetEventName = "reset";
9
9
  export declare const ErrorEventName = "error";
10
+ export declare const EffectEventName = "effect";
10
11
  type StoreControlEvents<PropMap extends BasePropMap> = {
11
12
  [BeforeChangeEventName]: <K extends KeyOf<PropMap>, V extends PropMap[K]>(name: K, value: V) => boolean;
12
13
  [ChangeEventName]: (names: KeyOf<PropMap>[]) => void;
13
14
  [ResetEventName]: () => void;
14
15
  [ErrorEventName]: ErrorListenerSignature<any[]>;
16
+ [EffectEventName]: <K extends KeyOf<PropMap>, V extends PropMap[K]>(name: K, value: V) => void;
15
17
  };
16
18
  type StoreChangeEvents<PropMap extends BasePropMap> = {
17
19
  [K in KeyOf<PropMap>]: (value: PropMap[K], previousValue?: PropMap[K] | undefined) => void;
@@ -44,10 +46,10 @@ export declare function createStore<PropMap extends BasePropMap = BasePropMap>(i
44
46
  readonly reset: () => void;
45
47
  readonly onChange: <K extends KeyOf<import("./eventBus").GetEventsMap<StoreChangeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StoreChangeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, options?: import("./event").ListenerOptions) => void;
46
48
  readonly removeOnChange: <K extends KeyOf<import("./eventBus").GetEventsMap<StoreChangeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StoreChangeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
47
- readonly pipe: <K extends KeyOf<import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, options?: import("./event").ListenerOptions) => void;
48
- readonly removePipe: <K extends KeyOf<import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
49
49
  readonly control: <K extends KeyOf<import("./eventBus").GetEventsMap<StoreControlEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StoreControlEvents<PropMap>>[K]["signature"]>(name: K, handler: H, options?: import("./event").ListenerOptions) => void;
50
50
  readonly removeControl: <K extends KeyOf<import("./eventBus").GetEventsMap<StoreControlEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StoreControlEvents<PropMap>>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
51
+ readonly pipe: <K extends KeyOf<import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, options?: import("./event").ListenerOptions) => void;
52
+ readonly removePipe: <K extends KeyOf<import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>>, H extends import("./eventBus").GetEventsMap<StorePipeEvents<PropMap>>[K]["signature"]>(name: K, handler: H, context?: object | null, tag?: string | null) => void;
51
53
  }>;
52
54
  export type BaseStoreDefinition = StoreDefinitionHelper<BasePropMap>;
53
55
  export type BaseStore = ReturnType<typeof createStore<any>>;
package/dist/store.js CHANGED
@@ -1,19 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ErrorEventName = exports.ResetEventName = exports.ChangeEventName = exports.BeforeChangeEventName = void 0;
3
+ exports.EffectEventName = exports.ErrorEventName = exports.ResetEventName = exports.ChangeEventName = exports.BeforeChangeEventName = void 0;
4
4
  exports.createStore = createStore;
5
5
  const eventBus_1 = require("./eventBus");
6
- exports.BeforeChangeEventName = "beforeChange";
6
+ exports.BeforeChangeEventName = "before";
7
7
  exports.ChangeEventName = "change";
8
8
  exports.ResetEventName = "reset";
9
9
  exports.ErrorEventName = "error";
10
+ exports.EffectEventName = "effect";
10
11
  function createStore(initialData = {}) {
11
12
  const data = new Map(Object.entries(initialData));
12
13
  const changes = (0, eventBus_1.createEventBus)();
13
14
  const pipe = (0, eventBus_1.createEventBus)();
14
15
  const control = (0, eventBus_1.createEventBus)();
16
+ let effectKeys = [];
15
17
  const _set = (name, value, triggerChange = true) => {
16
- var _a, _b, _c;
18
+ var _a, _b, _c, _d, _e;
17
19
  const prev = data.get(name);
18
20
  if (prev !== value) {
19
21
  if (control.firstNonEmpty(exports.BeforeChangeEventName, name, value)
@@ -64,9 +66,45 @@ function createStore(initialData = {}) {
64
66
  }
65
67
  throw error;
66
68
  }
69
+ if ((_c = control.get(exports.EffectEventName)) === null || _c === void 0 ? void 0 : _c.hasListener()) {
70
+ try {
71
+ const isIntercepting = control.isIntercepting();
72
+ if (!isIntercepting) {
73
+ const interceptor = (name, args) => {
74
+ if (name === exports.ChangeEventName) {
75
+ effectKeys.push(...args[0]);
76
+ return false;
77
+ }
78
+ return true;
79
+ };
80
+ control.intercept(interceptor);
81
+ }
82
+ control.trigger(exports.EffectEventName, name, value);
83
+ if (!isIntercepting) {
84
+ control.stopIntercepting();
85
+ }
86
+ }
87
+ catch (error) {
88
+ control.trigger(exports.ErrorEventName, {
89
+ error: error instanceof Error
90
+ ? error
91
+ : new Error(String(error)),
92
+ args: [name],
93
+ type: "store-control",
94
+ name,
95
+ });
96
+ if ((_d = control.get(exports.ErrorEventName)) === null || _d === void 0 ? void 0 : _d.hasListener()) {
97
+ return true;
98
+ }
99
+ throw error;
100
+ }
101
+ }
67
102
  if (triggerChange) {
68
103
  try {
69
- control.trigger(exports.ChangeEventName, [name]);
104
+ control.trigger(exports.ChangeEventName, [name, ...effectKeys]);
105
+ if (!control.isIntercepting()) {
106
+ effectKeys = [];
107
+ }
70
108
  }
71
109
  catch (error) {
72
110
  control.trigger(exports.ErrorEventName, {
@@ -77,7 +115,7 @@ function createStore(initialData = {}) {
77
115
  type: "store-control",
78
116
  name,
79
117
  });
80
- if ((_c = control.get(exports.ErrorEventName)) === null || _c === void 0 ? void 0 : _c.hasListener()) {
118
+ if ((_e = control.get(exports.ErrorEventName)) === null || _e === void 0 ? void 0 : _e.hasListener()) {
81
119
  return true;
82
120
  }
83
121
  throw error;
@@ -99,6 +137,7 @@ function createStore(initialData = {}) {
99
137
  }, 0);
100
138
  }
101
139
  function set(name, value) {
140
+ var _a;
102
141
  if ((typeof name === "string")
103
142
  && value !== undefined) {
104
143
  _set(name, value);
@@ -110,7 +149,28 @@ function createStore(initialData = {}) {
110
149
  changedKeys.push(k);
111
150
  }
112
151
  });
113
- control.trigger(exports.ChangeEventName, changedKeys);
152
+ try {
153
+ control.trigger(exports.ChangeEventName, [
154
+ ...changedKeys,
155
+ ...effectKeys,
156
+ ]);
157
+ if (!control.isIntercepting()) {
158
+ effectKeys = [];
159
+ }
160
+ }
161
+ catch (error) {
162
+ control.trigger(exports.ErrorEventName, {
163
+ error: error instanceof Error
164
+ ? error
165
+ : new Error(String(error)),
166
+ args: [name],
167
+ type: "store-control",
168
+ });
169
+ if ((_a = control.get(exports.ErrorEventName)) === null || _a === void 0 ? void 0 : _a.hasListener()) {
170
+ return true;
171
+ }
172
+ throw error;
173
+ }
114
174
  }
115
175
  else {
116
176
  throw new Error(`Invalid key: ${String(name)}`);
@@ -184,10 +244,10 @@ function createStore(initialData = {}) {
184
244
  reset,
185
245
  onChange: changes.addListener,
186
246
  removeOnChange: changes.removeListener,
187
- pipe: pipe.addListener,
188
- removePipe: pipe.removeListener,
189
247
  control: control.addListener,
190
248
  removeControl: control.removeListener,
249
+ pipe: pipe.addListener,
250
+ removePipe: pipe.removeListener,
191
251
  };
192
252
  return api;
193
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kuindji/reactive",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A JavaScript/TypeScript utility library for building reactive applications with events, actions, stores, and React hooks",
5
5
  "keywords": [
6
6
  "events",