Compare commits
2 Commits
e83fcd0b4d
...
e9216cc3e4
| Author | SHA1 | Date | |
|---|---|---|---|
| e9216cc3e4 | |||
| cafc857e56 |
@ -1,36 +1,29 @@
|
|||||||
import { type CursorPos } from 'node:readline'
|
import { type CursorPos } from 'node:readline'
|
||||||
import type { Either } from '../util/basic/either'
|
import type { Either } from '../util/basic/either'
|
||||||
import {
|
import { none, type Option, some } from '../util/basic/option'
|
||||||
DoOption,
|
import { type Tag } from '../util/basic/utility'
|
||||||
getOrElse,
|
import { type CanvasEnd, type CanvasOrigin, end, origin } from './canvas'
|
||||||
none,
|
|
||||||
type Option,
|
|
||||||
some,
|
|
||||||
} from '../util/basic/option'
|
|
||||||
import { hasTag, pipe, type Tag } from '../util/basic/utility'
|
|
||||||
import {
|
|
||||||
type CanvasEnd,
|
|
||||||
type CanvasOrigin,
|
|
||||||
cursorPos,
|
|
||||||
end,
|
|
||||||
origin,
|
|
||||||
} from './canvas'
|
|
||||||
|
|
||||||
interface PainterExit extends Tag<'PainterExit'> {}
|
interface PainterExit<E> extends Tag<'PainterExit'> {
|
||||||
|
value: Either<E, Error>
|
||||||
|
}
|
||||||
|
|
||||||
interface PainterTask<T extends object = {}> extends Tag<'PainterTask'> {
|
interface PainterTask<E, T extends object = {}> extends Tag<'PainterTask'> {
|
||||||
task: {
|
task: {
|
||||||
origin: CanvasOrigin,
|
origin: CanvasOrigin,
|
||||||
end: CanvasEnd,
|
end: CanvasEnd,
|
||||||
effect: PainterEffect<T & Tag<'PainterState'>>,
|
effect: PainterEffect<E, T>,
|
||||||
state: Option<T & Tag<'PainterState'>>,
|
state: Option<T & Tag<'PainterState'>>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type TaskConstructor<T extends object = {}> = (
|
type TaskConstructor = (
|
||||||
x: CursorPos,
|
x: CursorPos,
|
||||||
y: CursorPos,
|
y: CursorPos,
|
||||||
) => (f: PainterEffect, state?: T & Tag<'PainterState'>) => PainterTask<T>
|
) => <E = number, T extends object = {}>(
|
||||||
|
f: PainterEffect<E, T>,
|
||||||
|
state?: T & Tag<'PainterState'>,
|
||||||
|
) => PainterTask<E, T>
|
||||||
const task: TaskConstructor = (x, y) => (f, state?) => {
|
const task: TaskConstructor = (x, y) => (f, state?) => {
|
||||||
return {
|
return {
|
||||||
_tag: 'PainterTask',
|
_tag: 'PainterTask',
|
||||||
@ -43,59 +36,22 @@ const task: TaskConstructor = (x, y) => (f, state?) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExitConstructor = <T>(x: Either<T, Error>) => PainterExit<T>
|
||||||
|
const exit: ExitConstructor = x => ({_tag: 'PainterExit', value: x})
|
||||||
|
|
||||||
type PainterEffect<
|
type PainterEffect<
|
||||||
|
E,
|
||||||
T extends object = {},
|
T extends object = {},
|
||||||
> = (task: PainterTask<T>) => Either<PainterTask<T>, PainterExit>
|
> = (task: PainterTask<T>) => Either<PainterTask<T>, PainterExit<E>>
|
||||||
|
|
||||||
type EffectState = <T extends object = {}>(x: T) => T & Tag<'PainterState'>
|
type EffectState = <T extends object = {}>(x: T) => T & Tag<'PainterState'>
|
||||||
const effectState: EffectState = x => ({_tag: 'PainterState', ...x})
|
const effectState: EffectState = x => ({_tag: 'PainterState', ...x})
|
||||||
|
|
||||||
export { type PainterEffect, task }
|
export {
|
||||||
|
type EffectState as PainterState,
|
||||||
//
|
effectState as tagState,
|
||||||
// testing
|
exit,
|
||||||
//
|
type PainterEffect,
|
||||||
|
type PainterTask,
|
||||||
type TestEffect = (x: PainterTask<{message: Option<string>}>) => PainterTask<
|
task,
|
||||||
{message: Option<string>}
|
|
||||||
>
|
|
||||||
const testEffect: PainterEffect<{message: string, counter: number}> = x => {
|
|
||||||
const state = DoOption.start()
|
|
||||||
.bind('state', x.task.state)
|
|
||||||
.doL(ctx => {
|
|
||||||
console.log('Message:', ctx.state.message)
|
|
||||||
return some(undefined)
|
|
||||||
})
|
|
||||||
.doL(ctx => {
|
|
||||||
console.log('Counter:', ctx.state.counter)
|
|
||||||
return some(undefined)
|
|
||||||
})
|
|
||||||
.return(ctx =>
|
|
||||||
(ctx.state.counter < 100000)
|
|
||||||
? {...ctx.state, counter: ctx.state.counter + 1}
|
|
||||||
: none
|
|
||||||
)
|
|
||||||
if (pipe(state, getOrElse(none), hasTag('None'))) return {_tag: 'PainterExit'}
|
|
||||||
|
|
||||||
return task(x.task.origin.value, x.task.end.value)(
|
|
||||||
x.task.effect,
|
|
||||||
getOrElse(none)(state),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const textTask = task<{message: string, counter: number}>(
|
|
||||||
cursorPos(2)(0),
|
|
||||||
cursorPos(4)(0),
|
|
||||||
)(
|
|
||||||
testEffect,
|
|
||||||
{
|
|
||||||
_tag: 'PainterState',
|
|
||||||
message: 'test message',
|
|
||||||
counter: 1,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
const testFn = (x: PainterTask | PainterExit) => {
|
|
||||||
return hasTag('PainterExit')(x) ? 'Exit' : testFn(x.task.effect(x))
|
|
||||||
}
|
|
||||||
|
|
||||||
testFn(textTask)
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user