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