event effect is implemented

This commit is contained in:
병준 박 2019-10-08 13:18:05 +09:00
parent 9aae0885aa
commit abd87579ef
12 changed files with 178 additions and 12 deletions

View File

@ -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<InfoRequest>()
);
export const infoSuccess = createAction(
'[Messenger::Event] Info Success',
props<{
infoList: Info[];
res: InfoResponse;
}>()
);
export const infoFailure = createAction(
'[Messenger::Event] Info Failure',
props<{ error: any }>()
);

View File

@ -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<any>,
private eventProtocolService: EventProtocolService,
private sessionStorageService: SessionStorageService,
private logger: NGXLogger
) {}
}

View File

@ -0,0 +1,4 @@
export * from './actions';
export * from './effects';
export * from './reducers';
export * from './state';

View File

@ -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
};
})
);

View File

@ -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<S>(selector: Selector<any, State>) {
return {
infoList: createSelector(
selector,
(state: State) => state.infoList
),
infoStatus: createSelector(
selector,
(state: State) => state.infoStatus
)
};
}

View File

@ -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<any>[] = [
ChatStore.Effects,
EventStore.Effects,
OptionStore.Effects,
QueryStore.Effects,
RoomStore.Effects,
@ -29,6 +32,7 @@ export const effects: Type<any>[] = [
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<S>(selector: Selector<any, State>) {
(state: State) => state.chat
)
),
EventSelector: EventStore.selectors(
createSelector(
selector,
(state: State) => state.event
)
),
OptionSelector: OptionStore.selectors(
createSelector(
selector,

View File

@ -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<InfoData> = (
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 {

View File

@ -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)

View File

@ -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)

View File

@ -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';