reactish-state 1.1.1 → 1.2.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.
package/README.md CHANGED
@@ -648,8 +648,6 @@ const myTodos = state<string[], { add: (newTodo: string) => void }>(
648
648
  );
649
649
  ```
650
650
 
651
- However, if you choose this method, you need to specify the types for both the state value and actions.
652
-
653
651
  # Examples
654
652
 
655
653
  - Counter – [sandbox](https://codesandbox.io/p/sandbox/reactish-counter-z42qt7) | [source](https://github.com/szhsin/reactish-state/tree/master/examples/examples/counter)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reactish-state",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Simple, decentralized (atomic) state management for React.",
5
5
  "author": "Zheng Song",
6
6
  "license": "MIT",
@@ -83,22 +83,22 @@
83
83
  "use-sync-external-store": "^1.5.0"
84
84
  },
85
85
  "devDependencies": {
86
- "@babel/core": "^7.28.0",
87
- "@babel/preset-env": "^7.28.0",
86
+ "@babel/core": "^7.28.3",
87
+ "@babel/preset-env": "^7.28.3",
88
88
  "@babel/preset-react": "^7.27.1",
89
89
  "@babel/preset-typescript": "^7.27.1",
90
90
  "@redux-devtools/extension": "^3.3.0",
91
91
  "@rollup/plugin-babel": "^6.0.4",
92
92
  "@rollup/plugin-node-resolve": "^16.0.1",
93
93
  "@rollup/plugin-replace": "^6.0.2",
94
- "@testing-library/jest-dom": "^6.6.4",
94
+ "@testing-library/jest-dom": "^6.7.0",
95
95
  "@testing-library/react": "^16.3.0",
96
96
  "@types/jest": "^30.0.0",
97
- "@types/react": "^19.1.9",
97
+ "@types/react": "^19.1.10",
98
98
  "@types/use-sync-external-store": "^1.5.0",
99
99
  "babel-plugin-pure-annotations": "^0.1.2",
100
100
  "deplift": "^1.0.1",
101
- "eslint": "^9.32.0",
101
+ "eslint": "^9.33.0",
102
102
  "eslint-config-prettier": "^10.1.8",
103
103
  "eslint-plugin-jest": "^29.0.1",
104
104
  "eslint-plugin-react": "^7.37.5",
@@ -113,7 +113,7 @@
113
113
  "react": "^19.1.1",
114
114
  "react-dom": "^19.1.1",
115
115
  "rollup": "^4.46.2",
116
- "typescript": "^5.8.3",
117
- "typescript-eslint": "^8.38.0"
116
+ "typescript": "^5.9.2",
117
+ "typescript-eslint": "^8.39.1"
118
118
  }
119
119
  }
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './common';
1
+ export * from './types';
2
2
  export * from './vanilla/state';
3
3
  export * from './vanilla/selector';
4
4
  export * from './react/useSnapshot';
@@ -1,5 +1,5 @@
1
- import type { Middleware } from '../common';
2
- declare const applyMiddleware: (middlewares: (Middleware | undefined)[], options?: {
1
+ import type { Middleware } from '../types';
2
+ declare const applyMiddleware: <TConfig>(middlewares: (Middleware<TConfig> | undefined)[], options?: {
3
3
  fromRight?: boolean;
4
- }) => Middleware;
4
+ }) => Middleware<TConfig>;
5
5
  export { applyMiddleware };
@@ -1,3 +1,3 @@
1
- import type { Middleware } from '../common';
1
+ import type { Middleware } from '../types';
2
2
  declare const immer: Middleware;
3
3
  export { immer };
@@ -1,5 +1,5 @@
1
- import type { Middleware } from '../common';
2
- interface PersistMiddleware extends Middleware {
1
+ import type { Middleware, Config } from '../types';
2
+ interface PersistMiddleware extends Middleware<Config> {
3
3
  hydrate(this: void): void;
4
4
  }
5
5
  type Persist = (options?: {
@@ -1,7 +1,7 @@
1
- import type { Middleware } from '../common';
1
+ import type { Middleware, Config } from '../types';
2
2
  type ReduxDevtools = (options?: {
3
3
  name?: string;
4
- }) => Middleware | undefined;
4
+ }) => Middleware<Config> | undefined;
5
5
  declare const reduxDevtools: ReduxDevtools;
6
6
  export type { ReduxDevtools };
7
7
  export { reduxDevtools };
@@ -1,3 +1,3 @@
1
- import type { Plugin } from '../common';
2
- declare const applyPlugin: (plugins: (Plugin | undefined)[]) => Plugin;
1
+ import type { Plugin } from '../types';
2
+ declare const applyPlugin: <TConfig>(plugins: (Plugin<TConfig> | undefined)[]) => Plugin<TConfig>;
3
3
  export { applyPlugin };
@@ -1,7 +1,7 @@
1
- import type { Plugin } from '../common';
1
+ import type { Plugin, Config } from '../types';
2
2
  type ReduxDevtools = (options?: {
3
3
  name?: string;
4
- }) => Plugin | undefined;
4
+ }) => Plugin<Config> | undefined;
5
5
  declare const reduxDevtools: ReduxDevtools;
6
6
  export type { ReduxDevtools };
7
7
  export { reduxDevtools };
@@ -1,3 +1,3 @@
1
- import type { SelectorArray, SelectorParams } from '../common';
2
- declare const useSelector: <TArray extends SelectorArray, T>(selectorParamFactory: () => SelectorParams<TArray, T>, deps?: unknown[]) => T;
1
+ import type { SelectorArray, SelectorParams } from '../types';
2
+ declare const useSelector: <TArray extends SelectorArray, TValue>(selectorParamFactory: () => SelectorParams<TArray, TValue>, deps?: unknown[]) => TValue;
3
3
  export { useSelector };
@@ -1,3 +1,3 @@
1
- import type { Selector } from '../common';
2
- declare const useSnapshot: <T>({ subscribe, get }: Selector<T>) => T;
1
+ import type { Selector } from '../types';
2
+ declare const useSnapshot: <TValue>({ subscribe, get }: Selector<TValue>) => TValue;
3
3
  export { useSnapshot };
@@ -0,0 +1,46 @@
1
+ export type Getter<TValue> = () => TValue;
2
+ export type Setter<TValue, TContext = unknown> = (newValue: TValue | ((value: TValue) => TValue), context?: TContext) => void;
3
+ export type Unsubscriber = () => void;
4
+ export type StateListener<TValue> = (nextValue: TValue, prevValue: TValue) => void;
5
+ export type StateSubscriber<TValue> = (listener: StateListener<TValue>) => Unsubscriber;
6
+ export interface State<TValue, TContext = unknown> {
7
+ get: Getter<TValue>;
8
+ set: Setter<TValue, TContext>;
9
+ subscribe: StateSubscriber<TValue>;
10
+ }
11
+ export type StateWithAction<TValue, TAction, TContext = unknown> = Omit<TAction, keyof State<TValue, TContext>> & State<TValue, TContext>;
12
+ export type ActionBuilder<TValue, TAction, TContext = unknown> = (set: Setter<TValue, TContext>, get: () => TValue) => TAction;
13
+ export interface StateBuilder<TConfig = unknown> {
14
+ <TValue, TContext = unknown>(): State<TValue | undefined, TContext>;
15
+ <TValue, TContext = unknown>(initialValue: TValue): State<TValue, TContext>;
16
+ <TValue, TContext = unknown>(initialValue: TValue, actionBuilder: null | undefined, config?: TConfig): State<TValue, TContext>;
17
+ <TValue, TAction, TContext = unknown>(initialValue: TValue, actionBuilder: ActionBuilder<TValue, TAction, TContext>, config?: TConfig): StateWithAction<TValue, TAction, TContext>;
18
+ }
19
+ export type SelectorListener = () => void;
20
+ export type SelectorSubscriber = (listener: SelectorListener) => Unsubscriber;
21
+ export interface Selector<TValue> {
22
+ get: Getter<TValue>;
23
+ subscribe: SelectorSubscriber;
24
+ }
25
+ export interface Config {
26
+ key?: string;
27
+ }
28
+ export interface Middleware<TConfig = unknown> {
29
+ <TValue, TContext = unknown>(state: State<TValue, TContext>, config?: TConfig): Setter<TValue, TContext>;
30
+ }
31
+ export interface Plugin<TConfig = unknown> {
32
+ <TValue>(selector: Selector<TValue>, config?: TConfig): void;
33
+ }
34
+ export type SelectorArray = Selector<unknown>[];
35
+ export type SelectorValueArray<TArray extends SelectorArray> = {
36
+ [index in keyof TArray]: ReturnType<TArray[index]['get']>;
37
+ };
38
+ export type SelectorFunc<TArray extends SelectorArray, TValue> = (...args: SelectorValueArray<TArray>) => TValue;
39
+ export type SelectorParams<TArray extends SelectorArray, TValue> = [
40
+ ...TArray,
41
+ SelectorFunc<TArray, TValue>
42
+ ];
43
+ export interface SelectorBuilder<TConfig = unknown> {
44
+ <TArray extends SelectorArray, TValue>(...items: SelectorParams<TArray, TValue>): Selector<TValue>;
45
+ <TArray extends SelectorArray, TValue>(...items: [...SelectorParams<TArray, TValue>, TConfig]): Selector<TValue>;
46
+ }
package/types/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SelectorSubscriber, SelectorArray, SelectorValueArray } from './common';
1
+ import type { SelectorSubscriber, SelectorArray, SelectorValueArray } from './types';
2
2
  export declare const isEqual: (args1: unknown[], args2: unknown[]) => boolean;
3
3
  export declare const createSubscriber: (items: SelectorArray) => SelectorSubscriber;
4
4
  export declare const getSelectorValues: <RA extends SelectorArray>(items: SelectorArray) => SelectorValueArray<RA>;
@@ -1,6 +1,6 @@
1
- import type { Plugin, SelectorBuilder } from '../common';
2
- declare const createSelector: ({ plugin }?: {
3
- plugin?: Plugin;
4
- }) => SelectorBuilder;
5
- declare const selector: SelectorBuilder;
1
+ import type { Plugin, SelectorBuilder } from '../types';
2
+ declare const createSelector: <TConfig>({ plugin }?: {
3
+ plugin?: Plugin<TConfig>;
4
+ }) => SelectorBuilder<TConfig>;
5
+ declare const selector: SelectorBuilder<unknown>;
6
6
  export { selector, createSelector };
@@ -1,7 +1,6 @@
1
- import type { ActionBuilder, StateWithAction, Config, Middleware } from '../common';
2
- declare const createState: ({ middleware }?: {
3
- middleware?: Middleware;
4
- }) => <T, A>(initialValue: T, actionBuilder?: ActionBuilder<T, A>, config?: Config) => StateWithAction<T, A>;
5
- declare const state: <T, A>(initialValue: T, actionBuilder?: ActionBuilder<T, A>, config?: Config) => StateWithAction<T, A>;
6
- type StateBuilder = typeof state;
7
- export { state, createState, type StateBuilder };
1
+ import type { StateBuilder, Middleware } from '../types';
2
+ declare const createState: <TConfig>({ middleware }?: {
3
+ middleware?: Middleware<TConfig>;
4
+ }) => StateBuilder<TConfig>;
5
+ declare const state: StateBuilder<unknown>;
6
+ export { state, createState };
package/types/common.d.ts DELETED
@@ -1,37 +0,0 @@
1
- export type Getter<T> = () => T;
2
- export type Setter<T> = (newValue: T | ((value: T) => T), context?: unknown) => void;
3
- export type Unsubscriber = () => void;
4
- export type StateListener<T> = (nextValue: T, prevValue: T) => void;
5
- export type StateSubscriber<T> = (listener: StateListener<T>) => Unsubscriber;
6
- export interface State<T> {
7
- get: Getter<T>;
8
- set: Setter<T>;
9
- subscribe: StateSubscriber<T>;
10
- }
11
- export type ActionBuilder<T, A> = ((set: Setter<T>, get: () => T) => A) | null | undefined;
12
- export type StateWithAction<T, A> = Omit<A, keyof State<T>> & State<T>;
13
- export type SelectorListener = () => void;
14
- export type SelectorSubscriber = (listener: SelectorListener) => Unsubscriber;
15
- export interface Selector<T> {
16
- get: Getter<T>;
17
- subscribe: SelectorSubscriber;
18
- }
19
- export interface Config {
20
- key?: string;
21
- }
22
- export interface Middleware {
23
- <T>(state: State<T>, config?: Config): Setter<T>;
24
- }
25
- export interface Plugin {
26
- <T>(selector: Selector<T>, config?: Config): void;
27
- }
28
- export type SelectorArray = Selector<unknown>[];
29
- export type SelectorValueArray<TArray extends SelectorArray> = {
30
- [index in keyof TArray]: ReturnType<TArray[index]['get']>;
31
- };
32
- export type SelectorFunc<TArray extends SelectorArray, T> = (...args: SelectorValueArray<TArray>) => T;
33
- export type SelectorParams<TArray extends SelectorArray, T> = [...TArray, SelectorFunc<TArray, T>];
34
- export interface SelectorBuilder {
35
- <RA extends SelectorArray, T>(...items: SelectorParams<RA, T>): Selector<T>;
36
- <RA extends SelectorArray, T>(...items: [...SelectorParams<RA, T>, Config]): Selector<T>;
37
- }