Announcing Starbeam: Universal Reactivity
From Author:
Starbeam is a library for building reactive data systems that integrate natively with UI frameworks such as React, Vue, Svelte or Ember. In this talk, Yehuda will announce Starbeam. He will cover the motivation for the library, and then get into the details of how Starbeam reactivity works, and most importantly, how you can use it to build reactive libraries today that will work natively in any UI framework. If you're really adventurous, he will also talk about how you could use Starbeam in an existing app using your framework of choice and talk about the benefits of using Starbeam as the state management system in your application.
This talk has been presented at JSNation 2022, check out the latest edition of this JavaScript Conference.
FAQ
Starbeam is a library for building reactive user interfaces. It allows you to write reactive code just like any other code, aiming to simplify and enhance the reliability of user interfaces using existing JavaScript skills.
No, Starbeam is not a JavaScript framework. It is a library that can be used within existing frameworks such as React, Svelte, Vue, and Ember to build reactive user interfaces.
Starbeam can be integrated inside applications built with various frameworks like React, Svelte, Vue, and Ember. It works seamlessly within these environments, showing its versatility and adaptability in different development contexts.
The demo app features user management capabilities such as creating new users, filtering and sorting users based on criteria, and deleting users. It utilizes Starbeam to manage reactive states and interactions efficiently.
In the demo, Starbeam manages data through a structured table and query system. Functions like appending rows, deleting rows, and clearing rows are handled directly through simple JavaScript operations, leveraging Starbeam's reactive capabilities for updates.
Starbeam's filtering functionality allows filtering data based on specified conditions. It uses a query object that contains filters, which are functions that take a row and return a Boolean indicating whether the row matches the filter criteria.
The 'useStarBeam' hook is used to encapsulate a component within the Starbeam environment, enabling access to reactive data and ensuring that the component re-renders only when necessary, based on specific data dependencies.
Starbeam simplifies the development process by allowing developers to use standard JavaScript practices for reactive coding, reducing the need for learning complex new concepts and increasing the reliability of the interfaces built.
Video Transcription
1. Introduction to Starbeam
Today I'm here to talk to you about Starbeam, a library for building reactive user interfaces with JavaScript. Starbeam is not the next hot new JavaScript framework. We have similar examples in Svelte, Vue, and Ember. Let's start by taking a look at the app we're going to be building. It has a form to create new users, filtering, deleting items, and more.
Hi, everybody. Today I'm here to talk to you about Starbeam. Starbeam is a library for building reactive user interfaces based on the guiding principle that if you write your reactive code just like any other code, you can build a radically simpler, more reliable user interfaces with the JavaScript skills you already have.
Now, people have said a lot of similar things like that before, so I don't blame you for being skeptical and jaded. Before I get started, I want to be really clear about something. Starbeam is not the next hot new JavaScript framework. The demo I'm doing today uses Starbeam inside a React app, and we have similar examples in Svelte, Vue, and Ember. The people who work with me on Starbeam are pretty tired of the way that new front-end ideas are always packaged up with a whole new framework you need to migrate to. We think we can do better, and I hope that by the time I'm done here, I'll have piqued your interest.
Let's start by taking a look at what the app that we're going to be building looks like. Here's the finished app. So we have a form we can create some new users. I can write leah-silver-portland. I can append it. As you can see, once I have appended it, there is some information on the bottom about what happened. I can filter. I can write portland...here. And when I do that, you see that this changes to say two filtered out of five. I can delete items while I'm filtered. Everything works as you would expect. I can delete all the items. I can add people back in. Like that. And that's about it. That's basically what this app does.
2. Data Structure and Query Feature
Let's start by using the non-finished version of the demo. The data that backs our component consists of a table with rows and columns. Each row represents a person with a name and a location. We can append, delete, and clear rows. Additionally, we can use the query feature to implement filtering and sorting.
Well, the very first thing I want to do is change to using the non-finished version of the demo. So let's start there.
Okay, great. And as you can see, there's some features here already, but nothing, there's no filters. There's nothing that made it very interesting. And more importantly, nothing works here.
Before we get started, let's take a look at the data that backs our component. Now it's a little bit of an involved JavaScript thing here, but the reason it's involved is less that Starbeam cares that you do anything interesting here and more that I want to show that even if you use pretty advanced fancy JavaScript features, everything still continues to work.
So let's take a look at the first piece of this, which is the table. So a table has an ID which starts at zero and that's what we can allocate IDs as we create new rows. We have a list of rows, which is a map from a string to a row. Now what's a row? Well, here we have an example of a person. So a person has a name, which is a string, a location, which is a string, and all a row is is that thing plus an ID added to it. So we're going to map from the ID to a row of a person, in this case, and that means it's going to be the person plus an ID. We're going to take a bunch of columns so that we have a way of reflectively creating headers. We're going to have a getter for rows. So what does the getter for rows do? It gets the values out of our map and splats it onto an array. This gives us an array back. How do we append a row? Well, we make a new ID as a string and then we make a new object that contains the ID and the columns that we specified, and then we set the string ID onto the map and give it the row as the value, return the row, pretty vanilla stuff. How do we delete an ID? Well, we delete it from the row. How do we clear rows? We clear the map. And then there's one additional feature here, which we're going to use to implement filtering, which is the query feature. So what is query? A query is another very simple object. A query has a table that backs it, and then it has a bunch of filters and an optional sort. What's a filter? It's a thing that takes a row and gives back a Boolean. What's a sort? It's a thing that takes two rows and gives you back a number, which is what the compare functions do. Okay, now what happens if you ask for a list of rows from a query? Well, the first thing that happens is that we get the rows from the table, and as a reminder, that does this splatting thing. Then, we loop through all the rows and we make sure that all the filters match the row. And then if we have a sort, we sort the filtered things by the sort, otherwise we just return the array. So basically it's a little object, wraps a table, has some filters, and gives us back new rows.
Comments