-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vms): update all ops to use retry client and effects
- Loading branch information
Showing
12 changed files
with
720 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
export const collapse = <T>(list: T[]): T | never => { | ||
if (list.length === 0) { | ||
throw new Error("Cannot collapse empty list"); | ||
} | ||
import { Effect as E, pipe } from "effect"; | ||
import { UnknownException } from "effect/Cause"; | ||
|
||
return list.reduce((acc, cur) => { | ||
if (acc === undefined) { | ||
return cur; | ||
} | ||
// TODO: serialized to account for objects but could be improved | ||
if (JSON.stringify(acc) !== JSON.stringify(cur)) { | ||
throw new Error(`Element mismatch: ${JSON.stringify(list)}`); | ||
} | ||
return cur; | ||
}); | ||
export const collapse = <T>(list: T[]): E.Effect<T, UnknownException> => { | ||
return pipe( | ||
E.succeed(list), | ||
E.filterOrFail( | ||
(ls) => ls.length > 0, | ||
() => new UnknownException("Empty list"), | ||
), | ||
E.map((ls) => ({ | ||
first: ls[0], | ||
asStrings: ls.map((e) => JSON.stringify(e)), | ||
})), | ||
E.filterOrFail( | ||
({ asStrings }) => asStrings.every((str) => str === asStrings[0]), | ||
() => new UnknownException("Not all elements are equal"), | ||
), | ||
E.map(() => list[0] as T), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.