From 66481c0a022129f3b69a34f403e37e4a3c6709b4 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Fri, 16 Mar 2018 15:28:28 +0100 Subject: [PATCH] feat: Add function.microtaskDelay method --- function/#/index.js | 1 + function/#/microtask-delay.js | 14 ++++++++++++++ package.json | 3 ++- test/function/#/microtask-delay.js | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 function/#/microtask-delay.js create mode 100644 test/function/#/microtask-delay.js diff --git a/function/#/index.js b/function/#/index.js index 74c7f017..02ae9f49 100644 --- a/function/#/index.js +++ b/function/#/index.js @@ -5,6 +5,7 @@ module.exports = { copy: require("./copy"), curry: require("./curry"), lock: require("./lock"), + microtaskDelay: require("./microtask-delay"), not: require("./not"), partial: require("./partial"), spread: require("./spread"), diff --git a/function/#/microtask-delay.js b/function/#/microtask-delay.js new file mode 100644 index 00000000..89d47be5 --- /dev/null +++ b/function/#/microtask-delay.js @@ -0,0 +1,14 @@ +"use strict"; + +var ensurePlainFunction = require("../../object/ensure-plain-function") + , defineLength = require("../_define-length") + , nextTick = require("next-tick"); + +var apply = Function.prototype.apply; + +module.exports = function () { + var src = ensurePlainFunction(this); + return defineLength(function () { + nextTick(apply.bind(src, this, arguments)); + }, this.length); +}; diff --git a/package.json b/package.json index b2aa553a..f661b8a4 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ }, "dependencies": { "es6-iterator": "~2.0.3", - "es6-symbol": "~3.1.1" + "es6-symbol": "~3.1.1", + "next-tick": "1" }, "devDependencies": { "eslint": "^4.15", diff --git a/test/function/#/microtask-delay.js b/test/function/#/microtask-delay.js new file mode 100644 index 00000000..8bd0dc12 --- /dev/null +++ b/test/function/#/microtask-delay.js @@ -0,0 +1,22 @@ +"use strict"; + +var nextTick = require("next-tick"); + +module.exports = function (t, a, d) { + var wasInvoked = false, args = [{}, {}], context = {}; + var target = t.call(function () { + a(this, context); + a.deep(arguments, args); + wasInvoked = true; + }); + + nextTick(function () { + a(wasInvoked, false); + target.apply(context, args); + a(wasInvoked, false); + nextTick(function () { + a(wasInvoked, true); + d(); + }); + }); +};