-
Notifications
You must be signed in to change notification settings - Fork 219
/
Async.tsx
145 lines (123 loc) · 3.39 KB
/
Async.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as React from 'react';
import {
LoadProps,
DeferTiming,
RequestIdleCallbackHandle,
WindowWithRequestIdleCallback,
} from '@shopify/async';
import {Omit} from '@shopify/useful-types';
import {Effect} from '@shopify/react-effect';
import {
IntersectionObserver,
UnsupportedBehavior,
} from '@shopify/react-intersection-observer';
import {AsyncAssetContext, AsyncAssetManager} from './context/assets';
import {trySynchronousResolve, resolve} from './utilities';
export interface AsyncPropsRuntime {
defer?: DeferTiming;
renderLoading?(): React.ReactNode;
}
interface Props<Value> extends LoadProps<Value>, AsyncPropsRuntime {
manager?: AsyncAssetManager;
render?(value: Value | null): React.ReactNode;
renderLoading?(): React.ReactNode;
}
interface State<Value> {
resolved: Value | null;
loading: boolean;
}
class ConnectedAsync<Value> extends React.Component<
Props<Value>,
State<Value>
> {
state: State<Value> = initialState(this.props);
private mounted = true;
private idleCallbackHandle?: RequestIdleCallbackHandle;
componentWillUnmount() {
this.mounted = false;
if (this.idleCallbackHandle != null && 'cancelIdleCallback' in window) {
(window as WindowWithRequestIdleCallback).cancelIdleCallback(
this.idleCallbackHandle,
);
}
}
componentDidMount() {
if (this.state.resolved != null) {
return;
}
const {defer = DeferTiming.Mount} = this.props;
if (this.props.defer === DeferTiming.Idle) {
if ('requestIdleCallback' in window) {
this.idleCallbackHandle = (window as WindowWithRequestIdleCallback).requestIdleCallback(
this.load,
);
} else {
this.load();
}
} else if (defer === DeferTiming.Mount) {
this.load();
}
}
render() {
const {
id,
defer,
manager,
render = defaultRender,
renderLoading = defaultRender,
} = this.props;
const {resolved, loading} = this.state;
const effect =
resolved != null && id != null && manager != null ? (
<Effect
kind={manager.effect}
perform={() => manager.markAsUsed(id())}
/>
) : null;
const content = loading ? renderLoading() : render(resolved);
const intersectionObserver =
loading && defer === DeferTiming.InViewport ? (
<IntersectionObserver
threshold={0}
unsupportedBehavior={UnsupportedBehavior.TreatAsIntersecting}
onIntersectionChange={this.loadIfIntersecting}
/>
) : null;
return (
<>
{effect}
{content}
{intersectionObserver}
</>
);
}
private loadIfIntersecting = ({isIntersecting = true}) => {
return isIntersecting ? this.load() : Promise.resolve();
};
private load = async () => {
const resolved = await resolve(this.props.load);
if (this.mounted) {
this.setState({resolved, loading: false});
}
};
}
export function Async<Value>(props: Omit<Props<Value>, 'manager'>) {
return (
<AsyncAssetContext.Consumer>
{manager => <ConnectedAsync manager={manager} {...props} />}
</AsyncAssetContext.Consumer>
);
}
function initialState<Value>(props: Props<Value>): State<Value> {
const resolved = trySynchronousResolve<Value>({
id: props.id,
defer: props.defer,
});
return {
resolved,
loading: resolved == null,
};
}
function defaultRender() {
return null;
}