adobe/twist

Name: twist

Owner: Adobe Systems Incorporated

Description: State-management library with data binding and observables

Created: 2017-12-15 23:05:35.0

Updated: 2018-03-18 15:39:59.0

Pushed: 2018-02-15 19:47:12.0

Homepage: null

Size: 358

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Twist: Reactive State-Management for JavaScript Applications

Build Status

A web application is a lot like an iceberg - what you see on the surface is just a tiny fraction of the complexity that lies beneath the waves. To a casual observer, it's easy to get absorbed by beautiful designs, and be amazed by how a little CSS and JavaScript can give rise to such immersive experiences. But the more complex an application becomes, the more critically it depends on a solid foundation - the nuts and bolts of component hierarchies, state management, and caching layers that hold the whole thing together.

Twist is a library that arose in light of these problems, by focusing on application state, and how it gets bound to reusable UI components. Twist is not a UI framework in its own right, but it does provide a syntax for writing components that's compatible with other frameworks, such as React. It uses a structured reactive approach, so that you easily scale your application, while maintaining an easy way of binding to the view - you'll recognize concepts that were inspired by Mobx and Redux, which are both very popular state-management libraries for React.

To give you a feel for what Twist looks like, here's a small fragment of code, showing part of a model (a store), and part of a view (a component). Notice that stores can nest other stores (DOB and Address are also stores, but aren't shown):

re
s User {
@State.byVal name;
@State.byVal description;
@State.byRef(DOB) birthday;
@State.byRef(Address) address;

@Action CHANGE_DESCRIPTION(newDescription) {
    this.description = newDescription;
}


ponent
s ProfileView {
@Attribute user;
@Observable newDescription;

constructor() {
    super();
    this.newDescription = this.user.description;
}

render() {
    return <g>
        <div>Hello { this.user.name }!</div>
        <if condition={ this.user.birthday.isToday() }>
            <div>Happy Birthday!!</div>
        </if>
        <div>About yourself: <input bind:value={ this.newDescription }/></div>
        <button onClick={ () => this.user.dispatch('CHANGE_DESCRIPTION', this.newDescription) }>Save</button>
    </g>;
}

The Twist Documentation explains everything in great detail, but here's a preview of the main concepts:

While the state portion of Twist is framework-agnostic, the implementation of components depends on which UI framework is used. Right now, Twist only supports React, via React-Twist, but it's designed so that there can be other implementations in the future (and we encourage contributions if you're interested in helping with this!). This would allow the same component to work with multiple UI frameworks, which would be great for shared component libraries! Even today, React-Twist makes it really easy to use Twist stores with React, and also adds a bunch of features on top of React to make your life easier. See here for a complete description of the features.

Note: In case you're wondering, React-Twist components are React components - they just automatically optimize when to re-render, so you don't need to think about it. If you write a component in React-Twist, it can be used directly by a normal React application. Similarly, React-Twist components can use normal React components directly - everything is rendered via React and ReactDOM.

Using Twist

If you want to use both the state-management and component layers of Twist, you'll need to install the following (via NPM or Yarn):

If you're not using webpack, you can also get hold of the Babel configuration directly, using @twist/configuration (this is done automatically by the webpack plugin).

After that, the only thing you need is a .twistrc file in the root of your project, that tells Twist which libraries to include (this is also used by the Twist ESLint plugin). There are a number of advanced options, but to get up and running, you just need to tell Twist that you're using React-Twist:


"libraries": [
    "@twist/react"
]

In your webpack.conf.js you can now include the React Twist plugin - by default this will compile all files that end in .jsx with Twist and React:

t ReactTwistPlugin = require('@twist/react-webpack-plugin');

le.exports = {
...
plugins: [
    new ReactTwistPlugin(),
    ...
],
...


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.