(async: boolean)
| 49 | * @param async Uses the asynchronous mode. |
| 50 | */ |
| 51 | export function createProgressiveStrategy(async: boolean): PiletLoadingStrategy { |
| 52 | return (options, cb) => { |
| 53 | const { |
| 54 | fetchPilets, |
| 55 | dependencies = {}, |
| 56 | createApi, |
| 57 | config, |
| 58 | pilets = [], |
| 59 | loadPilet = getDefaultLoader(config), |
| 60 | loaders, |
| 61 | hooks, |
| 62 | } = options; |
| 63 | const loadingAll = loadMetadata(fetchPilets); |
| 64 | const loadSingle = extendLoader(loadPilet, loaders); |
| 65 | |
| 66 | return registerDependencies(dependencies).then(() => { |
| 67 | if (!checkCreateApi(createApi)) { |
| 68 | cb(undefined, []); |
| 69 | return Promise.resolve(); |
| 70 | } |
| 71 | |
| 72 | return runPilets(createApi, pilets, hooks).then((integratedPilets) => { |
| 73 | if (async && integratedPilets.length > 0) { |
| 74 | cb(undefined, [...integratedPilets]); |
| 75 | } |
| 76 | |
| 77 | const followUp = loadingAll.then((metadata) => { |
| 78 | const promises = metadata.map((m) => |
| 79 | loadSingle(m).then((app) => { |
| 80 | const available = pilets.filter((m) => m.name === app.name).length === 0; |
| 81 | |
| 82 | if (available) { |
| 83 | return runPilet(createApi, app, hooks).then((additionalPilet) => { |
| 84 | integratedPilets.push(additionalPilet); |
| 85 | |
| 86 | if (async) { |
| 87 | cb(undefined, [...integratedPilets]); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | return Promise.all(promises).then(() => { |
| 95 | if (!async) { |
| 96 | cb(undefined, integratedPilets); |
| 97 | } |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | if (async) { |
| 102 | followUp.catch(() => {}); |
| 103 | return loadingAll.then(); |
| 104 | } else { |
| 105 | return followUp.then(); |
| 106 | } |
| 107 | }); |
| 108 | }); |
no test coverage detected