@kuindji/reactive 1.0.21 → 1.0.24

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.
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useStoreState = useStoreState;
4
- const react_1 = require("react");
5
- function useStoreState(store, key) {
6
- const [value, setValue] = (0, react_1.useState)(store.get(key));
7
- const storeRef = (0, react_1.useRef)(store);
8
- const keyRef = (0, react_1.useRef)(key);
9
- const onChange = (0, react_1.useCallback)((value) => {
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+ export function useStoreState(store, key) {
3
+ const [value, setValue] = useState(store.get(key));
4
+ const storeRef = useRef(store);
5
+ const keyRef = useRef(key);
6
+ const onChange = useCallback((value) => {
10
7
  setValue(value);
11
8
  }, []);
12
- const setter = (0, react_1.useCallback)((value) => {
9
+ const setter = useCallback((value) => {
13
10
  if (typeof value === "function") {
14
11
  storeRef.current.set(keyRef.current, value(storeRef.current.get(keyRef.current)));
15
12
  }
@@ -17,16 +14,17 @@ function useStoreState(store, key) {
17
14
  storeRef.current.set(keyRef.current, value);
18
15
  }
19
16
  }, []);
20
- (0, react_1.useEffect)(() => {
17
+ useEffect(() => {
21
18
  return () => {
22
19
  storeRef.current.removeOnChange(keyRef.current, onChange);
23
20
  };
24
21
  }, []);
25
- (0, react_1.useEffect)(() => {
22
+ useEffect(() => {
26
23
  storeRef.current.removeOnChange(keyRef.current, onChange);
27
24
  storeRef.current = store;
28
25
  keyRef.current = key;
29
26
  storeRef.current.onChange(keyRef.current, onChange);
27
+ setValue(store.get(key));
30
28
  }, [store, key]);
31
29
  return [value, setter];
32
30
  }
package/dist/react.js CHANGED
@@ -1,29 +1,13 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./react/ErrorBoundary"), exports);
18
- __exportStar(require("./react/useAction"), exports);
19
- __exportStar(require("./react/useActionBus"), exports);
20
- __exportStar(require("./react/useActionMap"), exports);
21
- __exportStar(require("./react/useEvent"), exports);
22
- __exportStar(require("./react/useEventBus"), exports);
23
- __exportStar(require("./react/useListenToAction"), exports);
24
- __exportStar(require("./react/useListenToActionBus"), exports);
25
- __exportStar(require("./react/useListenToEvent"), exports);
26
- __exportStar(require("./react/useListenToEventBus"), exports);
27
- __exportStar(require("./react/useListenToStoreChanges"), exports);
28
- __exportStar(require("./react/useStore"), exports);
29
- __exportStar(require("./react/useStoreState"), exports);
1
+ export * from "./react/ErrorBoundary";
2
+ export * from "./react/useAction";
3
+ export * from "./react/useActionBus";
4
+ export * from "./react/useActionMap";
5
+ export * from "./react/useEvent";
6
+ export * from "./react/useEventBus";
7
+ export * from "./react/useListenToAction";
8
+ export * from "./react/useListenToActionBus";
9
+ export * from "./react/useListenToEvent";
10
+ export * from "./react/useListenToEventBus";
11
+ export * from "./react/useListenToStoreChanges";
12
+ export * from "./react/useStore";
13
+ export * from "./react/useStoreState";
package/dist/store.js CHANGED
@@ -1,21 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EffectEventName = exports.ErrorEventName = exports.ResetEventName = exports.ChangeEventName = exports.BeforeChangeEventName = void 0;
4
- exports.createStore = createStore;
5
- const eventBus_1 = require("./eventBus");
6
- exports.BeforeChangeEventName = "before";
7
- exports.ChangeEventName = "change";
8
- exports.ResetEventName = "reset";
9
- exports.ErrorEventName = "error";
10
- exports.EffectEventName = "effect";
11
- function createStore(initialData = {}) {
1
+ import { createEventBus } from "./eventBus";
2
+ export const BeforeChangeEventName = "before";
3
+ export const ChangeEventName = "change";
4
+ export const ResetEventName = "reset";
5
+ export const ErrorEventName = "error";
6
+ export const EffectEventName = "effect";
7
+ export function createStore(initialData = {}) {
12
8
  const data = new Map(Object.entries(initialData));
13
- const changes = (0, eventBus_1.createEventBus)();
14
- const pipe = (0, eventBus_1.createEventBus)();
15
- const control = (0, eventBus_1.createEventBus)();
9
+ const changes = createEventBus();
10
+ const pipe = createEventBus();
11
+ const control = createEventBus();
16
12
  let effectKeys = [];
17
13
  const effectInterceptor = (name, args) => {
18
- if (name === exports.ChangeEventName) {
14
+ if (name === ChangeEventName) {
19
15
  effectKeys.push(...args[0]);
20
16
  return false;
21
17
  }
@@ -25,7 +21,7 @@ function createStore(initialData = {}) {
25
21
  var _a, _b, _c, _d, _e;
26
22
  const prev = data.get(name);
27
23
  if (prev !== value) {
28
- if (control.firstNonEmpty(exports.BeforeChangeEventName, name, value)
24
+ if (control.firstNonEmpty(BeforeChangeEventName, name, value)
29
25
  === false) {
30
26
  return;
31
27
  }
@@ -35,7 +31,7 @@ function createStore(initialData = {}) {
35
31
  newValue = pipe.pipe(name, ...pipeArgs);
36
32
  }
37
33
  catch (error) {
38
- control.trigger(exports.ErrorEventName, {
34
+ control.trigger(ErrorEventName, {
39
35
  error: error instanceof Error
40
36
  ? error
41
37
  : new Error(String(error)),
@@ -43,7 +39,7 @@ function createStore(initialData = {}) {
43
39
  type: "store-pipe",
44
40
  name,
45
41
  });
46
- if ((_a = control.get(exports.ErrorEventName)) === null || _a === void 0 ? void 0 : _a.hasListener()) {
42
+ if ((_a = control.get(ErrorEventName)) === null || _a === void 0 ? void 0 : _a.hasListener()) {
47
43
  return false;
48
44
  }
49
45
  throw error;
@@ -60,7 +56,7 @@ function createStore(initialData = {}) {
60
56
  changes.trigger(name, ...changeArgs);
61
57
  }
62
58
  catch (error) {
63
- control.trigger(exports.ErrorEventName, {
59
+ control.trigger(ErrorEventName, {
64
60
  error: error instanceof Error
65
61
  ? error
66
62
  : new Error(String(error)),
@@ -68,24 +64,25 @@ function createStore(initialData = {}) {
68
64
  type: "store-change",
69
65
  name,
70
66
  });
71
- if ((_b = control.get(exports.ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
67
+ if ((_b = control.get(ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
68
+ effectKeys = [];
72
69
  return true;
73
70
  }
74
71
  throw error;
75
72
  }
76
- if ((_c = control.get(exports.EffectEventName)) === null || _c === void 0 ? void 0 : _c.hasListener()) {
73
+ if ((_c = control.get(EffectEventName)) === null || _c === void 0 ? void 0 : _c.hasListener()) {
77
74
  try {
78
75
  const isIntercepting = control.isIntercepting();
79
76
  if (!isIntercepting) {
80
77
  control.intercept(effectInterceptor);
81
78
  }
82
- control.trigger(exports.EffectEventName, name, value);
79
+ control.trigger(EffectEventName, name, value);
83
80
  if (!isIntercepting) {
84
81
  control.stopIntercepting();
85
82
  }
86
83
  }
87
84
  catch (error) {
88
- control.trigger(exports.ErrorEventName, {
85
+ control.trigger(ErrorEventName, {
89
86
  error: error instanceof Error
90
87
  ? error
91
88
  : new Error(String(error)),
@@ -93,7 +90,8 @@ function createStore(initialData = {}) {
93
90
  type: "store-control",
94
91
  name,
95
92
  });
96
- if ((_d = control.get(exports.ErrorEventName)) === null || _d === void 0 ? void 0 : _d.hasListener()) {
93
+ if ((_d = control.get(ErrorEventName)) === null || _d === void 0 ? void 0 : _d.hasListener()) {
94
+ effectKeys = [];
97
95
  return true;
98
96
  }
99
97
  throw error;
@@ -101,13 +99,13 @@ function createStore(initialData = {}) {
101
99
  }
102
100
  if (triggerChange) {
103
101
  try {
104
- control.trigger(exports.ChangeEventName, [name, ...effectKeys]);
102
+ control.trigger(ChangeEventName, [name, ...effectKeys]);
105
103
  if (!control.isIntercepting()) {
106
104
  effectKeys = [];
107
105
  }
108
106
  }
109
107
  catch (error) {
110
- control.trigger(exports.ErrorEventName, {
108
+ control.trigger(ErrorEventName, {
111
109
  error: error instanceof Error
112
110
  ? error
113
111
  : new Error(String(error)),
@@ -115,7 +113,8 @@ function createStore(initialData = {}) {
115
113
  type: "store-control",
116
114
  name,
117
115
  });
118
- if ((_e = control.get(exports.ErrorEventName)) === null || _e === void 0 ? void 0 : _e.hasListener()) {
116
+ if ((_e = control.get(ErrorEventName)) === null || _e === void 0 ? void 0 : _e.hasListener()) {
117
+ effectKeys = [];
119
118
  return true;
120
119
  }
121
120
  throw error;
@@ -143,7 +142,7 @@ function createStore(initialData = {}) {
143
142
  else if (typeof name === "object") {
144
143
  const changedKeys = [];
145
144
  const isIntercepting = control.isIntercepting();
146
- const hasEffectListener = (_a = control.get(exports.EffectEventName)) === null || _a === void 0 ? void 0 : _a.hasListener();
145
+ const hasEffectListener = (_a = control.get(EffectEventName)) === null || _a === void 0 ? void 0 : _a.hasListener();
147
146
  if (hasEffectListener && !isIntercepting) {
148
147
  control.intercept(effectInterceptor);
149
148
  }
@@ -153,7 +152,7 @@ function createStore(initialData = {}) {
153
152
  }
154
153
  });
155
154
  try {
156
- control.trigger(exports.ChangeEventName, [
155
+ control.trigger(ChangeEventName, [
157
156
  ...changedKeys,
158
157
  ...effectKeys,
159
158
  ]);
@@ -163,15 +162,15 @@ function createStore(initialData = {}) {
163
162
  }
164
163
  }
165
164
  catch (error) {
166
- control.trigger(exports.ErrorEventName, {
165
+ control.trigger(ErrorEventName, {
167
166
  error: error instanceof Error
168
167
  ? error
169
168
  : new Error(String(error)),
170
169
  args: [name],
171
170
  type: "store-control",
172
171
  });
173
- if ((_b = control.get(exports.ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
174
- return true;
172
+ if ((_b = control.get(ErrorEventName)) === null || _b === void 0 ? void 0 : _b.hasListener()) {
173
+ return;
175
174
  }
176
175
  throw error;
177
176
  }
@@ -182,14 +181,16 @@ function createStore(initialData = {}) {
182
181
  }
183
182
  const get = (key) => {
184
183
  if (typeof key === "string") {
185
- return data.get(key);
184
+ const value = data.get(key);
185
+ return value;
186
186
  }
187
187
  else if (Array.isArray(key)) {
188
188
  // return object with given keys
189
- return key.reduce((acc, k) => {
190
- acc[k] = data.get(k);
191
- return acc;
192
- }, {});
189
+ const result = {};
190
+ for (const k of key) {
191
+ result[k] = data.get(k);
192
+ }
193
+ return result;
193
194
  }
194
195
  else {
195
196
  throw new Error(`Invalid key: ${String(key)}`);
@@ -204,11 +205,16 @@ function createStore(initialData = {}) {
204
205
  const getData = () => {
205
206
  return Object.fromEntries(data.entries());
206
207
  };
208
+ let batching = false;
207
209
  const batch = (fn) => {
210
+ if (batching) {
211
+ throw new Error("Nested batch() calls are not supported");
212
+ }
213
+ batching = true;
208
214
  const allChangedKeys = [];
209
215
  const log = [];
210
216
  const controlInterceptor = function (name, [changedKeys]) {
211
- if (name === exports.ChangeEventName) {
217
+ if (name === ChangeEventName) {
212
218
  allChangedKeys.push(...changedKeys);
213
219
  return false;
214
220
  }
@@ -220,9 +226,14 @@ function createStore(initialData = {}) {
220
226
  };
221
227
  changes.intercept(changeInterceptor);
222
228
  control.intercept(controlInterceptor);
223
- fn();
224
- control.stopIntercepting();
225
- changes.stopIntercepting();
229
+ try {
230
+ fn();
231
+ }
232
+ finally {
233
+ control.stopIntercepting();
234
+ changes.stopIntercepting();
235
+ batching = false;
236
+ }
226
237
  for (const [propName, value, prev] of log) {
227
238
  const changeArgs = [
228
239
  value,
@@ -231,12 +242,12 @@ function createStore(initialData = {}) {
231
242
  changes.trigger(propName, ...changeArgs);
232
243
  }
233
244
  if (allChangedKeys.length > 0) {
234
- control.trigger(exports.ChangeEventName, allChangedKeys);
245
+ control.trigger(ChangeEventName, allChangedKeys);
235
246
  }
236
247
  };
237
248
  const reset = () => {
238
249
  data.clear();
239
- control.trigger(exports.ResetEventName);
250
+ control.trigger(ResetEventName);
240
251
  };
241
252
  const api = {
242
253
  set,
package/package.json CHANGED
@@ -1,34 +1,35 @@
1
1
  {
2
2
  "name": "@kuindji/reactive",
3
- "version": "1.0.21",
3
+ "version": "1.0.24",
4
4
  "author": "Ivan Kuindzhi",
5
+ "type": "module",
5
6
  "repository": {
6
7
  "type": "git",
7
8
  "url": "git+https://github.com/kuindji/reactive.git"
8
9
  },
9
10
  "main": "dist/index.js",
10
11
  "devDependencies": {
11
- "@happy-dom/global-registrator": "^18.0.1",
12
+ "@happy-dom/global-registrator": "^20.0.11",
12
13
  "@testing-library/dom": "^10.4.1",
13
- "@testing-library/jest-dom": "^6.6.4",
14
+ "@testing-library/jest-dom": "^6.9.1",
14
15
  "@testing-library/react": "^16.3.0",
15
- "@types/bun": "^1.2.19",
16
- "@types/react": "^19.1.9",
17
- "@typescript-eslint/eslint-plugin": "^8.38.0",
18
- "react": "^19.1.1",
19
- "typescript": "^5",
20
- "typescript-eslint": "^8.38.0"
16
+ "@types/bun": "^1.3.3",
17
+ "@types/react": "^19.2.7",
18
+ "@typescript-eslint/eslint-plugin": "^8.48.0",
19
+ "globals": "^16.5.0",
20
+ "react": "^19.2.0",
21
+ "react-dom": "^19.2.0",
22
+ "typescript": "^5.9.3",
23
+ "typescript-eslint": "^8.48.0"
21
24
  },
22
25
  "exports": {
23
26
  ".": {
24
- "import": "./dist/index.js",
25
- "require": "./dist/index.js",
26
- "types": "./dist/index.d.ts"
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
27
29
  },
28
30
  "./react": {
29
- "import": "./dist/react.js",
30
- "require": "./dist/react.js",
31
- "types": "./dist/react.d.ts"
31
+ "types": "./dist/react.d.ts",
32
+ "import": "./dist/react.js"
32
33
  }
33
34
  },
34
35
  "bugs": {
@@ -62,9 +63,10 @@
62
63
  "scripts": {
63
64
  "build": "tsc -p ./tsconfig-build.json",
64
65
  "lint": "bun eslint .",
65
- "test": "bun test tests/**/*.spec.ts*"
66
+ "test": "bun test tests/**/*.spec.ts*",
67
+ "test:types": "tsc -p ./tests/types/tsconfig.json",
68
+ "test:all": "bun run test:types && bun run test"
66
69
  },
67
70
  "sideEffects": false,
68
- "type": "commonjs",
69
71
  "types": "dist/index.d.ts"
70
72
  }