-
Notifications
You must be signed in to change notification settings - Fork 82
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
base: master
Are you sure you want to change the base?
Conversation
support multi event types for listener
Event object add getArg function.
That's an awesome PR. Let me spent some time next few days to review it. |
Hello @krasimir , I tried to increase the event redirection function for EventBus, the purpose is to enable the event to meet the redirection conditions as the target event. Target event types support variability and latency, and can be determined when redirection occurs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bonashen First of all I want to say that this is massive work. You basically rewrote the library. However, I'm not sure that this is the implementation that I want. I'm happy with the API and and how the EventBus behaves. I'll suggest, let's first start with bunch of test cases that cover all the functionalities.
(To be honest I didn't put lots of efforts to this repo. It was created ages ago and it has 0 test coverage)
@@ -1,100 +1,413 @@ | |||
/** | |||
* Created by bona on 2016/12/12. | |||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to include that in a CHANGE.log file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry. The first time I found the EventBus library, I copied the code directly and pasted it into the project file created by IDEA without noticing the copyright information. Later, it was necessary to give feedback on the changes I made, the code will be submitted directly to the git.
function iterator(array, callback) { | ||
array = array || []; | ||
var iterator = { | ||
stop: function (result) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please add isStopped: false
and result: null
here as initial values.
|
||
function slice(array, start, end) { | ||
if (start > array.length)return []; | ||
return [].slice.call(array, start == undefined ? 0 : start, end == undefined ? array.length : end) || []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use tripple ===
here.
* @returns {EventBusClass} | ||
*/ | ||
on: function (type, callback, scope) { | ||
type = processMultiTypes.call(this, type, this.on, slice(arguments)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea about extending the API and make possible handling multiple types. However, processMultiTypes
is really really confusing. I mean it took me some time to understand that we call processMultiTypes
in every case and in those cases where we have multiple types we return an instance of EventBusClass
and because of that this function on
stops here (if (type == this)return this;
looks weird too). processMultiTypes
defines two logical branches that are too different.
I still want that feature though. I'll suggest to kill processMultiTypes
and go with something like:
on: function (types, callback, scope) {
if (typeof types === "string") {
types = types.trim().split(EventBusClass.DEFAULT.EVENT_TYPE_SPLIT_EXP)
}
types.forEach(function(type) {
// move all the other code here
});
}
We are making a few assumptions and we don't bother covering every single case:
- The function
on
accepts a string with comma separated types or an array of types - We are not re-calling the
on
function we just assume that we are processing many types inside
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, perhaps this increases the readability of the code. ProcessMultiTypes method is to increase the unity of the EventBusClass.on and EventBusClass.off method to deal with type parameters, support for arrays or by comma and space separated string. If the return value equals EventBusClass object description has been processed, if no processing returns a single type processed by subsequent code.
* @param scope | ||
* @returns {EventBusClass} | ||
*/ | ||
once: function (type, callback, scope) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a nice one!
if (!isRegExpType) { | ||
if (typeof this.listeners[eventType] != "undefined") { | ||
if (!(allCallback && allScope)) | ||
iterator(this.listeners[eventType], function (i, listener) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that the iterator
idea is kinda similar to forEach
. Do you think that we can use it instead? It's just easier to follow the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add iterator function like jQuery.each method, We can use it to traverse array elements and object attributes.
} | ||
} else { | ||
iterator(this.regexListeners, function (index, listener) { | ||
if (!(listener.eventType == eventType && isRemove(listener))) newArray.push(listener); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should really use tripple =
everywhere.
// origin:'click', | ||
// endpoint:'onClick' | ||
// }) | ||
if (arguments.length == 1 && typeof origin == "object") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to avoid such logic in my libraries. It's not like I'm not suing it though. But in the end it always makes the code complex. Treating the arguments of a function in different way based on their amount is kinda misleading for the developers that contribute to the project. Sounds like the function is doing more then one thing. Can you think of something like
if (arguments.length === 1 && typeof origin == "object")) { this._redirectObject(...) }
So we have single-job functions with clear signature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, support a unified style of writing, in order to avoid confusion when used.
* @param processor processor be called before event redirection | ||
* @return {EventBusClass} | ||
*/ | ||
redirect: function (origin, endpoint, condition, processor) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that I understand the purpose of redirect
. Can you please come with your use case.
Here we have again a method that does several things. I wish to have simpler methods that do only one thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of implementing the redirect method is to increase the renaming of the event so that the specified event is turned into other events, or a class event turns into a single event.
Specific use cases have been included in the example/node/cs-test.js.
}); | ||
|
||
if (result)return true; | ||
} else if (isRegExpType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should think about handling the regexp type of events in the same fashion as the other events. Otherwise we end up having those if (regex) { ... } else { ... }
. And it seems that we'll have such statements all over the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea for me to try.
support quick and intuitive configuration of event flow
# Conflicts: # example/node/flow-test.js # lib/eventbus.min.js # src/EventBus.js
support regex for event type
support multi event types for listener