Skip to content
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

Open
floer32 opened this issue Aug 14, 2021 · 4 comments
Open

Without React #46

floer32 opened this issue Aug 14, 2021 · 4 comments

Comments

@floer32
Copy link

floer32 commented Aug 14, 2021

Is there a client or usage example of statebus that does not depend on React?

@toomim
Copy link
Member

toomim commented Aug 14, 2021

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:

  • A namespace of state
  • You can get notified of changes with a callback: bus.fetch(key, cb)
  • You can define reactive functions that fetch a bunch of keys, and react whenever any key changes:
bus( () => {
   var a = fetch('a')
   if (something)
       var b = fetch('b')
   ...
})
  • You can define how state is fetched and saved programmatically with bus(key).to_fetch and bus(key).to_save.

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.

@toomim
Copy link
Member

toomim commented Aug 14, 2021

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.

@floer32
Copy link
Author

floer32 commented Aug 14, 2021

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!

@floer32
Copy link
Author

floer32 commented Aug 15, 2021

@toomim btw, might have a typo the example above - save() vs. bus.save()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants