Skip to content
This repository was archived by the owner on May 20, 2022. It is now read-only.

Commit be6a84e

Browse files
committed
using 'data' instead of the misleading 'action' on reducers
1 parent 26e07a0 commit be6a84e

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StoreInterface {
1818

1919
/**
2020
* Subscribe to store changes
21-
* @param {(state:any, action:any) => void} callback - The function to be invoked everytime the store is updated
21+
* @param {(state:any, data:any) => void} callback - The function to be invoked everytime the store is updated
2222
* @return {Function} - Call the function returned by the method to cancel the subscription
2323
*/
2424
subscribe(callback) {
@@ -35,11 +35,18 @@ class StoreInterface {
3535
}
3636
}
3737

38-
setState() {
38+
/**
39+
* Set the store state
40+
* @param {any} data - The new state value.
41+
*/
42+
setState(data) {
3943
console.warn(`[React Hookstore] Store ${this.name} uses a reducer to handle its state updates. use dispatch instead of setState`)
4044
}
41-
42-
dispatch() {
45+
/**
46+
* Dispatch data to the store reducer
47+
* @param {any} data - The data payload the reducer receives
48+
*/
49+
dispatch(data) {
4350
console.warn(`[React Hookstore] Store ${this.name} does not use a reducer to handle state updates. use setState instead of dispatch`)
4451
}
4552
}
@@ -56,7 +63,7 @@ function getStoreByIdentifier(identifier) {
5663
* Creates a new store
5764
* @param {String} name - The store namespace.
5865
* @param {*} state [{}] - The store initial state. It can be of any type.
59-
* @param {(state:any, action:any) => any} reducer [null] - The reducer handler. Optional
66+
* @param {(state:any, data:any) => any} reducer [null] - The reducer handler. Optional
6067
* @returns {StoreInterface} The store instance.
6168
*/
6269
export function createStore(name, state = {}, reducer=defaultReducer) {
@@ -66,23 +73,24 @@ export function createStore(name, state = {}, reducer=defaultReducer) {
6673
if (stores[name]) {
6774
throw `Store with name ${name} already exists`;
6875
}
76+
6977

7078
const store = {
7179
state,
7280
reducer,
73-
setState(action, callback) {
81+
setState(data, callback) {
7482
const isPrimitiveStateWithoutReducerAndIsPreviousState =
7583
this.reducer === defaultReducer
76-
&& action === this.state
77-
&& typeof action !== 'object';
84+
&& data === this.state
85+
&& typeof data !== 'object';
7886

7987
if (isPrimitiveStateWithoutReducerAndIsPreviousState) {
8088
if (typeof callback === 'function') callback(this.state)
8189
return;
8290
}
8391

8492
const currentState = this.state;
85-
const newState = this.reducer(this.state, action);
93+
const newState = this.reducer(this.state, data);
8694
this.state = newState;
8795

8896
this.updatersPerMemoFunction.forEach((updaters, memoFn) => {
@@ -97,7 +105,7 @@ export function createStore(name, state = {}, reducer=defaultReducer) {
97105
});
98106

99107
if (subscriptions[name].length) {
100-
subscriptions[name].forEach(c => c(this.state, action));
108+
subscriptions[name].forEach(c => c(this.state, data));
101109
}
102110

103111
if (typeof callback === 'function') callback(this.state)

types/index.d.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
declare module 'react-hookstore' {
22
type StateCallback<TState> = (state: TState) => void;
33

4-
type ReducerType<TState, TPayload = any> = (state: TState, payload: TPayload) => TState;
4+
type ReducerType<TState, TData = any> = (state: TState, data: TData) => TState;
55

66
type SetStateType<TState> = (state: TState, callback?: StateCallback<TState>) => void;
77

8-
type DispatchType<TState, TPayload = any> = (payload: TPayload, callback?: StateCallback<TState>) => void;
8+
type DispatchType<TState, TData = any> = (data: TData, callback?: StateCallback<TState>) => void;
99

1010
type StoreStateHookType<TState> = [TState, SetStateType<TState>];
1111

12-
type StoreDispatchHookType<TState, TPayload = any> = [TState, DispatchType<TState, TPayload>];
12+
type StoreDispatchHookType<TState, TData = any> = [TState, DispatchType<TState, TData>];
1313

1414
const defaultReducer: ReducerType<any>;
1515

16-
export interface StoreSpec<TState, TPayload = any> {
16+
export interface StoreSpec<TState, TData = any> {
1717
state: TState;
18-
reducer: ReducerType<TState, TPayload>;
19-
setState: SetStateType<TState> | DispatchType<TState, TPayload>;
18+
reducer: ReducerType<TState, TData>;
19+
setState: SetStateType<TState> | DispatchType<TState, TData>;
2020
setters: StateCallback<TState>[]
2121
}
2222

@@ -26,27 +26,27 @@ declare module 'react-hookstore' {
2626
setState(state: TState, callback?: StateCallback<TState>): void;
2727
}
2828

29-
export interface ReducerStoreInterface<TState, TPayload = any> {
29+
export interface ReducerStoreInterface<TState, TData = any> {
3030
readonly name: string;
3131
getState(): TState;
32-
dispatch<TPayload>(payload: TPayload, callback?: StateCallback<TState>): void;
32+
dispatch<TData>(data: TData, callback?: StateCallback<TState>): void;
3333
}
3434

35-
export function createStore<TState, TPayload = any>(name: string, state: TState, reducer: ReducerType<TState, TPayload>): ReducerStoreInterface<TState, TPayload>;
35+
export function createStore<TState, TData = any>(name: string, state: TState, reducer: ReducerType<TState, TData>): ReducerStoreInterface<TState, TData>;
3636

3737
export function createStore<TState>(name: string, state: TState): StateStoreInterface<TState>;
3838

3939
export function createStore<TState>(name: string, state: TState, reducer: ReducerType<TState>): ReducerStoreInterface<TState>;
4040

41-
export function getStoreByName<TState, TPayload = any>(name: string): StateStoreInterface<TState> | ReducerStoreInterface<TState>;
41+
export function getStoreByName<TState, TData = any>(name: string): StateStoreInterface<TState> | ReducerStoreInterface<TState>;
4242

4343
export function getStoreByName<TState>(name: string): StateStoreInterface<TState> | ReducerStoreInterface<TState>;
4444

4545
export function useStore<TState>(identifier: string): StoreStateHookType<TState> | StoreDispatchHookType<TState>;
4646

47-
export function useStore<TState, TPayload = any>(identifier: string): StoreDispatchHookType<TState, TPayload>;
47+
export function useStore<TState, TData = any>(identifier: string): StoreDispatchHookType<TState, TData>;
4848

4949
export function useStore<TState>(store: StateStoreInterface<TState>): StoreStateHookType<TState>;
5050

51-
export function useStore<TState, TPayload = any>(store: ReducerStoreInterface<TState, TPayload>): StoreDispatchHookType<TState, TPayload>;
51+
export function useStore<TState, TData = any>(store: ReducerStoreInterface<TState, TData>): StoreDispatchHookType<TState, TData>;
5252
}

0 commit comments

Comments
 (0)