From 7403be11df184a0c47e6ae84fb5b91418cb74a8b Mon Sep 17 00:00:00 2001 From: Jason Johnston Date: Fri, 29 May 2020 15:34:20 -0600 Subject: [PATCH] feat(troika-core): add `update` convenience method to all facades For usage ala React's setState. Really just a shortcut for assigning some props and invoking the proper lifecycle responses. --- packages/troika-core/src/facade/Facade.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/troika-core/src/facade/Facade.js b/packages/troika-core/src/facade/Facade.js index 49ac9143..72415c94 100644 --- a/packages/troika-core/src/facade/Facade.js +++ b/packages/troika-core/src/facade/Facade.js @@ -65,6 +65,21 @@ export default class Facade { this.parent = parent } + /** + * Performs a manual update of this facade, invoking the afterUpdate lifecycle method and triggering a + * render. This can be called in event handlers, for example, to affect changes to this facade and its + * subtree. This process is synchronous. Never override this method as a way to react to updates, as it + * is not the only way a component is updated; instead override `afterUpdate` or use setters. + * @param {object} props - A set of properties to be copied to the facade + */ + update(props) { + if (props && typeof props === 'object') { + assign(this, props) + this.afterUpdate() + this.notifyWorld('needsRender') + } + } + /** * Called at the end of an update batch, after all individual properties have been assigned. */