Skip to content

Commit

Permalink
refactor: make result even more elm-y
Browse files Browse the repository at this point in the history
I started with uppercasing the `R`.
Then, I replace  `Err` with `Failure` so it doesn't get mixed up with native JS errors.
I did the same with `Ok` and `Success`, so that the constructors line up.
I tried uppercasing the constructors as well, to make it more elmish.*
However, Eslint triggers `new-cap`, so I skipped that one and kept the Task terminology.
Also, because objects are really inconvenient,
I moved it to a folder and used evil barrel exports.

* I tried to come up with a Fable pun, but I couldn't think of one.
  • Loading branch information
lishaduck authored and jfmengels committed Nov 10, 2024
1 parent 42b9055 commit 6e4e6ea
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 70 deletions.
61 changes: 0 additions & 61 deletions lib/core.js

This file was deleted.

61 changes: 61 additions & 0 deletions lib/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Port of `elm/core` primitives to TS.
* Enables functional-style programming without having to pull in `Effect.TS`.
*/

/**
* @import {Fail, Success, Result} from './types/result';
*/

const {intoError} = require('./utils');

/**
* Create a successful result.
*
* @template Value
*
* @param {Value} value
* @returns {Success<Value>}
*/
function succeed(value) {
return {tag: 'ok', value};
}

/**
* Create a failed result.
*
* @template Failure
*
* @param {Failure} failure
* @returns {Fail<Failure>}
*/
function fail(failure) {
return {tag: 'fail', failure};
}

/**
* Returns the value of a result, or throws if in an errored state.
*
* @remarks
* Converts errors into {@linkcode Error}s before throwing.
* For more details, see {@linkcode intoError}.
*
* @template Value
*
* @param {Result<unknown, Value>} value
* @returns {Value}
* @throws {Error}
*/
function orThrow(value) {
if (value.tag === 'ok') {
return value.value;
}

throw intoError(value.failure);
}

module.exports = {
succeed,
fail,
orThrow
};
6 changes: 3 additions & 3 deletions lib/sync-get-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const https = require('node:https');
const workerThreads = require('node:worker_threads');
const {result} = require('./core');
const Result = require('./result');

/** @type {WorkerThreads<WorkerData>} */
const {parentPort, workerData} = workerThreads;
Expand All @@ -17,11 +17,11 @@ if (parentPort) {
try {
const response = await getBody(url);
requestPort.postMessage(
/** @satisfies {PortResponse} */ (result.succeed(response))
/** @satisfies {PortResponse} */ (Result.succeed(response))
);
} catch (error) {
requestPort.postMessage(
/** @satisfies {PortResponse} */ (result.fail(error))
/** @satisfies {PortResponse} */ (Result.fail(error))
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/sync-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
MessageChannel,
receiveMessageOnPort
} = require('node:worker_threads');
const {result} = require('./core');
const Result = require('./result');

/**
* Start a worker thread and return a `syncGetWorker`
Expand Down Expand Up @@ -43,7 +43,7 @@ function startWorker() {
return '';
}

return result.orThrow(response.message);
return Result.orThrow(response.message);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions lib/types/core.ts

This file was deleted.

3 changes: 3 additions & 0 deletions lib/types/result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Result<Error, Value> = Success<Value> | Fail<Error>;
export type Success<Value> = {tag: 'ok'; value: Value};
export type Fail<Failure> = {tag: 'fail'; failure: Failure};
2 changes: 1 addition & 1 deletion lib/types/sync-get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {MessagePort} from 'node:worker_threads';
import type {Result} from './core.ts';
import type {Result} from './result.js';

export type WorkerData = {
sharedLock: SharedArrayBuffer;
Expand Down

0 comments on commit 6e4e6ea

Please sign in to comment.