class Opal::Nodes::BaseFlipFlop

def compile

def compile
  helper :truthy
  func_name = top_scope.new_temp
  flip_flop_state = "#{func_name}.$$ff"
  # Start function definition, checking and initializing it if necessary
  push "(#{func_name} = #{func_name} || function(_start_func, _end_func){"
  # If flip flop state is not defined, set it to false
  push "  var flip_flop = #{flip_flop_state} || false;"
  # If flip flop state is false, call the 'start_condition' function and store its truthy result into flip flop state
  push "  if (!flip_flop) #{flip_flop_state} = flip_flop = $truthy(_start_func());"
  # If flip flop state is true, call the 'end_condition' function and set flip flop state to false if 'end_condition' is truthy
  push "  #{excl}if (flip_flop && $truthy(_end_func())) #{flip_flop_state} = false;"
  # Return current state of flip flop
  push "  return flip_flop;"
  # End function definition
  push "})("
  # Call the function with 'start_condition' and 'end_condition' arguments wrapped in functions to ensure correct binding and delay evaluation
  push "  function() { ", stmt(compiler.returns(start_condition)), " },"
  push "  function() { ", stmt(compiler.returns(end_condition)), " }"
  push ")"
end