浏览代码

A little refactoring

Kirk Trombley 5 年之前
父节点
当前提交
2ac008631d
共有 1 个文件被更改,包括 4 次插入2 次删除
  1. 4 2
      client/src/store.js

+ 4 - 2
client/src/store.js

@@ -36,6 +36,8 @@ const createHook = obs => {
   return useObservable;
 }
 
+const mapValues = (src, fn) => Object.fromEntries(Object.entries(src).map(([key, val]) => [key, fn(val)]));
+
 export const createStore = (initial, actions = {}) => {
   const store = {}; // maps keys to observables
   const hooks = {};
@@ -55,9 +57,9 @@ export const createStore = (initial, actions = {}) => {
 
   mergeState(initial);
 
-  const dispatch = Object.fromEntries(Object.entries(actions).map(([name, action]) => [name, (...args) => mergeState(action(...args))]));
+  const dispatch = mapValues(actions, act => (...args) => mergeState(act(...args)));
 
-  const selector = Object.fromEntries(Object.entries(store).map(([key, obs]) => [key, obs._get]));
+  const selector = mapValues(store, obs => obs._get);
 
   return [hooks, dispatch, selector];
 }