Skip to content

Instantly share code, notes, and snippets.

@Masquerade-Circus
Forked from 140bytes/LICENSE.txt
Last active June 21, 2019 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Masquerade-Circus/6b2df4b7893e2a7d92ee to your computer and use it in GitHub Desktop.
Save Masquerade-Circus/6b2df4b7893e2a7d92ee to your computer and use it in GitHub Desktop.

Beam

Tiny event emmiter in 128 bytes

Usage

Assign Beam to a custom var.

var app = {event:{},Beam:function(a,b){var c,d=this,e=d.event[a]=d.event[a]||[];for(c in b&&b.call?+e.push(b):e)e[c].apply(d,arguments)}};

Create an event

  // Create a hello event
  app.Beam('hello', function (event, name, lastname) {
      console.log('Event fired "' + event + '": Hello ' + name + ' ' + lastname);
  });

Trigger an event

  // Trigger the hello event without arguments
  app.Beam('hello');
  
  // Trigger the hello event with arguments
  app.Beam('hello', 'Jack', 'Sparrow');

Delete an event

  // Delete the hello event
  delete app.event.hello;
  
  // This will dont run because hello event does not exists
  app.Beam('hello', 'Jack', 'Sparrow');

Create an event that will run just one time

  app.Beam('hello once', function(event, name, lastname){
      console.log('Event fired "' + event + '": Hello ' + name + ' ' + lastname);
      delete this.event[event];
  });

  // The first call of the hello once event
  app.Beam('hello once', 'Jack', 'Sparrow');
  
  // The second call of the hello once event will not fire because does not exists
  app.Beam('hello once', 'Jack', 'Sparrow');
{
//The event container
event: {},
/*
* The main function
* @param string
* @param function or arguments
*/
Beam: function(a, b) {
var c,
d = this,
e = d.event[a] = d.event[a] || []; // Assign or create the current event
for (c in
// If b is a function add it to the current event stack
b && b.call ?
// This will result in NaN if b is a function and for will do nothing, suggested by atk
+e.push(b) :
// else return b to the for sentence
e
)
// If b isn't a function call all the functions in the current event
e[c].apply(d, arguments)
}
};
{event:{},Beam:function(a,b){var c,d=this,e=d.event[a]=d.event[a]||[];for(c in b&&b.call?+e.push(b):e)e[c].apply(d,arguments)}};
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Beam",
"description": "Tiny event emmiter in 128 bytes",
"keywords": [
"beam",
"emmiter",
"event",
"signal"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var app = {event:{},Beam:function(a,b){var c,d=this,e=d.event[a]=d.event[a]||[];for(c in b&&b.call?+e.push(b):e)e[c].apply(d,arguments)}};
// Create a hello event
app.Beam('hello', function (event, name, lastname) {
console.log('Event fired "' + event + '": Hello ' + name + ' ' + lastname);
});
// Trigger the hello event without arguments
app.Beam('hello');
// Trigger the hello event with arguments
app.Beam('hello', 'Jack', 'Sparrow');
// Delete the hello event
delete app.event.hello;
// This will dont run because hello event does not exists
app.Beam('hello', 'Jack', 'Sparrow');
// Create an event that will run just one time
app.Beam('hello once', function(event, name, lastname){
console.log('Event fired "' + event + '": Hello ' + name + ' ' + lastname);
delete this.event[event];
});
// The first call of the hello once event
app.Beam('hello once', 'Jack', 'Sparrow');
// The second call of the hello once event will not fire because does not exists
app.Beam('hello once', 'Jack', 'Sparrow');
</script>
@atk
Copy link

atk commented Apr 17, 2015

With a small trick, one can shave a few more bytes off that. for(c in NaN){...} does nothing and coercing a function like +e.push(function) will result in NaN, thus removing the need for if/else:

{event:{},Beam:function(a,b){var c,d=this,e=d.event[a]=d.event[a]||[];for(c in(b&&b.call?+e.push(b):e))e[c].apply(d,arguments)}};

@Masquerade-Circus
Copy link
Author

Thanks for the trick, i have updated the gist. Every day you learn something new.

@atk
Copy link

atk commented Apr 20, 2015

it may even work without the brackets (saving another byte), but I wasn't 100% sure and hadn't got the time to test it thouroughly.

@Masquerade-Circus
Copy link
Author

You are right, it works without the brackets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment