
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@miapp/emitter
Advanced tools
事件触发器
import Emitter from '@miapp/emitter';
const emitter = new Emitter();
// 绑定事件
emitter.on('select', callback); // 字符串
emitter.on(['select', 'confirm'], callback); // 数组
emitter.on({ // 对象
'select': selectCallback,
'confirm': confirmCallback
});
// 单次绑定,API 同 on
emitter.once('select', callback);
// 触发事件
emitter.emit('select', { value });
// 解除绑定
emitter.off('select', callback); // 解除单个
emitter.off('select'); // 解除整个类型
emitter.off(); // 解除所有
// 销毁
emitter.destroy();
推荐用 mixinEmitter
,见下文
import Emitter from "@miapp/emitter";
// component.js
Component({
didMount() {
this.emitter = new Emitter(this);
this.emitter.on('select', (e) => {
console.log(e.src);
});
},
didUnmout() {
this.emitter.destroy();
},
methods: {
handleTap(e) {
this.emit('select', {
src: 'component'
});
}
}
});
// page.js
Page({
onLoad() {
this.emitter = new Emitter(this);
},
onUnload() {
this.emitter.destroy();
}
});
// app.js
App({
onLaunch() {
this.emitter = new Emitter(this);
}
});
温馨提示:
this.emitter
,内部会有一些相关处理逻辑this
哦destroy
方法mixEmitter
为了解决以上几个问题,更方便的使用方式是,直接扩充应用/页面/组件的方法
import { mixinEmitter } from "@miapp/emitter";
Component(mixinEmitter({
didMount() {
this.on('select', () => {});
},
methods: {
handleTap(e) {
this.emit('select', {
src: 'component'
});
}
}
}));
// page.js
Page(mixinEmitter({
onLoad() {
this.on('select', () => {});
}
}));
// app.js
App(mixinEmitter({
onLaunch() {
this.on('select', () => {});
}
}));
冒泡至页面
// component.js
// component => page
this.emit('selected|page', {
src: 'component'
});
// page.js
this.on('selected', (e) => {
console.log(e.src); // component
});
同时冒泡到页面和应用
// component.js
// component => page => app
this.emit('selected|app', {
src: 'component'
});
// page.js
this.on('selected', (e) => {
console.log(e.src); // component
});
// app.js
this.on('selected', (e) => {
console.log(e.src); // component
});
如果组件想捕获页面或者全局应用发出的事件
// app.js
this.emit('selected', {
src: 'app'
});
// page.js
this.emit('selected', {
src: 'page'
});
// component.js
// 监听 page 的 selected 事件
this.on('selected|page', (e) => {
console.log(e.src); // page
});
// 监听 app 的 selected 事件
this.on('selected|app', (e) => {
console.log(e.src); // app
});
利用页面事件作为中转
// a => page => b
// a.js
this.emit('a:selected|page', {
src: 'a'
});
// b.js
this.on('a:selected|page', (a) => {
console.log(e.src); // a
});
prefix:event|broadcast
event
: 事件名称prefix
: 自定义组件前缀,用于区分来源,默认为空broadcast
: 监听从页面或者应用触发的事件,或者触发事件传播至页面或应用,只有两个值,page
和 app
,默认为空miapp newbranch
: 新建分支miapp push
: 提交代码miapp prepub
: 预发(发布 beta 版本)miapp publish
: 正式发布FAQs
The npm package @miapp/emitter receives a total of 1 weekly downloads. As such, @miapp/emitter popularity was classified as not popular.
We found that @miapp/emitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 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
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.