From c2d7997f17c21009d3870f0dc7485e548d704ab3 Mon Sep 17 00:00:00 2001 From: LiJun Date: Tue, 10 Mar 2020 11:58:10 +0800 Subject: [PATCH] fix: loading plugin argument type and remove socket plugin --- src/plugin/loading.ts | 2 +- src/plugin/socket.ts | 67 ------------------------------------------- 2 files changed, 1 insertion(+), 68 deletions(-) delete mode 100644 src/plugin/socket.ts diff --git a/src/plugin/loading.ts b/src/plugin/loading.ts index e80ce3b..9e2f28e 100644 --- a/src/plugin/loading.ts +++ b/src/plugin/loading.ts @@ -5,7 +5,7 @@ type ValueOf, K> = K extends keyof T ? T[K] : never; -export default ({ use, createStore }: CubeAPI) => { +export default ({ use, createStore }: Pick) => { const loadingStore = createStore({ name: "loading", state: {} as Record, diff --git a/src/plugin/socket.ts b/src/plugin/socket.ts deleted file mode 100644 index 9d08d94..0000000 --- a/src/plugin/socket.ts +++ /dev/null @@ -1,67 +0,0 @@ -import cube from "../index"; - -enum STATUS { - OPEN = "open", - CLOSE = "close", - PENDING = "pending" -} -interface IState { - instance?: WebSocket; - timer?: number; - status: STATUS; - messages: MessageEvent[]; - latestMsg: MessageEvent; -} - -const { createStore } = cube(); -const socketStore = createStore({ - name: "socket", - state: { - instance: undefined, - timer: undefined, - status: "close", - messages: [] as MessageEvent[], - latestMsg: {} - } as IState, - effects: { - async open({ update, select }, url: string) { - const [ins, status] = select(s => [s.instance, s.status]); - - if (!ins && status !== STATUS.OPEN) { - const websocket = new WebSocket(url); - websocket.onopen = function(evt) { - const timer = setInterval(() => { - websocket.send("time: " + Date.now()); - }, 2000); - update({ timer, status: STATUS.OPEN }); - }; - // websocket.onclose = function(evt) { - // update({ status: STATUS.CLOSE, instance: undefined }); - // }; - websocket.onmessage = function(evt) { - socketStore.reducers.onMessage(evt); - }; - websocket.onerror = function(evt) { - console.log(evt); - }; - update({ instance: websocket, status: STATUS.PENDING }); - } - } - }, - reducers: { - close(state) { - if (state.instance) { - state.instance.close(); - state.instance = undefined; - state.status = STATUS.CLOSE; - clearInterval(state.timer); - } - }, - onMessage(state, payload) { - state.latestMsg = payload; - state.messages.push(payload); - } - } -}); - -export default socketStore;