import { ItineraryService } from '../services/itinerary' ; import { LinesDAO } from '../dao/lines' ; import { Line } from '../models/itinerary' ; import { Injectable } from '@angular/core' ; class @ Injectable ( ) export class LineManager { public constructor ( private service , private dao ) { } private return { Promise < Line [ ] > } private downloadLines ( ) { return new Promise < Line [ ] > ( resolve => { this . service . getItineraries ( ) . then ( ( lines ) => { this . dao . saveAll ( lines ) . then ( ( ) => { resolve ( lines ) ; } ) ; } ) ; } ) ; } return { Line [ ] } public getAll ( ) { return new Promise < Line [ ] > ( resolve => { this . dao . getAll ( ) . then ( daoLines => { if ( daoLines . length > 0 ) resolve ( daoLines ) ; else this . downloadLines ( ) . then ( downloadedLines => resolve ( downloadedLines ) ) ; } ) ; } ) ; } param { number } limit param { number } skip return { Promise < Line [ ] > } public getSlice ( limit , skip ) { return new Promise < Line [ ] > ( resolve => { this . dao . getLimited ( limit , skip ) . then ( lines => { if ( lines . length > 0 ) resolve ( lines ) ; else this . downloadLines ( ) . then ( downloadedLines => resolve ( downloadedLines . splice ( skip , limit ) ) ) ; } ) ; } ) ; } }