Skip to content

Commit 0ba8f66

Browse files
committed
refactor: make result even more elm-y
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.
1 parent d808c30 commit 0ba8f66

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

lib/core.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

lib/result.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Port of `elm/core` primitives to TS.
3+
* Enables functional-style programming without having to pull in `Effect.TS`.
4+
*/
5+
6+
/**
7+
* @import {Fail, Success, Result} from './types/result';
8+
*/
9+
10+
const {intoError} = require('./utils');
11+
12+
/**
13+
* Create a successful result.
14+
*
15+
* @template Value
16+
*
17+
* @param {Value} value
18+
* @returns {Success<Value>}
19+
*/
20+
function succeed(value) {
21+
return {tag: 'ok', value};
22+
}
23+
24+
/**
25+
* Create a failed result.
26+
*
27+
* @template Failure
28+
*
29+
* @param {Failure} failure
30+
* @returns {Fail<Failure>}
31+
*/
32+
function fail(failure) {
33+
return {tag: 'fail', failure};
34+
}
35+
36+
/**
37+
* Returns the value of a result, or throws if in an errored state.
38+
*
39+
* @remarks
40+
* Converts errors into {@linkcode Error}s before throwing.
41+
* For more details, see {@linkcode intoError}.
42+
*
43+
* @template Value
44+
*
45+
* @param {Result<unknown, Value>} value
46+
* @returns {Value}
47+
* @throws {Error}
48+
*/
49+
function orThrow(value) {
50+
if (value.tag === 'ok') {
51+
return value.value;
52+
}
53+
54+
throw intoError(value.failure);
55+
}
56+
57+
module.exports = {
58+
succeed,
59+
fail,
60+
orThrow
61+
};

lib/sync-get-worker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const https = require('node:https');
77
const workerThreads = require('node:worker_threads');
8-
const {result} = require('./core');
8+
const Result = require('./result');
99

1010
/** @type {WorkerThreads<WorkerData>} */
1111
const {parentPort, workerData} = workerThreads;
@@ -17,11 +17,11 @@ if (parentPort) {
1717
try {
1818
const response = await getBody(url);
1919
requestPort.postMessage(
20-
/** @satisfies {PortResponse} */ (result.succeed(response))
20+
/** @satisfies {PortResponse} */ (Result.succeed(response))
2121
);
2222
} catch (error) {
2323
requestPort.postMessage(
24-
/** @satisfies {PortResponse} */ (result.fail(error))
24+
/** @satisfies {PortResponse} */ (Result.fail(error))
2525
);
2626
}
2727

lib/sync-get.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
MessageChannel,
99
receiveMessageOnPort
1010
} = require('node:worker_threads');
11-
const {result} = require('./core');
11+
const Result = require('./result');
1212

1313
/**
1414
* Start a worker thread and return a `syncGetWorker`
@@ -43,7 +43,7 @@ function startWorker() {
4343
return '';
4444
}
4545

46-
return result.orThrow(response.message);
46+
return Result.orThrow(response.message);
4747
}
4848

4949
/**

lib/types/core.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/types/result.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Result<Error, Value> = Success<Value> | Fail<Error>;
2+
export type Success<Value> = {tag: 'ok'; value: Value};
3+
export type Fail<Failure> = {tag: 'fail'; failure: Failure};

lib/types/sync-get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {MessagePort} from 'node:worker_threads';
2-
import type {Result} from './core.ts';
2+
import type {Result} from './result.js';
33

44
export type WorkerData = {
55
sharedLock: SharedArrayBuffer;

0 commit comments

Comments
 (0)