Skip to content

Commit f15d7d4

Browse files
committed
📦 generate bundles and type definitions
1 parent 8f5a8bf commit f15d7d4

68 files changed

Lines changed: 184 additions & 179 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎dist/index.cjs‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ var ActionType;
3030
})(ActionType || (ActionType = {}));
3131
var ActionType$1 = ActionType;
3232

33-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34-
var reducer = function (state, action) {
33+
function reducer(state, action) {
3534
switch (action.type) {
3635
case ActionType$1.STARTED:
3736
return tslib.__assign(tslib.__assign({}, state), { pendingTasks: state.pendingTasks + 1 });
@@ -42,9 +41,9 @@ var reducer = function (state, action) {
4241
case ActionType$1.SUCCEEDED:
4342
return tslib.__assign(tslib.__assign({}, state), { error: null, result: action.result });
4443
default:
45-
throw new Error("An invalid action was dispatched to 'useTaskReducer'.");
44+
throw new Error('Invalid action type.');
4645
}
47-
};
46+
}
4847

4948
/** Get the initial state for the store. */
5049
function getInitialState() {
@@ -95,12 +94,25 @@ function useAbortController() {
9594
};
9695
}
9796

98-
function useImperativeAsyncTask() {
97+
function useAsyncTaskReducer() {
98+
var mounted = useMounted();
99+
var _a = react.useReducer(
100+
// Prettier doesn't yet support instantiation expressions.
101+
// eslint-disable-next-line prettier/prettier
102+
(reducer), react.useMemo((getInitialState), [])), state = _a[0], dispatch = _a[1];
103+
var dispatchOnlyIfMounted = react.useCallback(function (action) {
104+
if (mounted()) {
105+
dispatch(action);
106+
}
107+
}, []);
108+
return [state, dispatchOnlyIfMounted];
109+
}
110+
111+
function useAsyncTaskLazy() {
99112
var _this = this;
100113
var signal = useAbortController().signal;
101-
var initialState = react.useMemo(getInitialState, []);
102-
var _a = react.useReducer(reducer, initialState), state = _a[0], dispatch = _a[1];
103-
var executeTask = react.useCallback(function (task) { return tslib.__awaiter(_this, void 0, void 0, function () {
114+
var _a = useAsyncTaskReducer(), state = _a[0], dispatch = _a[1];
115+
var executeAsyncTask = react.useCallback(function (task) { return tslib.__awaiter(_this, void 0, void 0, function () {
104116
var _a, error_1;
105117
var _b;
106118
return tslib.__generator(this, function (_c) {
@@ -135,38 +147,28 @@ function useImperativeAsyncTask() {
135147
}
136148
});
137149
}); }, []);
138-
var error = state.error, result = state.result, pendingTasks = state.pendingTasks;
139150
return {
140-
error: error,
141-
result: result,
142-
pending: pendingTasks > 0,
143-
executeTask: executeTask,
151+
error: state.error,
152+
result: state.result,
153+
pending: state.pendingTasks > 0,
154+
executeAsyncTask: executeAsyncTask,
144155
};
145156
}
146157

147-
function useLazyAsyncTask(task) {
148-
var _a = useImperativeAsyncTask(), executeTask = _a.executeTask, state = tslib.__rest(_a, ["executeTask"]);
149-
return tslib.__assign(tslib.__assign({}, state), { executeTask: react.useCallback(function () { return executeTask(task); }, [task]) });
150-
}
151-
152-
function useImmediateAsyncTask(task) {
158+
function useAsyncTask(task) {
153159
var firstRenderRef = react.useRef(true);
154-
var _a = useLazyAsyncTask(task), error = _a.error, result = _a.result, pending = _a.pending, executeTask = _a.executeTask;
160+
var _a = useAsyncTaskLazy(), error = _a.error, result = _a.result, pending = _a.pending, executeAsyncTask = _a.executeAsyncTask;
155161
react.useLayoutEffect(function () {
156-
executeTask();
157-
return function () {
158-
firstRenderRef.current = false;
159-
};
160-
}, [executeTask]);
162+
firstRenderRef.current = false;
163+
executeAsyncTask(task);
164+
}, [task, executeAsyncTask]);
161165
return {
162166
error: error,
163167
result: result,
164168
pending: firstRenderRef.current || pending,
165-
executeTask: executeTask,
166169
};
167170
}
168171

169-
exports.useImmediateAsyncTask = useImmediateAsyncTask;
170-
exports.useImperativeAsyncTask = useImperativeAsyncTask;
171-
exports.useLazyAsyncTask = useLazyAsyncTask;
172+
exports.useAsyncTask = useAsyncTask;
173+
exports.useAsyncTaskLazy = useAsyncTaskLazy;
172174
//# sourceMappingURL=index.cjs.map

‎dist/index.cjs.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.iife.js‎

Lines changed: 31 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)