Effection Logo
@effection-contrib/fxv0.1.0thefrontside/effection-contrib
JSR BadgeNPM Badge with published versionBundle size badgeDependency count badgeTree shaking support badge
import { } from "@effection-contrib/fx"

@effection-contrib/fx

A collection of utility functions to streamline asynchronous workflows. These functions were originally part of the starfx project and have been adapted for use with the Effection framework.

API Reference

interface ParallelRet<T> extends Computation<Result<T>[]>

Type Parameters

T

Properties

sequence: Channel<Result<T>, void>

No documentation available.

immediate: Channel<Result<T>, void>

No documentation available.

function parallel<T>(operations: Callable<T>[]): Operation<ParallelRet<T>>

The goal of parallel is to make it easier to cooridnate multiple async operations in parallel, with different ways to receive completed tasks.

All tasks are called with fx which means they will never throw an exception. Instead all tasks will return a Result object that the end development must evaluate in order to grab the value.

Examples

Example 1
import { parallel } from "starfx";

function* run() {
 const task = yield* parallel([job1, job2]);
 // wait for all tasks to complete before moving to next yield point
 const results = yield* task;
 // job1 = results[0];
 // job2 = results[1];
}

Instead of waiting for all tasks to complete, we can instead loop over tasks as they arrive:


Example 2
function* run() {
 const task = yield* parallel([job1, job2]);
 for (const job of yield* each(task.immediate)) {
   // job2 completes first then it will be first in list
   console.log(job);
   yield* each.next();
 }
}

Or we can instead loop over tasks in order of the array provided to parallel:


Example 3
function* run() {
 const task = yield* parallel([job1, job2]);
 for (const job of yield* each(task.sequence)) {
   // job1 then job2 will be returned regardless of when the jobs
   // complete
   console.log(job);
   yield* each.next();
 }
}

Type Parameters

T

Parameters

operations: Callable<T>[]

Return Type

Operation<ParallelRet<T>>

function* safe<T>(operator: Callable<T>): Operation<Result<T>>

Type Parameters

T

Parameters

operator: Callable<T>

Return Type

Operation<Result<T>>

function raceMap<T>(opMap: OpMap): Operation<>

Type Parameters

T

Parameters

opMap: OpMap

Return Type

Operation<{}>

function* request(url: string | URL | Request, opts?: RequestInit): Operation<Response>

Parameters

url: string | URL | Request

optsoptional: RequestInit

Return Type

Operation<Response>

function* json(response: Response): Operation<unknown>

Parameters

response: Response

Return Type

Operation<unknown>