Closed urgent closed 4 years ago
Required for implementation:
If decode fails, mapped with error. How to detect threshold? Was just a count. Now need another loop.
For errors, need to emit in effect exec, or make needs deps.
Moved tasks to first comment
A reader patter avoids having to pass input
input1(deps) => input2(deps) => input3(deps) vs
input1() => input2() => input3() => deps => code
you ask for deps for last return
problem is if deps are not in series, or used in multiple places, gets confusing:
input1() => input2(deps) => input3() => input4() => input5(deps) => input6() => deps => code
~use an ask~
Combine multiple validation rules, receive all errors.
https://dev.to/gcanti/getting-started-with-fp-ts-either-vs-validation-5eja
Start with input in a functor. Chain functions to manipulate that input
Use t.Decoder<unknown, A> for generic decoder
export const decodeWith = function<A> (decoder: t.Decoder<unknown, A>) {
return flow(
decoder.decode,
E.mapLeft((errors: t.Errors) => new Error(failure(errors).join('\n')))
)
}
Use sequence to work on the entire collection, traverse to work on each element in the collection.
Need an applicative, something with of
, map
, and ap
.
Need to look at the applicative definition. With either, just returns right. Sequence returns first error to left.
Lift Either<Error, A>
to Either<NonEmptyArray<A>,A>
with a mapLeft(a => [a])
Use reduce to a fold. Use accumulator.
TE.chain((leads: Lead[]) =>
pipe(
// [Lead, Lead, Lead, Lead, Lead]
leads,
// [Either<Error, Lead>, Either<Error, Lead>, Either<Error, Lead>, Either<Error, Lead>, ]
reduce({ errors: [], leads: [] }, (acc: Result, lead) =>
pipe(
lead,
validate(state),
//side effects
E.fold<Error, Lead, Result>(
(e: Error) => {
acc['errors'] = [...acc['errors'], e]
return acc
},
(lead: Lead) => {
acc['leads'] = [...acc['leads'], lead]
return acc
}
)
)
),
TE.right
)
)
Reader asks for deps last. If something needs currying, such as an event emitter which needs to be removed to prevent memory leaks, it's better if deps are curried. Can't use reader here.
Also with ask, the dependencies parameter still needs to be written out, just in a chain instead of top level function. Really doesn't save much. Only for composition, taking something that doesn't read and turning it into a reader.
Really nice when mapping arrays. Allows Errors to be returned alongside valid, instead of a transversable which stops the enitre result.
Effect Utility
Abstract out of Lead component to utility, to share with other components.
Need component integration and core utility
[x] Remove references to Lead. Use generics.
[x] Merge codec, eq, state, deps into a single reader. Use Ask
[x] Fold errors in pipe to validate.left message.
[x] Better size comparison. Value now contains errors mapped. Filter with typeof.
[x] Error processing in action. deps.emit . Take a look at isEffect. Make effect return errors, and exec to pass an error handler.