Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions fx/parallel.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { describe, it } from "bdd";
import { expect } from "expect";
import { each, Err, Ok, run, sleep, spawn } from "npm:effection@3.0.3";
import {
each,
Err,
Ok,
run,
sleep,
spawn,
until,
} from "npm:effection@4.0.0-alpha.8";

import { parallel } from "./parallel.ts";

import type { Operation, Result } from "npm:effection@3.0.3";
import type { Operation, Result } from "npm:effection@4.0.0-alpha.8";

const test = describe("parallel()");

Expand Down Expand Up @@ -130,7 +138,7 @@ it(test, "should resolve all async items", async () => {
yield* sleep(15);
two.resolve(2);
});
const results = yield* parallel([one, () => two.promise]);
const results = yield* parallel([one, () => until(two.promise)]);
return yield* results;
});

Expand All @@ -144,7 +152,10 @@ it(test, "should stop all operations when there is an error", async () => {

function* genFn() {
try {
const results = yield* parallel([() => one.promise, () => two.promise]);
const results = yield* parallel([
() => until(one.promise),
() => until(two.promise),
]);
actual = yield* results;
} catch (_) {
actual = [Err(new Error("should not get hit"))];
Expand Down
19 changes: 11 additions & 8 deletions fx/parallel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Callable, Channel, Operation, Result } from "npm:effection@3.0.3";
import { createChannel, resource, spawn } from "npm:effection@3.0.3";
import type {
Channel,
Operation,
Result,
Task,
} from "npm:effection@4.0.0-alpha.8";
import { createChannel, resource, spawn } from "npm:effection@4.0.0-alpha.8";

import { safe } from "./safe.ts";

import type { Computation } from "./type.ts";
export interface ParallelRet<T> extends Computation<Result<T>[]> {
export interface ParallelRet<T> extends Operation<Result<T>[]> {
sequence: Channel<Result<T>, void>;
immediate: Channel<Result<T>, void>;
}
Expand Down Expand Up @@ -61,16 +65,16 @@ export interface ParallelRet<T> extends Computation<Result<T>[]> {
* }
* ```
*/
export function parallel<T>(
operations: Callable<T>[],
export function parallel<T, TArgs extends unknown[] = []>(
operations: ((...args: TArgs) => Operation<T>)[],
): Operation<ParallelRet<T>> {
const sequence = createChannel<Result<T>>();
const immediate = createChannel<Result<T>>();
const results: Result<T>[] = [];

return resource<ParallelRet<T>>(function* (provide) {
const task = yield* spawn(function* () {
const tasks = [];
const tasks = [] as Task<Result<T>>[];
for (const op of operations) {
tasks.push(
yield* spawn(function* () {
Expand All @@ -95,7 +99,6 @@ export function parallel<T>(
yield* task;
return results;
}

yield* provide({
sequence,
immediate,
Expand Down
2 changes: 1 addition & 1 deletion fx/race.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "bdd";
import { expect } from "expect";
import { run, sleep } from "npm:effection@3.0.3";
import { run, sleep } from "npm:effection@4.0.0-alpha.8";

import { raceMap } from "./race.ts";

Expand Down
29 changes: 17 additions & 12 deletions fx/race.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// deno-lint-ignore-file no-explicit-any
import type { Callable, Operation, Task } from "npm:effection@3.0.3";
import { action, call, resource, spawn } from "npm:effection@3.0.3";
import type { Operation, Task } from "npm:effection@4.0.0-alpha.8";
import { resource, spawn, withResolvers } from "npm:effection@4.0.0-alpha.8";

interface OpMap<T = unknown> {
[key: string]: Callable<T>;
interface OpMap<T = unknown, TArgs extends unknown[] = []> {
[key: string]: (...args: TArgs) => Operation<T>;
}

export function raceMap<T>(opMap: OpMap): Operation<
Expand All @@ -16,21 +16,26 @@ export function raceMap<T>(opMap: OpMap): Operation<
return resource(function* Race(provide) {
const keys = Object.keys(opMap);
const taskMap: { [key: string]: Task<unknown> } = {};
const resultMap: { [key: keyof OpMap]: OpMap[keyof OpMap] } = {};
const resultMap: { [key: keyof OpMap]: ReturnType<OpMap[keyof OpMap]> } =
{};

function* start() {
const resolvers = withResolvers();

const winner = yield* action<Task<unknown>>(function* (resolve) {
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
yield* spawn(function* () {
const task = yield* spawn(function* () {
yield* call(opMap[key] as any);
});
const task = yield* spawn(opMap[key]);
taskMap[key] = task;
(resultMap as any)[key] = yield* task;
resolve(task);
(resultMap[key] as any) = yield* task;
resolvers.resolve(task);
});
}
});

return yield* resolvers.operation;
}

const winner = yield* start();

for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
Expand Down
2 changes: 1 addition & 1 deletion fx/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "bdd";
import { expect } from "expect";
import { run } from "npm:effection@3.0.3";
import { run } from "npm:effection@4.0.0-alpha.8";

import { json, request } from "./request.ts";

Expand Down
10 changes: 7 additions & 3 deletions fx/request.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { call, type Operation, useAbortSignal } from "npm:effection@3.0.3";
import {
type Operation,
until,
useAbortSignal,
} from "npm:effection@4.0.0-alpha.8";

export function* request(
url: string | URL | Request,
opts?: RequestInit,
): Operation<Response> {
const signal = yield* useAbortSignal();
const response = yield* call(() => fetch(url, { signal, ...opts }));
const response = yield* until(fetch(url, { signal, ...opts }));
return response;
}

export function* json(response: Response): Operation<unknown> {
return yield* call(() => response.json());
return yield* until(response.json());
}
2 changes: 1 addition & 1 deletion fx/safe.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "bdd";
import { expect } from "expect";
import { call, run } from "npm:effection@3.0.3";
import { call, run } from "npm:effection@4.0.0-alpha.8";

const tests = describe("call()");

Expand Down
11 changes: 6 additions & 5 deletions fx/safe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Callable, Operation, Result } from "npm:effection@3.0.3";
import { call, Err, Ok } from "npm:effection@3.0.3";
import type { Operation, Result } from "npm:effection@4.0.0-alpha.8";
import { call, Err, Ok } from "npm:effection@4.0.0-alpha.8";

/**
* The goal of `safe` is to wrap Operations to prevent them from raising
Expand All @@ -23,10 +23,11 @@ function isError(error: unknown): error is Error {
return error instanceof Error;
}

export function* safe<T>(operator: Callable<T>): Operation<Result<T>> {
export function* safe<T, TArgs extends unknown[] = []>(
operator: (...args: TArgs) => Operation<T>,
): Operation<Result<T>> {
try {
// deno-lint-ignore no-explicit-any
const value = yield* call<T>(operator as any);
const value = yield* call(operator);
return Ok(value);
} catch (error) {
return Err(isError(error) ? error : new Error(String(error)));
Expand Down
5 changes: 0 additions & 5 deletions fx/type.ts

This file was deleted.