Packhouse
Search…
Merger
Packhouse Merger

Property

Molds: 參數型別,可視為Interface或Type的功能。
Groups:Group群組,應用方法請至How To Use查看。
let merger = {
molds: 'molds',
groups: 'groups'
}

TypeScript

如果你使用TypeScript,可以使用我們提供的Declaration File。
import { Merger } from 'packhouse/types'
let merger: Merger = {}

How To Use

我們可以藉由merger方法加入Merger,並傳遞Options,但我們不推薦這麼做,請參閱正規化方法Main
Merger會以[email protected]的Sign建立Mold與Group。

Molds

Merger的Mold會被註冊進Core Mold中,各地都能引用。
const group = {
tools: {
sum: {
handler(self, v1, v2) {
self.success(v1 + v2)
}
}
}
}
const merger = {
molds: {
int(value) {
return Math.floor(value)
}
}
}
packhouse.merger('myMerger', merger)
packhouse.addGroup('math', group)
packhouse.tool('math/sum').action(1.1, 2.5, (err, result) => {
console.log(result) // 3
})

Groups

const group = {
tools: {
sum: {
handler(self, v1, v2) {
self.success(v1 + v2)
}
}
}
}
const merger = {
groups: {
math(options) {
console.log(options.PI) // 3.14159...
return {
data: group
}
}
}
}
packhouse.merger('myMerger', merger, {
PI: Math.PI
})
packhouse.tool('[email protected]/sum').action(...)

Module

Merger的另一個特點是內部引用,當Merger互相引用對方時不需要添加sign
let group1 = {
tools: {
sum: {
handler(self, v1, v2) {
self.success(v1 + v2)
}
}
}
}
let group2 = {
mergers: {
mySumGroup: 'sumGroup'
},
tools: {
double: {
install({ include }) {
include('sum').tool('mySumGroup/sum')
},
handler(self, v1) {
self.tool('sum').action(v1, v1, (e, r) => {
self.success(r)
})
}
}
}
}
let merger = {
groups: {
sumGroup() {
return {
data: group1
}
},
doubleGroup() {
return {
data: group2
}
}
}
}
packhouse.merger('myMerger', merger)
packhouse.tool('[email protected]/double').action(10, (e, r) => {
console.log(r) // 20
})
Copy link
On this page
Property
TypeScript
How To Use
Molds
Groups
Module