-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7069 from lukasolson/plugin/preInit
Plugins: Add preInit capability
- Loading branch information
Showing
4 changed files
with
121 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {values} from 'lodash'; | ||
import expect from 'expect.js'; | ||
import sinon from 'auto-release-sinon'; | ||
import pluginInit from '../plugin_init'; | ||
|
||
describe('Plugin init', () => { | ||
const getPluginCollection = (plugins) => ({ | ||
byId: plugins, | ||
toArray: () => values(plugins) | ||
}); | ||
|
||
it('should call preInit before init', async () => { | ||
const plugins = { | ||
foo: { | ||
id: 'foo', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: [] | ||
}, | ||
bar: { | ||
id: 'bar', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: [] | ||
}, | ||
baz: { | ||
id: 'baz', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: [] | ||
} | ||
}; | ||
|
||
await pluginInit(getPluginCollection(plugins)); | ||
|
||
expect(plugins.foo.preInit.calledBefore(plugins.foo.init)).to.be.ok(); | ||
expect(plugins.foo.preInit.calledBefore(plugins.bar.init)).to.be.ok(); | ||
expect(plugins.foo.preInit.calledBefore(plugins.baz.init)).to.be.ok(); | ||
|
||
expect(plugins.bar.preInit.calledBefore(plugins.foo.init)).to.be.ok(); | ||
expect(plugins.bar.preInit.calledBefore(plugins.bar.init)).to.be.ok(); | ||
expect(plugins.bar.preInit.calledBefore(plugins.baz.init)).to.be.ok(); | ||
|
||
expect(plugins.baz.preInit.calledBefore(plugins.foo.init)).to.be.ok(); | ||
expect(plugins.baz.preInit.calledBefore(plugins.bar.init)).to.be.ok(); | ||
expect(plugins.baz.preInit.calledBefore(plugins.baz.init)).to.be.ok(); | ||
}); | ||
|
||
it('should call preInits in correct order based on requirements', async () => { | ||
const plugins = { | ||
foo: { | ||
id: 'foo', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: ['bar', 'baz'] | ||
}, | ||
bar: { | ||
id: 'bar', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: [] | ||
}, | ||
baz: { | ||
id: 'baz', | ||
init: sinon.spy(), | ||
preInit: sinon.spy(), | ||
requiredIds: ['bar'] | ||
} | ||
}; | ||
|
||
await pluginInit(getPluginCollection(plugins)); | ||
|
||
expect(plugins.bar.preInit.firstCall.calledBefore(plugins.foo.init.firstCall)).to.be.ok(); | ||
expect(plugins.bar.preInit.firstCall.calledBefore(plugins.baz.init.firstCall)).to.be.ok(); | ||
expect(plugins.baz.preInit.firstCall.calledBefore(plugins.foo.init.firstCall)).to.be.ok(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { includes } from 'lodash'; | ||
|
||
export default async (plugins) => { | ||
let path = []; | ||
|
||
const initialize = async function (id, fn) { | ||
let plugin = plugins.byId[id]; | ||
|
||
if (includes(path, id)) { | ||
throw new Error(`circular dependencies found: "${path.concat(id).join(' -> ')}"`); | ||
} | ||
|
||
path.push(id); | ||
|
||
for (let reqId of plugin.requiredIds) { | ||
if (!plugins.byId[reqId]) { | ||
throw new Error(`Unmet requirement "${reqId}" for plugin "${id}"`); | ||
} | ||
|
||
await initialize(reqId, fn); | ||
} | ||
|
||
await plugin[fn](); | ||
path.pop(); | ||
}; | ||
|
||
const collection = plugins.toArray(); | ||
for (let {id} of collection) { | ||
await initialize(id, 'preInit'); | ||
} | ||
|
||
for (let {id} of collection) { | ||
await initialize(id, 'init'); | ||
} | ||
}; |