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 committed Nov 10, 2024
1 parent d808c30 commit 0fc215e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 69 deletions.
61 changes: 0 additions & 61 deletions lib/core.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Result = require('./result');

module.exports = {
Result
};
61 changes: 61 additions & 0 deletions lib/core/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
};
3 changes: 3 additions & 0 deletions lib/core/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};
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('./core');

/** @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('./core');

/**
* 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.

0 comments on commit 0fc215e

Please sign in to comment.