stampit API
Search…
Composers
Composers are callback functions executed each time a composition happens. There can be as many as you wish composers on each stamp.
Composers are a very powerful feature. If you can implement something without composers then it's a good idea to avoid them.
It is highly recommended to not create new stamps in the composers, but mutate the given stamp if necessary.
1
const InstanceOf = stampit.composers(({ stamp, composables }) => {
2
if (!stamp.compose.methods) stamp.compose.methods = {} // mutating stamp
3
stamp.compose.methods.getStamp = () => stamp // mutating stamp
4
5
Object.defineProperty(stamp, Symbol.hasInstance, { // mutating stamp
6
value(obj) {
7
return obj && typeof obj.getStamp === 'function' && obj.getStamp() === stamp
8
}
9
})
10
})
11
12
console.log(InstanceOf() instanceof InstanceOf) // true
13
console.log(InstanceOf().getStamp() === InstanceOf) // true
14
15
const Stamp = InstanceOf.props({ anything: 1 })
16
console.log(Stamp() instanceof Stamp) // true
17
console.log(Stamp().getStamp() === Stamp) // true
18
19
console.log(Stamp() instanceof InstanceOf) // false
20
console.log(InstanceOf() instanceof Stamp) // false
Copied!
The above InstanceOf stamp overrides the default instanceof behavior using ES6 will-known symbol Symbol.hasInstance. Compose it with your stamps if you need the instanceof to work as expected for you.
NOTE
You can find the stamp above as the @stamp/instanceof NPM module.

Descriptor merging algorithm

The composers are concatenated into a deduplicated array. As the result, the order of composition becomes the order of composer execution.
1
const {composers} = stampit
2
3
const Log1 = composers(() => console.log(1))
4
const Log2 = composers(() => console.log(1))
5
const Log3 = composers(() => console.log(1))
6
7
const MultiLog = stampit(Log1, Log2, Log3)
8
9
MultiLog() // Will print three times:
10
// 1
11
// 1
12
// 1
13
14
// because there are 3 composers
15
MultiLog.compose.composers.length === 3
Copied!
Stamps remove duplicate composers.
1
const {composers} = stampit
2
3
const func = () => console.log(1)
4
const Log1 = composers(func)
5
const Log2 = composers(func)
6
const Log3 = composers(func)
7
8
const MultiLog = stampit(Log1, Log2, Log3)
9
10
MultiLog() // Will print only once:
11
// 1
12
13
// because there is only one composer
14
MultiLog.compose.composers.length === 1
Copied!

Composer arguments

Every composer always receive this object: { stamp, composables }. Where:
    stamp is the stamp which was used to create this object. Useful to retrieve stamp's metadata (aka descriptor).
    composables is the array of stamps and descriptors the above stamp was composed with.

Other ways to add composers

Exactly the same stamp can be created in few ways. Here they all are.
1
function myDebugComposer ({ stamp, composables }) {
2
console.log(`Stamp ${stamp} was composed of ${composables}`)
3
}
4
5
const Logger = stampit({
6
composers: myDebugComposer
7
})
8
9
const Logger = stampit({
10
composers: [myDebugComposer]
11
})
12
13
const Logger = stampit.composers(myDebugComposer)
14
const Logger = stampit.composers([myDebugComposer])
15
16
const Logger = stampit().composers(myDebugComposer)
17
const Logger = stampit().composers([myDebugComposer])
Copied!
Last modified 3yr ago