From 97c58f6889d27530f94ea06224bbf010eef24918 Mon Sep 17 00:00:00 2001 From: Sabine Lim Date: Sat, 20 May 2023 23:02:48 +1000 Subject: [PATCH] Fix --- Basalt/plugins/reactive.lua | 135 +++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/Basalt/plugins/reactive.lua b/Basalt/plugins/reactive.lua index a45b8ff..fb10fae 100644 --- a/Basalt/plugins/reactive.lua +++ b/Basalt/plugins/reactive.lua @@ -1,5 +1,72 @@ local XMLParser = require("xmlParser") +local Reactive = {} + +Reactive.currentEffect = nil + +Reactive.observable = function(initialValue) + local value = initialValue + local observerEffects = {} + local get = function() + if (Reactive.currentEffect ~= nil) then + table.insert(observerEffects, Reactive.currentEffect) + table.insert(Reactive.currentEffect.dependencies, observerEffects) + end + return value + end + local set = function(newValue) + value = newValue + local observerEffectsCopy = {} + for index, effect in ipairs(observerEffects) do + observerEffectsCopy[index] = effect + end + for _, effect in ipairs(observerEffectsCopy) do + effect.execute() + end + end + return get, set +end + +Reactive.untracked = function(getter) + local parentEffect = Reactive.currentEffect + Reactive.currentEffect = nil + local value = getter() + Reactive.currentEffect = parentEffect + return value +end + +Reactive.effect = function(effectFn) + local effect = {dependencies = {}} + local execute = function() + Reactive.clearEffectDependencies(effect) + local parentEffect = Reactive.currentEffect + Reactive.currentEffect = effect + effectFn() + Reactive.currentEffect = parentEffect + end + effect.execute = execute + effect.execute() +end + +Reactive.derived = function(computeFn) + local getValue, setValue = Reactive.observable(); + Reactive.effect(function() + setValue(computeFn()) + end) + return getValue; +end + +Reactive.clearEffectDependencies = function(effect) + for _, dependency in ipairs(effect.dependencies) do + for index, backlink in ipairs(dependency) do + if (backlink == effect) then + table.remove(dependency, index) + end + end + end + effect.dependencies = {}; +end + local Layout = { fromXML = function(text) local nodes = XMLParser.parseText(text) @@ -31,19 +98,6 @@ local registerFunctionEvent = function(object, event, script, env) end) end -local currentEffect = nil - -local clearEffectDependencies = function(effect) - for _, dependency in ipairs(effect.dependencies) do - for index, backlink in ipairs(dependency) do - if (backlink == effect) then - table.remove(dependency, index) - end - end - end - effect.dependencies = {}; -end - return { basalt = function(basalt) local createObjectsFromXMLNode = function(node, env) @@ -79,57 +133,10 @@ return { end local object = { - observable = function(initialValue) - local value = initialValue - local observerEffects = {} - local get = function() - if (currentEffect ~= nil) then - table.insert(observerEffects, currentEffect) - table.insert(currentEffect.dependencies, observerEffects) - end - return value - end - local set = function(newValue) - value = newValue - local observerEffectsCopy = {} - for index, effect in ipairs(observerEffects) do - observerEffectsCopy[index] = effect - end - for _, effect in ipairs(observerEffectsCopy) do - effect.execute() - end - end - return get, set - end, - - untracked = function(getter) - local parentEffect = currentEffect - currentEffect = nil - local value = getter() - currentEffect = parentEffect - return value - end, - - effect = function(effectFn) - local effect = {dependencies = {}} - local execute = function() - clearEffectDependencies(effect) - local parentEffect = currentEffect - currentEffect = effect - effectFn() - currentEffect = parentEffect - end - effect.execute = execute - effect.execute() - end, - - derived = function(computeFn) - local getValue, setValue = basalt.reactive(); - basalt.effect(function() - setValue(computeFn()) - end) - return getValue; - end, + observable = Reactive.observable, + untracked = Reactive.untracked, + effect = Reactive.effect, + derived = Reactive.derived, layout = function(path) if (not fs.exists(path)) then -- 2.49.1