From df1d9bea28dbb59db40a11a5103b8aa32e1a1784 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Thu, 10 Oct 2019 15:44:39 +0900 Subject: [PATCH] event message sync is modified --- .../src/app/store/messenger/sync/actions.ts | 9 ++++ .../src/app/store/messenger/sync/effects.ts | 42 ++++++++++++++++++- .../src/app/store/messenger/sync/reducers.ts | 21 ++++++---- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts index cba55fd5..b30d35ea 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts @@ -12,6 +12,7 @@ import { UserInfoShort, UserInfo as RoomUserInfo } from '@ucap-webmessenger/protocol-room'; +import { Info } from '@ucap-webmessenger/protocol-event'; export const buddy2 = createAction( '[Messenger::Sync] Buddy2', @@ -66,3 +67,11 @@ export const roomFailure = createAction( '[Messenger::Sync] Room Failure', props<{ error: any }>() ); + +export const updateRoomForNewEventMessage = createAction( + '[Messenger::Sync] updateRoomForNewEventMessage', + props<{ + roomSeq: string; + info: Info; + }>() +); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts index fdf5dfd4..9ff0bda7 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts @@ -25,7 +25,8 @@ import { group2Failure, room, roomFailure, - roomSuccess + roomSuccess, + updateRoomForNewEventMessage } from './actions'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; @@ -57,6 +58,8 @@ import { } from '@ucap-webmessenger/protocol-room'; import { LoginInfo, KEY_LOGIN_INFO } from '@app/types'; +import * as ChatStore from '@app/store/messenger/chat'; + @Injectable() export class Effects { buddy2$ = createEffect( @@ -199,6 +202,43 @@ export class Effects { { dispatch: false } ); + newEventMessageForRoomInfoList$ = createEffect( + () => + this.actions$.pipe( + ofType(ChatStore.newEventMessage), + withLatestFrom( + this.store.pipe( + select((state: any) => state.messenger.sync.roomList as RoomInfo[]) + ), + this.store.pipe( + select((state: any) => state.messenger.sync.roomSyncDate as string) + ) + ), + tap(([action, roomList, roomSyncDate]) => { + const index = roomList.findIndex( + (roomInfo, i) => roomInfo.roomSeq === action.roomSeq + ); + + if (-1 === index) { + const loginInfo = this.sessionStorageService.get( + KEY_LOGIN_INFO + ); + + this.store.dispatch( + room({ + syncDate: roomSyncDate, + localeCode: loginInfo.localeCode + }) + ); + return; + } + + this.store.dispatch(updateRoomForNewEventMessage(action)); + }) + ), + { dispatch: false } + ); + constructor( private actions$: Actions, private store: Store, diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts index 8585e494..88b8fbcc 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts @@ -1,8 +1,12 @@ import { createReducer, on } from '@ngrx/store'; import { initialState } from './state'; -import { buddy2Success, group2Success, roomSuccess } from './actions'; +import { + buddy2Success, + group2Success, + roomSuccess, + updateRoomForNewEventMessage +} from './actions'; -import * as ChatStore from '@app/store/messenger/chat'; import { RoomInfo } from '@ucap-webmessenger/protocol-room'; export const reducer = createReducer( @@ -10,7 +14,7 @@ export const reducer = createReducer( on(buddy2Success, (state, action) => { return { ...state, - buddyInfoList: action.buddyList, + buddyInfoList: [...state.buddyInfoList, ...action.buddyList], buddy2SyncDate: action.syncDate }; }), @@ -18,7 +22,7 @@ export const reducer = createReducer( on(group2Success, (state, action) => { return { ...state, - groupList: action.groupList, + groupList: [...state.groupList, ...action.groupList], group2SyncDate: action.syncDate }; }), @@ -26,13 +30,16 @@ export const reducer = createReducer( on(roomSuccess, (state, action) => { return { ...state, - roomList: action.roomList, - roomUserInfoMap: action.roomUserInfoMap, + roomList: [...state.roomList, ...action.roomList], + roomUserInfoMap: { + ...state.roomUserInfoMap, + ...action.roomUserInfoMap + }, roomSyncDate: action.syncDate }; }), - on(ChatStore.newEventMessage, (state, action) => { + on(updateRoomForNewEventMessage, (state, action) => { const roomList: RoomInfo[] = []; state.roomList.forEach((roomInfo, index) => {