有没有办法只允许自定义eventEmitter中的某些事件?
例如,如果我有这样的东西,一切都按预期工作:
<Files ~ "^\.(htaccess|.htpasswd)$">
deny from all
</Files>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.example\.co\.uk)(:80)? [NC]
RewriteRule ^(.*) http://example.example.co.uk/$1 [R=301,L]
order deny,allow
<Files ~ "^\.(htaccess|.htpasswd)$">
deny from all
</Files>
AuthType Basic
AuthName "Please enter your ID and password"
AuthUserFile /var/www/vhosts/example.co.uk/subdomains/example/httpdocs/.htpasswd
AuthGroupFile /dev/null
require valid-user
order deny,allow
Allow from 192.87.22.18
# satisfy any
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}index.php [L]
我在第一个事件上每秒都会收到一次回调,第二个事件每2秒收到一次回调。因为没有发生任何事情,因为我没有发出这个事件。有没有一种方法,这个类告诉我这个事件没有定义,我不能添加一个监听器,或者只能将监听器添加到事件1和2?
答案 0 :(得分:0)
没有内置功能,但您可以创建某种包装来执行此操作。
const EventEmitter = require('events');
class myEvent extends EventEmitter {
constructor() {
super();
this._events = new Set();
this._events.add('1stEvent');
this._events.add('2ndEvent');
var self = this;
setInterval(function() {
self.emit('1stEvent');
}, 1000)
setInterval(function() {
self.emit('2ndEvent');
}, 2000)
}
on(event, callback) {
if(!this._events.has(event)) {
console.log('Event not registered');
return;
}
super.on(event, callback);
}
}