docsify/src/core/init/lifecycle.js
John Hildenbiddle c49c39a4a2
refactor: Update test environments and lint configuration (#1736)
* Update test environments and lint configuration

Update Jest (unit + integration) and Playwright (e2e) test environments. Includes stability improvements for e2e tests using newer, more stable methods per the Playwright docs.

- Update Jest 26 => 27
- Update Jest-related libs (babel parser)
- Update Playwright 1.8 => Playwright Test 1.18
- Update GitHub CI (action versions, job parallelization, and matrices)
- Update ESLint 5 => 8
- Update ESLint-related libs (parser, prettier, Jest, Playwright)
- Fix test failures on M1-based Macs
- Fix e2e stability issues by replacing PW $ method calls
- Fix ESLint errors
- Fix incorrect CI flag on Jest runs (-ci => --ci)
- Refactor e2e test runner from Jest to Playwright Test
- Refactor e2e test files for Playwright Test
- Refactor fix-lint script name to lint:fix for consistency
- Refactor npm scripts order for readability
- Remove unnecessary configs and libs
- Remove example image snapshots
2022-01-30 21:40:21 -06:00

58 lines
1.3 KiB
JavaScript

import { noop } from '../util/core';
/** @typedef {import('../Docsify').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Lifecycle(Base) {
return class Lifecycle extends Base {
initLifecycle() {
const hooks = [
'init',
'mounted',
'beforeEach',
'afterEach',
'doneEach',
'ready',
];
this._hooks = {};
this._lifecycle = {};
hooks.forEach(hook => {
const arr = (this._hooks[hook] = []);
this._lifecycle[hook] = fn => arr.push(fn);
});
}
callHook(hookName, data, next = noop) {
const queue = this._hooks[hookName];
const step = function (index) {
const hookFn = queue[index];
if (index >= queue.length) {
next(data);
} else if (typeof hookFn === 'function') {
if (hookFn.length === 2) {
hookFn(data, result => {
data = result;
step(index + 1);
});
} else {
const result = hookFn(data);
data = result === undefined ? data : result;
step(index + 1);
}
} else {
step(index + 1);
}
};
step(0);
}
};
}