239 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-09-25 17:26:19 +09:00
import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs';
2019-10-02 14:34:17 +09:00
import {
catchError,
exhaustMap,
map,
withLatestFrom,
switchMap,
tap
} from 'rxjs/operators';
2019-09-25 17:26:19 +09:00
2019-09-25 18:08:50 +09:00
import { Store, select } from '@ngrx/store';
2019-09-26 11:11:22 +09:00
import { NGXLogger } from 'ngx-logger';
2019-09-25 18:08:50 +09:00
import {
buddy2,
buddy2Success,
buddy2Failure,
group2,
group2Success,
2019-10-02 15:49:25 +09:00
group2Failure,
room,
roomFailure,
roomSuccess
2019-09-25 18:08:50 +09:00
} from './actions';
2019-09-25 17:26:19 +09:00
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import {
SyncProtocolService,
SSVC_TYPE_SYNC_BUDDY2_DATA,
BuddyResponse,
2019-09-25 18:08:50 +09:00
BuddyDetailData,
SSVC_TYPE_SYNC_GROUP_DATA2,
GroupDetailData,
2019-10-02 14:34:17 +09:00
GroupResponse,
UserInfo,
SSVC_TYPE_SYNC_BUDDY2_RES,
2019-10-02 15:49:25 +09:00
SSVC_TYPE_SYNC_GROUP_RES2,
SSVC_TYPE_SYNC_ROOM_DATA,
SSVC_TYPE_SYNC_ROOM_USER,
SSVC_TYPE_SYNC_ROOM_USER2,
SSVC_TYPE_SYNC_ROOM_RES,
RoomData,
RoomUserData,
RoomUserDetailData,
RoomResponse
2019-09-25 17:26:19 +09:00
} from '@ucap-webmessenger/protocol-sync';
import { regViewSuccess } from '@app/store/setting/option';
2019-10-02 15:49:25 +09:00
import {
RoomInfo,
UserInfoShort,
UserInfo as RoomUserInfo
} from '@ucap-webmessenger/protocol-room';
import { LoginInfo, KEY_LOGIN_INFO } from '@app/types';
2019-09-25 17:26:19 +09:00
@Injectable()
export class Effects {
2019-10-02 14:34:17 +09:00
buddy2$ = createEffect(
() => {
let buddyList: UserInfo[];
2019-09-25 17:26:19 +09:00
2019-10-02 14:34:17 +09:00
return this.actions$.pipe(
ofType(buddy2),
tap(() => {
buddyList = [];
}),
switchMap(req => {
return this.syncProtocolService.buddy2(req).pipe(
map(res => {
switch (res.Type) {
case SSVC_TYPE_SYNC_BUDDY2_DATA:
buddyList.push(...(res as BuddyDetailData).buddyInfos);
break;
case SSVC_TYPE_SYNC_BUDDY2_RES:
this.store.dispatch(
buddy2Success({
buddyList,
syncDate: (res as BuddyResponse).syncDate
})
);
break;
}
}),
catchError(error => of(buddy2Failure({ error })))
);
})
);
},
{ dispatch: false }
2019-09-25 18:08:50 +09:00
);
2019-10-02 14:34:17 +09:00
group2$ = createEffect(
() => {
let groupList: GroupDetailData[];
2019-09-25 18:08:50 +09:00
2019-10-02 14:34:17 +09:00
return this.actions$.pipe(
ofType(group2),
tap(() => {
groupList = [];
}),
switchMap(req => {
return this.syncProtocolService.group2(req).pipe(
map(res => {
switch (res.Type) {
case SSVC_TYPE_SYNC_GROUP_DATA2:
groupList.push(res as GroupDetailData);
break;
case SSVC_TYPE_SYNC_GROUP_RES2:
this.store.dispatch(
group2Success({
groupList,
syncDate: (res as GroupResponse).syncDate
})
);
break;
}
}),
catchError(error => of(group2Failure({ error })))
);
})
);
},
{ dispatch: false }
2019-09-25 18:08:50 +09:00
);
2019-10-02 18:09:39 +09:00
// buddy2SuccessPostRoom$ = createEffect(() =>
// this.actions$.pipe(
// ofType(buddy2Success),
// map(action => {
// const loginInfo = this.sessionStorageService.get<LoginInfo>(
// KEY_LOGIN_INFO
// );
2019-10-02 15:49:25 +09:00
2019-10-02 18:09:39 +09:00
// return loginInfo.localeCode;
// }),
// withLatestFrom(
// this.store.pipe(
// select(state => {
// return state.messenger.sync.roomSyncDate as string;
// })
// )
// ),
// map(([localeCode, roomSyncDate]) =>
// room({ syncDate: roomSyncDate, localeCode })
// )
// )
// );
2019-10-02 15:49:25 +09:00
room$ = createEffect(
() => {
let roomList: RoomInfo[];
let roomUserInfoMap: {
[param: string]: {
userInfoShortList: UserInfoShort[];
userInfoList: RoomUserInfo[];
};
};
return this.actions$.pipe(
ofType(room),
tap(() => {
roomList = [];
roomUserInfoMap = {};
}),
switchMap(req => {
return this.syncProtocolService.room(req).pipe(
map(res => {
switch (res.Type) {
case SSVC_TYPE_SYNC_ROOM_DATA:
roomList.push(...(res as RoomData).roomInfos);
break;
case SSVC_TYPE_SYNC_ROOM_USER:
{
const roomUserData = res as RoomUserData;
if (!roomUserInfoMap[roomUserData.roomSeq]) {
roomUserInfoMap[roomUserData.roomSeq] = {
userInfoList: [],
userInfoShortList: []
};
}
roomUserInfoMap[
roomUserData.roomSeq
].userInfoShortList.push(...roomUserData.userInfos);
}
break;
case SSVC_TYPE_SYNC_ROOM_USER2:
{
const roomUserDetailData = res as RoomUserDetailData;
if (!roomUserInfoMap[roomUserDetailData.roomSeq]) {
roomUserInfoMap[roomUserDetailData.roomSeq] = {
userInfoList: [],
userInfoShortList: []
};
}
roomUserInfoMap[
roomUserDetailData.roomSeq
].userInfoList.push(...roomUserDetailData.userInfos);
}
break;
case SSVC_TYPE_SYNC_ROOM_RES:
{
this.logger.debug(
'roomList',
roomList,
'roomUserInfoMap',
roomUserInfoMap
);
this.store.dispatch(
roomSuccess({
roomList,
roomUserInfoMap,
syncDate: (res as RoomResponse).syncDate
})
);
}
break;
}
}),
catchError(error => of(roomFailure({ error })))
);
})
);
},
{ dispatch: false }
);
2019-09-25 17:26:19 +09:00
constructor(
private actions$: Actions,
2019-09-25 18:08:50 +09:00
private store: Store<any>,
2019-09-25 17:26:19 +09:00
private syncProtocolService: SyncProtocolService,
2019-09-26 11:11:22 +09:00
private sessionStorageService: SessionStorageService,
private logger: NGXLogger
2019-09-25 17:26:19 +09:00
) {}
}