Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.snap]
trim_trailing_whitespace = false
18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ jobs:
cache: yarn
- name: Install dependencies
run: yarn --frozen-lockfile --ignore-engines
if: matrix.node-version == '6.x' || matrix.node-version == '8.x' || matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x' || matrix.node-version == '16.x'
- name: Install dependencies
run: yarn --frozen-lockfile
if: matrix.node-version != '6.x' && matrix.node-version != '8.x' && matrix.node-version != '10.x' && matrix.node-version != '12.x' && matrix.node-version != '14.x' && matrix.node-version != '16.x'
- name: Run tests with coverage
run: yarn test --ci --coverage
- uses: codecov/codecov-action@v5
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"use strict";

module.exports = {
printWidth: 80,
useTabs: true,
tabWidth: 2,
trailingComma: "none",
arrowParens: "always",
overrides: [
{
files: "*.json",
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ For sync hooks, `tap` is the only valid method to add a plugin. Async hooks also
```js
myCar.hooks.calculateRoutes.tapPromise(
"GoogleMapsPlugin",
(source, target, routesList) => {
(source, target, routesList) =>
// return a promise
return google.maps.findRoute(source, target).then((route) => {
google.maps.findRoute(source, target).then((route) => {
routesList.add(route);
});
}
})

);
myCar.hooks.calculateRoutes.tapAsync(
"BingMapsPlugin",
Expand Down Expand Up @@ -106,7 +106,7 @@ class Car {
/**
* You won't get returned value from SyncHook or AsyncParallelHook,
* to do that, use SyncWaterfallHook and AsyncSeriesWaterfallHook respectively
**/
*/

setSpeed(newSpeed) {
// following call returns undefined even when you returned values
Expand All @@ -117,10 +117,10 @@ class Car {
const routesList = new List();
return this.hooks.calculateRoutes
.promise(source, target, routesList)
.then((res) => {
.then((res) =>
// res is undefined for AsyncParallelHook
return routesList.getRoutes();
});
routesList.getRoutes()
);
}

useNavigationSystemAsync(source, target, callback) {
Expand Down
20 changes: 20 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from "eslint/config";
import config from "eslint-config-webpack";

export default defineConfig([
{
extends: [config],
rules: {
"no-new-func": "off",
"n/prefer-node-protocol": "off"
}
},
{
languageOptions: {
parserOptions: {
ecmaVersion: 2018
}
},
files: ["lib/__tests__/**/*.js"]
}
]);
6 changes: 3 additions & 3 deletions lib/AsyncParallelBailHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AsyncParallelBailHookCodeFactory extends HookCodeFactory {
code += "}\n";
return code;
},
onTap: (i, run, done, doneBreak) => {
onTap: (i, run, done, _doneBreak) => {
let code = "";
if (i > 0) {
code += `if(${i} >= _results.length) {\n`;
Expand All @@ -68,10 +68,10 @@ class AsyncParallelBailHookCodeFactory extends HookCodeFactory {

const factory = new AsyncParallelBailHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncParallelBailHook(args = [], name = undefined) {
const hook = new Hook(args, name);
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncParallelHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class AsyncParallelHookCodeFactory extends HookCodeFactory {

const factory = new AsyncParallelHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncParallelHook(args = [], name = undefined) {
const hook = new Hook(args, name);
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncSeriesBailHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class AsyncSeriesBailHookCodeFactory extends HookCodeFactory {

const factory = new AsyncSeriesBailHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncSeriesBailHook(args = [], name = undefined) {
const hook = new Hook(args, name);
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncSeriesHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class AsyncSeriesHookCodeFactory extends HookCodeFactory {

const factory = new AsyncSeriesHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncSeriesHook(args = [], name = undefined) {
const hook = new Hook(args, name);
Expand Down
4 changes: 2 additions & 2 deletions lib/AsyncSeriesLoopHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class AsyncSeriesLoopHookCodeFactory extends HookCodeFactory {

const factory = new AsyncSeriesLoopHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncSeriesLoopHook(args = [], name = undefined) {
const hook = new Hook(args, name);
Expand Down
11 changes: 6 additions & 5 deletions lib/AsyncSeriesWaterfallHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const Hook = require("./Hook");
const HookCodeFactory = require("./HookCodeFactory");

class AsyncSeriesWaterfallHookCodeFactory extends HookCodeFactory {
content({ onError, onResult, onDone }) {
content({ onError, onResult, _onDone }) {
return this.callTapsSeries({
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
onResult: (i, result, next) => {
let code = "";
code += `if(${result} !== undefined) {\n`;
code += `${this._args[0]} = ${result};\n`;
code += `}\n`;
code += "}\n";
code += next();
return code;
},
Expand All @@ -26,14 +26,15 @@ class AsyncSeriesWaterfallHookCodeFactory extends HookCodeFactory {

const factory = new AsyncSeriesWaterfallHookCodeFactory();

const COMPILE = function (options) {
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
};
}

function AsyncSeriesWaterfallHook(args = [], name = undefined) {
if (args.length < 1)
if (args.length < 1) {
throw new Error("Waterfall hooks must have at least one argument");
}
const hook = new Hook(args, name);
hook.constructor = AsyncSeriesWaterfallHook;
hook.compile = COMPILE;
Expand Down
32 changes: 19 additions & 13 deletions lib/Hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
"Hook.context is deprecated and will be removed"
);

const CALL_DELEGATE = function (...args) {
function CALL_DELEGATE(...args) {
this.call = this._createCall("sync");
return this.call(...args);
};
const CALL_ASYNC_DELEGATE = function (...args) {
}

function CALL_ASYNC_DELEGATE(...args) {
this.callAsync = this._createCall("async");
return this.callAsync(...args);
};
const PROMISE_DELEGATE = function (...args) {
}

function PROMISE_DELEGATE(...args) {
this.promise = this._createCall("promise");
return this.promise(...args);
};
}

class Hook {
constructor(args = [], name = undefined) {
Expand All @@ -38,13 +40,17 @@
this.promise = PROMISE_DELEGATE;
this._x = undefined;

// eslint-disable-next-line no-self-assign
this.compile = this.compile;
// eslint-disable-next-line no-self-assign
this.tap = this.tap;
// eslint-disable-next-line no-self-assign
this.tapAsync = this.tapAsync;
// eslint-disable-next-line no-self-assign
this.tapPromise = this.tapPromise;
}

compile(options) {
compile(_options) {

Check warning on line 53 in lib/Hook.js

View check run for this annotation

Codecov / codecov/patch

lib/Hook.js#L53

Added line #L53 was not covered by tests
throw new Error("Abstract: should be overridden");
}

Expand All @@ -53,7 +59,7 @@
taps: this.taps,
interceptors: this.interceptors,
args: this._args,
type: type
type
});
}

Expand Down Expand Up @@ -150,12 +156,12 @@
let i = this.taps.length;
while (i > 0) {
i--;
const x = this.taps[i];
this.taps[i + 1] = x;
const xStage = x.stage || 0;
const tap = this.taps[i];
this.taps[i + 1] = tap;
const xStage = tap.stage || 0;
if (before) {
if (before.has(x.name)) {
before.delete(x.name);
if (before.has(tap.name)) {
before.delete(tap.name);
continue;
}
if (before.size > 0) {
Expand Down
Loading