UNPKG

@aller/blink

Version:

A library for tracking user behaviour.

62 lines (59 loc) 1.4 kB
import { ARTICLE_ACTIVITY_START, ARTICLE_ACTIVITY_STOP, PAGE_INIT, } from '../actions'; import { TimeEvent } from '../utils/event-time'; function activity(state: TimeEvent[] = [], action: any) { switch (action.type) { case ARTICLE_ACTIVITY_START: return [ ...state, { type: 'start', time: action.payload.time || new Date(), }, ]; case ARTICLE_ACTIVITY_STOP: // don't add anything if last thing we added was a stop event if (state.length > 0 && state[state.length - 1].type === 'stop') { return state; } return [ ...state, { type: 'stop', time: action.payload.time || new Date(), }, ]; default: break; } return state; } export default function activeTime( state: { [key: string]: any } = {}, action: any, ) { switch (action.type) { case PAGE_INIT: return {}; // Flush the store on pageInit case ARTICLE_ACTIVITY_START: case ARTICLE_ACTIVITY_STOP: { const actState = state[action.payload.id] ? state[action.payload.id].activity : []; return { ...state, [action.payload.id]: { activity: activity(actState, action), url: action.payload.url, id: action.payload.id, }, }; } default: break; } return state; }