Skip to content

Javascript events without any DOM overheat. Inspired by Wordpress hooks system and jQuery

License

Notifications You must be signed in to change notification settings

KeySeeDev/evented

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Evented.js

Javascript events without any DOM overheat Inspired by WordPress hooks system and jQuery

Roadmap

  • namespacing : es. triggering "updated" will trigger "updated.namespace"
  • refactoring : there is always space to improvement!
  • once : trigger the function or filter just once
  • context : assing a context to the run function
  • async : we can trigger events without locking current script
  • stopPropagation : stop further events from running
  • priority : set function priority just like in wordpress
  • debug : console log events and action perfomed
  • filter system : triggers and events and passes the result allowing changing value
  • failure handler : every action is run in try-catch mode so the chain wont be broken
  • trigger system : event listner core concept

So what we can do?

This library will allow you to trigger custom events and filter data in kinda WordPress fashion!

Do actions, the output will not be changed

First we need to declare listeners

Evented.on('test', function(e,a,b,c,d,e,f) {console.log(e,a,b,c,d,e,f);});

Evented.on('test', function(e,a,b,c,d,e,f) {console.log('Second',e,a,b,c,d,e,f);});

Evented.on('test', function() {console.log(arguments);});

And than we can run a trigger which will execute the code by triggering

Evented.trigger('test', [1,2,3,4,5,6]);

Do action and change the output (or FILTER)

First we need to declare listeners

Evented.add_filter('test', function(data) {data.addme += 2; return data;});

Evented.add_filter('test', function(data) {data.addme += 5; return data;});

And than we can run a trigger which will execute the code by triggering

Evented.filter('test', [{addme: 5, deleteme: true}]);

Events / filters priority

Sometimes you need to perform action before others. Every event added will have a priority set. The default value is 50. If that space is already occupied it will become priority 51 or greater until a free slot is found.

Evented.on('test', function() {}, 50);

You can overwrite any event by forcing priority like this:

Evented.on('test', function() {}, 50, true);

Debugging?

Set Evented.debug = true; to see what is happening under the hood ;)

How to stopEventPropagation?

To stop event propagation set Evented.stop = true; and no further action will be triggered. (Maybe in conflict with async functions, cos it's a shared value, for now).

Trigger in async mode (testing)

Sometimes we need to keep the current workflow stable and don't want to lock the current workflow. Se we can send the event(not filter) to background trought.setTimeout() and that function will be executed as soon as possible probably at the end of all the others. This function is still under research and just an idea...and may be removed. Evented.trigger(name, args, context, async, is_filter)

About

Javascript events without any DOM overheat. Inspired by Wordpress hooks system and jQuery

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published