Permalink
Switch branches/tags
refract-xstream@1.0.0 refract-xstream@1.0.0-10 refract-xstream@1.0.0-9 refract-xstream@1.0.0-7 refract-xstream@1.0.0-6 refract-xstream@1.0.0-5 refract-xstream@1.0.0-4 refract-xstream@1.0.0-3 refract-xstream@1.0.0-2 refract-xstream@1.0.0-1 refract-xstream@1.0.0-0 refract-rxjs@1.0.0 refract-rxjs@1.0.0-10 refract-rxjs@1.0.0-9 refract-rxjs@1.0.0-7 refract-rxjs@1.0.0-6 refract-rxjs@1.0.0-5 refract-rxjs@1.0.0-4 refract-rxjs@1.0.0-3 refract-rxjs@1.0.0-2 refract-rxjs@1.0.0-1 refract-rxjs@1.0.0-0 refract-redux-xstream@1.0.0 refract-redux-xstream@1.0.0-8 refract-redux-xstream@1.0.0-7 refract-redux-xstream@1.0.0-5 refract-redux-xstream@1.0.0-4 refract-redux-xstream@1.0.0-3 refract-redux-xstream@1.0.0-2 refract-redux-xstream@1.0.0-1 refract-redux-xstream@1.0.0-0 refract-redux-rxjs@1.0.0 refract-redux-rxjs@1.0.0-8 refract-redux-rxjs@1.0.0-7 refract-redux-rxjs@1.0.0-5 refract-redux-rxjs@1.0.0-4 refract-redux-rxjs@1.0.0-3 refract-redux-rxjs@1.0.0-2 refract-redux-rxjs@1.0.0-1 refract-redux-rxjs@1.0.0-0 refract-redux-most@1.0.0 refract-redux-most@1.0.0-8 refract-redux-most@1.0.0-7 refract-redux-most@1.0.0-5 refract-redux-most@1.0.0-4 refract-redux-most@1.0.0-3 refract-redux-most@1.0.0-2 refract-redux-most@1.0.0-1 refract-redux-most@1.0.0-0 refract-redux-callbag@1.0.0 refract-redux-callbag@1.0.0-8 refract-redux-callbag@1.0.0-7 refract-redux-callbag@1.0.0-5 refract-redux-callbag@1.0.0-4 refract-redux-callbag@1.0.0-3 refract-redux-callbag@1.0.0-2 refract-redux-callbag@1.0.0-1 refract-redux-callbag@1.0.0-0 refract-preact-xstream@1.0.0 refract-preact-xstream@1.0.0-2 refract-preact-xstream@1.0.0-1 refract-preact-rxjs@1.0.0 refract-preact-rxjs@1.0.0-2 refract-preact-rxjs@1.0.0-1 refract-preact-most@1.0.0 refract-preact-most@1.0.0-2 refract-preact-most@1.0.0-1 refract-preact-callbag@1.0.0 refract-preact-callbag@1.0.0-2 refract-preact-callbag@1.0.0-1 refract-most@1.0.0 refract-most@1.0.0-10 refract-most@1.0.0-9 refract-most@1.0.0-7 refract-most@1.0.0-6 refract-most@1.0.0-5 refract-most@1.0.0-4 refract-most@1.0.0-3 refract-most@1.0.0-2 refract-most@1.0.0-1 refract-most@1.0.0-0 refract-inferno-xstream@1.0.0 refract-inferno-xstream@1.0.0-2 refract-inferno-xstream@1.0.0-1 refract-inferno-rxjs@1.0.0 refract-inferno-rxjs@1.0.0-2 refract-inferno-rxjs@1.0.0-1 refract-inferno-most@1.0.0 refract-inferno-most@1.0.0-2 refract-inferno-most@1.0.0-1 refract-inferno-callbag@1.0.0 refract-inferno-callbag@1.0.0-2 refract-inferno-callbag@1.0.0-1 refract-callbag@1.0.0 refract-callbag@1.0.0-10 refract-callbag@1.0.0-9 refract-callbag@1.0.0-7 refract-callbag@1.0.0-6 refract-callbag@1.0.0-5 refract-callbag@1.0.0-4
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
167 lines (113 sloc) 7.21 KB


Master your component's effects through the
power of reactive programming.


Why? · Install · The Gist · Learn · Contribute · Discuss


