File tree Expand file tree Collapse file tree 7 files changed +70
-70
lines changed Expand file tree Collapse file tree 7 files changed +70
-70
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 5
5
6
6
const https = require ( 'node:https' ) ;
7
7
const workerThreads = require ( 'node:worker_threads' ) ;
8
- const { result } = require ( './core ' ) ;
8
+ const Result = require ( './result ' ) ;
9
9
10
10
/** @type {WorkerThreads<WorkerData> } */
11
11
const { parentPort, workerData} = workerThreads ;
@@ -17,11 +17,11 @@ if (parentPort) {
17
17
try {
18
18
const response = await getBody ( url ) ;
19
19
requestPort . postMessage (
20
- /** @satisfies {PortResponse } */ ( result . succeed ( response ) )
20
+ /** @satisfies {PortResponse } */ ( Result . succeed ( response ) )
21
21
) ;
22
22
} catch ( error ) {
23
23
requestPort . postMessage (
24
- /** @satisfies {PortResponse } */ ( result . fail ( error ) )
24
+ /** @satisfies {PortResponse } */ ( Result . fail ( error ) )
25
25
) ;
26
26
}
27
27
Original file line number Diff line number Diff line change 8
8
MessageChannel,
9
9
receiveMessageOnPort
10
10
} = require ( 'node:worker_threads' ) ;
11
- const { result } = require ( './core ' ) ;
11
+ const Result = require ( './result ' ) ;
12
12
13
13
/**
14
14
* Start a worker thread and return a `syncGetWorker`
@@ -43,7 +43,7 @@ function startWorker() {
43
43
return '' ;
44
44
}
45
45
46
- return result . orThrow ( response . message ) ;
46
+ return Result . orThrow ( response . message ) ;
47
47
}
48
48
49
49
/**
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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 } ;
Original file line number Diff line number Diff line change 1
1
import type { MessagePort } from 'node:worker_threads' ;
2
- import type { Result } from './core.ts ' ;
2
+ import type { Result } from './result.js ' ;
3
3
4
4
export type WorkerData = {
5
5
sharedLock : SharedArrayBuffer ;
You can’t perform that action at this time.
0 commit comments