|
1 | 1 | import reconciler, { appContainer } from "../reconciler"; |
2 | 2 | import { initDevtools } from "../utils/devtools"; |
3 | 3 |
|
| 4 | +type Options = { |
| 5 | + enableDevtools?: boolean; |
| 6 | + onRender?: () => void; |
| 7 | +}; |
| 8 | +const defaultOptions = { |
| 9 | + enableDevtools: false, |
| 10 | + onRender: () => {} |
| 11 | +}; |
| 12 | + |
4 | 13 | export const Renderer = { |
5 | | - render(element: React.ReactNode, callback: () => void) { |
6 | | - // element: This is the react element for App component |
7 | | - // window: This is the container that will contain the app. |
8 | | - // callback: if specified will be called after render is done. |
| 14 | + render(element: React.ReactNode, options?: Options) { |
9 | 15 | const containerInfo = appContainer; |
10 | 16 | const isConcurrent = false; //disabling since there seems to be a bug with onclick listeneres (when called without a console.log inside them) |
11 | 17 | const hydrate = false; |
12 | 18 |
|
| 19 | + const rendererOptions = Object.assign({}, defaultOptions, options); |
| 20 | + |
13 | 21 | const container = reconciler.createContainer( |
14 | 22 | containerInfo, |
15 | 23 | isConcurrent, |
16 | 24 | hydrate |
17 | 25 | ); // Creates root fiber node. |
18 | 26 |
|
19 | | - initDevtools(reconciler); //TODO: Do it on dev mode only |
| 27 | + if (rendererOptions.enableDevtools) { |
| 28 | + initDevtools(reconciler); |
| 29 | + } |
20 | 30 | const parentComponent = null; // Since there is no parent (since this is the root fiber). We set parentComponent to null. |
21 | 31 | reconciler.updateContainer(element, container, parentComponent, () => { |
22 | | - callback(); |
| 32 | + rendererOptions.onRender(); |
23 | 33 | }); // Start reconcilation and render the result |
24 | 34 | } |
25 | 35 | }; |
0 commit comments