A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Adding priority when you attach a listener to a specific node - priority event in HML

Cristian_Lorenzetto
2017-02-14

There case in which you have a component receiving multiple events and you can not know what is the order but you have the necesssity to execute a block of code on the top of events sequence.

you have 2 different components completely decoupled and indipendent so you cant touch the code in the other component but i d like to change just to force that a event is executed before of another is collected in a specific node.

simevidas
2017-02-19

Could you provide an example scenario?

Cristian_Lorenzetto
2017-02-20

The need appairs when you work using a high number of jQuery plugins and you use a high modularity and usability level . You can have a conflict in the action executed by a event in different plugins. You have a not controlled sequence in the events queue attached to a node so it is difficult to handle this problem . In general for every event the state of a plugin can change, so if you have a order in the events you can foresee also the state without patch a indipendent block code . If you have a priority in the event sequence you can solve better this occurrences . JQuery is just a example .

phistuck
2017-02-21

See this proposal -

Using this mechanism, you can get the event listeners, remove them and re-add them in the order you want.

Regarding your specific idea, event properties, like element.onclick cannot specify priority - I guess they will have a specific priority.

(Regarding jQuery, you can probably already mess with the order of event listeners, at least those that were added using jQuery)

Cristian_Lorenzetto
2017-02-21

No I think there is actually a simple queue/array… the listeners are called in the order of array. My proposal is if no priority exist … if assign a default priority 0. node_events.sorts(function(e1,e1){ e1.priority-e2.priority })

execute listeners.

  1. The proposal above is not working for my opinion because i can understand what is the plugin loaded before so i could dont see that there are a plugin loaded after.
  2. JQuery directives is to remove internal events managements for passing to native one.
phistuck
2017-02-21

In my opinion, a priority is too fragile. If you want to control the order, you have to see the order and sort it (using the proposed mechanism I pointed out above). Say a few pieces of code think they need to run first, so all of them set priority 0 (or whatever). The first one that called it with priority 0 will be the first, the others will not.

Cristian_Lorenzetto
2017-02-21

no if you dont use stop backpropagation in event X all plugins receive event X. But if i defined event prority 1 for plugin A in X and priority 2 for plugin B in X
plugin A knows to be thr first in the dom manipulation and B to be the last . If i define pririty +Infinity i know to be aways the last in processing. if when the event X on node N plugin A hide N and plugin B call width on node N … the result is for B

  1. 0 if the order is plugin A, plugin B
  2. 100px if the order is plugin B , plugin A

in the case 1) there is a error!

having a casual order the error can occurs, using a priorty error dont occur. This is the difference :slight_smile:

phistuck
2017-02-21

I understand the need for event ordering, I was not debating the need. My point in the last post is that I do not think a priority parameter is a better answer then the suggestion I pointed out above (getEventListeners, removeEventListener all of the listeners and addEventListener listeners in the desired order).

Cristian_Lorenzetto
2017-02-21

your solution would be ok if i know the order in which the plugins are installed. Your code works just for the last plugin installed. nor for all. The last plugin can get all the listeners set before. But the first plugin installed how can get the listeners insstalled if the second one is not yet installed? Your solution could work if you add a additional event telling when the ready function is completed. so i m sure to get listeners when all are installed. another solution elegant is not to use priority but bind “a bind”

if i attach a listener i can notify a function that another block code is attaching in the same node for the same event. If i bind the same event inside the function of the bind listener i put this new handler before in the array order.

phistuck
2017-02-21

While this is true, it can (perhaps) do it on load, but I understand your point… However, again, you have, say, five plugins on your page and all of them add event listeners to a certain element (say, <body>) and all of them need to be the first, so all of them set the priority to 0. That does not solve the problem and plugins will likely not coordinate and negotiate the priority with other plugins as a standard practice…

I do not see this as a very useful feature.

Cristian_Lorenzetto
2017-02-21

i submitted another solution … (jquery language like just for simplicity)

$node.on(‘bind:click’,function A (){ $node.on(‘click’,function B()…)

}) this is a listener not to click event but to bind on click event.

if i execute on(‘click’) inside a a function listener of bind:click … the function B is put before in the priority because the clousure in higher level (or using your api get listners removelistners) in this way i can solve the problem without using priority by developers

in this way:

  1. i know another plugin is attached in the same node
  2. i can change behaviour of listeners

Maybe this solution is more elegant. It is handled internally