UNPKG

2.01 kBJavaScriptView Raw
1/**
2 * React Blessed
3 * ==============
4 *
5 * Exposing the renderer's API.
6 */
7import ReactInstanceHandles from 'react/lib/ReactInstanceHandles';
8import ReactElement from 'react/lib/ReactElement';
9import ReactUpdates from 'react/lib/ReactUpdates';
10import ReactBlessedIDOperations from './ReactBlessedIDOperations';
11import invariant from 'invariant';
12import instantiateReactComponent from 'react/lib/instantiateReactComponent';
13import inject from './ReactBlessedInjection';
14import {Screen} from 'blessed';
15
16/**
17 * Injecting dependencies.
18 */
19inject();
20
21/**
22 * Renders the given react element with blessed.
23 *
24 * @param {ReactElement} element - Node to update.
25 * @param {BlessedScreen} screen - The screen used to render the app.
26 * @return {ReactComponent} - The rendered component instance.
27 */
28function render(element, screen) {
29
30 // Is the given element valid?
31 invariant(
32 ReactElement.isValidElement(element),
33 'render(): You must pass a valid ReactElement.'
34 );
35
36 // Is the given screen valid?
37 invariant(
38 screen instanceof Screen,
39 'render(): You must pass a valid BlessedScreen.'
40 );
41
42 // Creating a root id & creating the screen
43 const id = ReactInstanceHandles.createReactRootID();
44
45 // Mounting the app
46 const component = instantiateReactComponent(element);
47
48 // Injecting the screen
49 ReactBlessedIDOperations.setScreen(screen);
50
51 // The initial render is synchronous but any updates that happen during
52 // rendering, in componentWillMount or componentDidMount, will be batched
53 // according to the current batching strategy.
54 ReactUpdates.batchedUpdates(() => {
55 // Batched mount component
56 const transaction = ReactUpdates.ReactReconcileTransaction.getPooled();
57 transaction.perform(() => {
58 component.mountComponent(id, transaction, {});
59 });
60 ReactUpdates.ReactReconcileTransaction.release(transaction);
61 });
62
63 // Returning the screen so the user can attach listeners etc.
64 return component._instance;
65}
66
67export {render};