@znemz/use-global-hook 1.1.9 → 1.1.11

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/lib/actions.d.ts CHANGED
@@ -14,3 +14,4 @@ declare const actions: {
14
14
  setRef: <T>(store: Store<T, OuterBaseActions<T>>, newState: T, doDigest?: boolean) => void;
15
15
  };
16
16
  export default actions;
17
+ //# sourceMappingURL=actions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC;AAE1B,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACpD,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACnD;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAC1F,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACzF;AAED,eAAO,MAAM,QAAQ,GAAI,CAAC,EACxB,OAAO,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,EACpC,UAAU,CAAC,EACX,WAAW,OAAO,SAGnB,CAAC;AAEF,eAAO,MAAM,MAAM,GAAI,CAAC,EACtB,OAAO,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,EACpC,UAAU,CAAC,EACX,WAAW,OAAO,SAGnB,CAAC;AAEF,QAAA,MAAM,OAAO;eAhBY,CAAC,SACjB,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,YAC1B,CAAC,aACA,OAAO;aAKG,CAAC,SACf,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,YAC1B,CAAC,aACA,OAAO;CAKgB,CAAC;AAErC,eAAe,OAAO,CAAC"}
package/lib/actions.js CHANGED
@@ -1,10 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setState = function (store, newState, doDigest) {
1
+ export const setState = (store, newState, doDigest) => {
4
2
  store.setState(newState, false, doDigest);
5
3
  };
6
- exports.setRef = function (store, newState, doDigest) {
4
+ export const setRef = (store, newState, doDigest) => {
7
5
  store.setRef(newState, doDigest);
8
6
  };
9
- var actions = { setState: exports.setState, setRef: exports.setRef };
10
- exports.default = actions;
7
+ const actions = { setState, setRef };
8
+ export default actions;
9
+ //# sourceMappingURL=actions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,KAAoC,EACpC,QAAW,EACX,QAAkB,EAClB,EAAE;IACF,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,KAAoC,EACpC,QAAW,EACX,QAAkB,EAClB,EAAE;IACF,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;AAErC,eAAe,OAAO,CAAC"}
package/lib/index.d.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { EffectCallback, DependencyList, Dispatch, SetStateAction } from 'react';
2
2
  import { InnerBaseActions, OuterBaseActions } from './actions';
3
- export declare type UseEffect = (effect: EffectCallback, deps?: DependencyList) => void;
4
- export declare type UseState = <S>(initialState?: S | (() => S)) => [S, Dispatch<SetStateAction<S>>];
3
+ export type UseEffect = (effect: EffectCallback, deps?: DependencyList) => void;
4
+ export type UseState = <S>(initialState?: S | (() => S)) => [S, Dispatch<SetStateAction<S>>];
5
5
  export interface ReactLib {
6
6
  useEffect: UseEffect;
7
7
  useState: UseState;
8
8
  }
9
- export declare type SetStateFn<T> = (newState: T, isRef?: boolean, doDigest?: boolean) => void;
10
- export declare type SetRefFn<T> = (newState: T, doDigest?: boolean) => void;
11
- export declare type UseCustomFn = (React: ReactLib) => void;
12
- export declare type AssociateActionsFn = <T, A>(store: Store<T, A>, actions: A) => A;
13
- export declare type Initializer<T, A> = (_: Store<T, A>) => void;
9
+ export type SetStateFn<T> = (newState: T, isRef?: boolean, doDigest?: boolean) => void;
10
+ export type SetRefFn<T> = (newState: T, doDigest?: boolean) => void;
11
+ export type UseCustomFn = (React: ReactLib) => void;
12
+ export type AssociateActionsFn = <T, A>(store: Store<T, A>, actions: A) => A;
13
+ export type Initializer<T, A> = (_: Store<T, A>) => void;
14
14
  export interface UseStoreProps<T, InnerA = InnerBaseActions<T>, OuterA = OuterBaseActions<T>, WorkR = undefined> {
15
15
  React: ReactLib;
16
16
  initialState?: T;
@@ -18,7 +18,7 @@ export interface UseStoreProps<T, InnerA = InnerBaseActions<T>, OuterA = OuterBa
18
18
  initializer?: Initializer<T, OuterA>;
19
19
  hookWork?: () => WorkR;
20
20
  }
21
- export declare type UseStoreFn = <T, InnerA = InnerBaseActions<T>, OuterA = OuterBaseActions<T>, WorkR = undefined>(_: UseStoreProps<T, InnerA, OuterA, WorkR>) => () => [T, OuterA, ...WorkR[]];
21
+ export type UseStoreFn = <T, InnerA = InnerBaseActions<T>, OuterA = OuterBaseActions<T>, WorkR = undefined>(_: UseStoreProps<T, InnerA, OuterA, WorkR>) => () => [T, OuterA, ...WorkR[]];
22
22
  export interface Store<T, OuterA = OuterBaseActions<T>> {
23
23
  setState: SetStateFn<T>;
24
24
  setRef: SetRefFn<T>;
@@ -26,6 +26,7 @@ export interface Store<T, OuterA = OuterBaseActions<T>> {
26
26
  state: T;
27
27
  listeners: Dispatch<unknown>[];
28
28
  }
29
- export declare type HookWork<T = undefined> = () => T;
30
- declare const useStore: <T, InnerA = InnerBaseActions<T>, OuterA = OuterBaseActions<T>, WorkR = undefined>({ React, initialState, actions, initializer, }?: UseStoreProps<T, InnerA, OuterA, WorkR>) => (_?: HookWork<WorkR>) => [T, OuterA];
29
+ export type HookWork<T = undefined> = () => T;
30
+ declare const useStore: <T, InnerA = InnerBaseActions<T>, OuterA = OuterBaseActions<T>, WorkR = undefined>({ React, initialState, actions, initializer, }?: UseStoreProps<T, InnerA, OuterA, WorkR>) => ((_?: HookWork<WorkR>) => [T, OuterA]);
31
31
  export default useStore;
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AACjF,OAAoB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAE5E,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,cAAc,EAAE,IAAI,CAAC,EAAE,cAAc,KAAK,IAAI,CAAC;AAChF,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7F,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,CAAC,CAAC,EAC5B,KAAK,GAAG,SAAS,EAEjB,CAAC,EAAE,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,KACvC,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;AAEnC,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,gBAAgB,CAAC,CAAC,CAAC;IACpD,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,CAAC,CAAC;IACT,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;CAChC;AAwBD,MAAM,MAAM,QAAQ,CAAC,CAAC,GAAG,SAAS,IAAI,MAAM,CAAC,CAAC;AAuD9C,QAAA,MAAM,QAAQ,GACZ,CAAC,EACD,MAAM,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAC5B,MAAM,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAC5B,KAAK,GAAG,SAAS,EAEjB,iDAKG,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAiD,KACzF,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CASvC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
package/lib/index.js CHANGED
@@ -1,28 +1,11 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- var actions_1 = __importDefault(require("./actions"));
18
- function setState(newState, isRef, doDigest) {
19
- if (doDigest === void 0) { doDigest = true; }
20
- var listenersLength = this.listeners.length;
21
- this.state = isRef ? newState : __assign({}, this.state, newState);
1
+ import baseActions from './actions';
2
+ function setState(newState, isRef, doDigest = true) {
3
+ const listenersLength = this.listeners.length;
4
+ this.state = isRef ? newState : { ...this.state, ...newState };
22
5
  if (!doDigest)
23
6
  return;
24
- for (var i = 0; i < listenersLength; i++) {
25
- var fn = this.listeners[i];
7
+ for (let i = 0; i < listenersLength; i++) {
8
+ const fn = this.listeners[i];
26
9
  if (!fn || typeof fn !== 'function') {
27
10
  continue;
28
11
  }
@@ -33,18 +16,17 @@ function setRef(newState, doDigest) {
33
16
  setState.call(this, newState, true, doDigest);
34
17
  }
35
18
  function useCustom(React, hookWork) {
36
- var _this = this;
37
- var newListener = React.useState()[1];
38
- var oldState = this.state;
39
- React.useEffect(function () {
40
- _this.listeners.push(newListener);
41
- if (oldState !== _this.state)
42
- newListener(_this.state);
43
- return function () {
44
- var listenersLength = _this.listeners.length;
45
- var filtered = [];
46
- for (var i = 0; i < listenersLength; i++) {
47
- var listener = _this.listeners[i];
19
+ const newListener = React.useState()[1];
20
+ const oldState = this.state;
21
+ React.useEffect(() => {
22
+ this.listeners.push(newListener);
23
+ if (oldState !== this.state)
24
+ newListener(this.state);
25
+ return () => {
26
+ const listenersLength = this.listeners.length;
27
+ const filtered = [];
28
+ for (let i = 0; i < listenersLength; i++) {
29
+ const listener = this.listeners[i];
48
30
  if (listener !== newListener) {
49
31
  filtered.push(listener);
50
32
  }
@@ -54,21 +36,21 @@ function useCustom(React, hookWork) {
54
36
  setting the listeners should come after the dust has settled.
55
37
  Using an immutable store might protect this.
56
38
  */
57
- _this.listeners = filtered;
39
+ this.listeners = filtered;
58
40
  };
59
41
  }, []);
60
- var workAdditions = [];
42
+ let workAdditions = [];
61
43
  if (hookWork) {
62
44
  workAdditions = hookWork() || [];
63
45
  }
64
- return [this.state, this.actions].concat(workAdditions);
46
+ return [this.state, this.actions, ...workAdditions];
65
47
  }
66
48
  function associateActions(store, actions) {
67
- var associatedActions = {};
68
- var actionsKeys = Object.keys(actions);
69
- var actionsKeysLength = actionsKeys.length;
70
- for (var i = 0; i < actionsKeysLength; i++) {
71
- var key = actionsKeys[i];
49
+ const associatedActions = {};
50
+ const actionsKeys = Object.keys(actions);
51
+ const actionsKeysLength = actionsKeys.length;
52
+ for (let i = 0; i < actionsKeysLength; i++) {
53
+ const key = actionsKeys[i];
72
54
  if (typeof actions[key] === 'function') {
73
55
  associatedActions[key] = actions[key].bind(null, store);
74
56
  }
@@ -78,9 +60,8 @@ function associateActions(store, actions) {
78
60
  }
79
61
  return associatedActions;
80
62
  }
81
- var useStore = function (_a) {
82
- var _b = _a === void 0 ? {} : _a, React = _b.React, initialState = _b.initialState, _c = _b.actions, actions = _c === void 0 ? actions_1.default : _c, initializer = _b.initializer;
83
- var store = { state: initialState, listeners: [] };
63
+ const useStore = ({ React, initialState, actions = baseActions, initializer, } = {}) => {
64
+ const store = { state: initialState, listeners: [] };
84
65
  store.setState = setState.bind(store);
85
66
  store.setRef = setRef.bind(store);
86
67
  store.actions = associateActions(store, actions);
@@ -89,4 +70,5 @@ var useStore = function (_a) {
89
70
  }
90
71
  return useCustom.bind(store, React);
91
72
  };
92
- exports.default = useStore;
73
+ export default useStore;
74
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,WAAmD,MAAM,WAAW,CAAC;AAkD5E,SAAS,QAAQ,CAEf,QAAW,EACX,KAAe,EACf,WAAoB,IAAI;IAExB,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;IAC9C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,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AM,QAAQ,GAAG,CAMf,EACE,KAAK,EACL,YAAY,EACZ,OAAO,GAAG,WAAW,EACrB,WAAW,MACgC,EAA6C,EAClD,EAAE;IAC1C,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,EAAE,EAAsB,CAAC;IACzE,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACtC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClC,KAAK,CAAC,OAAO,GAAG,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACjD,IAAI,WAAW,EAAE,CAAC;QAChB,WAAW,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,61 +1,50 @@
1
1
  {
2
2
  "name": "@znemz/use-global-hook",
3
- "version": "1.1.9",
4
- "repository": "github:nmccready/use-global-hook",
3
+ "version": "1.1.11",
4
+ "repository": "github:brickhouse-tech/use-global-hook",
5
5
  "license": "ISC",
6
6
  "author": "nmccready",
7
+ "originalAuthor": "andregardi",
7
8
  "files": [
8
9
  "lib",
9
- "src",
10
- "umd",
11
- "module"
10
+ "src"
12
11
  ],
12
+ "type": "module",
13
13
  "main": "lib/index.js",
14
- "module": "module/index.js",
14
+ "types": "lib/index.d.ts",
15
15
  "scripts": {
16
- "build": "yarn gulp default && yarn roll:it",
17
- "coveralls": "cat ./coverage/lcov.info | coveralls",
18
- "docs:api:markdown": "yarn typedoc --theme markdown --exclude ./src/test --out ./docs/api ./src",
19
- "docs:api:website": "yarn typedoc --exclude ./src/test --out docs ./src",
20
- "gulp": "node -r esm ./node_modules/.bin/gulp",
21
- "jest": "node -r esm ./node_modules/.bin/jest",
22
- "lint": "eslint --ext .js,.ts,.tsx *.js src --color",
23
- "lint:watch": "yarn lint --watch",
24
- "postinstall:1": "yarn js-common-editorconfig-clone",
25
- "postinstall:2": "yarn js-common-eslint-config-react-mono-clone && js-common-babel-config-clone",
26
- "postinstall:3": "yarn js-common-prettierrc-clone && yarn js-common-eslint-config-mono-clone",
27
- "prepare": "yarn postinstall:1 && yarn postinstall:2 && yarn postinstall:3 && yarn sort-package-json",
28
- "roll:it": "yarn rollup -c ./rollup.config.ts",
29
- "test": "yarn jest",
30
- "test:ci": "yarn lint && yarn test --coverage && yarn coveralls",
31
- "preversion": "yarn build"
16
+ "build": "tsc",
17
+ "lint": "eslint .",
18
+ "test": "vitest run",
19
+ "test:watch": "vitest",
20
+ "test:coverage": "vitest run --coverage",
21
+ "prepare": "npm run build"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=16.8"
32
25
  },
33
26
  "devDependencies": {
34
- "@testing-library/react-hooks": "^2.0.1",
35
- "@types/bluebird": "^3.5.27",
36
- "@types/react": "^16.8.19",
37
- "@znemz/js-common-babel-config": "^0.0.23",
38
- "@znemz/js-common-babel-config-clone": "^0.0.23",
39
- "@znemz/js-common-editorconfig-clone": "^0.0.23",
40
- "@znemz/js-common-eslint-config-mono-clone": "^0.0.23",
41
- "@znemz/js-common-eslint-config-react-mono-clone": "^0.0.23",
42
- "@znemz/js-common-gulp-monorepo-typescript": "^0.0.23",
43
- "@znemz/js-common-prettierrc-clone": "^0.0.23",
44
- "@znemz/react-extras-jest": "^1.1.0",
45
- "bluebird": "^3.5.5",
46
- "coveralls": "^3.0.4",
47
- "del": "^4.1.1",
48
- "esm": "^3.2.22",
49
- "gulp-run": "^1.7.1",
50
- "prettier": "^1.18.2",
51
- "react": "^16.8",
52
- "rollup": "^1.20.1",
53
- "rollup-plugin-typescript": "^1.0.1",
54
- "sort-package-json": "^1.21.0",
55
- "typedoc": "^0.15.0",
56
- "typedoc-plugin-markdown": "^2.1.4",
57
- "typescript": "^3.5.1"
27
+ "@commitlint/cli": "^19.0.0",
28
+ "@commitlint/config-conventional": "^19.0.0",
29
+ "@eslint/js": "^9.0.0",
30
+ "@testing-library/react": "^16.0.0",
31
+ "@types/react": "^18.0.0",
32
+ "@types/react-dom": "^18.0.0",
33
+ "@vitest/coverage-v8": "^3.0.0",
34
+ "commit-and-tag-version": "^12.0.0",
35
+ "eslint": "^9.0.0",
36
+ "eslint-plugin-n": "^17.0.0",
37
+ "globals": "^15.0.0",
38
+ "jsdom": "^25.0.0",
39
+ "react": "^18.0.0",
40
+ "react-dom": "^18.0.0",
41
+ "typescript": "^5.0.0",
42
+ "typescript-eslint": "^8.0.0",
43
+ "vitest": "^3.0.0"
58
44
  },
59
- "originalAuthor": "andregardi",
60
- "umd": "umd/index.js"
45
+ "commitlint": {
46
+ "extends": [
47
+ "@commitlint/config-conventional"
48
+ ]
49
+ }
61
50
  }
package/readme.md CHANGED
@@ -1,10 +1,20 @@
1
- # @znemz/use-global-hook [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url]
1
+ # @znemz/use-global-hook
2
2
 
3
- Easy state management for react using hooks in less than 1kb.
3
+ [![npm version](https://img.shields.io/npm/v/@znemz/use-global-hook.svg)](https://www.npmjs.com/package/@znemz/use-global-hook)
4
+ [![tests](https://github.com/brickhouse-tech/use-global-hook/actions/workflows/tests.yml/badge.svg)](https://github.com/brickhouse-tech/use-global-hook/actions/workflows/tests.yml)
5
+ [![GitHub stars](https://img.shields.io/github/stars/brickhouse-tech/use-global-hook.svg?style=social)](https://github.com/brickhouse-tech/use-global-hook)
4
6
 
5
- ---
7
+ **Easy state management for React using hooks — less than 1KB.**
6
8
 
7
- ## Minimal example:
9
+ No Redux. No Context boilerplate. Just a hook that shares state across components.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install @znemz/use-global-hook
15
+ ```
16
+
17
+ ## Quick Start
8
18
 
9
19
  ```javascript
10
20
  import React from 'react';
@@ -27,10 +37,7 @@ const App = () => {
27
37
  const [globalState, globalActions] = useGlobal();
28
38
  return (
29
39
  <div>
30
- <p>
31
- counter:
32
- {globalState.counter}
33
- </p>
40
+ <p>counter: {globalState.counter}</p>
34
41
  <button type="button" onClick={() => globalActions.addToCounter(1)}>
35
42
  +1 to global
36
43
  </button>
@@ -41,42 +48,69 @@ const App = () => {
41
48
  export default App;
42
49
  ```
43
50
 
44
- ---
51
+ ## Advanced Usage
52
+
53
+ ### `setRef` vs `setState`
54
+
55
+ Sometimes you need to set a reference and wipe the state clean. Common use cases:
56
+
57
+ - **Reset / wipe** — clear state without triggering re-renders
58
+ - **Tracking DOM elements** — `ref={setRef}`
59
+
60
+ ```javascript
61
+ const useGlobal = useGlobalHook({ React, initialState: { data: null } });
62
+
63
+ // In a component:
64
+ const [state, actions] = useGlobal();
65
+
66
+ // setState merges (like React's setState)
67
+ actions.setState({ data: { key: 'value' } });
68
+
69
+ // setRef replaces the entire reference
70
+ actions.setRef({ completely: 'new object' });
71
+ ```
72
+
73
+ ### Why React Injection?
74
+
75
+ This library accepts React as a parameter, which allows:
76
+
77
+ - Support for different React implementations
78
+ - Use of alternative hook implementations (e.g. [reinspect](https://github.com/troch/reinspect) for Redux DevTools integration)
79
+ - Testing with mock React objects
45
80
 
46
- ## Complete examples:
81
+ ## API
47
82
 
48
- ### [Several counters, one value](https://codesandbox.io/s/v6zz2nwow5 'CodeSandBox')
83
+ ### `useGlobalHook({ React, initialState, actions })`
49
84
 
50
- Add as many counters as you want, it will all share the same global value.
51
- Every time one counter add 1 to the global value, all counters will render.
52
- The parent component won't render again.
85
+ Returns a hook function that provides `[state, actions]` when called in a component.
53
86
 
54
- ---
87
+ | Parameter | Type | Description |
88
+ |-----------|------|-------------|
89
+ | `React` | object | Your React instance |
90
+ | `initialState` | object | Initial global state |
91
+ | `actions` | object | Action functions receiving `(store, ...args)` |
55
92
 
56
- ### [Asynchronous ajax requests](https://codesandbox.io/s/wqvykj5497 'CodeSandBox')
93
+ ### Store Methods
57
94
 
58
- Search GitHub repos by username.
59
- Handle the ajax request asynchronously with async/await.
60
- Update the requests counter on every search.
95
+ | Method | Description |
96
+ |--------|-------------|
97
+ | `store.setState(newState)` | Merge new state and trigger re-renders |
98
+ | `store.setRef(newRef)` | Replace state reference and trigger re-renders |
61
99
 
62
- ## [ setRef vs setState ](https://codepen.io/nmccready/pen/vYBgrGR)
100
+ ## Contributing
63
101
 
64
- Sometimes you just need to set a reference and wipe the state clean.
102
+ Contributions are welcome! Please open an issue or submit a pull request.
65
103
 
66
- There are many use cases:
104
+ 1. Fork the repo
105
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
106
+ 3. Commit your changes (using [conventional commits](https://www.conventionalcommits.org/))
107
+ 4. Push to the branch
108
+ 5. Open a Pull Request
67
109
 
68
- - reset / wipe
69
- - tracking dom elements `ref={setRef}`
110
+ ## Sponsor
70
111
 
71
- ## Why do we allow React Injection?
112
+ If you find this project useful, consider [sponsoring @nmccready](https://github.com/sponsors/nmccready) to support ongoing maintenance and development. ❤️
72
113
 
73
- - to support different React implementation
74
- - to allow different versions of useEffect, useState or other hooks
75
- - say to allow inspection for redux-devtools see [reinspect](https://github.com/troch/reinspect) to debug your global hooks
114
+ ## License
76
115
 
77
- [npm-image]: https://img.shields.io/npm/v/@znemz/use-global-hook.svg
78
- [npm-url]: https://www.npmjs.com/package/@znemz/use-global-hook
79
- [travis-image]: https://img.shields.io/travis/nmccready/use-global-hook.svg
80
- [travis-url]: https://travis-ci.org/nmccready/use-global-hook
81
- [coveralls-image]: https://coveralls.io/repos/github/nmccready/use-global-hook/badge.svg
82
- [coveralls-url]: https://coveralls.io/github/nmccready/use-global-hook?branch=master
116
+ See [LICENSE](./LICENSE) for details.
@@ -1,8 +1,6 @@
1
1
  import React, { useEffect } from 'react';
2
- import Promise from 'bluebird';
3
- import 'jest-extended';
4
- import 'jest-expect-message';
5
- import { renderHook, act } from '@testing-library/react-hooks';
2
+ import { describe, it, expect, vi } from 'vitest';
3
+ import { renderHook, act } from '@testing-library/react';
6
4
 
7
5
  import useGlobalHook, { HookWork, Store } from '..';
8
6
  import {
@@ -64,47 +62,6 @@ describe('useGlobalHook', () => {
64
62
  expect(result.current[0].a).toEqual('a');
65
63
  expect(result2.current[0].a).toEqual('a');
66
64
  });
67
-
68
- it('works - multi destroy chase -- continue', () => {
69
- const useTest = useGlobalHook<SomeState>({ React, initialState: { a: undefined } });
70
- const { unmount } = renderHook(() => useTest());
71
- const promiseFns = [];
72
- for (let i; i < 20; i++) {
73
- promiseFns.push(() =>
74
- Promise.resolve(
75
- renderHook(() => {
76
- const hook = useTest();
77
- if (i === 10) {
78
- // TODO: somehow produce the race condition
79
- // TODO which will hit continue in setState
80
- // attempt to call setState while listeners are being added
81
- hook[1].setState({ a: 'b' });
82
- }
83
- return hook;
84
- })
85
- )
86
- );
87
- }
88
-
89
- const race = Promise.map(promiseFns, (fn) => fn());
90
-
91
- const promise = Promise.join(
92
- race,
93
- // @ts-ignore
94
- Promise.delay(10).then(() => {
95
- unmount();
96
- })
97
- );
98
-
99
- const { result: result2 } = renderHook(() => useTest());
100
-
101
- act(() => {
102
- result2.current[1].setState({ a: 'a' });
103
- });
104
- return promise.then(() => {
105
- expect(result2.current[0].a).toEqual('a');
106
- });
107
- });
108
65
  });
109
66
 
110
67
  describe('setRef', () => {
@@ -117,7 +74,7 @@ describe('useGlobalHook', () => {
117
74
  result.current[1].setRef(ref);
118
75
  });
119
76
 
120
- expect(result.current[0], 'checks for reference not deep equal').toBe(ref);
77
+ expect(result.current[0]).toBe(ref);
121
78
  });
122
79
 
123
80
  it('skip digest', () => {
@@ -140,7 +97,7 @@ describe('useGlobalHook', () => {
140
97
  it('hookWork is called', () => {
141
98
  const useTest = useGlobalHook<SomeState>({ React });
142
99
 
143
- const hookWork = jest.fn();
100
+ const hookWork = vi.fn();
144
101
 
145
102
  renderHook(() => {
146
103
  useTest(hookWork);
@@ -152,8 +109,8 @@ describe('useGlobalHook', () => {
152
109
  it('hookWork is called, other Hooks', () => {
153
110
  const useTest = useGlobalHook<SomeState>({ React });
154
111
 
155
- const effectFn = jest.fn();
156
- const hookWork = jest.fn(() => {
112
+ const effectFn = vi.fn();
113
+ const hookWork = vi.fn(() => {
157
114
  useEffect(() => {
158
115
  effectFn();
159
116
  }, []);
package/module/index.js DELETED
@@ -1,110 +0,0 @@
1
- /*! *****************************************************************************
2
- Copyright (c) Microsoft Corporation. All rights reserved.
3
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
- this file except in compliance with the License. You may obtain a copy of the
5
- License at http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10
- MERCHANTABLITY OR NON-INFRINGEMENT.
11
-
12
- See the Apache Version 2.0 License for specific language governing permissions
13
- and limitations under the License.
14
- ***************************************************************************** */
15
-
16
- var __assign = function() {
17
- __assign = Object.assign || function __assign(t) {
18
- for (var s, i = 1, n = arguments.length; i < n; i++) {
19
- s = arguments[i];
20
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
21
- }
22
- return t;
23
- };
24
- return __assign.apply(this, arguments);
25
- };
26
-
27
- var setState = function (store, newState, doDigest) {
28
- store.setState(newState, false, doDigest);
29
- };
30
- var setRef = function (store, newState, doDigest) {
31
- store.setRef(newState, doDigest);
32
- };
33
- var actions = { setState: setState, setRef: setRef };
34
-
35
- function setState$1(newState, isRef, doDigest) {
36
- if (doDigest === void 0) { doDigest = true; }
37
- var listenersLength = this.listeners.length;
38
- this.state = isRef ? newState : __assign({}, this.state, newState);
39
- if (!doDigest)
40
- return;
41
- for (var i = 0; i < listenersLength; i++) {
42
- var fn = this.listeners[i];
43
- if (!fn || typeof fn !== 'function') {
44
- continue;
45
- }
46
- fn(this.state);
47
- }
48
- }
49
- function setRef$1(newState, doDigest) {
50
- setState$1.call(this, newState, true, doDigest);
51
- }
52
- function useCustom(React, hookWork) {
53
- var _this = this;
54
- var newListener = React.useState()[1];
55
- var oldState = this.state;
56
- React.useEffect(function () {
57
- _this.listeners.push(newListener);
58
- if (oldState !== _this.state)
59
- newListener(_this.state);
60
- return function () {
61
- var listenersLength = _this.listeners.length;
62
- var filtered = [];
63
- for (var i = 0; i < listenersLength; i++) {
64
- var listener = _this.listeners[i];
65
- if (listener !== newListener) {
66
- filtered.push(listener);
67
- }
68
- }
69
- /*
70
- This is dangerous as we could be in the middle of calling setState or setRef.
71
- setting the listeners should come after the dust has settled.
72
- Using an immutable store might protect this.
73
- */
74
- _this.listeners = filtered;
75
- };
76
- }, []);
77
- var workAdditions = [];
78
- if (hookWork) {
79
- workAdditions = hookWork() || [];
80
- }
81
- return [this.state, this.actions].concat(workAdditions);
82
- }
83
- function associateActions(store, actions) {
84
- var associatedActions = {};
85
- var actionsKeys = Object.keys(actions);
86
- var actionsKeysLength = actionsKeys.length;
87
- for (var i = 0; i < actionsKeysLength; i++) {
88
- var key = actionsKeys[i];
89
- if (typeof actions[key] === 'function') {
90
- associatedActions[key] = actions[key].bind(null, store);
91
- }
92
- if (typeof actions[key] === 'object') {
93
- associatedActions[key] = associateActions(store, actions[key]);
94
- }
95
- }
96
- return associatedActions;
97
- }
98
- var useStore = function (_a) {
99
- var _b = _a === void 0 ? {} : _a, React = _b.React, initialState = _b.initialState, _c = _b.actions, actions$1 = _c === void 0 ? actions : _c, initializer = _b.initializer;
100
- var store = { state: initialState, listeners: [] };
101
- store.setState = setState$1.bind(store);
102
- store.setRef = setRef$1.bind(store);
103
- store.actions = associateActions(store, actions$1);
104
- if (initializer) {
105
- initializer(store);
106
- }
107
- return useCustom.bind(store, React);
108
- };
109
-
110
- export default useStore;
package/umd/index.js DELETED
@@ -1,118 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = global || self, global.UseGlobalHook = factory());
5
- }(this, function () { 'use strict';
6
-
7
- /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation. All rights reserved.
9
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
10
- this file except in compliance with the License. You may obtain a copy of the
11
- License at http://www.apache.org/licenses/LICENSE-2.0
12
-
13
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16
- MERCHANTABLITY OR NON-INFRINGEMENT.
17
-
18
- See the Apache Version 2.0 License for specific language governing permissions
19
- and limitations under the License.
20
- ***************************************************************************** */
21
-
22
- var __assign = function() {
23
- __assign = Object.assign || function __assign(t) {
24
- for (var s, i = 1, n = arguments.length; i < n; i++) {
25
- s = arguments[i];
26
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
27
- }
28
- return t;
29
- };
30
- return __assign.apply(this, arguments);
31
- };
32
-
33
- var setState = function (store, newState, doDigest) {
34
- store.setState(newState, false, doDigest);
35
- };
36
- var setRef = function (store, newState, doDigest) {
37
- store.setRef(newState, doDigest);
38
- };
39
- var actions = { setState: setState, setRef: setRef };
40
-
41
- function setState$1(newState, isRef, doDigest) {
42
- if (doDigest === void 0) { doDigest = true; }
43
- var listenersLength = this.listeners.length;
44
- this.state = isRef ? newState : __assign({}, this.state, newState);
45
- if (!doDigest)
46
- return;
47
- for (var i = 0; i < listenersLength; i++) {
48
- var fn = this.listeners[i];
49
- if (!fn || typeof fn !== 'function') {
50
- continue;
51
- }
52
- fn(this.state);
53
- }
54
- }
55
- function setRef$1(newState, doDigest) {
56
- setState$1.call(this, newState, true, doDigest);
57
- }
58
- function useCustom(React, hookWork) {
59
- var _this = this;
60
- var newListener = React.useState()[1];
61
- var oldState = this.state;
62
- React.useEffect(function () {
63
- _this.listeners.push(newListener);
64
- if (oldState !== _this.state)
65
- newListener(_this.state);
66
- return function () {
67
- var listenersLength = _this.listeners.length;
68
- var filtered = [];
69
- for (var i = 0; i < listenersLength; i++) {
70
- var listener = _this.listeners[i];
71
- if (listener !== newListener) {
72
- filtered.push(listener);
73
- }
74
- }
75
- /*
76
- This is dangerous as we could be in the middle of calling setState or setRef.
77
- setting the listeners should come after the dust has settled.
78
- Using an immutable store might protect this.
79
- */
80
- _this.listeners = filtered;
81
- };
82
- }, []);
83
- var workAdditions = [];
84
- if (hookWork) {
85
- workAdditions = hookWork() || [];
86
- }
87
- return [this.state, this.actions].concat(workAdditions);
88
- }
89
- function associateActions(store, actions) {
90
- var associatedActions = {};
91
- var actionsKeys = Object.keys(actions);
92
- var actionsKeysLength = actionsKeys.length;
93
- for (var i = 0; i < actionsKeysLength; i++) {
94
- var key = actionsKeys[i];
95
- if (typeof actions[key] === 'function') {
96
- associatedActions[key] = actions[key].bind(null, store);
97
- }
98
- if (typeof actions[key] === 'object') {
99
- associatedActions[key] = associateActions(store, actions[key]);
100
- }
101
- }
102
- return associatedActions;
103
- }
104
- var useStore = function (_a) {
105
- var _b = _a === void 0 ? {} : _a, React = _b.React, initialState = _b.initialState, _c = _b.actions, actions$1 = _c === void 0 ? actions : _c, initializer = _b.initializer;
106
- var store = { state: initialState, listeners: [] };
107
- store.setState = setState$1.bind(store);
108
- store.setRef = setRef$1.bind(store);
109
- store.actions = associateActions(store, actions$1);
110
- if (initializer) {
111
- initializer(store);
112
- }
113
- return useCustom.bind(store, React);
114
- };
115
-
116
- return useStore;
117
-
118
- }));