@vue-widget/hooks
Version:
hooks from react to vue
36 lines (35 loc) • 1.3 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import { onMounted, watch, onUpdated, onBeforeUnmount } from "vue";
export function createEffect(type) {
return function (callback, deps) {
var lastEffects = null;
function cleanup() {
typeof lastEffects === "function" && lastEffects();
}
function watchCallback(newValue, oldValue) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
cleanup();
lastEffects = callback(newValue, oldValue) || null;
return [2 /*return*/];
});
});
}
(deps === null || deps === void 0 ? void 0 : deps.length) && watch(deps, watchCallback, { immediate: true });
var isMounted = false;
if (!(deps === null || deps === void 0 ? void 0 : deps.length)) {
!deps &&
onUpdated(function () {
isMounted && watchCallback([], []);
});
onMounted(function () {
watchCallback([], []);
isMounted = true;
});
}
onBeforeUnmount(function () {
cleanup();
isMounted = false;
});
};
}