Giter Site home page Giter Site logo

broadcast's Introduction

Broadcast

An Event Handling library written in gml.


Broadcasts

hello = Broadcast( function() {
  syslog("Hello, ");
});

hello.dispatch();

prints:

"Hello, "

This creates a Broadcast and then dispatches it. Broadcasts are event sources and dispatching them cascades event invocation down to Broadcast Hooks. Broadcasts can be Broadcast Hooks too.

Broadcasts take two optional arguments, a callback and their desired scope.


Subscribers

hello = Broadcast( function() {
  syslog("Hello, ");
});

world = Subscriber( function() {
  syslog("World!");
}).watch(hello);

hello.dispatch();

prints:

"Hello, "
"World!"

This creates a Subscriber. A Subscriber is a type of Broadcast Hook. A Subscriber can only watch Broadcasts and cannot cascade dispatches downwards.

Subscribers take two optional arguments, a callback and their desired scope.


Consumers

update = Broadcast( function() {
  syslog("Update");
});
Consumer( function() {
  syslog("First Frame!");
}).watch(update);

update.dispatch();
update.dispatch();

prints:

"Update"
"First Frame!"
"Update"

This creates a Consumer. A Consumer is a type of Broadcast Hook. A Consumer can only watch Broadcasts and cannot cascade dispatches downwards. A Consumer is volatile and will only be dispatched once.

Consumers take two optional arguments, a callback and their desired scope.


Combined Subscribers and Consumers can be used as following:

update = Broadcast( function() {
  syslog("Update");
});
Consumer( function() {
  syslog("First Frame!");
}).watch(update);
other_frames = Subscriber( function() {
  syslog("Other Frame!");
}).watch(update);

update.dispatch();
update.dispatch();

prints:

"Update"
"First Frame!"
"Other Frame!"
"Update"
"Other Frame!"

The order for Hook dispatch is FIFO.


Watchlists

Watchlists serve as Broadcast queues and are a type of Broadcast.

watchlist = Watchlist( function() {
  syslog("Watchlist Finished!")
});
broadcastA = Broadcast( function() {
  syslog("Dispatched broadcastA.")
});
broadcastB = Broadcast( function() {
  syslog("Dispatched broadcastB.")
});
watchlist.add( broadcastA );
watchlist.add( broadcastB );

Once all registered Broadcasts have been dispatched the watchlist will dispatch.

Watchlists take two optional arguments, a callback and their desired scope.


Radios

Radios are a type of Broadcast and serve as repeating broadcast sources.

radio = Radio( function() {
  syslog("Radio cycle!")
}, false, 1);
IHappenEverySecond = Broadcast( function() {
  syslog("Cycle call")
});

A radio will call infinitely with the set frequency. Radios are not a type of Hook.

Warning: If you are using a pre 2022.5 runtime you will need to call radio.update() in a step event somewhere.

Radios take four optional arguments, a callback, the unit of time (false: seconds, true: frames), the update frequence and their desired scope.


Safety

As previously mentioned Broadcasts can be Hooks too :

hello = Broadcast( function() {
  syslog("Hello, ");
});

world = Broadcast( function() {
  syslog("World!");
}).watch(hello);

hello.dispatch();

This introduces potential problems due to recursion but BROADCAST tries to warn the user of Recursive Subscriptions. Unfortunately these safety checks are quite expensive. To circumvent them locally you can call the private version of either watch or dispatch :

recursive_fiend = Broadcast(function() {
  syslog("I called myself!");
}, id);
recursive_fiend.__watch(recursive_fiend);
recursive_fiend.__dispatch();

prints:

"I called myself!"
"I called myself!"
"I called myself!"
"I called myself!"
"I called myself!"
...
"I called myself!"

This will eventually crash due to a Recursion Depth error. Max recursion depth is set via the BROADCAST_RECURSION_MAX_DEPTH macro (default: 0xFFFF)

broadcast's People

Contributors

juliandicken avatar

Stargazers

 avatar Kirill Zhosul avatar Prateek Saxena avatar Toto avatar Rowan Douglas avatar Roman Erfilov [SilentPhil] avatar Brent Frymire avatar  avatar Henry Kirk avatar Faulty avatar Predrag Kostic avatar Evoleo avatar Edgar Sayohm Takamura avatar

Watchers

 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.