Skip to content

Commit

Permalink
library rewrite (#6)
Browse files Browse the repository at this point in the history
* complete overhaul

* changed wally.toml
  • Loading branch information
Ocipa authored Dec 23, 2023
1 parent 9a45a1c commit b805563
Show file tree
Hide file tree
Showing 26 changed files with 363 additions and 1,018 deletions.
15 changes: 3 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
# Project place file
/accord.rbxlx
/*.rbxl
/*.rbxlx

# Roblox Studio lock files
/*.rbxlx.lock
/*.rbxl.lock

/*.rbxl
/*.rbxlx.lock

/Packages
/DevPackages
/client

wally.lock


/out
/node_modules
/.vscode
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

3 changes: 3 additions & 0 deletions .luaurc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"languageMode": "strict"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
70 changes: 22 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,36 @@
<div align="center">⚠️Repo is in development, EVERYTHING is subject to change, until v1.0.0⚠️</div>
<br>

<p align="center" width="100%">
<img src="./assets/logo.png">
<p align="center"">
<img src="./assets/logo.png" width="450" />
</p>
<div align="center">⚠️Library is in development, EVERYTHING is subject to change until v1.0.0⚠️</div>
<br>

## Example
```lua
local accord = require("path to accord")

local balance = accord:NewState(0) {
Add = function(self, num: number)
self.value += num
end,

Sub = function(self, num: number)
self.value -= num
end,
}
-- basic example
local num = Accord.new(0, Guard.Number)

balance:Add(1)

-- by default, :Connect is defered, and in this
-- example, it will only print '3' one time
balance:Connect(function()
print(balance:Get())
num:Connect(function(value, oldValue)
print(`num changed from {oldValue} to {value}`)
end)

balance:Add(1)
balance:Add(1)
```

## API

`Accord`
```lua
Accord:NewState<T>(defaultValue: T, config: Config?): <S>(methods: S | Methods<T>) -> State<T> & S
-- Creates a new state.
```
-- react hook example
return function<T>(value: Accord.Accord<T>): T
local state, setState = React.useState(value:get())

`State<T>`
```lua
State:Get(): T
-- Gets the value of the state.
```
```lua
State:Connect(callback: (): nil): Connection
-- Connects a callback to when the state value changes.
```
```lua
State:ConnectOnce(callback: (): nil): Connection
-- Connects a callback to when the state value changes once.
```
```lua
State:DisconnectAll(): nil
-- Disconnects all connections to the state.
React.useEffect(function()
local connection = value:connect(setState)

return function()
connection:disconnect()
end
end, { value })

return state
end
```

## License

Distributed under the MIT License. See [LICENSE](./LICENSE) for more information.
Distributed under the MIT License. See [LICENSE](./LICENSE) for more information.
10 changes: 10 additions & 0 deletions aftman.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file lists tools managed by Aftman, a cross-platform toolchain manager.
# For more information, see https://github.com/LPGhatguy/aftman

# To add a new tool, add an entry to this table.
[tools]
StyLua = "JohnnyMorganz/StyLua@0.18.2"
luau-lsp = "JohnnyMorganz/luau-lsp@1.25.0"
selene = "Kampfkarren/selene@0.25.0"
rojo = "UpliftGames/rojo@7.3.0-uplift.12.pre.8"
wally = "UpliftGames/wally@0.3.2"
2 changes: 1 addition & 1 deletion default.project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "accord",
"tree": {
"$path": "src"
"$path": "lib"
}
}
81 changes: 81 additions & 0 deletions lib/Accord.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local Signal = require(script.Parent.Signal)
local Symbol = require(script.Parent.Symbol)

-- TODO: figure out a better name for this.
type AccordMain<T> = {
value: T,
guard: (value: unknown) -> T,

_signal: Signal.Signal<(T, T)>,
}

type AccordImpl<T> = {
set: (self: Accord<T>, value: T | (value: T) -> T) -> (),
get: (self: Accord<T>) -> T,
forceUpdate: (self: Accord<T>) -> (),

connect: (self: Accord<T>, fn: (value: T) -> ()) -> Signal.Connection<T>,
once: (self: Accord<T>, fn: (value: T) -> ()) -> Signal.Connection<T>,
disconnectAll: (self: Accord<T>) -> (),
}

export type Accord<T> = typeof(setmetatable({} :: AccordMain<T>, {} :: { __index: AccordImpl<T> }))

local module: AccordImpl<unknown> = {} :: AccordImpl<unknown>

function module:set(value)
local oldValue = self.value

local v = if typeof(value) == "function" then (value :: any)(oldValue) else value

if v == Symbol.abort then
return
end

self.value = self.guard(v)

self._signal:fire(self.value, oldValue)
end

function module:get()
return self.value
end

function module:forceUpdate()
self._signal:fire(self.value, self.value)
end

function module:connect(fn)
return self._signal:connect(fn)
end

function module:once(fn)
return self._signal:once(fn)
end

function module:disconnectAll()
self._signal:disconnectAll()
end

local constructors = {
new = function<T>(value: T, guard: (value: unknown) -> T): Accord<T>
local self = {
value = guard(value),
guard = guard,

_signal = Signal.new(),
}

return setmetatable(self, { __index = module }) :: any
end,

is = function<T>(value: Accord<T>): boolean
if typeof(value) ~= "table" then
return false
end

return getmetatable(value) == module
end,
}

return constructors
Loading

0 comments on commit b805563

Please sign in to comment.