UNPKG

@splitsoftware/splitio-commons

Version:
31 lines (30 loc) 1.31 kB
import { __spreadArray } from "tslib"; function greedyFetch(fetchSegmentChanges, since, segmentName, noCache, targetTill) { return fetchSegmentChanges(since, segmentName, noCache, targetTill) .then(function (resp) { return resp.json(); }) .then(function (json) { var since = json.since, till = json.till; if (since === till) { return [json]; } else { return Promise.all([json, greedyFetch(fetchSegmentChanges, till, segmentName, noCache, targetTill)]).then(function (flatMe) { return __spreadArray([flatMe[0]], flatMe[1], true); }); } }); } /** * Factory of SegmentChanges fetcher. * SegmentChanges fetcher is a wrapper around `segmentChanges` API service that parses the response and handle errors and retries. */ export function segmentChangesFetcherFactory(fetchSegmentChanges) { return function segmentChangesFetcher(since, segmentName, noCache, till, // Optional decorator for `fetchSegmentChanges` promise, such as timeout or time tracker decorator) { var segmentsPromise = greedyFetch(fetchSegmentChanges, since, segmentName, noCache, till); if (decorator) segmentsPromise = decorator(segmentsPromise); return segmentsPromise; }; }