Skip to content

Commit

Permalink
feat(fio): new operator runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Aug 4, 2019
1 parent 920faf3 commit f94f03e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/internals/Evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Evaluate = <E, A>(
rej: CB<E>,
res: CB<A>
): void => {
const {stackA, stackE, stackEnv, cancellationList, sh} = context
const {stackA, stackE, stackEnv, cancellationList, sh, runtime} = context
let data: unknown

while (true) {
Expand Down Expand Up @@ -97,6 +97,10 @@ export const Evaluate = <E, A>(
data = j.i0(env)
break

case Tag.Runtime:
data = runtime
break

case Tag.Async:
const id = cancellationList.push(
j.i0(
Expand Down
4 changes: 3 additions & 1 deletion src/internals/FiberContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Exit} from '../main/Exit'
import {Fiber} from '../main/Fiber'
import {FIO, UIO} from '../main/FIO'
import {Instruction} from '../main/Instructions'
import {IRuntime} from '../runtimes/IRuntime'

import {CancellationList} from './CancellationList'
import {CB} from './CB'
Expand Down Expand Up @@ -36,6 +37,7 @@ export class FiberContext<E = never, A = never> extends Fiber<E, A>
public readonly stackEnv: unknown[] = []

public constructor(
public readonly runtime: IRuntime,
public readonly sh: IScheduler,
io: Instruction,
public readonly cancellationList: CancellationList = new CancellationList()
Expand All @@ -56,7 +58,7 @@ export class FiberContext<E = never, A = never> extends Fiber<E, A>
* Creates a new FiberContext with the provided instruction
*/
public $fork<E2, A2>(ins: Instruction): FiberContext<E2, A2> {
return new FiberContext<E2, A2>(this.sh, ins)
return new FiberContext<E2, A2>(this.runtime, this.sh, ins)
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/FIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ICancellable, IScheduler} from 'ts-scheduler'

import {CB} from '../internals/CB'
import {coordinate} from '../internals/Coordinate'
import {IRuntime} from '../runtimes/IRuntime'

import {Await} from './Await'
import {Exit} from './Exit'
Expand Down Expand Up @@ -317,6 +318,13 @@ export class FIO<E1 = unknown, A1 = unknown, R1 = NoEnv> {
return new FIO(Tag.TryM, cb)
}

/**
* Returns the current runtime in a pure way.
*/
public static runtime(): UIO<IRuntime> {
return new FIO(Tag.Runtime)
}

/**
* Executes the provided IOs in sequences and returns their intermediatory results as an Array.
* Checkout [[FIO.par]] to run multiple IOs in parallel.
Expand Down
5 changes: 5 additions & 0 deletions src/main/Instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum Tag {
Never,
Provide,
Reject,
Runtime,
Try,
TryM
}
Expand Down Expand Up @@ -71,6 +72,9 @@ export interface IProvide<R = unknown> {
i1: R
tag: Tag.Provide
}
export interface IRuntime {
tag: Tag.Runtime
}

/**
* @ignore
Expand All @@ -86,5 +90,6 @@ export type Instruction =
| INever
| IProvide
| IReject
| IRuntime
| ITry
| ITryM
9 changes: 5 additions & 4 deletions src/runtimes/BaseRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export abstract class BaseRuntime implements IRuntime {
res: (e: A) => void = noop,
rej: (e: E) => void = noop
): ICancellable {
return new FiberContext<E, A>(this.scheduler, io.asInstruction).$resume(
rej,
res
)
return new FiberContext<E, A>(
this,
this.scheduler,
io.asInstruction
).$resume(rej, res)
}
}
11 changes: 11 additions & 0 deletions test/FIO.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Exit} from '../src/main/Exit'
import {Fiber} from '../src/main/Fiber'
import {FIO, UIO} from '../src/main/FIO'
import {defaultRuntime} from '../src/runtimes/DefaultRuntime'
import {IRuntime} from '../src/runtimes/IRuntime'
import {testRuntime} from '../src/runtimes/TestRuntime'

import {Counter} from './internals/Counter'
Expand Down Expand Up @@ -591,4 +592,14 @@ describe('FIO', () => {
assert.strictEqual(actual, expected)
})
})

describe('runtime', () => {
it('should give access to current runtime', () => {
const runtime = testRuntime()

const actual = runtime.executeSync(FIO.runtime())

assert.strictEqual(actual, runtime as IRuntime)
})
})
})

0 comments on commit f94f03e

Please sign in to comment.