-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Without React #46
Comments
Sure. The core statebus.js doesn't depend on React, Coffeescript, Eval, or anything. If you want you can import it directly and use it like this: <body></body>
<script src="https://stateb.us/statebus6.js"></script>
<script>
// Now we have a statebus() constructor.
// Let's make a bus from it
bus = statebus()
// Let's set some state
bus.save({key: 'body', text: 'hello!'})
// We can react to changes with a reactive function
bus(() => {
var body = bus.fetch('body')
document.body.innerText = body.text
})
// That will update each time the state changes
setInterval( () => {
var body = fetch('body')
body.text += '!'
bus.save(body)
}, 1000 )
</script> This also doesn't give you any networking. You can write that by hand if you want, too. The client.js file just defines some useful helpers on top of this core statebus functionality. The core statebus.js functionality gives you:
bus( () => {
var a = fetch('a')
if (something)
var b = fetch('b')
...
})
The client.js and server.js files just have example code for how you can use these basic features to synchronize state over a network, into a React UI, and into a database on disk. You don't have to use any of them. |
Note that you also don't have to use the reactive functions: // We can react to changes with a reactive function
bus(() => {
var body = bus.fetch('body')
document.body.innerText = body.text
}) This code is equivalent to: // We can react to changes with a callback
bus.fetch('body', (body) => {
document.body.innerText = body.text
}) The reactive function form is useful when you are writing a function that depends on multiple resources, with multiple keys. It also cleanly handles state that is being loaded over the network (enabling automatic loading indicators), or via arbitrary logic to compute which keys to load. But I'm getting the feeling that you want to see what's in the core of statebus, so I thought you might want to see that simple callback form. |
Exactly 😉 Thank you for the answers! |
@toomim btw, might have a typo the example above - |
Is there a client or usage example of statebus that does not depend on React?
The text was updated successfully, but these errors were encountered: