|
| 1 | +import React from 'react'; |
| 2 | +import {ensure} from './util'; |
| 3 | +import {update, unmount} from './Mixin'; |
| 4 | + |
| 5 | +// Mixin for RethinkDB query subscription support in React components. You'll |
| 6 | +// generally want to use DefaultHoC or PropsHoC, which use BaseHoC to |
| 7 | +// create more usable versions. |
| 8 | +// |
| 9 | +// In your component, you should define an observe(props, state) function that |
| 10 | +// returns an object mapping query names to QueryRequests. See |
| 11 | +// QueryRequest.js for the API. |
| 12 | +// |
| 13 | +// In the child component, you will have access to this.props.data, which is an |
| 14 | +// object mapping from the same query names returned in observe() to the |
| 15 | +// results of each query as an QueryResult. See QueryResult.js for the |
| 16 | +// API. |
| 17 | +// |
| 18 | +// Here is a simple example of the mixin API: |
| 19 | +// const observe = (props, state) => ({ |
| 20 | +// turtles: new QueryRequest({ |
| 21 | +// query: r.table('turtles'), |
| 22 | +// changes: true, |
| 23 | +// initial: [], |
| 24 | +// }), |
| 25 | +// }); |
| 26 | + |
| 27 | +// class App extends Component { |
| 28 | +// render() { |
| 29 | +// return <div> |
| 30 | +// {this.props.data.turtles.value().map(function(x) { |
| 31 | +// return <div key={x.id}>{x.firstName}</div>; |
| 32 | +// })}; |
| 33 | +// </div>; |
| 34 | +// }, |
| 35 | +// }; |
| 36 | + |
| 37 | +// BaseHoC(new Session())(observe)(App); |
| 38 | + |
| 39 | +export const BaseHoC = sessionGetter => observe => ChildComponent => class ReactRethinkDB extends React.Component { |
| 40 | + constructor(props, state) { |
| 41 | + super(); |
| 42 | + this.observe = observe; |
| 43 | + } |
| 44 | + |
| 45 | + componentWillMount() { |
| 46 | + const session = sessionGetter(this); |
| 47 | + this.dispatch = session.runQuery.bind(session); |
| 48 | + ensure(session && session._subscriptionManager, |
| 49 | + `Must define Session`); |
| 50 | + ensure(this.observe, `Must define observe()`); |
| 51 | + ensure(session._connPromise, `Must connect() before mounting react-rethinkdb`); |
| 52 | + this._rethinkMixinState = {session, subscriptions: {}}; |
| 53 | + this.data = this.data || {}; |
| 54 | + update(this, this.props, this.state); |
| 55 | + } |
| 56 | + |
| 57 | + componentDidMount() { |
| 58 | + this._rethinkMixinState.isMounted = true; |
| 59 | + } |
| 60 | + |
| 61 | + componentWillUnmount() { |
| 62 | + unmount(this); |
| 63 | + this._rethinkMixinState.isMounted = false; |
| 64 | + } |
| 65 | + |
| 66 | + componentWillUpdate(nextProps, nextState) { |
| 67 | + if (nextProps !== this.props || nextState !== this.state) { |
| 68 | + update(this, nextProps, nextState); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + render() { |
| 73 | + return <ChildComponent data={this.data} dispatch={this.dispatch} {...this.props} />; |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +// HoC that uses rethink session from props. For example: |
| 78 | +// class MyComponent extends Component { |
| 79 | +// ... |
| 80 | +// }); |
| 81 | +// var session = new Session(); |
| 82 | +// React.render(<MyComponent rethinkSession={session} />, mountNode); |
| 83 | +export const PropsHoC = name => BaseHoC(component => component.props[name]); |
0 commit comments