Giter Site home page Giter Site logo

broadcast's Introduction

broadcast: JavaScript function passing done the Scratch way

broadcast is a small CoffeeScript library (around 38 lines of CoffeeScript) that adds Scratch's broadcasts to JavaScript. If you don't know what broadcasts are, or even what Scratch is, you can look here. Broadcasts are basically very simple function calls, but they don't stop the flow of the script, so you can run a broadcast that calls any number of functions and then returns with a callback function.

Usage

First, initialize a new Broadcast() class like so:

var myBroadcasts = new Broadcast();

After you initialize the Broadcast class, you can create new Broadcasts inside of it by specifying a Broadcast ID (a.k.a "123") and an array including functions it should call when referenced (a.k.a [function() { alert('ey b0ss'); }])

// Create new Broadcast
myBroadcasts.create(
	"123", // specify first parameter, the Broadcast ID which you will later use to call the Broadcast.
	[function() {
	  // the functions to be run when the Broadcast shouts
	  /* e.g. */ alert("Broadcast is running!");
	}
);

When you are ready to call a Broadcast, you should pass the Broadcast ID, a callback function, and the sync type to your Broadcast class like this:

// Call the Broadcast, and when it finishes, do something (synchronous)
myBroadcasts.shout("123", "sync", function() {
	alert("Broadcast has completed!"); // Callback
});
// Call the Broadcast, and asynchronously do the callback with the specified functions.
myBroadcasts.shout("123", "async", function() {
  alert("Broadcast is completing!");
});

When you call the Broadcast with ID "123", it will run each function in the array of functions you originally specified (a.k.a [function() { alert('ey b0ss'); }]), and then run the callback. You do not need to specify a callback, but keep in mind that Broadcast is built on callbacks, and is pretty much useless as a standalone library without using them.

You are not restricted to one Broadcast per class, and you can create as many Broadcasts as you want as long as they all have different ID's. An example of multiple Broadcasts in one class is this:

// Create new Broadcast
myBroadcasts.create(
	"123", // specify first parameter, the Broadcast ID which you will later use to call the Broadcast.
	[
	  function() {
	    console.log("a");
	  },
	  function() {
	    console.log("b");
	  },
	  function() {
	    console.log("c");
	    console.log("d");
	  }
	]
);

// Create another new Broadcast
myBroadcasts.create(
	"ABC", // specify first parameter, the Broadcast ID which you will later use to call the Broadcast.
	[function() {
	  console.log('1'); // Functions
	  console.log('2'); // To
	  console.log('3'); // Run
	}]
);

// Call "123" first, then call "ABC", and finally alert "Broadcasts have completed"
myBroadcasts.shout("123", "sync", function(){
	myBroadcasts.shout("ABC", function(){
		alert("Broadcasts have completed.");
	});
});

You can also modify Broadcasts after they have been created. Currently, you can change the Broadcast ID, and you can change the array of functions that the Broadcast will call when it shouts.

// Create new Broadcast
myBroadcasts.create(
	"123", // specify first parameter, the Broadcast ID which you will later use to call the Broadcast.
	[function() {
	  console.log('a'); // Functions
	  console.log('b'); // To
	  console.log('c'); // Run
	}]
);

// Modify the Broadcast.
myBroadcasts.modify(
	"123", // pass current Broadcast ID
	[function() {
	  console.log('9'); // Functions
	  console.log('8'); // To         // pass new functions to call
	  console.log('7'); // Run
	}],
	"XYZ" // pass new Broadcast ID
);

// Run the modified Broadcast asynchronously
myBroadcasts.shout("XYZ", "async" function() {
   alert("XYZ has finished.");
});

A newly added feature in Broadcast 1.1.0 is Broadcast::add, which allows you to add a new event handler to an existing Broadcast like so:

myBroadcasts.create("123", [function() {
  console.log("a");
}]);

myBroadcasts.add("123", 
  [
    function() {
      console.log("b");
    },
    function() {
      console.log("c");
    }
  ]
);

myBroadcasts.shout("123", "sync", function() {
  console.log("Done!");
});

// Outputs:
// a
// b
// c
// Done!

Broadcast is open-source under the MIT license, which basically means you can do whatever the hell you want with it under proper attribution. Installing Broadcast is easy, and you can either use Bower,

bower install scratch-broadcast

...or you can just include the script in your HTML document's <head>,

<script src="your_javascript_folder/broadcast.min.js"></script>

broadcast's People

Contributors

ethel-dev avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.