@typed/fx 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintcache +1 -1
- package/cjs/Env/Env.js +1 -1
- package/cjs/Env/Env.js.map +1 -1
- package/cjs/FiberRef/FiberRef.d.ts +1 -1
- package/cjs/FiberRef/FiberRef.d.ts.map +1 -1
- package/cjs/FiberRef/FiberRef.js +9 -8
- package/cjs/FiberRef/FiberRef.js.map +1 -1
- package/cjs/Fx/Fx.d.ts +11 -0
- package/cjs/Fx/Fx.d.ts.map +1 -1
- package/cjs/Fx/Fx.js +22 -10
- package/cjs/Fx/Fx.js.map +1 -1
- package/cjs/Fx/index.d.ts +2 -0
- package/cjs/Fx/index.d.ts.map +1 -1
- package/cjs/Fx/index.js +2 -0
- package/cjs/Fx/index.js.map +1 -1
- package/cjs/Fx/run.d.ts +9 -0
- package/cjs/Fx/run.d.ts.map +1 -0
- package/cjs/Fx/run.js +22 -0
- package/cjs/Fx/run.js.map +1 -0
- package/cjs/Fx/scheduler.d.ts +14 -0
- package/cjs/Fx/scheduler.d.ts.map +1 -0
- package/cjs/Fx/scheduler.js +69 -0
- package/cjs/Fx/scheduler.js.map +1 -0
- package/cjs/Runtime/Runtime.d.ts +11 -0
- package/cjs/Runtime/Runtime.d.ts.map +1 -0
- package/cjs/Runtime/Runtime.js +29 -0
- package/cjs/Runtime/Runtime.js.map +1 -0
- package/cjs/Runtime/index.d.ts +2 -0
- package/cjs/Runtime/index.d.ts.map +1 -0
- package/cjs/Runtime/index.js +18 -0
- package/cjs/Runtime/index.js.map +1 -0
- package/esm/Env/Env.js +1 -1
- package/esm/Env/Env.js.map +1 -1
- package/esm/FiberRef/FiberRef.d.ts +1 -1
- package/esm/FiberRef/FiberRef.d.ts.map +1 -1
- package/esm/FiberRef/FiberRef.js +11 -10
- package/esm/FiberRef/FiberRef.js.map +1 -1
- package/esm/Fx/Fx.d.ts +11 -0
- package/esm/Fx/Fx.d.ts.map +1 -1
- package/esm/Fx/Fx.js +20 -8
- package/esm/Fx/Fx.js.map +1 -1
- package/esm/Fx/index.d.ts +2 -0
- package/esm/Fx/index.d.ts.map +1 -1
- package/esm/Fx/index.js +2 -0
- package/esm/Fx/index.js.map +1 -1
- package/esm/Fx/run.d.ts +9 -0
- package/esm/Fx/run.d.ts.map +1 -0
- package/esm/Fx/run.js +18 -0
- package/esm/Fx/run.js.map +1 -0
- package/esm/Fx/scheduler.d.ts +14 -0
- package/esm/Fx/scheduler.d.ts.map +1 -0
- package/esm/Fx/scheduler.js +39 -0
- package/esm/Fx/scheduler.js.map +1 -0
- package/esm/Runtime/Runtime.d.ts +11 -0
- package/esm/Runtime/Runtime.d.ts.map +1 -0
- package/esm/Runtime/Runtime.js +25 -0
- package/esm/Runtime/Runtime.js.map +1 -0
- package/esm/Runtime/index.d.ts +2 -0
- package/esm/Runtime/index.d.ts.map +1 -0
- package/esm/Runtime/index.js +2 -0
- package/esm/Runtime/index.js.map +1 -0
- package/package.json +41 -1
- package/src/Env/Env.ts +1 -1
- package/src/FiberRef/FiberRef.ts +5 -4
- package/src/Fx/Fx.ts +56 -0
- package/src/Fx/index.ts +2 -0
- package/src/Fx/run.ts +27 -0
- package/src/Fx/scheduler.ts +76 -0
- package/src/Runtime/Runtime.ts +44 -0
- package/src/Runtime/index.ts +1 -0
package/src/Fx/Fx.ts
CHANGED
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
|
|
46
46
|
import { getCauseError } from '@/Cause/CauseError.js'
|
|
47
47
|
import * as Cause from '@/Cause/index.js'
|
|
48
|
+
import { timeToDate, timeToUnixTime } from '@/Clock/index.js'
|
|
48
49
|
import { settable } from '@/Disposable/Disposable.js'
|
|
49
50
|
import type { Env } from '@/Env/Env.js'
|
|
50
51
|
import { Exit } from '@/Exit/index.js'
|
|
@@ -282,6 +283,61 @@ export const interruptable = <R, E, A>(fx: Fx<R, E, A>) => SetInterruptStatus.ma
|
|
|
282
283
|
|
|
283
284
|
export const getFiberContext = GetFiberContext.make()
|
|
284
285
|
|
|
286
|
+
export const getFiberId = pipe(
|
|
287
|
+
getFiberContext,
|
|
288
|
+
map((c) => c.id),
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
export const getFiberRefs = pipe(
|
|
292
|
+
getFiberContext,
|
|
293
|
+
map((c) => c.fiberRefs),
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
export const getPlatform = pipe(
|
|
297
|
+
getFiberContext,
|
|
298
|
+
map((c) => c.platform),
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
export const getTimer = pipe(
|
|
302
|
+
getPlatform,
|
|
303
|
+
map((p) => p.timer),
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
export const getCurrentTime = pipe(
|
|
307
|
+
getTimer,
|
|
308
|
+
flatMap((t) => fromLazy(() => t.getCurrentTime())),
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
export const getCurrentUnixTime = pipe(
|
|
312
|
+
getTimer,
|
|
313
|
+
flatMap((t) => fromLazy(() => timeToUnixTime(t.getCurrentTime())(t))),
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
export const getCurrentDate = pipe(
|
|
317
|
+
getTimer,
|
|
318
|
+
flatMap((t) => fromLazy(() => timeToDate(t.getCurrentTime())(t))),
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
export const getLogger = pipe(
|
|
322
|
+
getFiberContext,
|
|
323
|
+
map((c) => c.logger),
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
export const getParentContext = pipe(
|
|
327
|
+
getFiberContext,
|
|
328
|
+
map((c) => c.parent),
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
export const getFiberScope = pipe(
|
|
332
|
+
getFiberContext,
|
|
333
|
+
map((c) => c.scope),
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
export const getSupervisor = pipe(
|
|
337
|
+
getFiberContext,
|
|
338
|
+
map((c) => c.supervisor),
|
|
339
|
+
)
|
|
340
|
+
|
|
285
341
|
export const both =
|
|
286
342
|
<R2, E2, B>(second: Fx<R2, E2, B>, __trace?: string) =>
|
|
287
343
|
<R, E, A>(first: Fx<R2, E, A>): Fx<R | R2, E | E2, readonly [A, B]> =>
|
package/src/Fx/index.ts
CHANGED
package/src/Fx/run.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { pipe } from 'hkt-ts'
|
|
2
|
+
|
|
3
|
+
import { Of, getFiberContext, map } from './Fx.js'
|
|
4
|
+
|
|
5
|
+
import { FiberContext } from '@/FiberContext/FiberContext.js'
|
|
6
|
+
import { FiberId } from '@/FiberId/FiberId.js'
|
|
7
|
+
import { Platform } from '@/Platform/Platform.js'
|
|
8
|
+
import { Runtime } from '@/Runtime/index.js'
|
|
9
|
+
import { GlobalScope } from '@/Scope/GlobalScope.js'
|
|
10
|
+
|
|
11
|
+
export const MainPlatform = Platform()
|
|
12
|
+
export const MainRuntime = Runtime(
|
|
13
|
+
FiberContext({
|
|
14
|
+
platform: MainPlatform,
|
|
15
|
+
id: FiberId.None as any,
|
|
16
|
+
scope: GlobalScope,
|
|
17
|
+
}),
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
export const { run: runMain, runExit: runMainExit, runFiber: runMainFiber } = MainRuntime
|
|
21
|
+
|
|
22
|
+
export function getRuntime<R>(overrides?: Partial<FiberContext>): Of<Runtime<R>> {
|
|
23
|
+
return pipe(
|
|
24
|
+
getFiberContext,
|
|
25
|
+
map((c) => Runtime({ ...c, ...overrides })),
|
|
26
|
+
)
|
|
27
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { pipe } from 'hkt-ts'
|
|
2
|
+
import { isRight } from 'hkt-ts/Either'
|
|
3
|
+
|
|
4
|
+
import { join } from './join.js'
|
|
5
|
+
|
|
6
|
+
import * as Cause from '@/Cause/Cause.js'
|
|
7
|
+
import { Exit } from '@/Exit/Exit.js'
|
|
8
|
+
import { Live } from '@/Fiber/Fiber.js'
|
|
9
|
+
import * as Fx from '@/Fx/Fx.js'
|
|
10
|
+
import * as Schedule from '@/Schedule/Schedule.js'
|
|
11
|
+
import { ScheduleState } from '@/Schedule/ScheduleState.js'
|
|
12
|
+
import { Scheduler } from '@/Scheduler/Scheduler.js'
|
|
13
|
+
import { Delay, Time } from '@/Time/index.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Runs an Fx on a given Schedule
|
|
17
|
+
*/
|
|
18
|
+
export function retry(schedule: Schedule.Schedule) {
|
|
19
|
+
return <R, E, A>(fx: Fx.Fx<R, E, A>): Fx.Fx<R | Scheduler, E, A> => {
|
|
20
|
+
return Fx.Fx(function* () {
|
|
21
|
+
const timer = yield* Fx.getTimer
|
|
22
|
+
let [state, decision] = schedule.step(timer.getCurrentTime(), new ScheduleState())
|
|
23
|
+
|
|
24
|
+
const errors: Array<Cause.Cause<E>> = []
|
|
25
|
+
|
|
26
|
+
while (decision.tag === 'Continue') {
|
|
27
|
+
// Schedule a Task to run the next iteration of this Fx
|
|
28
|
+
const exit: Exit<E, A> = yield* delayed(decision.delay)(Fx.attempt(fx))
|
|
29
|
+
|
|
30
|
+
if (isRight(exit)) {
|
|
31
|
+
return exit.right
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
errors.push(exit.left)
|
|
35
|
+
|
|
36
|
+
// Calculate if we should continue or not
|
|
37
|
+
const [nextState, nextDecision] = schedule.step(timer.getCurrentTime(), state)
|
|
38
|
+
state = nextState
|
|
39
|
+
decision = nextDecision
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return yield* Fx.fromCause<E>(
|
|
43
|
+
errors.reduce(Cause.makeSequentialAssociative<any>().concat, Cause.Empty),
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function schedule(schedule: Schedule.Schedule) {
|
|
50
|
+
return <R, E, A>(fx: Fx.Fx<R, E, A>): Fx.Fx<R | Scheduler, never, Live<E, ScheduleState>> =>
|
|
51
|
+
pipe(
|
|
52
|
+
Fx.ask(Scheduler),
|
|
53
|
+
Fx.bindTo('scheduler'),
|
|
54
|
+
Fx.bind('env', () => Fx.getEnv<R>()),
|
|
55
|
+
Fx.bind('context', () => Fx.getFiberContext),
|
|
56
|
+
Fx.map(({ scheduler, env, context }) =>
|
|
57
|
+
scheduler.schedule(fx, schedule, env, context.fork()),
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const delayed =
|
|
63
|
+
(delay: Delay) =>
|
|
64
|
+
<R, E, A>(fx: Fx.Fx<R, E, A>): Fx.Fx<R | Scheduler, E, A> =>
|
|
65
|
+
pipe(
|
|
66
|
+
sleep(delay),
|
|
67
|
+
Fx.flatMap(() => fx),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
export const sleep = (delay: Delay): Fx.RIO<Scheduler, Time> =>
|
|
71
|
+
pipe(
|
|
72
|
+
Fx.unit,
|
|
73
|
+
schedule(Schedule.delayed(delay)),
|
|
74
|
+
Fx.flatMap(join),
|
|
75
|
+
Fx.flatMap(() => Fx.getCurrentTime),
|
|
76
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { match } from 'hkt-ts/Either'
|
|
2
|
+
|
|
3
|
+
import { CauseError } from '@/Cause/CauseError.js'
|
|
4
|
+
import { Exit } from '@/Exit/Exit.js'
|
|
5
|
+
import { Live } from '@/Fiber/Fiber.js'
|
|
6
|
+
import { FiberContext } from '@/FiberContext/FiberContext.js'
|
|
7
|
+
import { FiberRuntime } from '@/FiberRuntime/FiberRuntime.js'
|
|
8
|
+
import { Fx } from '@/Fx/Fx.js'
|
|
9
|
+
|
|
10
|
+
export interface Runtime<R> {
|
|
11
|
+
readonly runFiber: <E, A>(fx: Fx<R, E, A>) => Live<E, A>
|
|
12
|
+
readonly runExit: <E, A>(fx: Fx<R, E, A>) => Promise<Exit<E, A>>
|
|
13
|
+
readonly run: <E, A>(fx: Fx<R, E, A>) => Promise<A>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function Runtime<R>(context: FiberContext): Runtime<R> {
|
|
17
|
+
return {
|
|
18
|
+
runFiber: <E, A>(fx: Fx<R, E, A>) => {
|
|
19
|
+
const r = new FiberRuntime(fx, context.fork())
|
|
20
|
+
|
|
21
|
+
r.startAsync()
|
|
22
|
+
|
|
23
|
+
return r
|
|
24
|
+
},
|
|
25
|
+
runExit: <E, A>(fx: Fx<R, E, A>) =>
|
|
26
|
+
new Promise<Exit<E, A>>((resolve) => {
|
|
27
|
+
const r = new FiberRuntime(fx, context.fork())
|
|
28
|
+
|
|
29
|
+
r.addObserver(resolve)
|
|
30
|
+
r.startSync()
|
|
31
|
+
|
|
32
|
+
return r
|
|
33
|
+
}),
|
|
34
|
+
run: <E, A>(fx: Fx<R, E, A>) =>
|
|
35
|
+
new Promise<A>((resolve, reject) => {
|
|
36
|
+
const r = new FiberRuntime(fx, context.fork())
|
|
37
|
+
|
|
38
|
+
r.addObserver(match((cause) => reject(new CauseError(cause, context.renderer)), resolve))
|
|
39
|
+
r.startSync()
|
|
40
|
+
|
|
41
|
+
return r
|
|
42
|
+
}),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Runtime.js'
|