import { EventEmitter } from 'events' ; import { injectable , inject } from 'inversify' ; import OrderImpl , { OrderInit } from './OrderImpl' ; import symbols from './symbols' ; import { HistoricalOrderStore } from './types' ; import _ = require ( 'lodash' ) ; @ injectable ( ) export default class OrderService extends EventEmitter { orders : OrderImpl [ ] = [ ] ; constructor ( @ inject ( symbols . HistoricalOrderStore ) private readonly historicalOrderStore ) { super ( ) ; } createOrder ( init ) { const order = new OrderImpl ( init ) ; this . orders . push ( order ) ; this . emit ( 'orderCreated' , order ) ; return order ; } emitOrderUpdated ( order ) { this . emit ( 'orderUpdated' , order ) ; } async finalizeOrder ( order ) { await this . historicalOrderStore . put ( order ) ; _ . pull ( this . orders , order ) ; this . emit ( 'orderFinalized' , order ) ; } }