From c1ae4c133d3a546b86f4a2de813deeeec552d784 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 14 Dec 2017 21:01:37 -0800 Subject: [PATCH] Add `afterLoad` plugin interface --- docs/customization/plugin-api.md | 31 ++++++++++++++++++++++++++++++- src/core/system.js | 8 ++++++++ test/core/system/system.js | 27 +++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/docs/customization/plugin-api.md b/docs/customization/plugin-api.md index 245b5a14..ee08ba07 100644 --- a/docs/customization/plugin-api.md +++ b/docs/customization/plugin-api.md @@ -19,7 +19,8 @@ A plugin return value may contain any of these keys, where `myStateKey` is a nam }, components: {}, wrapComponents: {}, - fn: {} + afterLoad: (system) => {} + fn: {}, } ``` @@ -363,7 +364,35 @@ const MyWrapComponentPlugin = function(system) { } ``` +##### `afterLoad` +The `afterLoad` plugin method allows you to get a reference to the system after your plugin has been registered with the system. + +This interface is used in the core code to attach methods that are driven by bound selectors or actions directly to the system. + +```javascript +const MyMethodProvidingPlugin = function() { + return { + afterLoad(system) { + // at this point in time, your actions have been bound into the system + // so you can do things with them + system.myMethod = system.exampleActions.updateFavoriteColor + }, + statePlugins: { + example: { + actions: { + updateFavoriteColor: (str) => { + return { + type: "EXAMPLE_SET_FAV_COLOR", + payload: str + } + } + } + } + } + } +} +``` ##### fn diff --git a/src/core/system.js b/src/core/system.js index 22707ae4..87a25887 100644 --- a/src/core/system.js +++ b/src/core/system.js @@ -68,6 +68,14 @@ export default class Store { if(rebuild) { this.buildSystem() } + + if(Array.isArray(plugins)) { + plugins.forEach(plugin => { + if(plugin.afterLoad) { + plugin.afterLoad(this.getSystem()) + } + }) + } } buildSystem(buildReducer=true) { diff --git a/test/core/system/system.js b/test/core/system/system.js index 073390fa..205fc8a6 100644 --- a/test/core/system/system.js +++ b/test/core/system/system.js @@ -683,4 +683,31 @@ describe("bound system", function(){ }) }) + describe("afterLoad", function() { + it("should call an plugin's `afterLoad` method after the plugin is loaded", function() { + // Given + const system = new System({ + plugins: [ + { + afterLoad(system) { + system.wow = system.dogeSelectors.wow + }, + statePlugins: { + doge: { + selectors: { + wow: () => (system) => { + return "so selective" + } + } + } + } + } + ] + }) + + // When + var res = system.getSystem().wow() + expect(res).toEqual("so selective") + }) + }) })