"use strict"
Object.defineProperty(exports, "__esModule", { value: true })
exports.gen = exports.GenSTM = void 0
// tracing: off
/**
 * inspired by https://github.com/tusharmath/qio/pull/22 (revised)
 */
const commons_1 = require("../../Effect/commons")
const core_1 = require("./core")
class GenSTM {
  constructor(effect) {
    this.effect = effect
  }
  *[Symbol.iterator]() {
    return yield this
  }
}
exports.GenSTM = GenSTM
const adapter = (_, __) => {
  return new GenSTM(_)
}
/**
 * Do simulation using Generators
 */
function gen(f) {
  return (0, core_1.suspend)(() => {
    const iterator = f(adapter)
    const state = iterator.next()
    function run(state) {
      if (state.done) {
        return (0, core_1.succeed)(state.value)
      }
      return (0, core_1.chain_)(state.value["effect"], (val) => {
        const next = iterator.next(val)
        return run(next)
      })
    }
    return run(state)
  })
}
exports.gen = gen
//# sourceMappingURL=gen.js.map
