-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (155 loc) · 5.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable */
import { useState, useEffect } from "react";
let stores = {};
let subscriptions = {};
const defaultReducer = (state, payload) => payload;
/** The public interface of a store */
class StoreInterface {
constructor(name, store, useReducer) {
this.name = name;
useReducer
? (this.dispatch = store.setState)
: (this.setState = store.setState);
this.getState = () => store.state;
this.subscribe = this.subscribe.bind(this);
}
/**
* Subscribe to store changes
* @callback callback - The function to be invoked everytime the store is updated
* @return {Function} - Call the function returned by the method to cancel the subscription
*/
/**
*
* @param {callback} state, action
*/
subscribe(callback) {
if (!callback || typeof callback !== "function") {
throw new Error(
`store.subscribe callback argument must be a function. got '${typeof callback}' instead.`
);
}
if (subscriptions[this.name].find(c => c === callback)) {
console.warn(
"This callback is already subscribed to this store. skipping subscription"
);
return;
}
subscriptions[this.name].push(callback);
return () => {
subscriptions[this.name] = subscriptions[this.name].filter(
c => c !== callback
);
};
}
setState() {
console.warn(
`[React Hookstore] Store ${this.name} uses a reducer to handle its state updates. use dispatch instead of setState`
);
}
dispatch() {
console.warn(
`[React Hookstore] Store ${this.name} does not use a reducer to handle state updates. use setState instead of dispatch`
);
}
}
function getStoreByIdentifier(identifier) {
const name =
identifier instanceof StoreInterface ? identifier.name : identifier;
if (!stores[name]) {
throw new Error(`Store with name ${name} does not exist`);
}
return stores[name];
}
/**
* Creates a new store
* @param {String} name - The store namespace.
* @param {*} state [{}] - The store initial state. It can be of any type.
* @callback reducer [null]
* @returns {StoreInterface} The store instance.
*/
/**
*
* @param {reducer} prevState, action - The reducer handler. Optional.
*/
export function createStore(name, state = {}, reducer = defaultReducer) {
if (typeof name !== "string") {
throw new Error("Store name must be a string");
}
if (stores[name]) {
return;
}
const store = {
state,
reducer,
setState(action, callback) {
this.state = this.reducer(this.state, action);
this.setters.forEach(setter => setter(this.state));
if (subscriptions[name].length) {
subscriptions[name].forEach(c => c(this.state, action));
}
if (typeof callback === "function") callback(this.state);
},
setters: []
};
store.setState = store.setState.bind(store);
subscriptions[name] = [];
store.public = new StoreInterface(name, store, reducer !== defaultReducer);
stores = Object.assign({}, stores, { [name]: store });
return store.public;
}
/**
* Returns a store instance based on its name
* @callback {String} name - The name of the wanted store
* @returns {StoreInterface} the store instance
*/
export function getStoreByName(name) {
try {
return stores[name].public;
} catch (e) {
throw new Error(`Store with name ${name} does not exist`);
}
}
/**
* Returns a [ state, setState ] pair for the selected store. Can only be called within React Components
* @param {String|StoreInterface} identifier - The identifier for the wanted store
* @returns {Array} the [state, setState] pair.
* @example
* const [state, dispatch] = useStore('todoList'); | const [state, dispatch] = useStore(referenceStoreObject) | const [{ v1, v2, v3 }, dispatch] = useStore(referenceStoreObject);
*/
export function useStore(identifier) {
const store = getStoreByIdentifier(identifier);
const [state, set] = useState(store.state);
useEffect(() => {
if (!store.setters.includes(set)) {
store.setters.push(set);
}
return () => {
store.setters = store.setters.filter(setter => setter !== set);
};
}, []);
return [state, store.setState];
}
/**
* Call this function outside of components. you can use this in any functions.
* Get current store value directly without use of Hooks,
* Note: this function does not subscribe to store. just return current value of store.
* @param {*} identifier string or object ref
* @example
* const u=readOnlyStore(storesName.user);
*/
export function readOnlyStore(identifier) {
const store = getStoreByIdentifier(identifier);
return store.public.getState();
}
/**
* Call this function outside of components. you can use this in any functions.
* Change value of store directly, but this function call setter and all subscribers will notify and receive this changes.
* @param {*} identifier string or object ref
* @param {object} action call an action function that return object with type and payload
* @example
* dispatchDirectly(storesName.user,actions.user.change_username());
*/
export function dispatchDirectly(identifier, action) {
const store = getStoreByIdentifier(identifier);
store.setState(action);
}