From abd87579efb3c8dce7134d4f3a447d98581764bb Mon Sep 17 00:00:00 2001 From: Richard Park Date: Tue, 8 Oct 2019 13:18:05 +0900 Subject: [PATCH] event effect is implemented --- .../src/app/store/messenger/event/actions.ts | 24 ++++++ .../src/app/store/messenger/event/effects.ts | 81 +++++++++++++++++++ .../src/app/store/messenger/event/index.ts | 4 + .../src/app/store/messenger/event/reducers.ts | 14 ++++ .../src/app/store/messenger/event/state.ts | 25 ++++++ .../src/app/store/messenger/index.ts | 10 +++ .../src/lib/protocols/info.ts | 25 +++--- .../src/lib/protocols/push.ts | 2 +- .../src/lib/protocols/send.ts | 2 +- .../types/{pushCl.type.ts => push-cl.type.ts} | 0 ...pushStatus.type.ts => push-status.type.ts} | 0 .../src/public-api.ts | 3 + 12 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts create mode 100644 projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts create mode 100644 projects/ucap-webmessenger-app/src/app/store/messenger/event/index.ts create mode 100644 projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts create mode 100644 projects/ucap-webmessenger-app/src/app/store/messenger/event/state.ts rename projects/ucap-webmessenger-protocol-event/src/lib/types/{pushCl.type.ts => push-cl.type.ts} (100%) rename projects/ucap-webmessenger-protocol-event/src/lib/types/{pushStatus.type.ts => push-status.type.ts} (100%) diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts new file mode 100644 index 00000000..e7a38836 --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts @@ -0,0 +1,24 @@ +import { createAction, props } from '@ngrx/store'; +import { + InfoRequest, + Info, + InfoResponse +} from '@ucap-webmessenger/protocol-event'; + +export const info = createAction( + '[Messenger::Event] Info', + props() +); + +export const infoSuccess = createAction( + '[Messenger::Event] Info Success', + props<{ + infoList: Info[]; + res: InfoResponse; + }>() +); + +export const infoFailure = createAction( + '[Messenger::Event] Info Failure', + props<{ error: any }>() +); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts new file mode 100644 index 00000000..65bbb60c --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts @@ -0,0 +1,81 @@ +import { Injectable } from '@angular/core'; + +import { Actions, createEffect, ofType } from '@ngrx/effects'; + +import { Store } from '@ngrx/store'; + +import { NGXLogger } from 'ngx-logger'; + +import { of } from 'rxjs'; +import { tap, switchMap, map, catchError } from 'rxjs/operators'; +import { + InfoData, + Info, + InfoResponse, + EventProtocolService, + SSVC_TYPE_EVENT_INFO_DATA, + SSVC_TYPE_EVENT_INFO_RES +} from '@ucap-webmessenger/protocol-event'; + +import * as ChatStore from '@app/store/messenger/chat'; + +import { info, infoSuccess, infoFailure } from './actions'; +import { SessionStorageService } from '@ucap-webmessenger/web-storage'; + +@Injectable() +export class Effects { + selectedRoomForInfo$ = createEffect(() => + this.actions$.pipe( + ofType(ChatStore.selectedRoom), + map(action => { + return info({ + roomSeq: action.roomSeq, + baseSeq: 0, + requestCount: 50 + }); + }) + ) + ); + + info$ = createEffect( + () => { + let infoList: Info[]; + + return this.actions$.pipe( + ofType(info), + tap(() => { + infoList = []; + }), + switchMap(req => { + return this.eventProtocolService.info(req).pipe( + map(res => { + switch (res.Type) { + case SSVC_TYPE_EVENT_INFO_DATA: + infoList.push(...(res as InfoData).infoList); + break; + case SSVC_TYPE_EVENT_INFO_RES: + this.store.dispatch( + infoSuccess({ + infoList, + res: res as InfoResponse + }) + ); + break; + } + }), + catchError(error => of(infoFailure({ error }))) + ); + }) + ); + }, + { dispatch: false } + ); + + constructor( + private actions$: Actions, + private store: Store, + private eventProtocolService: EventProtocolService, + private sessionStorageService: SessionStorageService, + private logger: NGXLogger + ) {} +} diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/index.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/index.ts new file mode 100644 index 00000000..2663cade --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/index.ts @@ -0,0 +1,4 @@ +export * from './actions'; +export * from './effects'; +export * from './reducers'; +export * from './state'; diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts new file mode 100644 index 00000000..10a813bc --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts @@ -0,0 +1,14 @@ +import { createReducer, on } from '@ngrx/store'; +import { initialState } from './state'; +import { infoSuccess } from './actions'; + +export const reducer = createReducer( + initialState, + on(infoSuccess, (state, action) => { + return { + ...state, + infoList: action.infoList, + infoStatus: action.res + }; + }) +); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/state.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/state.ts new file mode 100644 index 00000000..a6e56dfa --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/state.ts @@ -0,0 +1,25 @@ +import { Selector, createSelector } from '@ngrx/store'; +import { InfoResponse, Info } from '@ucap-webmessenger/protocol-event'; + +export interface State { + infoList: Info[] | null; + infoStatus: InfoResponse | null; +} + +export const initialState: State = { + infoList: null, + infoStatus: null +}; + +export function selectors(selector: Selector) { + return { + infoList: createSelector( + selector, + (state: State) => state.infoList + ), + infoStatus: createSelector( + selector, + (state: State) => state.infoStatus + ) + }; +} diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/index.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/index.ts index 8c20d898..86c65df5 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/index.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/index.ts @@ -2,6 +2,7 @@ import { Type } from '@angular/core'; import { Action, combineReducers, Selector, createSelector } from '@ngrx/store'; import * as ChatStore from './chat'; +import * as EventStore from './event'; import * as OptionStore from './option'; import * as QueryStore from './query'; import * as RoomStore from './room'; @@ -10,6 +11,7 @@ import * as SyncStore from './sync'; export interface State { chat: ChatStore.State; + event: EventStore.State; option: OptionStore.State; query: QueryStore.State; room: RoomStore.State; @@ -19,6 +21,7 @@ export interface State { export const effects: Type[] = [ ChatStore.Effects, + EventStore.Effects, OptionStore.Effects, QueryStore.Effects, RoomStore.Effects, @@ -29,6 +32,7 @@ export const effects: Type[] = [ export function reducers(state: State | undefined, action: Action) { return combineReducers({ chat: ChatStore.reducer, + event: EventStore.reducer, option: OptionStore.reducer, query: QueryStore.reducer, room: RoomStore.reducer, @@ -45,6 +49,12 @@ export function selectors(selector: Selector) { (state: State) => state.chat ) ), + EventSelector: EventStore.selectors( + createSelector( + selector, + (state: State) => state.event + ) + ), OptionSelector: OptionStore.selectors( createSelector( selector, diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/info.ts b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/info.ts index ee829745..c23afcc6 100644 --- a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/info.ts +++ b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/info.ts @@ -6,7 +6,8 @@ import { ProtocolDecoder, ProtocolMessage, ProtocolStream, - PacketBodyValue + PacketBodyValue, + BodyStringDivider } from '@ucap-webmessenger/protocol'; import { EventType } from '../types/event.type'; import { Info } from '../models/info'; @@ -53,15 +54,19 @@ export const decodeInfoData: ProtocolDecoder = ( message: ProtocolMessage ) => { const infoList: Info[] = []; - for (let i = 1; i < message.bodyList.length; ) { - infoList.push({ - seq: message.bodyList[0], - type: message.bodyList[1], - senderSeq: message.bodyList[2], - sendDate: message.bodyList[3], - sentMessage: message.bodyList[4], - receiverCount: message.bodyList[5] - }); + + for (const body of message.bodyList) { + const info = body.split(BodyStringDivider); + if (info.length > 5) { + infoList.push({ + seq: info[0], + type: info[1] as EventType, + senderSeq: info[2], + sendDate: info[3], + sentMessage: info[4], + receiverCount: info[5] + }); + } } return { diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/push.ts b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/push.ts index ba01d642..a45e9cad 100644 --- a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/push.ts +++ b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/push.ts @@ -7,7 +7,7 @@ import { PacketBodyValue } from '@ucap-webmessenger/protocol'; import { EventType } from '../types/event.type'; -import { PushClType } from '../types/pushCl.type'; +import { PushClType } from '../types/push-cl.type'; export interface PushRequest extends ProtocolRequest { // 대화방SEQ(s) diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts index e1b57327..d6badf6a 100644 --- a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts +++ b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts @@ -10,7 +10,7 @@ import { ProtocolNotification } from '@ucap-webmessenger/protocol'; import { EventType } from '../types/event.type'; -import { PushStatus } from '../types/pushStatus.type'; +import { PushStatus } from '../types/push-status.type'; export interface SendRequest extends ProtocolRequest { // 0. 대화방SEQ(s) diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/types/pushCl.type.ts b/projects/ucap-webmessenger-protocol-event/src/lib/types/push-cl.type.ts similarity index 100% rename from projects/ucap-webmessenger-protocol-event/src/lib/types/pushCl.type.ts rename to projects/ucap-webmessenger-protocol-event/src/lib/types/push-cl.type.ts diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/types/pushStatus.type.ts b/projects/ucap-webmessenger-protocol-event/src/lib/types/push-status.type.ts similarity index 100% rename from projects/ucap-webmessenger-protocol-event/src/lib/types/pushStatus.type.ts rename to projects/ucap-webmessenger-protocol-event/src/lib/types/push-status.type.ts diff --git a/projects/ucap-webmessenger-protocol-event/src/public-api.ts b/projects/ucap-webmessenger-protocol-event/src/public-api.ts index ff743256..44ee9937 100644 --- a/projects/ucap-webmessenger-protocol-event/src/public-api.ts +++ b/projects/ucap-webmessenger-protocol-event/src/public-api.ts @@ -12,3 +12,6 @@ export * from './lib/services/event-protocol.service'; export * from './lib/ucap-event-protocol.module'; export * from './lib/types/event.type'; +export * from './lib/types/push-cl.type'; +export * from './lib/types/push-status.type'; +export * from './lib/types/service';