![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@smt-lib/observer
Advanced tools
解释: 在小程序开发中,总会遇到事件监听派发的需求,swan-observer是一个订阅发布器。
小程序种使用三方npm包方法,见npm使用说明
npm install @baidu/swan-observer
const eventsEmitter = new EventsEmitter();
实例化之后,获得eventsEmitter对象。
eventsEmitter对象方法 fireMessage
说明:派发事件
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
message | Object | 是 | 发布的内容和监听的类型 | |
options | Object | 否 | {} | 发布器配置信息,包括once |
message参数
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
type | string | 是 | 监听类型 |
options参数
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
once | boolean | 否 | 回调是否只执行一次 |
eventsEmitter对象方法 onMessage
说明:监听事件
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
type | string | Array | 是 | |
handler | Function | 否 | 回调函数 | |
options | Object | 否 | {} | 监听器配置信息,包括once和listenPreviousEvent |
options参数
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
once | boolean | 否 | 回调是否只执行一次 | |
listenPreviousEvent | boolean | 否 | 是否先发布后监听 |
eventsEmitter对象方法 delHandler
说明:删除事件监听
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
type | string | 是 | 监听类型,如果传入*则清除所有监听 | |
handler | Function | 否 | 希望删除的回调函数 |
EventsEmitter静态方法 merge
说明:融合多条事件流成为一条
参数 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
eventsEmitter | Object | 是 | eventsEmitter对象 |
// 订阅发布常规用法
const eventsEmitter = new EventsEmitter();
let a = 0;
eventsEmitter.onMessage('addA', message => {
a += message.num;
});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
console.log(a); // 5
// 监听多个type变化
const eventsEmitter = new EventsEmitter();
let a = 0;
eventsEmitter.onMessage(['addA', 'addB'], message => {
a += message.num;
});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.fireMessage({
type: 'addB',
num: 5
});
console.log(a); // 10
// 监听类型为*时,执行任何fireMessage都会调用回调
const eventsEmitter = new EventsEmitter();
let a = 0;
let b = 0;
eventsEmitter.onMessage('addA', message => {
a += message.num;
});
eventsEmitter.onMessage('*', message => {
b += message.num;
});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
console.log(b); // 5
// 只执行一次回调
const eventsEmitter = new EventsEmitter();
let a = 0;
eventsEmitter.onMessage('addA', message => {
a += message.num;
}, {once: true});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
console.log(a); // 5
// 先发布后订阅
const eventsEmitter = new EventsEmitter();
let a = 0;
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.onMessage('addA', message => {
a += message.num;
}, {
listenPreviousEvent: true
});
console.log(a); // 10
// 删除某个type下的具体回调
const eventsEmitter = new EventsEmitter();
let a = 0;
let b = 0;
const addA = message => {
a += message.num;
};
const addB = message => {
b += message.num;
};
eventsEmitter.onMessage('addA', addA);
eventsEmitter.onMessage('addA', addB);
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.delHandler('addA', addA);
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
console.log(a); // 5
console.log(b); // 10
// 删除所有回调
const eventsEmitter = new EventsEmitter();
let a = 0;
const addA = message => {
a += message.num;
};
eventsEmitter.onMessage('addA', addA);
eventsEmitter.onMessage('addB', addA);
eventsEmitter.fireMessage({
type: 'addA',
num: 5
});
eventsEmitter.delHandler('*');
eventsEmitter.fireMessage({
type: 'addB',
num: 5
});
console.log(a); // 5
// 融合多条事件流成为一条
const eventsEmitterA = new EventsEmitter();
const eventsEmitterB = new EventsEmitter();
const mergedEventsEmitter = EventsEmitter.merge(eventsEmitterA, eventsEmitterB);
let a = 0;
mergedEventsEmitter.onMessage(['addA', 'addB'], message => {
a += message.num;
});
eventsEmitterA.fireMessage({
type: 'addA',
num: 5
});
eventsEmitterB.fireMessage({
type: 'addB',
num: 5
});
console.log(a); // 10
FAQs
@smt-lib/observer是一个订阅发布器。
The npm package @smt-lib/observer receives a total of 2 weekly downloads. As such, @smt-lib/observer popularity was classified as not popular.
We found that @smt-lib/observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.