mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor: move modules to packages (#41)
* refactor: move modules to `packages` * chore: remove .vscode
This commit is contained in:
parent
89f1dfeaa5
commit
8eddd182d3
49 changed files with 152 additions and 174 deletions
9
packages/vide/src/index.d.ts
vendored
Normal file
9
packages/vide/src/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Selector } from "@rbxts/charm";
|
||||
|
||||
/**
|
||||
* Subscribes to the state of an atom and returns a Vide source.
|
||||
*
|
||||
* @param callback The atom or selector to subscribe to.
|
||||
* @returns The reactive source.
|
||||
*/
|
||||
export function useAtom<T>(callback: Selector<T>): () => T;
|
||||
27
packages/vide/src/init.luau
Normal file
27
packages/vide/src/init.luau
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
local Charm = require(script.Parent.Charm)
|
||||
local Vide = if script.Parent:FindFirstChild("vide")
|
||||
then require(script.Parent.vide.src) :: never
|
||||
else require(script.Parent.Vide)
|
||||
|
||||
--[=[
|
||||
Subscribes to the state of an atom and returns a Vide source.
|
||||
|
||||
@param callback The atom or selector to subscribe to.
|
||||
@return The reactive source.
|
||||
]=]
|
||||
local function useAtom<State>(callback: () -> State): () -> State
|
||||
local state = Vide.source(callback())
|
||||
local unsubscribe = Charm.subscribe(callback, function(value)
|
||||
task.spawn(function()
|
||||
state(value)
|
||||
end)
|
||||
end)
|
||||
|
||||
Vide.cleanup(unsubscribe)
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return {
|
||||
useAtom = useAtom,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue