MCPcopy
hub / github.com/smapiot/piral / createProgressiveStrategy

Function createProgressiveStrategy

src/framework/piral-base/src/strategies.ts:51–110  ·  view source on GitHub ↗
(async: boolean)

Source from the content-addressed store, hash-verified

49 * @param async Uses the asynchronous mode.
50 */
51export 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 });

Callers 2

strategies.test.tsFile · 0.90
blazingStrategyFunction · 0.85

Calls 11

getDefaultLoaderFunction · 0.90
loadMetadataFunction · 0.90
extendLoaderFunction · 0.90
registerDependenciesFunction · 0.90
checkCreateApiFunction · 0.90
runPiletsFunction · 0.90
runPiletFunction · 0.90
mapMethod · 0.80
pushMethod · 0.80
cbFunction · 0.70
filterMethod · 0.65

Tested by

no test coverage detected