Sha256: ad5df5e9032c1a191a34f68d86ce5eef86ab73423079d68c3a45fe8ccdb757bb

Contents?: true

Size: 1.97 KB

Versions: 131

Compression:

Stored size: 1.97 KB

Contents

local function InputCell(value)
  local children = {}

  return {
    get_value = function()
      return value
    end,

    set_value = function(new_value)
      value = new_value
      for _, child in ipairs(children) do
        child._propagate()
      end
      for _, child in ipairs(children) do
        child._update()
      end
    end,

    _add_child = function(child)
      table.insert(children, child)
    end
  }
end

local function ComputeCell(...)
  local callbacks = {}
  local children = {}
  local value
  local previous_value
  local cell = {}

  local inputs, f do
    local args = table.pack(...)
    if args.n == 3 then
      inputs = { args[1], args[2] }
      f = args[3]
    else
      inputs = { args[1] }
      f = args[2]
    end
  end

  local function calculate_value()
    local args = {}
    for _, input in ipairs(inputs) do
      table.insert(args, input.get_value())
    end
    return f(table.unpack(args))
  end

  function cell.get_value()
    return value
  end

  function cell.watch(callback)
    table.insert(callbacks, callback)
  end

  function cell.unwatch(callback)
    for i, _callback in ipairs(callbacks) do
      if callback == _callback then
        table.remove(callbacks, i)
        return
      end
    end
  end

  function cell._propagate()
    value = calculate_value()
    for _, child in ipairs(children) do
      child._propagate()
    end
  end

  function cell._update()
    if previous_value ~= value then
      previous_value = value
      for _, callback in ipairs(callbacks) do
        callback(value)
      end
    end
    for _, child in ipairs(children) do
      child._update()
    end
  end

  function cell._add_child(child)
    table.insert(children, child)
  end

  value = calculate_value()
  previous_value = value

  for _, input in ipairs(inputs) do
    input._add_child(cell)
  end

  return cell
end

local function Reactor()
  return {
    InputCell = InputCell,
    ComputeCell = ComputeCell
  }
end

return { Reactor = Reactor }

Version data entries

131 entries across 131 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/lua/exercises/react/example.lua
trackler-2.2.1.138 tracks/lua/exercises/react/example.lua
trackler-2.2.1.137 tracks/lua/exercises/react/example.lua
trackler-2.2.1.136 tracks/lua/exercises/react/example.lua
trackler-2.2.1.135 tracks/lua/exercises/react/example.lua
trackler-2.2.1.134 tracks/lua/exercises/react/example.lua
trackler-2.2.1.133 tracks/lua/exercises/react/example.lua
trackler-2.2.1.132 tracks/lua/exercises/react/example.lua
trackler-2.2.1.131 tracks/lua/exercises/react/example.lua
trackler-2.2.1.130 tracks/lua/exercises/react/example.lua
trackler-2.2.1.129 tracks/lua/exercises/react/example.lua
trackler-2.2.1.128 tracks/lua/exercises/react/example.lua
trackler-2.2.1.127 tracks/lua/exercises/react/example.lua
trackler-2.2.1.126 tracks/lua/exercises/react/example.lua
trackler-2.2.1.125 tracks/lua/exercises/react/example.lua
trackler-2.2.1.124 tracks/lua/exercises/react/example.lua
trackler-2.2.1.123 tracks/lua/exercises/react/example.lua
trackler-2.2.1.122 tracks/lua/exercises/react/example.lua
trackler-2.2.1.121 tracks/lua/exercises/react/example.lua
trackler-2.2.1.120 tracks/lua/exercises/react/example.lua