Skip to content

Commit 4c1133f

Browse files
committed
Revert to vanilla and fix HOCs
1 parent 8d8f206 commit 4c1133f

File tree

7 files changed

+92
-32
lines changed

7 files changed

+92
-32
lines changed

Diff for: packages/babel-plugin-hot-reload/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ function decorateFunctionId(t, name, generatedName) {
8383
}
8484

8585
module.exports = function({ types: t }) {
86+
return { visitor: {} };
87+
8688
return {
8789
visitor: {
8890
ExportDefaultDeclaration(path, state) {

Diff for: packages/react-scripts/template/src/CounterClass.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React, { Component } from 'react';
22
import CounterFunction from './CounterFunction';
33
import HOCFunction from './HOCFunction';
4-
const HFF = HOCFunction(CounterFunction);
4+
5+
const HFF = window.__createProxy(module, 'HFF');
6+
HFF.__setImpl(HOCFunction(CounterFunction));
57

68
export default class Counter extends Component {
79
state = { value: 0 };
+13-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import React, { useState, useLayoutEffect } from 'react';
1+
import React, { useReducer, useLayoutEffect } from 'react';
22
import HOCFunction from './HOCFunction';
3-
const HFF = HOCFunction(Counter);
43

5-
export default function Counter(props) {
6-
const [value, setValue] = useState(0);
4+
let Counter = window.__createProxy(module, 'Counter');
5+
let HFF = window.__createProxy(module, 'HFF');
6+
7+
HFF.__setImpl(HOCFunction(Counter));
8+
Counter.__setImpl(function CounterImpl(props) {
9+
const [value, dispatch] = useReducer((v, a) => {
10+
return a === 'inc' ? v + 1 : v;
11+
}, 0);
712
useLayoutEffect(() => {
8-
const id = setInterval(() => setValue(c => c + 1), 1000);
13+
const id = setInterval(() => dispatch('inc'), 1000);
914
return () => clearInterval(id);
1015
}, []);
1116
return (
@@ -18,4 +23,6 @@ export default function Counter(props) {
1823
)}
1924
</span>
2025
);
21-
}
26+
});
27+
28+
export default Counter;

Diff for: packages/react-scripts/template/src/Hello.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ import CounterClass from './CounterClass';
33
import CounterFunction from './CounterFunction';
44
import HOCClass from './HOCClass';
55
import HOCFunction from './HOCFunction';
6-
const HCC = HOCClass(CounterClass);
7-
const HCF = HOCClass(CounterFunction);
8-
const HFC = HOCFunction(CounterClass);
9-
const HFF = HOCFunction(CounterFunction);
106

11-
export default function Hello() {
7+
let Hello = window.__createProxy(module, 'Hello');
8+
let HCC = window.__createProxy(module, 'CounterClass');
9+
let HCF = window.__createProxy(module, 'HCF');
10+
let HFC = window.__createProxy(module, 'HFC');
11+
let HFF = window.__createProxy(module, 'HFF');
12+
13+
HCC.__setImpl(HOCClass(CounterClass));
14+
HCF.__setImpl(HOCClass(CounterFunction));
15+
HFC.__setImpl(HOCFunction(CounterClass));
16+
HFF.__setImpl(HOCFunction(CounterFunction));
17+
18+
Hello.__setImpl(function HelloImpl() {
1219
const [value] = useState(Math.random());
1320

1421
return (
1522
<h3>
1623
{value.toString().slice(0, 5)}
1724
<br />
18-
hello!
25+
world
1926
<br />
2027
class: <CounterClass hocChild />
2128
<br />
@@ -24,4 +31,6 @@ export default function Hello() {
2431
hocs: <HCC /> <HCF /> <HFC /> <HFF />
2532
</h3>
2633
);
27-
}
34+
});
35+
36+
export default Hello;

Diff for: packages/react-scripts/template/src/TODO.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
- frags
22
- error handling
33
- hocs?
4+
- with self as input
5+
- returning a class
6+
- HOC itself by mistake
7+
- protect against nested?
8+
- type equality
9+
- memo and friends
10+
- nested / applied twice
11+
- mutually recursive
12+
- re-exports
13+
- same HOC applied early, two callsites
14+
- classes
15+
- how to detect and reload?
416
- render props
17+
- directives?
18+
- when to accept?
19+
- test integrations

Diff for: packages/react-scripts/template/src/hot.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
3+
let HotContext = React.createContext();
4+
let _invalidate;
5+
export function HotContainer({ children }) {
6+
const [inc, setInc] = React.useState(0);
7+
_invalidate = () => setInc(c => c + 1);
8+
return <HotContext.Provider value={inc}>{children}</HotContext.Provider>;
9+
}
10+
11+
window.__createProxy = function(m, localId) {
12+
m.hot.accept();
13+
m.hot.dispose(() => {
14+
setTimeout(() => window.__enqueueForceUpdate());
15+
});
16+
const id = m.i + '$' + localId;
17+
18+
if (!window[`${id}_proxy`]) {
19+
function P(...args) {
20+
let impl = window[`${id}_impl`];
21+
if (impl.prototype && impl.prototype.isReactComponent) {
22+
return new impl(...args);
23+
}
24+
let res = impl.apply(this, arguments);
25+
window.__renderHook();
26+
return res;
27+
}
28+
window[`${id}_proxy`] = P;
29+
P.__setImpl = impl => {
30+
window[`${id}_impl`] = impl;
31+
};
32+
}
33+
return window[`${id}_proxy`];
34+
};
35+
36+
window.__enqueueForceUpdate = function(type) {
37+
_invalidate();
38+
};
39+
40+
window.__renderHook = function(type) {
41+
React.useContext(HotContext);
42+
};

Diff for: packages/react-scripts/template/src/index.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3+
import { HotContainer } from './hot';
34
import './index.css';
45
import App from './App';
56
import * as serviceWorker from './serviceWorker';
67

7-
let HotContext = React.createContext();
8-
let _invalidate;
9-
function HotContainer({ children }) {
10-
const [inc, setInc] = React.useState(0);
11-
_invalidate = () => setInc(c => c + 1);
12-
return <HotContext.Provider value={inc}>{children}</HotContext.Provider>;
13-
}
14-
15-
window.__enqueueForceUpdate = function(type) {
16-
console.log('force', type.name);
17-
_invalidate();
18-
};
19-
20-
window.__renderHook = function(type) {
21-
console.log('render', type.name);
22-
React.useContext(HotContext);
23-
};
24-
258
ReactDOM.render(
269
<HotContainer>
2710
<App />

0 commit comments

Comments
 (0)