Skip to content

Commit

Permalink
Reimplement all with compposables
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed May 22, 2024
1 parent 2b42134 commit 033198e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
45 changes: 41 additions & 4 deletions src/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,38 @@ import type {
} from './types.ts'
import * as Future from 'npm:composable-functions@beta'

function composableToDF<R>(
function toComposable<R>(
df: DomainFunction<R>,
): Future.Composable<(input?: unknown, environment?: unknown) => R> {
return (async (input?: unknown, environment?: unknown) => {
const result = await df(input, environment)

if (result.success) {
return {
success: true,
data: result.data,
errors: [],
inputErrors: [],
environmentErrors: [],
}
} else {
return {
success: false,
errors: [
...result.errors.map((e) => e.exception ?? new Error(e.message)),
...result.inputErrors.map(
(e) => new Future.InputError(e.message, e.path),
),
...result.environmentErrors.map(
(e) => new Future.EnvironmentError(e.message, e.path),
),
],
}
}
}) as Future.Composable<(input?: unknown, environment?: unknown) => R>
}

function fromComposable<R>(
cf: Future.Composable<(inout?: unknown, environment?: unknown) => R>,
) {
return (async (input?: unknown, environment?: unknown) => {
Expand Down Expand Up @@ -92,7 +123,7 @@ function composableToDF<R>(
* }
*/
async function safeResult<T>(fn: () => T): Promise<Result<T>> {
return await composableToDF(Future.composable(fn))()
return await fromComposable(Future.composable(fn))()
}

/**
Expand Down Expand Up @@ -129,7 +160,7 @@ function makeDomainFunction<I, E>(
safeParse: () => inputResult,
}

return composableToDF(
return fromComposable(
Future.withSchema(
futureInputSchema as any,
futureEnvSchema as any,
Expand Down Expand Up @@ -164,4 +195,10 @@ const undefinedSchema: ParserSchema<undefined> = {
},
}

export { makeDomainFunction, makeDomainFunction as mdf, safeResult }
export {
makeDomainFunction,
makeDomainFunction as mdf,
safeResult,
fromComposable,
toComposable
}
25 changes: 5 additions & 20 deletions src/domain-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import type {
} from './types.ts'
import type { Last } from './types.ts'
import type { SuccessResult } from './types.ts'
import { safeResult } from './constructor.ts'
import { safeResult, fromComposable, toComposable } from './constructor.ts'
import * as Future from 'npm:composable-functions@beta'

/**
* Creates a single domain function out of multiple domain functions. It will pass the same input and environment to each provided function. The functions will run in parallel. If all constituent functions are successful, The data field will be a tuple containing each function's output.
Expand All @@ -29,25 +30,9 @@ const df = all(a, b, c)
function all<Fns extends DomainFunction[]>(
...fns: Fns
): DomainFunction<UnpackAll<Fns>> {
return ((input, environment) => {
return safeResult(async () => {
const results = await Promise.all(
fns.map((fn) => (fn as DomainFunction)(input, environment)),
)

if (!isListOfSuccess(results)) {
throw new ResultError({
errors: results.map(({ errors }) => errors).flat(),
inputErrors: results.map(({ inputErrors }) => inputErrors).flat(),
environmentErrors: results
.map(({ environmentErrors }) => environmentErrors)
.flat(),
})
}

return results.map(({ data }) => data)
})
}) as DomainFunction<UnpackAll<Fns>>
return fromComposable(Future.all(...fns.map(toComposable))) as DomainFunction<
UnpackAll<Fns>
>
}

/**
Expand Down

0 comments on commit 033198e

Please sign in to comment.