the-moby-effect
Version:
Moby/Docker API client built using effect-ts
90 lines • 4.28 kB
JavaScript
/**
* Docker engine callbacks api
*
* @since 1.0.0
*/
import * as Effect from "effect/Effect";
import * as Function from "effect/Function";
import * as Layer from "effect/Layer";
import * as ManagedRuntime from "effect/ManagedRuntime";
import * as Match from "effect/Match";
import * as Predicate from "effect/Predicate";
import * as Stream from "effect/Stream";
import * as DockerEngine from "./DockerEngine.js";
import * as MobyConnection from "./MobyConnection.js";
import * as MobyConvey from "./MobyConvey.js";
/**
* @since 1.0.0
* @category Callbacks
*/
export const runCallbackForEffect = services => effect => callback => Effect.runCallbackWith(services)(effect, {
onExit: callback
});
export function runCallback(services, arity) {
return function (function_) {
if (Effect.isEffect(function_) || Predicate.isUndefined(arity)) {
return callback => runCallbackForEffect(services)(function_)(callback);
}
switch (arity) {
case 0:
return callback => runCallbackForEffect(services)(function_())(callback);
case 1:
return (v, callback) => runCallbackForEffect(services)(function_(v))(callback);
case 2:
return (v, w, callback) => runCallbackForEffect(services)(function_(v, w))(callback);
case 3:
return (v, w, x, callback) => runCallbackForEffect(services)(function_(v, w, x))(callback);
case 4:
return (v, w, x, y, callback) => runCallbackForEffect(services)(function_(v, w, x, y))(callback);
case 5:
return (v, w, x, y, z, callback) => runCallbackForEffect(services)(function_(v, w, x, y, z))(callback);
}
return Function.absurd(arity);
};
}
/**
* Create a callback client for the docker engine
*
* @since 1.0.0
* @category Callbacks
*/
export const callbackClient = async (layer, connectionOptions) => {
const connectionOptionsEffect = Match.value(connectionOptions).pipe(Match.when(Match.undefined, () => MobyConnection.connectionOptionsFromPlatformSystemSocketDefault), Match.when(Effect.isEffect, effect => effect), Match.orElse(options => Effect.succeed(options)));
const unwrappedLayer = connectionOptionsEffect.pipe(Effect.map(options => layer(options)), Layer.unwrap);
const managedRuntime = ManagedRuntime.make(unwrappedLayer);
const services = await managedRuntime.context();
const runCallbackZero = runCallback(services, 0);
const runCallbackSingle = runCallback(services, 1);
// const runCallbackDouble = runCallback(services, 2);
// const runCallbackTriple = runCallback(services, 3);
// const runCallbackQuadruple = runCallback(services, 4);
// const runCallbackQuintuple = runCallback(services, 5);
return {
pull: Function.compose(DockerEngine.pull, Stream.toReadableStreamWith(services)),
build: Function.compose(DockerEngine.build, Stream.toReadableStreamWith(services)),
stop: runCallbackSingle(DockerEngine.stop),
start: runCallbackSingle(DockerEngine.start),
run: runCallbackSingle(DockerEngine.run),
exec: runCallbackSingle(DockerEngine.exec),
execNonBlocking: runCallbackSingle(DockerEngine.execNonBlocking),
execWebsockets: runCallbackSingle(DockerEngine.execWebsockets),
execWebsocketsNonBlocking: Function.compose(DockerEngine.execWebsocketsNonBlocking, effect => Stream.toReadableStreamWith(Stream.fromEffect(effect), services)),
ps: runCallbackSingle(DockerEngine.ps),
push: Function.flow(DockerEngine.push, Stream.toReadableStreamWith(services)),
images: runCallbackSingle(DockerEngine.images),
search: runCallbackSingle(DockerEngine.search),
version: runCallbackZero(DockerEngine.version),
info: runCallbackZero(DockerEngine.info),
ping: runCallbackZero(DockerEngine.ping),
pingHead: runCallbackZero(DockerEngine.pingHead),
followProgressInConsole: Function.pipe(Function.flow(readable => Stream.fromReadableStream({
evaluate: () => readable,
onError: Function.identity
}), MobyConvey.followProgressInConsole), runCallbackSingle),
waitForProgressToComplete: Function.pipe(Function.flow(readable => Stream.fromReadableStream({
evaluate: () => readable,
onError: Function.identity
}), MobyConvey.waitForProgressToComplete), runCallbackSingle)
};
};
//# sourceMappingURL=Callbacks.js.map