1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var react_1 = require("react");
|
5 | var react_effect_1 = require("@shopify/react-effect");
|
6 | var react_hooks_1 = require("@shopify/react-hooks");
|
7 | var assets_1 = require("./context/assets");
|
8 | function usePreload() {
|
9 | var args = [];
|
10 | for (var _i = 0; _i < arguments.length; _i++) {
|
11 | args[_i] = arguments[_i];
|
12 | }
|
13 | var _a = tslib_1.__read(args, 2), asyncComponent = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
14 | return asyncComponent.usePreload(options);
|
15 | }
|
16 | exports.usePreload = usePreload;
|
17 | function usePrefetch() {
|
18 | var args = [];
|
19 | for (var _i = 0; _i < arguments.length; _i++) {
|
20 | args[_i] = arguments[_i];
|
21 | }
|
22 | var _a = tslib_1.__read(args, 2), asyncComponent = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
23 | return asyncComponent.usePrefetch(options);
|
24 | }
|
25 | exports.usePrefetch = usePrefetch;
|
26 | function useKeepFresh() {
|
27 | var args = [];
|
28 | for (var _i = 0; _i < arguments.length; _i++) {
|
29 | args[_i] = arguments[_i];
|
30 | }
|
31 | var _a = tslib_1.__read(args, 2), asyncComponent = _a[0], _b = _a[1], options = _b === void 0 ? {} : _b;
|
32 | return asyncComponent.useKeepFresh(options);
|
33 | }
|
34 | exports.useKeepFresh = useKeepFresh;
|
35 | function useAsync(resolver, _a) {
|
36 | var _this = this;
|
37 | var _b = _a === void 0 ? {} : _a, assets = _b.assets, _c = _b.scripts, scripts = _c === void 0 ? assets : _c, _d = _b.styles, styles = _d === void 0 ? assets : _d, _e = _b.immediate, immediate = _e === void 0 ? true : _e;
|
38 | var _f = tslib_1.__read(react_1.useState(function () {
|
39 | return immediate || typeof window !== 'undefined' ? resolver.resolved : null;
|
40 | }), 2), value = _f[0], setValue = _f[1];
|
41 | var mounted = react_hooks_1.useMountedRef();
|
42 | var load = react_1.useCallback(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
43 | var resolved_1, error_1;
|
44 | return tslib_1.__generator(this, function (_a) {
|
45 | switch (_a.label) {
|
46 | case 0:
|
47 | if (value != null) {
|
48 | return [2 , value];
|
49 | }
|
50 | _a.label = 1;
|
51 | case 1:
|
52 | _a.trys.push([1, 3, , 4]);
|
53 | return [4 , resolver.resolve()];
|
54 | case 2:
|
55 | resolved_1 = _a.sent();
|
56 | if (mounted.current) {
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | setValue(function () { return resolved_1; });
|
64 | }
|
65 | return [2 , resolved_1];
|
66 | case 3:
|
67 | error_1 = _a.sent();
|
68 | if (mounted.current) {
|
69 | setValue(error_1);
|
70 | }
|
71 | return [2 , error_1];
|
72 | case 4: return [2 ];
|
73 | }
|
74 | });
|
75 | }); }, [mounted, resolver, value]);
|
76 | var id = resolver.id;
|
77 | useAsyncAsset(id, { scripts: scripts, styles: styles });
|
78 | return value instanceof Error
|
79 | ? { id: id, resolved: null, error: value, loading: false, load: load }
|
80 | : {
|
81 | id: id,
|
82 | resolved: value,
|
83 | error: null,
|
84 | loading: value == null,
|
85 | load: load,
|
86 | };
|
87 | }
|
88 | exports.useAsync = useAsync;
|
89 | function useAsyncAsset(id, _a) {
|
90 | var _b = _a === void 0 ? {} : _a, scripts = _b.scripts, styles = _b.styles;
|
91 | var async = react_1.useContext(assets_1.AsyncAssetContext);
|
92 | react_effect_1.useServerEffect(function () {
|
93 | if (async && id) {
|
94 | async.markAsUsed(id, { scripts: scripts, styles: styles });
|
95 | }
|
96 | }, async ? async.effect : undefined);
|
97 | }
|
98 | exports.useAsyncAsset = useAsyncAsset;
|