import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ErrorBag , Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import * as _ from 'lodash' ; import { config } from '../../constants/config' ; export class FetchVocabularyDelegate { private eventBus : EventBus ; public constructor ( eventBus ) { this . eventBus = eventBus ; } public fetch ( callback ) { this . eventBus . pubsub ( createAction ( ActionType . ATOM__FETCH_VOCABULARY , null ) , group ( on ( ActionType . ATOM__FETCHING_VOCABULARY , callback . onFetching ) , once ( ActionType . ATOM__FETCH_VOCABULARY_SUCCEEDED , ( { vocabularyList , noMore } ) : void => callback . onFetchSucceeded ( vocabularyList , noMore ) ) , once ( ActionType . ATOM__FETCH_VOCABULARY_FAILED , ( errorBag ) : void => callback . onFetchFailed ( errorBag ) ) ) ) ; } public fetchVocabularyIfBelowThreshold ( currentNumberOfQuestionLeft , callback ) { if ( currentNumberOfQuestionLeft < config . atom . fetchTriggerThreshold ) { this . fetch ( { onFetching : _ . noop , onFetchSucceeded : callback . onFetchSucceeded , onFetchFailed : _ . noop , } ) ; } } public clearFetch ( ) { this . eventBus . publish ( createAction ( ActionType . ATOM__CLEAR_FETCH_VOCABULARY , null ) ) ; } }