Skip to content

Commit 63f7187

Browse files
committed
Fix lazy and double wrapping
1 parent 759f87c commit 63f7187

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

packages/react-scripts/template/src/Hello.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import React, { useState } from 'react';
2-
import CounterClass from './CounterClass';
3-
import CounterFunction from './CounterFunction';
1+
import React, { Suspense, lazy, useState } from 'react';
42
import HOCClass from './HOCClass';
53
import HOCFunction from './HOCFunction';
4+
import CounterClass from './CounterClass';
5+
import CounterFunction from './CounterFunction';
66

7+
let LazyCC;
8+
let LazyCF;
9+
let DblCC;
10+
let DblCF;
711
let HCC;
812
let HCF;
913
let HFC;
@@ -13,20 +17,38 @@ let Hello = window.__assign(module, 'Hello', function Hello() {
1317
const [value] = useState(Math.random());
1418

1519
return (
16-
<h3>
17-
{value.toString().slice(0, 5)}
18-
<br />
19-
hello world!
20-
<br />
21-
class: <CounterClass hocChild />
22-
<br />
23-
function: <CounterFunction hocChild />
24-
<br />
25-
hocs: <HCC /> <HCF /> <HFC /> <HFF />
26-
</h3>
20+
<Suspense fallback={<div />}>
21+
<h3>
22+
{value.toString().slice(0, 5)}
23+
<br />
24+
hello world!
25+
<br />
26+
class: <CounterClass hocChild />
27+
<br />
28+
function: <CounterFunction hocChild />
29+
<br />
30+
doublewrapped: <DblCC /> <DblCF />
31+
<br />
32+
lazy: <LazyCC /> <LazyCF />
33+
<br />
34+
hocs: <HCC /> <HCF /> <HFC /> <HFF />
35+
</h3>
36+
</Suspense>
2737
);
2838
});
2939

40+
LazyCC = window.__assign(
41+
module,
42+
'LazyCC',
43+
lazy(() => import('./CounterClass'))
44+
);
45+
LazyCF = window.__assign(
46+
module,
47+
'LazyCF',
48+
lazy(() => import('./CounterFunction'))
49+
);
50+
DblCC = window.__assign(module, 'DblCC', CounterClass);
51+
DblCF = window.__assign(module, 'DblCF', CounterFunction);
3052
HCC = window.__assign(module, 'HCC', HOCClass(CounterClass, 'red'));
3153
HCF = window.__assign(module, 'HCF', HOCClass(CounterFunction, 'orange'));
3254
HFC = window.__assign(module, 'HFC', HOCFunction(CounterClass, 'yellow'));

packages/react-scripts/template/src/TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
lazy()?
2626

27+
- lazy of memo of proxy
28+
2729
- maybe Proxy?
2830
- do I want to preserve type? elementType? or what?
2931
or lazy element?

packages/react-scripts/template/src/hot.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export function HotContainer({ children }) {
88
return <HotContext.Provider value={inc}>{children}</HotContext.Provider>;
99
}
1010

11-
let storage = new Map();
12-
let latestRawFunctions = new Map();
11+
let idToPersistentType = new Map();
12+
let idToRawFunction = new Map();
13+
let proxies = new WeakSet();
1314

1415
function getKind(type) {
1516
if (typeof type === 'function') {
@@ -21,6 +22,8 @@ function getKind(type) {
2122
} else if (type !== null && typeof type === 'object') {
2223
if (type.$$typeof === Symbol.for('react.memo')) {
2324
return 'memo';
25+
} else if (type.$$typeof === Symbol.for('react.lazy')) {
26+
return 'lazy';
2427
}
2528
}
2629
return 'other';
@@ -30,19 +33,27 @@ function init(rawType, id) {
3033
let kind = getKind(rawType);
3134
switch (kind) {
3235
case 'function': {
33-
latestRawFunctions.set(id, rawType);
34-
return new Proxy(rawType, {
36+
if (proxies.has(rawType)) {
37+
return rawType;
38+
}
39+
idToRawFunction.set(id, rawType);
40+
const proxy = new Proxy(rawType, {
3541
apply(target, thisArg, args) {
36-
let ret = latestRawFunctions.get(id).apply(null, args);
42+
let ret = idToRawFunction.get(id).apply(null, args);
3743
React.useContext(HotContext);
3844
return ret;
3945
},
4046
});
47+
proxies.add(proxy);
48+
return proxy;
4149
}
4250
case 'memo': {
4351
rawType.type = init(rawType.type, id);
4452
return rawType;
4553
}
54+
case 'lazy': {
55+
return rawType;
56+
}
4657
default: {
4758
return rawType;
4859
}
@@ -57,12 +68,15 @@ function accept(type, nextRawType, id) {
5768
}
5869
switch (kind) {
5970
case 'function': {
60-
latestRawFunctions.set(id, nextRawType);
71+
idToRawFunction.set(id, nextRawType);
6172
return true;
6273
}
6374
case 'memo': {
6475
return accept(type.type, nextRawType.type, id);
6576
}
77+
case 'lazy': {
78+
return true;
79+
}
6680
default: {
6781
return false;
6882
}
@@ -75,10 +89,10 @@ window.__assign = function(webpackModule, localId, nextRawType) {
7589
setTimeout(() => _invalidate());
7690
});
7791
const id = webpackModule.i + '$' + localId;
78-
let type = storage.get(id);
92+
let type = idToPersistentType.get(id);
7993
if (!accept(type, nextRawType, id)) {
8094
type = init(nextRawType, id);
81-
storage.set(id, type);
95+
idToPersistentType.set(id, type);
8296
}
8397
return type;
8498
};

0 commit comments

Comments
 (0)