|
| 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 | +// Along with your component, you should pass 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) => ({ |
| 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) { |
| 41 | + super(); |
| 42 | + this.observe = observe; |
| 43 | + } |
| 44 | + |
| 45 | + componentWillMount() { |
| 46 | + const session = sessionGetter(this); |
| 47 | + this.runQuery = session.runQuery.bind(session); |
| 48 | + ensure(session && session._subscriptionManager, 'Must define Session'); |
| 49 | + ensure(this.observe, 'Must define observe()'); |
| 50 | + ensure(session._connPromise, 'Must connect() before mounting react-rethinkdb'); |
| 51 | + this._rethinkMixinState = {session, subscriptions: {}}; |
| 52 | + this.data = this.data || {}; |
| 53 | + update(this, this.props); |
| 54 | + } |
| 55 | + |
| 56 | + componentDidMount() { |
| 57 | + this._rethinkMixinState.isMounted = true; |
| 58 | + } |
| 59 | + |
| 60 | + componentWillUnmount() { |
| 61 | + unmount(this); |
| 62 | + this._rethinkMixinState.isMounted = false; |
| 63 | + } |
| 64 | + |
| 65 | + componentWillUpdate(nextProps) { |
| 66 | + if (nextProps !== this.props) { |
| 67 | + update(this, nextProps); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + render() { |
| 72 | + return <ChildComponent data={this.data} runQuery={this.runQuery} {...this.props} />; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +// Within ChildComponent, you can then run queries like this: |
| 77 | +// var App = React.createClass({ |
| 78 | +// handleSubmit: function(event) { |
| 79 | +// event.preventDefault(); |
| 80 | +// var { runQuery } = this.props |
| 81 | +// var nameInput = this.refs.firstName; |
| 82 | +// var query = r.table('turtles').insert({firstName: nameInput.value}); |
| 83 | +// nameInput.value = ''; |
| 84 | +// runQuery(query); |
| 85 | +// }, |
| 86 | + |
| 87 | +// render: function() { |
| 88 | +// var turtleDivs = this.data.turtles.value().map(function(x) { |
| 89 | +// return <div key={x.id}>{x.firstName}</div>; |
| 90 | +// }); |
| 91 | +// return <div> |
| 92 | +// <form onSubmit={this.handleSubmit}> |
| 93 | +// <input type="text" ref="firstName" /> |
| 94 | +// <input type="submit" /> |
| 95 | +// </form> |
| 96 | +// {turtleDivs} |
| 97 | +// </div>; |
| 98 | +// }, |
| 99 | +// }); |
| 100 | + |
| 101 | +// HoC that uses rethink session from props. For example: |
| 102 | +// class MyComponent extends Component { |
| 103 | +// ... |
| 104 | +// }); |
| 105 | +// var session = new Session(); |
| 106 | +// React.render(<MyComponent rethinkSession={session} />, mountNode); |
| 107 | +export const PropsHoC = name => BaseHoC(component => component.props[name]); |
0 commit comments