forked from kanium/starcheese
Event Bus
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
class_name DefaultEventBus
|
||||
extends EventBus
|
||||
|
||||
var subscribers: Dictionary = {}
|
||||
|
||||
func _init():
|
||||
subscribers = {}
|
||||
|
||||
func subscribers_of(event_scope: EventScope, event_type: String) -> Array[Subscription]:
|
||||
var key = [event_scope, event_type]
|
||||
return subscribers.get(key, [])
|
||||
|
||||
func subscribe_to(event_scope: EventScope, event_type: String, fn: Callable) -> Subscription:
|
||||
var key = [event_scope, event_type]
|
||||
var subscriber_set: Array[EventBusSubscription] = subscribers.get(key, [] as Array[EventBusSubscription])
|
||||
var subscription = EventBusSubscription.new(key, fn, self)
|
||||
subscriber_set.append(subscription)
|
||||
subscribers[key] = subscriber_set
|
||||
return subscription
|
||||
|
||||
func publish(event_scope: EventScope, event: Event) -> void:
|
||||
var key = [event_scope, event.get_type()]
|
||||
var subscriber_set: Array[EventBusSubscription] = subscribers.get(key, [] as Array[EventBusSubscription]).duplicate(true)
|
||||
for subscriber in subscriber_set:
|
||||
var result = subscriber.callback.call(event)
|
||||
if result == EventResult.ResultType.DISPOSE_SUBSCRIPTION:
|
||||
subscriber.dispose()
|
||||
|
||||
func cancel_scope(event_scope: EventScope) -> void:
|
||||
var keys_to_cancel: Array = []
|
||||
for key in subscribers.keys():
|
||||
if key[0] == event_scope:
|
||||
keys_to_cancel.append(key)
|
||||
for key in keys_to_cancel:
|
||||
var subscriber_set: Array[EventBusSubscription] = subscribers[key]
|
||||
for subscriber in subscriber_set:
|
||||
subscriber.dispose()
|
||||
|
||||
func dispose() -> void:
|
||||
for subscriber_set in subscribers.values():
|
||||
for subscriber in subscriber_set:
|
||||
subscriber.dispose()
|
||||
subscribers.clear()
|
||||
@@ -0,0 +1,2 @@
|
||||
class_name DefaultScope
|
||||
extends EventScope
|
||||
@@ -0,0 +1,12 @@
|
||||
class_name Event
|
||||
extends RefCounted
|
||||
|
||||
var type: String
|
||||
var source: Variant
|
||||
|
||||
func _init(_source: Variant = null):
|
||||
source = _source
|
||||
type = self.get_class()
|
||||
|
||||
func get_type() -> String:
|
||||
return type
|
||||
@@ -0,0 +1,17 @@
|
||||
class_name EventBus
|
||||
extends Node
|
||||
|
||||
func subscribers_of(_event_scope: EventScope, _event_type: String) -> Array[Subscription]:
|
||||
return Array()
|
||||
|
||||
func subscribe_to(_event_scope: EventScope, _event_type: String, _fn: Callable) -> Subscription:
|
||||
return Subscription.new()
|
||||
|
||||
func publish(_event_scope: EventScope, _event: Event) -> void:
|
||||
pass
|
||||
|
||||
func cancel_scope(_event_scope: EventScope) -> void:
|
||||
pass
|
||||
|
||||
func dispose() -> void:
|
||||
pass
|
||||
@@ -0,0 +1,19 @@
|
||||
class_name EventBusSubscription
|
||||
extends Subscription
|
||||
|
||||
var key: Array
|
||||
var callback: Callable
|
||||
var bus: DefaultEventBus
|
||||
|
||||
func _init(_key: Array, _callback: Callable, _bus: DefaultEventBus):
|
||||
key = _key
|
||||
callback = _callback
|
||||
bus = _bus
|
||||
|
||||
func dispose() -> void:
|
||||
var subscriber_set: Array[EventBusSubscription] = bus.subscribers.get(key, [])
|
||||
subscriber_set.erase(self)
|
||||
if subscriber_set.is_empty():
|
||||
bus.subscribers.erase(key)
|
||||
else:
|
||||
bus.subscribers[key] = subscriber_set
|
||||
@@ -0,0 +1,7 @@
|
||||
class_name EventResult
|
||||
extends RefCounted
|
||||
|
||||
enum ResultType {
|
||||
KEEP_SUBSCRIPTION,
|
||||
DISPOSE_SUBSCRIPTION
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
class_name EventScope
|
||||
extends Node
|
||||
@@ -0,0 +1,5 @@
|
||||
class_name Subscription
|
||||
extends RefCounted
|
||||
|
||||
func dispose() -> void:
|
||||
pass
|
||||
Reference in New Issue
Block a user