This stamp (aka behavior) will check if there are any conflicts on every compose call. Throws an Error in case of a forbidden collision or ambiguous setup.
The defer collects same named methods and wraps them into a single method.
import Collision from'@stamp/collision';import {Border, Button, Graph} from'./drawable/primitives';constUiComponent=Collision.collisionSetup({defer: ['draw']}).compose(Border, Button, Graph);constcomponent=UiComponent();constresults=component.draw(); // will draw() all three primitivesconsole.log(results.length); // prints "3" to the console.
The deferred method returns the array of the values your methods returned. In the example above the results array will have the values the .draw() methods returned.
API
Static methods
collisionSetup
Forbid or Defer an exclusive method stamp.collisionSetup({forbid: ['methodName1'], defer: ['methodName2']}) -> Stamp
collisionProtectAnyMethod
Forbid any collisions, excluding those allowed stamp.collisionProtectAnyMethod({allow: ['methoName']}) -> Stamp
collisionSettingsReset
Remove any Collision settings from the stamp stamp.collisionSettingsReset() -> Stamp
Example
See the comments in the code:
import compose from'@stamp/compose';import Collision from'@stamp/collision';import Privatize from'@stamp/privatize';// General purpose behavior to defer "draw()" method collisionsconstDeferDraw=Collision.collisionSetup({defer: ['draw']});constBorder=compose(DeferDraw, { methods: { draw:jest.fn() // Spy function }});constButton=compose(DeferDraw, { methods: { draw:jest.fn() // Spy function }});// General purpose behavior to privatize the "draw()" methodconstPrivateDraw=Privatize.privatizeMethods('draw');// General purpose behavior to forbid the "redraw()" method collisionconstForbidRedrawCollision=Collision.collisionSetup({forbid: ['redraw']});// The aggregating componentconstModalDialog=compose(PrivateDraw) // privatize the "draw()" method.compose(Border, Button) // modal will consist of Border and Button.compose(ForbidRedrawCollision) // there can be only one "redraw()" method.compose({ methods: {// the public method which calls the deferred private methodsredraw(int) {this.draw(int); } } });// Creating an instance of the stampconstdialog=ModalDialog();// Check if the "draw()" method is actually privatizedexpect(dialog.draw).toBeUndefined();// Calling the public method, which calls the deferred private methodsdialog.redraw(42);// Check if the spy "draw()" deferred functions were actually invokedexpect(Border.compose.methods.draw).toBeCalledWith(42);expect(Button.compose.methods.draw).toBeCalledWith(42);// Make sure the ModalDialog throws every time on the "redraw()" collisionsconstHaveRedraw=compose({methods: {redraw() {}}})expect(() =>compose(ModalDialog, HaveRedraw)).toThrow();