Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JulianDicken/Broadcast
An Event Handling library written in gml.
https://github.com/JulianDicken/Broadcast
Last synced: 2 months ago
JSON representation
An Event Handling library written in gml.
- Host: GitHub
- URL: https://github.com/JulianDicken/Broadcast
- Owner: JulianDicken
- License: mit
- Archived: true
- Created: 2021-12-17T12:28:56.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-04T13:06:22.000Z (over 2 years ago)
- Last Synced: 2024-06-21T18:02:11.607Z (7 months ago)
- Language: Game Maker Language
- Size: 432 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gamemaker - Broadcast - Event handling library. (Utilities / Recommendations)
README
# Broadcast
An Event Handling library written in gml.---
### Broadcasts
```js
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
```js
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
```js
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:
```js
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**.
```js
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.
```js
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 :
```js
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` :
```js
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)