Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/twlite/ruby-signal-test
https://github.com/twlite/ruby-signal-test
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/twlite/ruby-signal-test
- Owner: twlite
- Created: 2024-07-20T18:53:16.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-07-20T18:56:06.000Z (4 months ago)
- Last Synced: 2024-09-20T00:28:04.751Z (about 2 months ago)
- Language: Ruby
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
```rb
STORE = []def createSignal(defaultValue)
value = defaultValue.is_a?(Proc) ? defaultValue.call() : defaultValue
subs = []
get = ->() {
sub = STORE.last
subs << sub if sub && !subs.include?(sub)
value
}
set = ->(val) {
value = val.is_a?(Proc) ? val.call(value) : val
subs.each { |s| s.call() }
}
[get, set]
enddef createEffect(fn)
exec = ->() {
STORE.push(exec)
begin
fn.call()
ensure
STORE.pop()
end
}
exec.call()
endcount, set_count = createSignal(0)
createEffect(->() {
puts("The count is #{count.call()}")
})# The count is 0
set_count.call(count.call + 1) # The count is 1
set_count.call(count.call + 1) # The count is 2
set_count.call(count.call + 1) # The count is 3
set_count.call(count.call + 1) # The count is 4
set_count.call(count.call + 1) # The count is 5
```