From f25c29a1e97f697ff7db877ae8fe571ba3406adf Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Wed, 21 Jul 2021 17:15:29 +0200 Subject: [PATCH] Modernize & fix bad var assignment --- History.md | 4 ++++ packages/callback-hook/hook.js | 17 +++++++++-------- packages/callback-hook/hook_tests.js | 16 ++++++++-------- packages/callback-hook/package.js | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/History.md b/History.md index ae80eec32e..306fd58402 100644 --- a/History.md +++ b/History.md @@ -16,6 +16,10 @@ * `email@2.1.1` - Updated `nodemailer` to v6.6.3 +* `callback-hook@1.3.1` + - Modernized the code + - Fixed a variable assignment bug in `dontBindEnvironment` function + ## v2.3.2, 2021-07-13 #### Meteor Version Release diff --git a/packages/callback-hook/hook.js b/packages/callback-hook/hook.js index eb1a7a7bff..15faf5a64c 100644 --- a/packages/callback-hook/hook.js +++ b/packages/callback-hook/hook.js @@ -60,7 +60,7 @@ export class Hook { } register(callback) { - var exceptionHandler = this.exceptionHandler || function (exception) { + const exceptionHandler = this.exceptionHandler || function (exception) { // Note: this relies on the undocumented fact that if bindEnvironment's // onException throws, and you are invoking the callback either in the // browser or from within a Fiber in Node, the exception is propagated. @@ -73,7 +73,7 @@ export class Hook { callback = dontBindEnvironment(callback, exceptionHandler); } - var id = this.nextCallbackId++; + const id = this.nextCallbackId++; this.callbacks[id] = callback; return { @@ -100,12 +100,12 @@ export class Hook { // propagated), so we need to be in a Fiber. Meteor._nodeCodeMustBeInFiber(); - var ids = Object.keys(this.callbacks); - for (var i = 0; i < ids.length; ++i) { - var id = ids[i]; + const ids = Object.keys(this.callbacks); + for (let i = 0; i < ids.length; ++i) { + const id = ids[i]; // check to see if the callback was removed during iteration if (hasOwn.call(this.callbacks, id)) { - var callback = this.callbacks[id]; + const callback = this.callbacks[id]; if (! iterator(callback)) { break; } @@ -117,7 +117,7 @@ export class Hook { // Copied from Meteor.bindEnvironment and removed all the env stuff. function dontBindEnvironment(func, onException, _this) { if (!onException || typeof(onException) === 'string') { - var description = onException || "callback of async function"; + const description = onException || "callback of async function"; onException = function (error) { Meteor._debug( "Exception in " + description, @@ -127,8 +127,9 @@ function dontBindEnvironment(func, onException, _this) { } return function (...args) { + let ret; try { - var ret = func.apply(_this, args); + ret = func.apply(_this, args); } catch (e) { onException(e); } diff --git a/packages/callback-hook/hook_tests.js b/packages/callback-hook/hook_tests.js index 28b05242e9..1aa26aa718 100644 --- a/packages/callback-hook/hook_tests.js +++ b/packages/callback-hook/hook_tests.js @@ -1,6 +1,6 @@ Tinytest.add("callback-hook - binds to registrar's env by default", function (test) { - var hook = new Hook(); - var envVar = new Meteor.EnvironmentVariable; + const hook = new Hook(); + const envVar = new Meteor.EnvironmentVariable; envVar.withValue("registrar's value", function() { hook.register(function() { test.equal(envVar.get(), "registrar's value"); @@ -14,8 +14,8 @@ Tinytest.add("callback-hook - binds to registrar's env by default", function (te }); Tinytest.add("callback-hook - uses invoker's env with {bindEnvironment: false}", function (test) { - var hook = new Hook({ bindEnvironment: false }); - var envVar = new Meteor.EnvironmentVariable; + const hook = new Hook({ bindEnvironment: false }); + const envVar = new Meteor.EnvironmentVariable; envVar.withValue("registrar's value", function() { hook.register(function() { test.equal(envVar.get(), "invoker's value"); @@ -29,7 +29,7 @@ Tinytest.add("callback-hook - uses invoker's env with {bindEnvironment: false}", }); Tinytest.add("callback-hook - exceptions unhandled with {bindEnvironment: false}", function (test) { - var hook = new Hook({ bindEnvironment: false }); + const hook = new Hook({ bindEnvironment: false }); hook.register(function() { throw new Error("Test error"); }); @@ -39,9 +39,9 @@ Tinytest.add("callback-hook - exceptions unhandled with {bindEnvironment: false} }); Tinytest.add("callback-hook - exceptionHandler used with {bindEnvironment: false}", function (test) { - var exToThrow = new Error("Test error"); - var thrownEx = null; - var hook = new Hook({ + const exToThrow = new Error("Test error"); + let thrownEx = null; + const hook = new Hook({ bindEnvironment: false, exceptionHandler: function (ex) { thrownEx = ex; } }); diff --git a/packages/callback-hook/package.js b/packages/callback-hook/package.js index 97222aae47..bfbcdd3168 100644 --- a/packages/callback-hook/package.js +++ b/packages/callback-hook/package.js @@ -1,6 +1,6 @@ Package.describe({ summary: "Register callbacks on a hook", - version: '1.3.0' + version: '1.3.1' }); Package.onUse(function (api) {