Skip to content

Commit

Permalink
add createReducer for v0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Jan 5, 2022
1 parent 626e3a2 commit 4f92a70
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 785 deletions.
2 changes: 1 addition & 1 deletion packages/reshow-flux-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reshow-flux-base",
"version": "0.15.0",
"version": "0.15.1",
"description": "Pure flux dispatch mechanism",
"main": "./build/cjs/index.js",
"module": "./build/es/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux-base/src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const createReducer = (reduce, initState = {}) => {
}
if (startingState !== endingState) {
state.current = endingState;
mitt.emit(state);
mitt.emit(state, action);
}
};
const store = {
Expand Down
8 changes: 7 additions & 1 deletion packages/reshow-flux/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export { default as useConnect } from "./useConnect";
export { default as ReduceStore } from "./ReduceStore";
export { default as ImmutableStore, Map, Set, getMap, mergeMap } from "./ImmutableStore";
export {
default as ImmutableStore,
Map,
Set,
getMap,
mergeMap,
} from "./ImmutableStore";
export { default as getStores } from "./getStores";
export { Dispatcher } from "reshow-flux-base";
10 changes: 1 addition & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
// Flux
// Connect A.K.A Facebook's flux Container.create
export { ReduceStore, Dispatcher } from "reshow-flux";

// Stores
export { default as pageStore } from "./stores/pageStore";
export { default as pageStore, dispatch } from "./stores/pageStore";
export { default as realTimeStore } from "./stores/realTimeStore";
export { default as localStorageStore } from "./stores/localStorageStore";
export { default as sessionStorageStore } from "./stores/sessionStorageStore";
export { default as messageStore } from "./stores/messageStore";

// Dispatch
export { default as dispatcher, dispatch } from "./dispatcher";
export { storageDispatch } from "./storageDispatcher";

// Ajax
export { AjaxLink as ReLink, AjaxForm as ReForm } from "organism-react-ajax";

Expand Down
32 changes: 7 additions & 25 deletions src/stores/localStorageStore.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { ReduceStore } from "reshow-flux";
import { createReducer } from "reshow-flux-base";
import get from "get-object-value";
import { localStorage, Storage } from "get-storage";

import storageDispatcher from "../storageDispatcher";
const [store, localDispatch] = createReducer((state, action) => {
const params = get(action, ["params"]);
return state.merge(params);
}, new Storage(localStorage));

class LocalStorageStore extends ReduceStore {
getInitialState() {
return new Storage(localStorage);
}

updateStorage(state, action) {
const params = get(action, ["params"]);
return state.merge(params);
}

reduce(state, action) {
switch (action.type) {
case "local":
return this.updateStorage(state, action);
default:
return state;
}
}
}

export default new LocalStorageStore(storageDispatcher);

export { LocalStorageStore };
export default store;
export { localDispatch };
43 changes: 21 additions & 22 deletions src/stores/messageStore.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ReduceStore } from "reshow-flux";
import { ImmutableStore } from "reshow-flux";
import { Map, List } from "immutable";
import get from "get-object-value";
import callfunc from "call-func";
import { T_NULL, IS_ARRAY, KEYS } from "reshow-constant";

import dispatcher, { dispatch } from "../dispatcher";

let alertCount = 0;

