@apogeelabs/beacon-react-utils 0.1.1
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.
- package/LICENSE +14 -0
- package/README.md +5 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +30 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
Copyright (c) 2025 Apogee Travel, LLC
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
13
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
14
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BeaconState, BeaconDerived, BeaconActions, Store } from '@apogeelabs/beacon';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A custom React hook to watch for state changes in a beacon store.
|
|
5
|
+
*
|
|
6
|
+
* @template TState - The type of the store's state, extending BeaconState.
|
|
7
|
+
* @template TDerived - The type of the store's derived values, extending BeaconDerived<TState>. If you don't have derived values, use EmptyDerived<TState>.
|
|
8
|
+
* @template TActions - The type of the store's actions, extending BeaconActions<TState>. If you don't have actions, use EmptyActions.
|
|
9
|
+
* @template T - The type returned by the selector function, inferred from the selector.
|
|
10
|
+
*
|
|
11
|
+
* @param {Store<TState, TDerived, TActions>} store - The Beacon store instance.
|
|
12
|
+
* @param {(store: Store<TState, TDerived, TActions>) => T} selector - A function that extracts the part of the store to watch.
|
|
13
|
+
* @param {(value: T) => void} onChange - A callback invoked with the new value when the selected state changes.
|
|
14
|
+
* @param {boolean} [fireImmediately=false] - Whether to invoke onChange with the initial value.
|
|
15
|
+
*/
|
|
16
|
+
declare function useStoreWatcher<TState extends BeaconState, TDerived extends BeaconDerived<TState>, TActions extends BeaconActions<TState>, T>(store: Store<TState, TDerived, TActions>, selector: (store: Store<TState, TDerived, TActions>) => T, onChange: (value: T) => void, fireImmediately?: boolean): void;
|
|
17
|
+
|
|
18
|
+
export { useStoreWatcher };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BeaconState, BeaconDerived, BeaconActions, Store } from '@apogeelabs/beacon';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A custom React hook to watch for state changes in a beacon store.
|
|
5
|
+
*
|
|
6
|
+
* @template TState - The type of the store's state, extending BeaconState.
|
|
7
|
+
* @template TDerived - The type of the store's derived values, extending BeaconDerived<TState>. If you don't have derived values, use EmptyDerived<TState>.
|
|
8
|
+
* @template TActions - The type of the store's actions, extending BeaconActions<TState>. If you don't have actions, use EmptyActions.
|
|
9
|
+
* @template T - The type returned by the selector function, inferred from the selector.
|
|
10
|
+
*
|
|
11
|
+
* @param {Store<TState, TDerived, TActions>} store - The Beacon store instance.
|
|
12
|
+
* @param {(store: Store<TState, TDerived, TActions>) => T} selector - A function that extracts the part of the store to watch.
|
|
13
|
+
* @param {(value: T) => void} onChange - A callback invoked with the new value when the selected state changes.
|
|
14
|
+
* @param {boolean} [fireImmediately=false] - Whether to invoke onChange with the initial value.
|
|
15
|
+
*/
|
|
16
|
+
declare function useStoreWatcher<TState extends BeaconState, TDerived extends BeaconDerived<TState>, TActions extends BeaconActions<TState>, T>(store: Store<TState, TDerived, TActions>, selector: (store: Store<TState, TDerived, TActions>) => T, onChange: (value: T) => void, fireImmediately?: boolean): void;
|
|
17
|
+
|
|
18
|
+
export { useStoreWatcher };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var isEqual = require('lodash/isEqual');
|
|
4
|
+
var mobx = require('mobx');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var isEqual__default = /*#__PURE__*/_interopDefault(isEqual);
|
|
10
|
+
|
|
11
|
+
// src/useStoreWatcher.ts
|
|
12
|
+
function useStoreWatcher(store, selector, onChange, fireImmediately = false) {
|
|
13
|
+
react.useEffect(() => {
|
|
14
|
+
const disposer = mobx.reaction(
|
|
15
|
+
() => {
|
|
16
|
+
return mobx.toJS(selector(store));
|
|
17
|
+
},
|
|
18
|
+
(value, previousValue) => {
|
|
19
|
+
if (!previousValue || !isEqual__default.default(value, previousValue)) {
|
|
20
|
+
onChange(value);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{ fireImmediately }
|
|
24
|
+
);
|
|
25
|
+
store.registerCleanup(() => {
|
|
26
|
+
disposer();
|
|
27
|
+
});
|
|
28
|
+
return () => {
|
|
29
|
+
disposer();
|
|
30
|
+
};
|
|
31
|
+
}, [store, selector, onChange, fireImmediately]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.useStoreWatcher = useStoreWatcher;
|
|
35
|
+
//# sourceMappingURL=out.js.map
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useStoreWatcher.ts"],"names":[],"mappings":";AACA,OAAO,aAAa;AACpB,SAAS,UAAU,YAAY;AAC/B,SAAS,iBAAiB;AAenB,SAAS,gBAMZ,OACA,UACA,UACA,kBAA2B,OACvB;AACJ,YAAU,MAAM;AAEZ,UAAM,WAAW;AAAA,MACb,MAAM;AAEF,eAAO,KAAK,SAAS,KAAK,CAAC;AAAA,MAC/B;AAAA,MACA,CAAC,OAAO,kBAAkB;AAEtB,YAAI,CAAC,iBAAiB,CAAC,QAAQ,OAAO,aAAa,GAAG;AAClD,mBAAS,KAAK;AAAA,QAClB;AAAA,MACJ;AAAA,MACA,EAAE,gBAAgB;AAAA,IACtB;AAGA,UAAM,gBAAgB,MAAM;AACxB,eAAS;AAAA,IACb,CAAC;AAGD,WAAO,MAAM;AACT,eAAS;AAAA,IACb;AAAA,EACJ,GAAG,CAAC,OAAO,UAAU,UAAU,eAAe,CAAC;AACnD","sourcesContent":["import type { BeaconActions, BeaconDerived, BeaconState, Store } from \"@apogeelabs/beacon\";\nimport isEqual from \"lodash/isEqual\";\nimport { reaction, toJS } from \"mobx\";\nimport { useEffect } from \"react\";\n\n/**\n * A custom React hook to watch for state changes in a beacon store.\n *\n * @template TState - The type of the store's state, extending BeaconState.\n * @template TDerived - The type of the store's derived values, extending BeaconDerived<TState>. If you don't have derived values, use EmptyDerived<TState>.\n * @template TActions - The type of the store's actions, extending BeaconActions<TState>. If you don't have actions, use EmptyActions.\n * @template T - The type returned by the selector function, inferred from the selector.\n *\n * @param {Store<TState, TDerived, TActions>} store - The Beacon store instance.\n * @param {(store: Store<TState, TDerived, TActions>) => T} selector - A function that extracts the part of the store to watch.\n * @param {(value: T) => void} onChange - A callback invoked with the new value when the selected state changes.\n * @param {boolean} [fireImmediately=false] - Whether to invoke onChange with the initial value.\n */\nexport function useStoreWatcher<\n TState extends BeaconState,\n TDerived extends BeaconDerived<TState>,\n TActions extends BeaconActions<TState>,\n T,\n>(\n store: Store<TState, TDerived, TActions>,\n selector: (store: Store<TState, TDerived, TActions>) => T,\n onChange: (value: T) => void,\n fireImmediately: boolean = false\n): void {\n useEffect(() => {\n // Set up a MobX reaction to watch the selected state\n const disposer = reaction(\n () => {\n // Convert to plain JS to strip MobX observable properties\n return toJS(selector(store));\n },\n (value, previousValue) => {\n // Only trigger if the value actually changed using deep comparison\n if (!previousValue || !isEqual(value, previousValue)) {\n onChange(value);\n }\n },\n { fireImmediately }\n );\n\n // Register the disposer with the store for cleanup\n store.registerCleanup(() => {\n disposer();\n });\n\n // Cleanup when the component unmounts or dependencies change\n return () => {\n disposer();\n };\n }, [store, selector, onChange, fireImmediately]);\n}\n\nexport default useStoreWatcher;\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import isEqual from 'lodash/isEqual';
|
|
2
|
+
import { reaction, toJS } from 'mobx';
|
|
3
|
+
import { useEffect } from 'react';
|
|
4
|
+
|
|
5
|
+
// src/useStoreWatcher.ts
|
|
6
|
+
function useStoreWatcher(store, selector, onChange, fireImmediately = false) {
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const disposer = reaction(
|
|
9
|
+
() => {
|
|
10
|
+
return toJS(selector(store));
|
|
11
|
+
},
|
|
12
|
+
(value, previousValue) => {
|
|
13
|
+
if (!previousValue || !isEqual(value, previousValue)) {
|
|
14
|
+
onChange(value);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
{ fireImmediately }
|
|
18
|
+
);
|
|
19
|
+
store.registerCleanup(() => {
|
|
20
|
+
disposer();
|
|
21
|
+
});
|
|
22
|
+
return () => {
|
|
23
|
+
disposer();
|
|
24
|
+
};
|
|
25
|
+
}, [store, selector, onChange, fireImmediately]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { useStoreWatcher };
|
|
29
|
+
//# sourceMappingURL=out.js.map
|
|
30
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useStoreWatcher.ts"],"names":[],"mappings":";AACA,OAAO,aAAa;AACpB,SAAS,UAAU,YAAY;AAC/B,SAAS,iBAAiB;AAenB,SAAS,gBAMZ,OACA,UACA,UACA,kBAA2B,OACvB;AACJ,YAAU,MAAM;AAEZ,UAAM,WAAW;AAAA,MACb,MAAM;AAEF,eAAO,KAAK,SAAS,KAAK,CAAC;AAAA,MAC/B;AAAA,MACA,CAAC,OAAO,kBAAkB;AAEtB,YAAI,CAAC,iBAAiB,CAAC,QAAQ,OAAO,aAAa,GAAG;AAClD,mBAAS,KAAK;AAAA,QAClB;AAAA,MACJ;AAAA,MACA,EAAE,gBAAgB;AAAA,IACtB;AAGA,UAAM,gBAAgB,MAAM;AACxB,eAAS;AAAA,IACb,CAAC;AAGD,WAAO,MAAM;AACT,eAAS;AAAA,IACb;AAAA,EACJ,GAAG,CAAC,OAAO,UAAU,UAAU,eAAe,CAAC;AACnD","sourcesContent":["import type { BeaconActions, BeaconDerived, BeaconState, Store } from \"@apogeelabs/beacon\";\nimport isEqual from \"lodash/isEqual\";\nimport { reaction, toJS } from \"mobx\";\nimport { useEffect } from \"react\";\n\n/**\n * A custom React hook to watch for state changes in a beacon store.\n *\n * @template TState - The type of the store's state, extending BeaconState.\n * @template TDerived - The type of the store's derived values, extending BeaconDerived<TState>. If you don't have derived values, use EmptyDerived<TState>.\n * @template TActions - The type of the store's actions, extending BeaconActions<TState>. If you don't have actions, use EmptyActions.\n * @template T - The type returned by the selector function, inferred from the selector.\n *\n * @param {Store<TState, TDerived, TActions>} store - The Beacon store instance.\n * @param {(store: Store<TState, TDerived, TActions>) => T} selector - A function that extracts the part of the store to watch.\n * @param {(value: T) => void} onChange - A callback invoked with the new value when the selected state changes.\n * @param {boolean} [fireImmediately=false] - Whether to invoke onChange with the initial value.\n */\nexport function useStoreWatcher<\n TState extends BeaconState,\n TDerived extends BeaconDerived<TState>,\n TActions extends BeaconActions<TState>,\n T,\n>(\n store: Store<TState, TDerived, TActions>,\n selector: (store: Store<TState, TDerived, TActions>) => T,\n onChange: (value: T) => void,\n fireImmediately: boolean = false\n): void {\n useEffect(() => {\n // Set up a MobX reaction to watch the selected state\n const disposer = reaction(\n () => {\n // Convert to plain JS to strip MobX observable properties\n return toJS(selector(store));\n },\n (value, previousValue) => {\n // Only trigger if the value actually changed using deep comparison\n if (!previousValue || !isEqual(value, previousValue)) {\n onChange(value);\n }\n },\n { fireImmediately }\n );\n\n // Register the disposer with the store for cleanup\n store.registerCleanup(() => {\n disposer();\n });\n\n // Cleanup when the component unmounts or dependencies change\n return () => {\n disposer();\n };\n }, [store, selector, onChange, fireImmediately]);\n}\n\nexport default useStoreWatcher;\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@apogeelabs/beacon-react-utils",
|
|
3
|
+
"author": "Apogee Travel",
|
|
4
|
+
"contributors": [
|
|
5
|
+
{
|
|
6
|
+
"name": "Jim Cowart"
|
|
7
|
+
}
|
|
8
|
+
],
|
|
9
|
+
"version": "0.1.1",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"description": "beacon middleware for react-query integration with beacon stores",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"module": "dist/index.mjs",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"require": "./dist/index.js",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"keywords": [
|
|
31
|
+
"beacon",
|
|
32
|
+
"mobx",
|
|
33
|
+
"store",
|
|
34
|
+
"state management"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"lodash": "4.17.16",
|
|
41
|
+
"mobx": "^6.13.7",
|
|
42
|
+
"react": "^18.0.0",
|
|
43
|
+
"@apogeelabs/beacon": "0.4.1"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/jest": "^29.5.5",
|
|
47
|
+
"@types/lodash": "4.17.16",
|
|
48
|
+
"@types/react": "^18.0.0",
|
|
49
|
+
"eslint": "^8.0.1",
|
|
50
|
+
"eslint-config-standard": "^17.1.0",
|
|
51
|
+
"eslint-plugin-import": "^2.25.2",
|
|
52
|
+
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
|
|
53
|
+
"eslint-plugin-promise": "^6.0.0",
|
|
54
|
+
"jest": "^29.7.0",
|
|
55
|
+
"ts-jest": "^29.1.1",
|
|
56
|
+
"tsup": "^7.2.0",
|
|
57
|
+
"typescript": "^5.2.2"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"dev": "tsup --watch",
|
|
61
|
+
"build": "tsup",
|
|
62
|
+
"test": "jest",
|
|
63
|
+
"test:watch": "jest --watch --collect-coverage",
|
|
64
|
+
"lint": "eslint --config ../../.eslintrc.js 'src/**/*.{ts,tsx}'"
|
|
65
|
+
}
|
|
66
|
+
}
|