Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Mar 7, 2024
1 parent 967a1f5 commit a98b21e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 78 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let res = delay.run(d)
The result of `delay_effect` is really just a first class function with a nice API wrapper. It isn't executed until put through one of `run/1`, `drain/1` or `fallthrough/1`. And can be called as many times as you want.

```gleam
import gleam/io
import delay
let d = delay.delay_effect(fn() {
Expand Down
45 changes: 22 additions & 23 deletions dist/delay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,93 @@
/**
* Type representing a delayed effect to be lazily evaluated
*/
export type Delay$<FXE, FXD> = Continue<FXD, FXE> | Stop<FXE>
export type Delay$<FXE, FXD> = Continue<FXE, FXD> | Stop<FXE>


/**
* store an effect to be run later
* if `f` returns an Error then chain will stop
* Stores an effect to be run later, short circuiting on errors
*/
export function delay_effect<FXF, FXG>(f: () => Result<FXF, FXG>): Delay$<FXF, FXG>
export function delay_effect<FXF, FXG>(func: () => Result<FXF, FXG>): Delay$<FXF, FXG>


/**
* chains an operation onto an existing delay to be run one then into the next
* if delayed has already error'd then `f` will be ignored
* Chains an operation onto an existing delay. The result of the current delay will be lazily passed to `func`
* `func` will not be called if the delay has already returned an error
*/
export function map<FXL, FXM, FXP>(delayed: Delay$<FXL, FXM>, f: (x0: FXL) => Result<FXP, FXM>): Delay$<FXP, FXM>
export function map<FXL, FXM, FXP>(delayed: Delay$<FXL, FXM>, func: (x0: FXL) => Result<FXP, FXM>): Delay$<FXP, FXM>


/**
* flatten nested Delay
* flatten a nested Delay
*/
export function flatten<FYD, FYE>(delayed: Delay$<Delay$<FYD, FYE>, FYE>): Delay$<FYD, FYE>


/**
* map and then flatten Delay
* Map and then flatten `delayed`
*/
export function flat_map<FYL, FYM, FYP>(
delayed: Delay$<FYL, FYM>,
f: (x0: FYL) => Result<Delay$<FYP, FYM>, FYM>
func: (x0: FYL) => Result<Delay$<FYP, FYM>, FYM>
): Delay$<FYP, FYM>


/**
* run a delayed effect and get the result
* short-circuiting if any in the chain returns an Error
* Run a delayed effect and get the result
* short-circuiting if any in delay in the chain returns an Error
*/
export function run<FZO, FZP>(delayed: Delay$<FZO, FZP>): Result<FZO, FZP>


/**
* returns a Delay that will be re-attempted `retries` times with `delay` ms in between
* Returns a new Delay that will be re-attempted `retries` times with `delay` ms in-between
* NOTE: `delay` is ignored in JS
*/
export function retry<FYW, FYX>(delayed: Delay$<FYW, FYX>, retries: number, delay: number): Delay$<FYW, FYX>


/**
* returns a Delay that will be re-attempted `retries` times with `delay` ms in between
* Returns a new Delay that will be re-attempted `retries` times with `delay` ms in-between
* NOTE: `delay` is ignored in JS
*/
export function retry_with_backoff<FZC, FZD>(delayed: Delay$<FZC, FZD>, retries: number): Delay$<FZC, FZD>


/**
* run a delayed effect and throw away the result
* Run a delayed effect and throw away the result
* short-circuiting if any in the chain returns an Error
*/
export function drain(delayed: Delay$<any, any>): null


/**
* run every effect in sequence and get their results
* Run every effect in sequence and get their results
*/
export function every<GAF, GAG>(effects: List<Delay$<GAF, GAG>>): List<Result<GAF, GAG>>


/**
* repeat a Delay and return the results in a list
* Repeat a Delay and return the results in a list
*/
export function repeat<FZY, FZZ>(delayed: Delay$<FZY, FZZ>, repetition: number): List<Result<FZY, FZZ>>


/**
* run all effects in sequence and return True if all succeed
* note this will _always_ run _every_ effect
* Run all effects in sequence and return True if all succeed
* NOTE: this will _always_ run _every_ effect
*/
export function all(effects: List<Delay$<any, any>>): boolean


/**
* run all effects in sequence and return True if any succeeds
* note this is different than `fallthrough/1` because it will _always_ run _every_ effect
* Run all effects in sequence and return True if any succeeds
* NOTE: this is different than `fallthrough/1` because it will _always_ run _every_ effect
*/
export function any(effects: List<Delay$<any, any>>): boolean


/**
* attempt multiple Delays until one returns an Ok
* Attempt multiple Delays until one returns an Ok
* unlike `any/1` this will short circuit on the first Ok
*/
export function fallthrough<GBI, GBJ>(effects: List<Delay$<GBI, GBJ>>): Result<GBI, GBJ>
Expand Down Expand Up @@ -168,7 +167,7 @@ export function divideInt(a: number, b: number): number

export function divideFloat(a: number, b: number): number

export class Continue<FXD, FXE> extends CustomType {
export class Continue<FXE, FXD> extends CustomType {
constructor(effect: () => Result<any, any>)
effect(): Result<any, any>
}
Expand Down
55 changes: 27 additions & 28 deletions dist/delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,28 +320,27 @@ var Stop = class extends CustomType {
}

/**
* store an effect to be run later
* if `f` returns an Error then chain will stop
* Stores an effect to be run later, short circuiting on errors
*/
function delay_effect(f) {
return new Continue(f)
function delay_effect(func) {
return new Continue(func)
}
__name(delay_effect, "delay_effect")
function chain(delayed_f, f) {
function chain(delayed_func, func) {
return () => {
return try$(delayed_f(), f)
return try$(delayed_func(), func)
}
}
__name(chain, "chain")

/**
* chains an operation onto an existing delay to be run one then into the next
* if delayed has already error'd then `f` will be ignored
* Chains an operation onto an existing delay. The result of the current delay will be lazily passed to `func`
* `func` will not be called if the delay has already returned an error
*/
function map3(delayed, f) {
function map3(delayed, func) {
if (delayed instanceof Continue) {
let delayed_f = delayed.effect
let _pipe = chain(delayed_f, f)
let _pipe = chain(delayed_f, func)
return delay_effect(_pipe)
} else {
let err = delayed.err
Expand All @@ -351,7 +350,7 @@ function map3(delayed, f) {
__name(map3, "map")

/**
* flatten nested Delay
* flatten a nested Delay
*/
function flatten(delayed) {
let _pipe = (() => {
Expand Down Expand Up @@ -388,11 +387,11 @@ function flatten(delayed) {
__name(flatten, "flatten")

/**
* map and then flatten Delay
* Map and then flatten `delayed`
*/
function flat_map(delayed, f) {
function flat_map(delayed, func) {
let _pipe = delayed
let _pipe$1 = map3(_pipe, f)
let _pipe$1 = map3(_pipe, func)
return flatten(_pipe$1)
}
__name(flat_map, "flat_map")
Expand All @@ -402,8 +401,8 @@ function sleep(_) {
__name(sleep, "sleep")

/**
* run a delayed effect and get the result
* short-circuiting if any in the chain returns an Error
* Run a delayed effect and get the result
* short-circuiting if any in delay in the chain returns an Error
*/
function run(delayed) {
if (delayed instanceof Continue) {
Expand Down Expand Up @@ -436,7 +435,7 @@ function do_retry(delayed, retries, delay, backoff) {
__name(do_retry, "do_retry")

/**
* returns a Delay that will be re-attempted `retries` times with `delay` ms in between
* Returns a new Delay that will be re-attempted `retries` times with `delay` ms in-between
* NOTE: `delay` is ignored in JS
*/
function retry(delayed, retries, delay) {
Expand All @@ -447,7 +446,7 @@ function retry(delayed, retries, delay) {
__name(retry, "retry")

/**
* returns a Delay that will be re-attempted `retries` times with `delay` ms in between
* Returns a new Delay that will be re-attempted `retries` times with `delay` ms in-between
* NOTE: `delay` is ignored in JS
*/
function retry_with_backoff(delayed, retries) {
Expand All @@ -458,7 +457,7 @@ function retry_with_backoff(delayed, retries) {
__name(retry_with_backoff, "retry_with_backoff")

/**
* run a delayed effect and throw away the result
* Run a delayed effect and throw away the result
* short-circuiting if any in the chain returns an Error
*/
function drain(delayed) {
Expand All @@ -479,22 +478,22 @@ function do_every(loop$effects, loop$results) {
loop$effects = rest
loop$results = toList([run(head)], results)
} else {
throw makeError("todo", "delay", 177, "do_every", "Empty list", {})
throw makeError("todo", "delay", 176, "do_every", "Empty list", {})
}
}
}
__name(do_every, "do_every")

/**
* run every effect in sequence and get their results
* Run every effect in sequence and get their results
*/
function every(effects) {
return do_every(effects, toList([]))
}
__name(every, "every")

/**
* repeat a Delay and return the results in a list
* Repeat a Delay and return the results in a list
*/
function repeat2(delayed, repetition) {
let _pipe = delayed
Expand All @@ -504,8 +503,8 @@ function repeat2(delayed, repetition) {
__name(repeat2, "repeat")

/**
* run all effects in sequence and return True if all succeed
* note this will _always_ run _every_ effect
* Run all effects in sequence and return True if all succeed
* NOTE: this will _always_ run _every_ effect
*/
function all2(effects) {
let _pipe = effects
Expand All @@ -516,8 +515,8 @@ function all2(effects) {
__name(all2, "all")

/**
* run all effects in sequence and return True if any succeeds
* note this is different than `fallthrough/1` because it will _always_ run _every_ effect
* Run all effects in sequence and return True if any succeeds
* NOTE: this is different than `fallthrough/1` because it will _always_ run _every_ effect
*/
function any(effects) {
return (
Expand All @@ -541,13 +540,13 @@ function do_fallthrough(effects) {
return fallthrough(rest)
})
} else {
throw makeError("todo", "delay", 192, "do_fallthrough", "Empty list", {})
throw makeError("todo", "delay", 191, "do_fallthrough", "Empty list", {})
}
}
__name(do_fallthrough, "do_fallthrough")

/**
* attempt multiple Delays until one returns an Ok
* Attempt multiple Delays until one returns an Ok
* unlike `any/1` this will short circuit on the first Ok
*/
function fallthrough(effects) {
Expand Down

0 comments on commit a98b21e

Please sign in to comment.