Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): introduce async version of plugin dispatch mechanism (#3994)
Refs #3993
- Loading branch information
Showing
8 changed files
with
179 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/apidom-core/src/refractor/plugins/dispatcher/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Element } from 'minim'; | ||
import { mergeDeepRight, propOr } from 'ramda'; | ||
import { invokeArgs } from 'ramda-adjunct'; | ||
|
||
import createToolbox from '../../toolbox'; | ||
import { getNodeType, mergeAllVisitors, visit } from '../../../traversal/visitor'; | ||
|
||
export interface DispatchPluginsOptions { | ||
toolboxCreator: typeof createToolbox; | ||
visitorOptions: { | ||
nodeTypeGetter: typeof getNodeType; | ||
exposeEdits: boolean; | ||
}; | ||
} | ||
|
||
export interface DispatchPluginsSync { | ||
<T extends Element, U extends Element = Element>( | ||
element: T, | ||
plugins: ((toolbox: any) => object)[], | ||
options?: Record<string, unknown>, | ||
): U; | ||
[key: symbol]: DispatchPluginsAsync; | ||
} | ||
|
||
export interface DispatchPluginsAsync { | ||
<T extends Element, U extends Element = Element>( | ||
element: T, | ||
plugins: ((toolbox: any) => object)[], | ||
options?: Record<string, unknown>, | ||
): Promise<U>; | ||
} | ||
|
||
const defaultDispatchPluginsOptions: DispatchPluginsOptions = { | ||
toolboxCreator: createToolbox, | ||
visitorOptions: { | ||
nodeTypeGetter: getNodeType, | ||
exposeEdits: true, | ||
}, | ||
}; | ||
|
||
export const dispatchPluginsSync: DispatchPluginsSync = ((element, plugins, options = {}) => { | ||
if (plugins.length === 0) return element; | ||
|
||
const mergedOptions = mergeDeepRight( | ||
defaultDispatchPluginsOptions, | ||
options, | ||
) as DispatchPluginsOptions; | ||
const { toolboxCreator, visitorOptions } = mergedOptions; | ||
const toolbox = toolboxCreator(); | ||
const pluginsSpecs = plugins.map((plugin) => plugin(toolbox)); | ||
const mergedPluginsVisitor = mergeAllVisitors(pluginsSpecs.map(propOr({}, 'visitor')), { | ||
...visitorOptions, | ||
}); | ||
|
||
pluginsSpecs.forEach(invokeArgs(['pre'], [])); | ||
const newElement = visit(element, mergedPluginsVisitor, visitorOptions as any); | ||
pluginsSpecs.forEach(invokeArgs(['post'], [])); | ||
return newElement; | ||
}) as DispatchPluginsSync; | ||
|
||
export const dispatchPluginsAsync: DispatchPluginsAsync = async ( | ||
element, | ||
plugins, | ||
options = {}, | ||
) => { | ||
if (plugins.length === 0) return element; | ||
|
||
const mergedOptions = mergeDeepRight( | ||
defaultDispatchPluginsOptions, | ||
options, | ||
) as DispatchPluginsOptions; | ||
const { toolboxCreator, visitorOptions } = mergedOptions; | ||
const toolbox = toolboxCreator(); | ||
const pluginsSpecs = plugins.map((plugin) => plugin(toolbox)); | ||
const mergeAllVisitorsAsync = mergeAllVisitors[Symbol.for('nodejs.util.promisify.custom')]; | ||
const visitAsync = (visit as any)[Symbol.for('nodejs.util.promisify.custom')]; | ||
const mergedPluginsVisitor = mergeAllVisitorsAsync(pluginsSpecs.map(propOr({}, 'visitor')), { | ||
...visitorOptions, | ||
}); | ||
|
||
await Promise.allSettled(pluginsSpecs.map(invokeArgs(['pre'], []))); | ||
const newElement = await visitAsync(element, mergedPluginsVisitor, visitorOptions as any); | ||
await Promise.allSettled(pluginsSpecs.map(invokeArgs(['post'], []))); | ||
return newElement; | ||
}; | ||
|
||
dispatchPluginsSync[Symbol.for('nodejs.util.promisify.custom')] = dispatchPluginsAsync; |
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
packages/apidom-core/test/refractor/plugins/dispatcher/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import sinon from 'sinon'; | ||
import { assert } from 'chai'; | ||
|
||
import { | ||
NumberElement, | ||
ObjectElement, | ||
toValue, | ||
dispatchRefractorPlugins as dispatchPluginsSync, | ||
} from '../../../../src'; | ||
|
||
const dispatchPluginsAsync = dispatchPluginsSync[Symbol.for('nodejs.util.promisify.custom')]; | ||
|
||
describe('refrator', function () { | ||
context('plugins', function () { | ||
context('dispatcher', function () { | ||
context('dispatchPluginsSync', function () { | ||
specify('should dispatch plugins synchronously', function () { | ||
const preSpy = sinon.spy(); | ||
const postSpy = sinon.spy(); | ||
const NumberElementSpy = sinon.spy(() => new NumberElement(2)); | ||
const plugin = () => ({ | ||
pre: preSpy, | ||
visitor: { | ||
NumberElement: NumberElementSpy, | ||
}, | ||
post: postSpy, | ||
}); | ||
const objectElement = new ObjectElement({ a: 1 }); | ||
const result = dispatchPluginsSync(objectElement, [plugin]); | ||
|
||
assert.isTrue(preSpy.calledBefore(NumberElementSpy)); | ||
assert.isTrue(postSpy.calledAfter(NumberElementSpy)); | ||
assert.deepEqual(toValue(result), { a: 2 }); | ||
}); | ||
}); | ||
|
||
context('dispatchPluginsASync', function () { | ||
specify('should dispatch plugins asynchronously', async function () { | ||
const preSpy = sinon.spy(); | ||
const postSpy = sinon.spy(); | ||
const NumberElementSpy = sinon.spy(async () => new NumberElement(2)); | ||
const plugin = () => ({ | ||
pre: preSpy, | ||
visitor: { | ||
NumberElement: NumberElementSpy, | ||
}, | ||
post: postSpy, | ||
}); | ||
const objectElement = new ObjectElement({ a: 1 }); | ||
const result = await dispatchPluginsAsync(objectElement, [plugin]); | ||
|
||
assert.isTrue(preSpy.calledBefore(NumberElementSpy)); | ||
assert.isTrue(postSpy.calledAfter(NumberElementSpy)); | ||
assert.deepEqual(toValue(result), { a: 2 }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |