stampit API
Search…
@stamp/collision
Controls collision behavior: forbid or defer
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.

Usage

1
import Collision from '@stamp/collision';
2
3
const ForbidRedrawCollision = Collision.collisionSetup({forbid: ['redraw']});
Copied!
Or if you don't want to import the stamp you can import only the method:
1
import {collisionSetup} from '@stamp/collision';
2
const ForbidRedrawCollision = collisionSetup({forbid: ['redraw']});
Copied!
The defer collects same named methods and wraps them into a single method.
1
import Collision from '@stamp/collision';
2
3
import {Border, Button, Graph} from './drawable/primitives';
4
5
const UiComponent = Collision.collisionSetup({defer: ['draw']})
6
.compose(Border, Button, Graph);
7
8
const component = UiComponent();
9
const results = component.draw(); // will draw() all three primitives
10
console.log(results.length); // prints "3" to the console.
Copied!
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:
1
import compose from '@stamp/compose';
2
import Collision from '@stamp/collision';
3
import Privatize from '@stamp/privatize';
4
5
// General purpose behavior to defer "draw()" method collisions
6
const DeferDraw = Collision.collisionSetup({defer: ['draw']});
7
8
const Border = compose(DeferDraw, {
9
methods: {
10
draw: jest.fn() // Spy function
11
}
12
});
13
const Button = compose(DeferDraw, {
14
methods: {
15
draw: jest.fn() // Spy function
16
}
17
});
18
19
// General purpose behavior to privatize the "draw()" method
20
const PrivateDraw = Privatize.privatizeMethods('draw');
21
22
// General purpose behavior to forbid the "redraw()" method collision
23
const ForbidRedrawCollision = Collision.collisionSetup({forbid: ['redraw']});
24
25
// The aggregating component
26
const ModalDialog = compose(PrivateDraw) // privatize the "draw()" method
27
.compose(Border, Button) // modal will consist of Border and Button
28
.compose(ForbidRedrawCollision) // there can be only one "redraw()" method
29
.compose({
30
methods: {
31
// the public method which calls the deferred private methods
32
redraw(int) {
33
this.draw(int);
34
}
35
}
36
});
37
38
// Creating an instance of the stamp
39
const dialog = ModalDialog();
40
41
// Check if the "draw()" method is actually privatized
42
expect(dialog.draw).toBeUndefined();
43
44
// Calling the public method, which calls the deferred private methods
45
dialog.redraw(42);
46
47
// Check if the spy "draw()" deferred functions were actually invoked
48
expect(Border.compose.methods.draw).toBeCalledWith(42);
49
expect(Button.compose.methods.draw).toBeCalledWith(42);
50
51
// Make sure the ModalDialog throws every time on the "redraw()" collisions
52
const HaveRedraw = compose({methods: {redraw() {}}})
53
expect(() => compose(ModalDialog, HaveRedraw)).toThrow();
Copied!
Last modified 3yr ago