From a57a934782598b4bc1f2d1dcc12a436946ea6458 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 27 Sep 2017 19:57:28 +0900 Subject: [PATCH] AsyncNotify have been added. --- src/ts/@overflow/app/config/index.ts | 2 ++ .../@overflow/app/redux/saga/AsyncNotify.ts | 29 +++++++++++++++++++ .../@overflow/commons/api/service/Service.ts | 13 ++++++--- .../commons/redux/action/asyncNotify.ts | 20 +++++++++++++ .../redux/payload/AsyncNotifyPayload.ts | 9 ++++++ .../commons/websocket/WebSocketRPC.ts | 6 ++++ .../api/service/NoAuthProbeService.ts | 4 +-- 7 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/ts/@overflow/app/redux/saga/AsyncNotify.ts create mode 100644 src/ts/@overflow/commons/redux/action/asyncNotify.ts create mode 100644 src/ts/@overflow/commons/redux/payload/AsyncNotifyPayload.ts diff --git a/src/ts/@overflow/app/config/index.ts b/src/ts/@overflow/app/config/index.ts index 19144e0..a65d4d2 100644 --- a/src/ts/@overflow/app/config/index.ts +++ b/src/ts/@overflow/app/config/index.ts @@ -57,6 +57,7 @@ import ForgotPassword from '@overflow/member/redux/reducer/forgotPassword'; import AuthCrawlerRegistReducer from '@overflow/auth/redux/reducer/regist'; import AsyncRequest from '@overflow/app/redux/saga/AsyncRequest'; +import AsyncNotify from '@overflow/app/redux/saga/AsyncNotify'; import AsyncRestRequest from '@overflow/app/redux/saga/AsyncRestRequest'; import DiscoveryService from '@overflow/discovery/api/service/DiscoveryService'; @@ -144,6 +145,7 @@ const reduxConfig: ReduxConfig = { ], sagaWatchers: [ AsyncRequest, + AsyncNotify, AsyncRestRequest, ], }; diff --git a/src/ts/@overflow/app/redux/saga/AsyncNotify.ts b/src/ts/@overflow/app/redux/saga/AsyncNotify.ts new file mode 100644 index 0000000..06a0341 --- /dev/null +++ b/src/ts/@overflow/app/redux/saga/AsyncNotify.ts @@ -0,0 +1,29 @@ +import { SagaIterator } from 'redux-saga'; +import { call, Effect, fork, put, takeEvery } from 'redux-saga/effects'; + +import Action from '@overflow/commons/redux/Action'; +import SagaWatcher from '@overflow/commons/redux/saga/SagaWatcher'; +import * as AsyncNotifyActions from '@overflow/commons/redux/action/asyncNotify'; +import AsyncNotifyPayload from '@overflow/commons/redux/payload/AsyncNotifyPayload'; + +import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC'; + +export class AsyncNotify implements SagaWatcher { + private _webSocketRPC: WebSocketRPC; + + public setWebSocketRPC(webSocketRPC: WebSocketRPC): void { + this._webSocketRPC = webSocketRPC; + } + + private * request(webSocketRPC: WebSocketRPC, action: Action): SagaIterator { + const {service, method, args} = action.payload; + yield call({context: webSocketRPC, fn: webSocketRPC.Notify}, `${service}.${method}`, args); + } + + public * watch(): SagaIterator { + yield takeEvery(AsyncNotifyActions.NOTIFY, this.request, this._webSocketRPC); + } + +} + +export default AsyncNotify; diff --git a/src/ts/@overflow/commons/api/service/Service.ts b/src/ts/@overflow/commons/api/service/Service.ts index 926607f..a01dad7 100644 --- a/src/ts/@overflow/commons/api/service/Service.ts +++ b/src/ts/@overflow/commons/api/service/Service.ts @@ -7,13 +7,18 @@ import Action from '@overflow/commons/redux/Action'; export abstract class Service { private store: Store; - protected dispatch(action: Action): void { - this.store.dispatch(action); - } - public setStore(store: Store): void { this.store = store; } + + protected dispatch(actionType: any, payload?: any): void { + const action: Action = { + type: actionType, + payload: payload, + }; + this.store.dispatch(action); + } + } export default Service; diff --git a/src/ts/@overflow/commons/redux/action/asyncNotify.ts b/src/ts/@overflow/commons/redux/action/asyncNotify.ts new file mode 100644 index 0000000..de8e63e --- /dev/null +++ b/src/ts/@overflow/commons/redux/action/asyncNotify.ts @@ -0,0 +1,20 @@ +import Action from '@overflow/commons/redux/Action'; +import AsyncNotifyPayload from '../payload/AsyncNotifyPayload'; + +// Action Type +export type NOTIFY = '@overflow/commons/async/notify/NOTIFY'; +export const NOTIFY: NOTIFY = '@overflow/commons/async/notify/NOTIFY'; + +// Action Creater +export type notify = (service: string, method: string, ...args: any[]) => Action; + +export const notify: notify = (service: string, method: string, ...args: any[]): Action => { + return { + type: NOTIFY, + payload: { + service: service, + method: method, + args: args, + }, + }; +}; diff --git a/src/ts/@overflow/commons/redux/payload/AsyncNotifyPayload.ts b/src/ts/@overflow/commons/redux/payload/AsyncNotifyPayload.ts new file mode 100644 index 0000000..797af44 --- /dev/null +++ b/src/ts/@overflow/commons/redux/payload/AsyncNotifyPayload.ts @@ -0,0 +1,9 @@ +import Action from '@overflow/commons/redux/Action'; + +interface AsyncNotifyPayload { + service: string; + method: string; + args?: any[]; +} + +export default AsyncNotifyPayload; diff --git a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts index 39be84d..fa0825c 100644 --- a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts +++ b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts @@ -72,6 +72,12 @@ export default class WebSocketRPC { }); } + public Notify(method: string, args: any): void { + let notification = new Notification(method, args); + notification.Protocol = PROTOCOL_NAME; + this.conn.send(JSON.stringify(notification)); + } + public OnDisconnect(cb: OnDisconnectFunc): void { this.onDisconnectListeners.push(cb); } diff --git a/src/ts/@overflow/noauthprobe/api/service/NoAuthProbeService.ts b/src/ts/@overflow/noauthprobe/api/service/NoAuthProbeService.ts index cb68d3d..a88ae18 100644 --- a/src/ts/@overflow/noauthprobe/api/service/NoAuthProbeService.ts +++ b/src/ts/@overflow/noauthprobe/api/service/NoAuthProbeService.ts @@ -9,11 +9,11 @@ export class NoAuthProbeService extends Service { } public regist(params: any): void { - const json: NoAuthProbe = JSON.parse(params); + const noAuthProbe: NoAuthProbe = JSON.parse(params); const action: Action = { type: 'TEST', }; - this.dispatch(action); + this.dispatch('TEST', noAuthProbe); } }