private getAllByRange()

in src/app/certificates/certificates.service.ts [81:120]


    private getAllByRange(start: number = 0, total: number = 0): Promise<Array<Certificate>> {
        let stop = false;
        let sub = <Subscription>(<any>this._stopRetrievals).take(1).subscribe(() => {
            stop = true;
        });

        if (start == 0) {
            this._loading++;
        }

        return (total == 0 ? this.getTotal() : Promise.resolve(total))
            .then(total => {
                let length = start + CertificatesService.RANGE_SIZE > total ? total - start : CertificatesService.RANGE_SIZE;
                return this.getRange(start, length)
                    .then(certs => {
                        if (stop) {
                            this._loading--;
                            return this._certificates.getValue();
                        }

                        let current = this._certificates.getValue();
                        certs.forEach(c => current.push(c));
                        this._certificates.next(current);

                        if (start + length < total && !stop) {
                            return this.getAllByRange(start + length, total);
                        }
                        else {
                            this._loading--;
                            sub.unsubscribe();
                            return this._certificates.getValue();
                        }
                    })
                    .catch(e => {
                        this._loading--;
                        sub.unsubscribe();
                        throw e;
                    });
            })
    }