const toMessage = (message) => {
Expand All @@ -21,7 +19,7 @@ const toMessage = (message) => {

const getMessage = (action) => toMessage(get(action, ["params", "message"]));

class MessageStore extends ReduceStore {
class MessageStore {
dialogCallback = T_NULL;
alertMap = {};

Expand Down Expand Up @@ -89,24 +87,25 @@ class MessageStore extends ReduceStore {
this.alertMap[message.id] = message;
return state.set("alerts", this.getAlertList());
}
}

reduce(state, action) {
switch (action.type) {
case "dialog/start":
return this.dialogStart(state, action);
case "dialog/end":
return this.dialogEnd(state, action);
case "alert/reset":
return this.alertReset(state, action);
case "alert/del":
return this.alertDel(state, action);
case "alert/add":
return this.alertAdd(state, action);
default:
return state;
}
const [store, messageDispatch] = ImmutableStore((state, action) => {
const oMess = new MessageStore();
switch (action.type) {
case "dialog/start":
return oMess.dialogStart(state, action);
case "dialog/end":
return oMess.dialogEnd(state, action);
case "alert/reset":
return oMess.alertReset(state, action);
case "alert/del":
return oMess.alertDel(state, action);
case "alert/add":
return oMess.alertAdd(state, action);
default:
return state;
}
}
});

// Export a singleton instance of the store
export default new MessageStore(dispatcher);
export default store;
export { messageDispatch };
59 changes: 37 additions & 22 deletions src/stores/pageStore.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import { ReduceStore } from "reshow-flux";
import dispatcher from "../dispatcher";
import { ImmutableStore, mergeMap } from "reshow-flux";
import { KEYS } from "reshow-constant";
import { realTimeDispatch } from "./realTimeStore";
import { messageDispatch } from "./messageStore";
import { sessionDispatch } from "./sessionStorageStore";
import { localDispatch } from "./localStorageStore";

const keys = Object.keys;

class PageStore extends ReduceStore {
reduce(state, action) {
switch (action.type) {
case "config/set":
return state.merge(action.params);
case "config/reset":
return state.clear().merge(action.params);
case "realTime":
const [store, dispatch] = ImmutableStore((state, action) => {
switch (action.type) {
case "dialog/start":
case "dialog/end":
case "alert/reset":
case "alert/del":
case "alert/add":
messageDispatch(action);
return state;
case "realTime":
realTimeDispatch(action);
return state;
case "local":
localDispatch(action);
return state;
case "session":
sessionDispatch(action);
return state;
case "config/set":
return mergeMap(state, action.params);
case "config/reset":
return mergeMap(state.clear(), action.params);
return state.clear().merge(action.params);
default:
if (KEYS(action)) {
return mergeMap(state, action);
} else {
return state;
default:
if (keys(action)) {
return state.merge(action);
} else {
return state;
}
}
}
}
}
});

// Export a singleton instance of the store
export default new PageStore(dispatcher);
export default store;
export { dispatch };
24 changes: 10 additions & 14 deletions src/stores/realTimeStore.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
"use strict";
import { ImmutableStore } from "reshow-flux";

import { ReduceStore } from "reshow-flux";
import dispatcher from "../dispatcher";

class RealTimeStore extends ReduceStore {
reduce(state, action) {
switch (action.type) {
case "realTime":
return action.params;
default:
return [];
}
const [store, realTimeDispatch] = ImmutableStore((state, action) => {
switch (action.type) {
case "realTime":
return action.params;
default:
return [];
}
}
});

export default new RealTimeStore(dispatcher);
export default store;
export { realTimeDispatch };
26 changes: 8 additions & 18 deletions src/stores/sessionStorageStore.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { createReducer } from "reshow-flux-base";
import get from "get-object-value";
import { sessionStorage, Storage } from "get-storage";

import { LocalStorageStore } from "./localStorageStore";
import storageDispatcher from "../storageDispatcher";
const [store, sessionDispatch] = createReducer((state, action) => {
const params = get(action, ["params"]);
return state.merge(params);
}, new Storage(sessionStorage));

class SessionStorageStore extends LocalStorageStore {
getInitialState() {
return new Storage(sessionStorage);
}

reduce(state, action) {
switch (action.type) {
case "session":
return this.updateStorage(state, action);
default:
return state;
}
}
}

export default new SessionStorageStore(storageDispatcher);
export default store;
export { sessionDispatch };
6 changes: 3 additions & 3 deletions ui/organisms/__tests__/RealTimeReturnTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mount, cleanIt } from "reshow-unit";

class TestEl extends PureComponent {
render() {
const props = {...this.props};
const props = { ...this.props };
delete props["--realTimeUrl--"];
return <div {...props} />;
}
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("Test RealTimeReturn", () => {
expect(uFake.el.props.data).to.equal("bar");
dispatch({ data: "foo" });
setTimeout(() => {
expect(uFake.el.props.data).to.equal("foo");
expect(uFake.el.props.data).to.equal("bar");
done();
}, 5);
}, 5);
Expand All @@ -68,7 +68,7 @@ describe("Test RealTimeReturn", () => {
dispatch({ type: "realTime", params: { r: { data: "bar" } } });
setTimeout(() => {
expect(uFake.el.props.data).to.equal("bar");
dispatch({ data: "foo" });
dispatch("realTime");
setTimeout(() => {
expect(uFake.el.props.data).to.be.null;
done();
Expand Down
7 changes: 3 additions & 4 deletions ui/organisms/__tests__/StorageReturnTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
Return,
localStorageStore,
sessionStorageStore,
storageDispatch,
dispatch,
} from "../../../src/index";


class TestEl extends PureComponent {
show = 0;
render() {
Expand Down Expand Up @@ -44,7 +43,7 @@ describe("Test Storage Return", () => {
const vDom = <FakeComponent storage={localStorageStore} />;
const uFake = mount(vDom).instance();
const uString = "test123";
storageDispatch("local", { data: uString });
dispatch("local", { data: uString });
setTimeout(() => {
expect(uFake.el.props.data).to.equal(uString);
done();
Expand All @@ -55,7 +54,7 @@ describe("Test Storage Return", () => {
const vDom = <FakeComponent storage={sessionStorageStore} />;
const uFake = mount(vDom).instance();
const uString = "test456";
storageDispatch("session", { data: uString });
dispatch("session", { data: uString });
setTimeout(() => {
expect(uFake.el.props.data).to.equal(uString);
done();
Expand Down
Loading

0 comments on commit 4f92a70

Please sign in to comment.