@dingdaoos/msg-transfer
v0.0.5
Published
- [smartBond](#smartbond) - [smartAction](#smartaction)
Downloads
59
Keywords
Readme
Atoms Bond
version: 1.0.x
为进一步简化调用方式,1.1.x 以上版本新增两个api
Install
npm install @dingdao/atoms-bond
How to use
import { createBond, receiveBond, createAction, receiveAction, preach, createCombo, receiveCombo, smartBond, smartAction } from '@dingdao/atoms-bond'
Action
Create action && Send action data
// 调起方
const action = await createAction(options) // options.category is required
action.send('test')
// 被调起方
preach.response('test')
Receive action data
// 被调起方
receiveAction( data => {
console.log(data) // test
})
// 调起方
receiveAction( data => {
console.log(data) // test
}, { isResult: true })
Bond
Create bond && Send bond data
const bond = await createBond(options) // options is optional
bond.send('test')
Receive bond data
// 被调起方接收数据
receiveBond( data => {
console.log(data) // test
})
Combo
Create combo && Send combo data
// 同时创建 bond 和action; 注意,需要传入options.category 才能成功创建action
const combo = await createCombo(options)
combo.send('test')
Receive combo data
// 被调起方接收数据
receiveCombo( data => {
console.log(data, e, eventName)
})
参数说明:
data: 'test'
e: {
custom: true,
origin: 'com.dingdaoos.examples.atom1_AND_com.dingdaoos.examples.atom3',
data: '123',
type: 'RESPONSE' // 'BOND' | 'action' | 'RESPONSE'
}
eventName: 'intentresult' | 'intentreceive' | 'bondIntent'
smartBond
根据配置的 input/output 自动创建所有 bond。同时使用者不再需要关心 schema 匹配问题,通过以 schema 为键值的 bonds 对象来对不同的 receive 进行分别处理
// @ts-ignore ignore mjs
import { bondOptions } from './.dsdl.mjs'
/* 读取 .dsdl.mjs 中的 bondOptions
bondOptions = {
input: ['CONTACT_LIST_TYPE', 'REQUEST_CATEGORY'],
output: ['CONTACT_ID_INFO', 'RESPONSE_CATEGORY', 'CONTACT_INFO']
}
*/
// 在原子服务初始化时调用,自动根据 input/output 创建所有bond
const bonds = smartBond(bondOptions)
// 发送 bond 数据
bonds['CONTACT_LIST_TYPE'].then((e: any) => {
e.send('test')
})
// 接收 bond 数据, 调用此 API 只有 schema 匹配时才会进行回调处理
bonds['CONTACT_LIST_TYPE'].receive((data: any, e: any, eventName: string) => {
// data
// e
// eventName: bondintent
})
smartAction
调起方根据 actionId 定向创建通信,被调起方根据 refetUrl 定向收发数据,只有匹配的 action 才能互相收发数据
调起方
// 通过 actionId 定向创建一个action
const action1 = smartAction['com.dingdaoos.address-book.userinfo.actions.1']
// 定向发送数据,只会发送给 actionId 相匹配的 action 页面
action1.then((e: any) => {
e.send('test')
})
// 接收 action response,只会接收此 actionId 回传的 response 数据
action1.receive((data: any, e: any, eventName: string) => {
// data
// e
// eventName: intentresult
})
被调起方
// 通过来源 referUrl 创建一个监听
const action2 = smartAction[`${process.env.VUE_APP_APPROTO_DOMAIN}/organization`]
// 只会接收到 referUrl 匹配的 action 传来的数据
action2.receive((data: any, e: any, eventName: string) => {
// data
// e
// eventName: intentreceive
})
// response 信息只会被 referUrl 相匹配的调起方接收到
action2.response('test')
simulator 模拟器
import { simulator } from '@dingdao/atoms-bond'
customElements.define('ding-simulator', simulator)
<ding-simulator class="approto-main" id="simulator" :config="JSON.stringify(minifestJson)"></ding-simulator>