Skip to content

Commit

Permalink
mirror implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sinlerdev committed Nov 30, 2022
1 parent 7a4d45f commit 44c34f8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ An ultra-fast, multi-paradigm and modern reactive state management library inten

Check the [docs](https://plothan.github.io/Vinum/) here.
___

## Features

* **Centralized or Self-Contained** - Vinum doesn't "force" you into either centralized or self-contained paradigms, in fact, it allows for powerful integration between the two to leverage the best of the two worlds.
Expand Down
43 changes: 43 additions & 0 deletions benchmarks/Mirror.bench.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local Vinum = require(game.ReplicatedStorage.Vinum)

local Hold = Vinum.Hold
local Mirror = Vinum.Mirror
local JustOk = Vinum.JustOk

return {
{
name = "creating a mirror object",
calls = 50000,

preRun = function()
return Hold(true, JustOk), Hold(false, JustOk)
end,

run = function(x, y)
Mirror(x, {
["default"] = y
})
end
},
{
name = "updating a mirror object",
calls = 50000,


preRun = function()
local x = Hold(true, JustOk)
local y = Hold(false, JustOk)

local miObj = Mirror(x, {
["default"] = y
})

miObj._currentCaseDependency = Hold("hi", JustOk)
return miObj
end,

run = function(miObj)
miObj:_update()
end
}
}
2 changes: 1 addition & 1 deletion code-runners/runner.server.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--!optimize 2
local shouldBenchmark = false
local shouldBenchmark = true
local shouldTest = true

local Tester = require(script.Parent.Tester)
Expand Down
53 changes: 53 additions & 0 deletions src/Mirror.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local graph = require(script.Parent.utils.graph)
local Symbols = require(script.Parent.utils.Symbols)

local class = { type = "state", kind = "mirror" }
local meta = { __index = class }

function class:_update()
local cases = self._cases
local chosenArm = cases[self._inputState:get()] or cases["default"]
local currentCaseDependency = self._currentCaseDependency

if currentCaseDependency ~= chosenArm then
local myGraph = self._graph

if currentCaseDependency then
local currenGraph = currentCaseDependency._graph
currenGraph._dependentSet[myGraph] = nil
myGraph._dependencySet[currenGraph] = nil
end

local chosenGraph = chosenArm._graph
chosenGraph._dependentSet[myGraph] = true
myGraph._dependencySet[chosenGraph] = true

self._currentCaseDependency = chosenArm
end

self._value = chosenArm:get()
return self._value
end

function class:get()
return self._value
end

return function(inputState, cases: { [any]: any })
local self = setmetatable({
_isSelfContained = true,
_isStaticDependency = false,
_cases = cases,
_value = Symbols.None,
_inputState = inputState,
_currentCaseDependency = false,
}, meta)

self._graph = graph(self)
local myGraph = self._graph

inputState._graph._dependentSet[myGraph] = true

myGraph:update()
return self
end
1 change: 1 addition & 0 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Vinum = {
Calc = require(script.Calc),
Match = require(script.Match),
Group = require(script.Group),
Mirror = require(script.Mirror),
JustOk = function()
return true
end,
Expand Down
47 changes: 47 additions & 0 deletions tests/Mirror.spec.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local Vinum = require(game.ReplicatedStorage.Vinum)

local Mirror = Vinum.Mirror
local Hold = Vinum.Hold
local JustOk = Vinum.JustOk

return {
["Should construct a Mirror object"] = function(tester)
local expect = tester.expect
local miObj = Mirror(Hold(true, JustOk), {
[true] = Hold(20),
["default"] = Hold("doesn'tExist", JustOk),
})

expect(miObj.type).to.equal("state")
expect(miObj).to.be.a("table")
expect(miObj.kind).to.equal("mirror")
end,
["Should change once InputState fires"] = function(tester)
local expect = tester.expect

local root = Hold(true, JustOk)
local miObj = Mirror(root, {
[true] = Hold(20),
["default"] = Hold("doesn'tExist", JustOk),
})

expect(miObj:get()).to.equal(20)
root:set(false)
expect(miObj:get()).to.equal("doesn'tExist")
end,
["Should change once a Case Object changes"] = function(tester)
local expect = tester.expect

local root = Hold(true, JustOk)
local trueCase = Hold(20, JustOk)
local miObj = Mirror(root, {
[true] = trueCase,
["default"] = Hold("doesn'tExist", JustOk),
})

expect(miObj:get()).to.equal(20)
trueCase:set(30)
expect(miObj:get()).to.equal(30)

end
}

0 comments on commit 44c34f8

Please sign in to comment.