Last active
May 1, 2025 17:13
-
-
Save john9francis/e71f6759668d4fb3ad85c5b0c590da09 to your computer and use it in GitHub Desktop.
Anywhere in your code you can call one of these functions or subscribe to one of these functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Basic implementation of an event bus in dart. | |
any function anywhere in code can subscribe themselves to specific methods in the event bus. such as: | |
EventBus.subscribe(eventBusFn, callbackFn) | |
to call an event bus function, and call any subscribing functions as well, run: | |
EventBus.call(eventBusFn, args) | |
*/ | |
class EventBus { | |
static Map<Function, Function> subscriptions = {}; | |
static void subscribe(Function fn, Function callbackFn){ | |
subscriptions[fn] = callbackFn; | |
} | |
static void call(Function fn){ | |
fn(); | |
if (subscriptions.containsKey(fn)){ | |
subscriptions[fn]!(); | |
} | |
} | |
} | |
/* | |
Future improvements: | |
- Async function support | |
- Enforce that the function is in fact an EventBus function and not some arbitrary function | |
- Allow to subscribe and call functions with arguments | |
- Allow multiple functions to subscribe to the same EventBusFn | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment