minart
minart copied to clipboard
Explore ammonite integration/live-coding possibilities
Minart can be called from ammonite, which can lead to some interesting live-coding usages.
I haven't thought about this in detail, but I'm thinking about adding a new minart-live package with some utilities such a special RenderLoop, like:
class LiveRenderLoop[S](innerRenderLoop: RenderLoop) {
private[this] var renderFrame: (Canvas, S) => S = _._2
def changeRenderFrame(newRenderFrame: (Canvas, S) => S): Unit
def init(canvasManager: CanvasManager, initialState: S, frameRate: FrameRate) {
innerRenderLoop.infiniteRenderLoop(canvasManager, initialState, {(c, s) => renderFrame(c, s)}, frameRate)
}
}
This would allow a render loop to keep the state and run in the background, while the rendering logic was updated in the REPL.
Another possibility here (which would just work on JS) would be to do something similar to Tyrian's HotReload
In this case, there would be something like:
class HotReloadRenderLoop[S](decodeState: String => Option[S], encodeState: S => String) {
private def loadState(): Option[S] =
Option(dom.window.localStorage.getItem(key)).flatMap(decodeState)
private def saveState(state: S): String =
dom.window.localStorage.setItem(key, encodeState(state))
def run(runner: LoopRunner, canvasManager: CanvasManager, canvasSettings: Canvas.Settings, initialState: S) = {
val canvas = canvasManager.init(canvasSettings)
runner
.finiteLoop(
(state: S) => renderFrame(canvas, state).tap(saveState),
(newState: S) => terminateWhen(newState) || !canvas.isCreated(),
frameRate,
() => if (canvas.isCreated()) canvas.close()
)
.run(loadState().getOrElse(initialState))
}
}
I think that with some adjustments, it might also be able to plug this logic into the PureRenderLoop without a ton of work.