import { now } from '@gilbarbara/helpers' ; import produce from 'immer' ; import { Reducer } from 'redux' ; import { ActionCreator , ActionsMapReducer , GenericFunction , StoreAction } from 'types' ; /**
 * Create an action
 */ export function createAction < T extends GenericFunction > ( type , payloadCreator ) { if ( ! payloadCreator ) { throw new TypeError ( 'Expected a function' ) ; } return ( ... args ) => ( { type , payload : payloadCreator ( ... args ) , } ) ; } /**
 * Create a reducer
 */ export function createReducer < State > ( actionsMap , defaultState ) { return ( state = defaultState , action ) => produce ( state , ( draft ) => { const fn = actionsMap [ action . type ] ; if ( fn ) { return fn ( draft , action ) ; } return draft ; } ) ; } /**
 * Check if cache is valid
 */ export function hasValidCache ( lastUpdated , max = 10 ) { if ( ! navigator . onLine ) { return true ; } return lastUpdated + max * 60 > now ( ) ; }