-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪚 OmniGraph™ Delusional code of the utterly deranged (#112)
- Loading branch information
1 parent
0c28371
commit dcf1bbb
Showing
14 changed files
with
354 additions
and
36 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// add all jest-extended matchers | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
expect.extend(require('jest-extended')); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './promise' |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Factory } from '@/types' | ||
import assert from 'assert' | ||
|
||
/** | ||
* Helper type for argumentless factories a.k.a. tasks | ||
*/ | ||
type Task<T> = Factory<[], T> | ||
|
||
/** | ||
* Executes tasks in sequence, waiting for each one to finish before starting the next one | ||
* | ||
* Will resolve with the output of all tasks or reject with the first rejection. | ||
* | ||
* @param {Task<T>[]} tasks | ||
* @returns {Promise<T[]>} | ||
*/ | ||
export const sequence = async <T>(tasks: Task<T>[]): Promise<T[]> => { | ||
const collector: T[] = [] | ||
|
||
for (const task of tasks) { | ||
collector.push(await task()) | ||
} | ||
|
||
return collector | ||
} | ||
|
||
/** | ||
* Executes tasks in parallel | ||
* | ||
* Will resolve with the output of all tasks or reject with the any rejection. | ||
* | ||
* @param {Task<T>[]} tasks | ||
* @returns {Promise<T[]>} | ||
*/ | ||
export const parallel = async <T>(tasks: Task<T>[]): Promise<T[]> => await Promise.all(tasks.map((task) => task())) | ||
|
||
/** | ||
* Executes tasks in a sequence until one resolves. | ||
* | ||
* Will resolve with the output of the first task that resolves | ||
* or reject with the last rejection. | ||
* | ||
* Will reject immediatelly if no tasks have been passed | ||
* | ||
* @param {Task<T>[]} tasks | ||
* @returns {Promise<T>} | ||
*/ | ||
export const first = async <T>(tasks: Task<T>[]): Promise<T> => { | ||
assert(tasks.length !== 0, `Must have at least one task for first()`) | ||
|
||
let lastError: unknown | ||
|
||
for (const task of tasks) { | ||
try { | ||
return await task() | ||
} catch (error) { | ||
lastError = error | ||
} | ||
} | ||
|
||
throw lastError | ||
} | ||
|
||
/** | ||
* Helper utility for currying first() - creating a function | ||
* that behaves like first() but accepts arguments that will be passed to the factory functions | ||
* | ||
* @param {Factory<TInput, TOutput>[]} factories | ||
* @returns {Factory<TInput, TOutput>} | ||
*/ | ||
export const firstFactory = | ||
<TInput extends unknown[], TOutput>(...factories: Factory<TInput, TOutput>[]): Factory<TInput, TOutput> => | ||
async (...input) => | ||
await first(factories.map((factory) => () => factory(...input))) |
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,3 +1,4 @@ | ||
export * from './common' | ||
export * from './omnigraph' | ||
export * from './transactions' | ||
export * from './types' |
Oops, something went wrong.