UNPKG

4.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var react_1 = require("react");
5var react_effect_1 = require("@shopify/react-effect");
6var react_hooks_1 = require("@shopify/react-hooks");
7var assets_1 = require("./context/assets");
8function 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}
16exports.usePreload = usePreload;
17function 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}
25exports.usePrefetch = usePrefetch;
26function 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}
34exports.useKeepFresh = useKeepFresh;
35function 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 /*return*/, value];
49 }
50 _a.label = 1;
51 case 1:
52 _a.trys.push([1, 3, , 4]);
53 return [4 /*yield*/, resolver.resolve()];
54 case 2:
55 resolved_1 = _a.sent();
56 if (mounted.current) {
57 // It's important to use the function form of setValue here.
58 // Resolved is going to be a function in most cases, since it's
59 // a React component. If you do not set it using the function form,
60 // React treats the component as the function that returns state,
61 // so it sets state with the result of manually calling the component
62 // (so, usually JSX).
63 setValue(function () { return resolved_1; });
64 }
65 return [2 /*return*/, resolved_1];
66 case 3:
67 error_1 = _a.sent();
68 if (mounted.current) {
69 setValue(error_1);
70 }
71 return [2 /*return*/, error_1];
72 case 4: return [2 /*return*/];
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}
88exports.useAsync = useAsync;
89function 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}
98exports.useAsyncAsset = useAsyncAsset;