Skip to content

Commit c4a23d8

Browse files
authored
Merge pull request #52 from nodegui/feature/hot-reload
Adds hot reloading support for react nodegui
2 parents 026c512 + 5625039 commit c4a23d8

File tree

12 files changed

+92
-24
lines changed

12 files changed

+92
-24
lines changed

extras/assets/nodegui.png

39.7 KB
Loading

extras/assets/nodegui_white.png

65.9 KB
Loading

package-lock.json

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
"docs": "typedoc"
2121
},
2222
"dependencies": {
23+
"react-deep-force-update": "^2.1.3",
24+
"react-proxy": "^2.0.8",
2325
"react-reconciler": "^0.21.0"
2426
},
2527
"peerDependencies": {
26-
"@nodegui/nodegui": "*",
28+
"@nodegui/nodegui": ">=0.3.0",
2729
"@nodegui/qode": "*",
2830
"react": "^16.9.0"
2931
},

src/components/Window/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WindowConfig extends ComponentConfig {
2020
const rootViewLayout = new FlexLayout();
2121
rootViewLayout.setFlexNode(rootView.getFlexNode());
2222
rootView.setLayout(rootViewLayout);
23+
rootView.setInlineStyle("width:'100%'; height:'100%';");
2324
window.setCentralWidget(rootView);
2425
window.setProps(newProps, {});
2526
return window;

src/components/config.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import { AppContainer } from "../reconciler";
55
type UpdatePayload = any;
66

77
export interface RNProps {}
8-
export abstract class RNWidget extends NodeWidget {
8+
export abstract class RNWidget extends NodeWidget implements RNComponent {
9+
static tagName: string;
10+
abstract setProps(newProps: RNProps, oldProps: RNProps): void;
11+
abstract appendInitialChild(child: NodeWidget): void;
12+
abstract appendChild(child: NodeWidget): void;
13+
abstract insertBefore(child: NodeWidget, beforeChild: NodeWidget): void;
14+
abstract removeChild(child: NodeWidget): void;
15+
}
16+
export abstract class RNComponent {
917
static tagName: string;
1018
abstract setProps(newProps: RNProps, oldProps: RNProps): void;
1119
abstract appendInitialChild(child: NodeWidget): void;
@@ -24,25 +32,25 @@ export abstract class ComponentConfig {
2432
rootInstance: AppContainer,
2533
context: any,
2634
workInProgress: Fiber
27-
): NodeWidget;
35+
): RNComponent;
2836
finalizeInitialChildren(
29-
instance: NodeWidget,
37+
instance: RNComponent,
3038
newProps: RNProps,
3139
rootContainerInstance: AppContainer,
3240
context: any
3341
) {
3442
return false;
3543
}
3644
commitMount(
37-
instance: NodeWidget,
45+
instance: RNComponent,
3846
newProps: RNProps,
3947
internalInstanceHandle: any
4048
) {
4149
return;
4250
}
4351
// Update methods:
4452
prepareUpdate(
45-
instance: NodeWidget,
53+
instance: RNComponent,
4654
oldProps: RNProps,
4755
newProps: RNProps,
4856
rootContainerInstance: AppContainer,
@@ -51,7 +59,7 @@ export abstract class ComponentConfig {
5159
return true;
5260
}
5361
abstract commitUpdate(
54-
instance: NodeWidget,
62+
instance: RNComponent,
5563
updatePayload: any,
5664
oldProps: RNProps,
5765
newProps: RNProps,

src/development/hot-reload/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import createProxy, { ReactProxyComponent } from "react-proxy";
2+
import React from "react";
3+
4+
export let appProxy: ReactProxyComponent; // need to export it so that it stays without being gc'd
5+
6+
export function hot(Component: React.ComponentType): React.ComponentType {
7+
if (appProxy) {
8+
appProxy.update(Component);
9+
} else {
10+
appProxy = createProxy(Component);
11+
}
12+
return appProxy.get();
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module "react-proxy" {
2+
interface ReactProxyComponent {
3+
update(Component: React.ComponentType): void;
4+
get(): React.ComponentType;
5+
}
6+
export default function createProxy(
7+
Component: React.ComponentType
8+
): ReactProxyComponent;
9+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { Dial } from "./components/Dial";
1313
export { SpinBox } from "./components/SpinBox";
1414
export { ScrollArea } from "./components/ScrollArea";
1515
export { useEventHandler } from "./hooks";
16+
export { hot, appProxy } from "./development/hot-reload";

src/reconciler/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import Reconciler from "react-reconciler";
22
import { NodeWidget } from "@nodegui/nodegui";
3-
import { getComponentByTagName, RNWidget, RNProps } from "../components/config";
3+
import {
4+
getComponentByTagName,
5+
RNWidget,
6+
RNProps,
7+
RNComponent
8+
} from "../components/config";
49

510
export type AppContainer = Set<NodeWidget>;
611
export const appContainer: AppContainer = new Set<NodeWidget>();
@@ -9,7 +14,7 @@ const HostConfig: Reconciler.HostConfig<
914
string,
1015
RNProps,
1116
AppContainer,
12-
NodeWidget,
17+
RNComponent,
1318
any,
1419
any,
1520
any,

0 commit comments

Comments
 (0)