Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support regex for event type #9

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
node_modules
*.log
*.log

### SublimeText template
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# workspace files are user-specific
*.sublime-workspace

# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project

# sftp configuration file
sftp-config.json

# Package control specific files
Package Control.last-run
Package Control.ca-list
Package Control.ca-bundle
Package Control.system-ca-bundle
Package Control.cache/
Package Control.ca-certs/
bh_unicode_properties.cache

# Sublime-github package stores a github token in this file
# https://packagecontrol.io/packages/sublime-github
GitHub.sublime-settings
### Windows template
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.idea/


## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ var EventBus = require('eventbusjs');
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.addEventListener(type, callback, scope)
//or
EventBus.on(type,callback,scope)
//or
EventBus.bind(type,callback,scope)
//or
EventBus.once(type,callback,scope)
//or
//support regex and multi event types
EventBus.on(['click','change',/\w_click/],function(event,value) {

});
```

### `removeEventListener`
Expand All @@ -36,6 +47,10 @@ EventBus.addEventListener(type, callback, scope)
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.removeEventListener(type, callback, scope)
//or
EventBus.off(type,callback,scope)
//or
EventBus.unbind(type,callback,scope)
```

### `hasEventListener`
Expand All @@ -45,6 +60,8 @@ EventBus.removeEventListener(type, callback, scope)
// @callback - function
// @scope - the scope where the @callback is defined
EventBus.hasEventListener(type, callback, scope)
//or
EventBus.has(type,calback,scope)
```

### `dispatch`
Expand All @@ -54,6 +71,10 @@ EventBus.hasEventListener(type, callback, scope)
// @target - the caller
// @args - pass as many arguments as you want
EventBus.dispatch(type, target, args ...)
//or
EventBus.trigger(type,target,args...)
//or
EventBus.emit(type,target,args...)
```

### `getEvents`
Expand All @@ -69,9 +90,28 @@ EventBus.getEvents()
```js
function myFunction(event) {
console.log("myFunction type=" + event.type);
//can stop event
event.stop();
//can obtain listener arguments
console.log(event.args);

//can access scope by this
console.log(this);

}
EventBus.addEventListener("my_function_event", myFunction);
var scope ={

};
EventBus.addEventListener("my_function_event", myFunction,scope,1,2,3...);
//or
EventBus.on("my_function_event",myFunction,scope,1,2,3...);

// dispatch event
EventBus.dispatch("my_function_event");
//or
EventBus.trigger("my_function_event");
//or
EventBus.emit("my_function_event");
```

## Keeping the scope
Expand Down
8 changes: 4 additions & 4 deletions example/browser/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@
var t1 = new TestClass1();
var t2 = new TestClass2();

EventBus.addEventListener("custom_event", t1.doSomething, t1);
EventBus.on("custom_event", t1.doSomething, t1);
t2.ready();

// ******************************************************************************************
console.log("\n");
var winterHandler = function() {
console.log("arguments", arguments);
}
EventBus.addEventListener("let-it-snow", winterHandler, null, "and happy", "new year");
EventBus.dispatch("let-it-snow", null, "merry", "christmas");
EventBus.dispatch("let-it-snow", null, "merry", "christmas");
EventBus.on("let-it-snow", winterHandler, null, "and happy", "new year");
EventBus.emit("let-it-snow", null, "merry", "christmas");
EventBus.trigger("let-it-snow", null, "merry", "christmas");
EventBus.dispatch("let-it-snow", null, "merry", "christmas");

}
Expand Down
133 changes: 133 additions & 0 deletions example/node/cs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Created by bona on 2016/12/14.
*/
var EventBus = require('../../lib/eventbus.min');

EventBus.DEFAULT.SHOW_LOG = false;

var Order = function (id, name, consumer) {
this.id = id;
this.name = name;
this.consumer = consumer;
};

var Producer = function (order) {
this.order = order;
};

Producer.prototype = {

make: function () {
console.log("============================", "start make ", this.order.id, "==============================");
EventBus.emit("make/" + this.order.consumer, this.order);
console.log("============================", "end make ", this.order.id, "==============================");
},
package: function () {
console.log("============================", "start package ", this.order.id, "==============================");
EventBus.emit("package/" + this.order.consumer, this.order);
console.log("============================", "end package ", this.order.id, "==============================");
},
send: function () {
console.log("============================", "start send ", this.order.id, "==============================");
EventBus.emit("send/" + this.order.consumer, this.order);
console.log("============================", "end send ", this.order.id, "==============================");
}
};

var Consumer = function (name, once) {
this.name = name;
this.init(once);
};

Consumer.prototype = {
onMake: function (event, order) {
console.log(this.name, "'s order is making. order id=", order.id, " order name:" + order.name);
},
onPackage: function (event, order) {
console.log(this.name, "'s order is packaging. order id=", order.id, " order name:" + order.name);
},
onReceive: function (event, order) {
console.log(this.name, "is ready to receive order goods. order id=", order.id, " order name:" + order.name);
},

init: function (once) {
var method = "on";
if (once) method = 'once';
EventBus[method]("make/" + this.name, this.onMake, this);
EventBus[method]("package/" + this.name, this.onPackage, this);
EventBus[method]("receive/" + this.name, this.onReceive, this);
}

};

EventBus.on(/QC\/\w*/, function (event, order) {
console.log(order.id, " quality testing");
});

EventBus.on(/\w*\/\w*/, monitor, {name: "kerry"}, /make\/\w*/);

EventBus.on(/\w*\/\w*/, monitor, {name: "peter"}, /package\/\w*/);

EventBus.on("make package receive", function (event, order) {
console.log(order.id, /*" ",order.name,*/" state is ", event.type);
});

function monitor(event, order) {
var state = event.getArg(3);
if (state.test(event.type) && order.consumer == this.name) {
console.log(event.type, " is dispatched! ");
}
}

EventBus.redirect(/(\w*)\/(\w*)/, function (event) {
return /(\w*)\/(\w*)/.exec(event.type)[1];
});

EventBus.redirect(/send\/(\w*)/, function (event) {//map one to one
return "receive/" + /(\w*)\/(\w*)/.exec(event.type)[2];
});

EventBus.redirect(/send\/(\w*)/, function (event) { //map one to more
var consumer = /(\w*)\/(\w*)/.exec(event.type)[2];
return ["trans/" + consumer, "print/" + consumer, "QC/" + consumer];
});

//map more to one
EventBus.redirect({
origin: [/QC\/\w*/],
endpoint: "qc-report-collect",
processor: function (event, order) {
console.log("redirect processor for ", event.id, "==>",
"origin:", event.getOriginType(), " endpoint:", event.getEndpoint());

if (order.consumer == "bona") event.setEmitArgs([order, "passed"]);
}
});

var qcReport = {
orders: [],
passed: [],
print: function () {
console.log("qc checked order count :", this.orders.length);
console.log("qc passed order count :", this.passed.length);
}
};

EventBus.on("qc-report-collect", function (event, order, checkState) {
this.orders.push(order);
if (checkState == "passed") this.passed.push(order);
}, qcReport);

new Consumer("peter");
new Consumer("kerry");
new Consumer("bona", true);

for (var i = 0; i < 4; i++) {
var order = new Order("order-" + (i + 1), "wa-li robot", ["peter", "kerry", "bona", "bona"][i]);
var maker = new Producer(order);
maker.make();
maker.package();
maker.send();
}

qcReport.print();
2 changes: 1 addition & 1 deletion lib/eventbus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading