UNPKG

cz-conventional-changelog-befe

Version:
55 lines (49 loc) 981 B
'use strict'; /** * @file memoize * @author Cuttle Cong * @date 2018/10/24 * */ var iseq = require('lodash.isequalwith'); var memoize = require('./memfn'); function allowFnEq(a, b) { var rlt = iseq(a, b, function (oldV, newV) { if (typeof oldV === 'function' && typeof newV === 'function') { return oldV.toString() === newV.toString(); } }); return rlt; } module.exports = function (fn) { var update = function update() { return mfn = memoize(fn, { eq: allowFnEq }); }; var mfn = void 0; update(); function wrapfn() { var rlt = void 0; try { rlt = mfn.apply(this, arguments); } catch (err) { update(); throw err; } if (rlt && typeof rlt.then === 'function') { return rlt.catch(function (err) { update(); throw err; }); } return rlt; } var ctx = { fn: wrapfn, cancel: function cancel() { return ctx.fn = fn; } }; return ctx; };