Build MIT License Styled with Prettier


Refract lets you isolate your app's side effects - API calls, analytics, logging, etc - so that you can write your code in a clear, pure, and declarative fashion by using reactive programming.

Refract is an extensible library built for React, with bindings available for Inferno and Preact. In addition we provide a Redux integration, which can also serve as a template for integrations with other libraries.

Why?

Component-based architecture and functional programming have become an increasingly popular approach for building UIs. They help make apps more predictable, more testable, and more maintainable.

However, our apps don't exist in a vacuum! They need to make network requests, handle data persistence, log analytics, deal with changing time, and so on. Any non-trivial app has to handle any number of these external effects.

These side-effects hold us back from writing fully declarative code. Wouldn't it be nice to cleanly separate them from our apps?

Refract solves this problem for you. For an in-depth introduction, head to Why Refract.

Installation

Refract is available for a number of reactive programming libraries. For each library, a Refract integration is available for React, Inferno, Preact and Redux.

Available packages:

React Inferno Preact Redux
Callbag refract-callbag refract-inferno-callbag refract-preact-callbag refract-redux-callbag
Most refract-most refract-inferno-most refract-preact-most refract-redux-most
RxJS refract-rxjs refract-inferno-rxjs refract-preact-rxjs refract-redux-rxjs
xstream refract-xstream refract-inferno-xstream refract-preact-xstream refract-redux-xstream

To use the latest stable version, simply npm install the package you want to use:

npm install --save refract-rxjs

The Gist

The example below uses refract-rxjs to send data to localstorage.

Every time the username prop changes, its new value is sent into the stream. The stream debounces the input for two seconds, then maps it into an object (with a type of localstorage) under the key value. Each time an effect with the correct type is emitted from this pipeline, the handler calls localstorage.setItem with the effect's name and value properties.

const aperture = initialProps => component => {
    return component.observe('username').pipe(
        debounce(2000),
        map(username => ({
            type: 'localstorage',
            name: 'username',
            value: username
        }))
    )
}

const handler = initialProps => effect => {
    switch (effect.type) {
        case 'localstorage':
            localstorage.setItem(effect.name, effect.value)
            return
    }
}

const WrappedComponent = withEffects(handler)(aperture)(BaseComponent)

Aperture

An aperture controls the streams of data entering Refract. It is a function which observes data sources within your app, passes this data through any necessary logic flows, and outputs a stream of effects.

Signature: (initialProps) => (component) => { return effectStream }.

  • The initialProps are all props passed into the WrappedComponent.
  • The component is an object which lets you observe your React component.
  • Within the body of the function, you observe the event source you choose, pipe the events through your stream library of choice, and return a single stream of effects.

Handler

A handler is a function which causes side-effects in response to any effect object output by the aperture.

Signature: (initialProps) => (effect) => { /* handle effects here */ }.

  • The initialProps are all props passed into the WrappedComponent.
  • The effect is each event emitted by your aperture.
  • Within the body of the function, you call any side-effects imperatively.

withEffects

The withEffects higher-order component implements your side-effect logic as a React component.

Signature: (handler) => (aperture) => (Component) => { return WrappedComponent }

  • The hoc takes in three curried arguments:
    • A handler function
    • An aperture function
    • A React Component
  • The hoc returns a WrappedComponent - an enhanced version of your original Component which includes your side-effect logic.

Learn Refract

Documentation

Documentation is available at refract.js.org. We aim to provide a helpful and thorough documentation: all documentation files are located on this repo and we welcome any pull request helping us achieve that goal.

Examples

We maintain and will grow over time a set of examples to illustrate the potential of Refract, as well as providing reactive programming examples: refract.js.org/examples.

Examples are illustrative and not the idiomatic way to use Refract. Each example is available for the four reactive libraries we support (RxJS, xstream, Most and Callbag), and we provide links to run the code live on codesandbox.io. All examples are hosted on this repo, and we welcome pull requests helping us maintaining them.

Contributing

Guidelines

We welcome many forms of contribution from anyone who wishes to get involved.

Before getting started, please read through our contributing guidelines and code of conduct.

Links

Logo

The Refract logo is available in the Logo directory.

License

Refract is available under the MIT license.

Discuss

Everyone is welcome to join our discussion channel - #refract on the Reactiflux Discord server.

Articles