Skip to content

Commit

Permalink
add guards to broadcasters
Browse files Browse the repository at this point in the history
  • Loading branch information
P.J.Shand committed Jan 23, 2020
1 parent 8e8c695 commit dee3ddf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/comms/Comms.hx
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,26 @@ class Comms {
EnterFrame.add(tick);
}

public function addBroadcast<K, T>(id:String, ?map:MapNotifier<K, T>, ?notifier:Notifier<T>):Void {
public function addBroadcast<K, T>(id:String, ?map:MapNotifier<K, T>, ?notifier:Notifier<T>):IBroadcaster {
var broadcaster:IBroadcaster = null;
if (notifier != null)
broadcasters.set(id, new NotifierBroadcaster<T>(this, notifier, id));
broadcaster = new NotifierBroadcaster<T>(this, notifier, id);
if (map != null)
broadcasters.set(id, new MapBroadcaster<K, T>(this, map, id));
broadcaster = new MapBroadcaster<K, T>(this, map, id);
if (broadcaster != null)
broadcasters.set(id, broadcaster);
return broadcaster;
}

public function addSubscriber<K, T>(id:String, ?map:MapNotifier<K, T>, ?notifier:Notifier<T>):Void {
public function addSubscriber<K, T>(id:String, ?map:MapNotifier<K, T>, ?notifier:Notifier<T>):ISubscriber {
var subscriber:ISubscriber = null;
if (notifier != null)
subscribers.set(id, new NotifierSubscriber<T>(this, notifier, id));
subscriber = new NotifierSubscriber<T>(this, notifier, id);
if (map != null)
subscribers.set(id, new MapSubscriber<K, T>(this, map, id));
subscriber = new MapSubscriber<K, T>(this, map, id);
if (subscriber != null)
subscribers.set(id, subscriber);
return subscriber;
}

// public function removeBroadcast(notifier:Notifier<Dynamic>, id:String):Void {}
Expand Down
1 change: 1 addition & 0 deletions src/comms/broadcaster/IBroadcaster.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface IBroadcaster {
var id:String;
var value(get, null):Dynamic;
function setCurrentValue():Void;
function addGuard(guard:(id:Dynamic, value:Dynamic) -> Bool):Void;
}
13 changes: 12 additions & 1 deletion src/comms/broadcaster/MapBroadcaster.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MapBroadcaster<K, T> implements IBroadcaster {
public var id:String;
public var value(get, null):Dynamic;

var guards:Array<(id:String, value:Dynamic) -> Bool> = [];

public function new(comms:Comms, map:MapNotifier<K, T>, id:String) {
this.comms = comms;
this.id = id;
Expand All @@ -33,8 +35,13 @@ class MapBroadcaster<K, T> implements IBroadcaster {
}

function send(commsKey:String, key:K, value:T) {
var payload:{key:K, value:T} = {key: key, value: value};
for (guard in guards) {
if (!guard(commsKey, untyped payload))
return;
}
// if (!comms.PAUSE_BROADCAST) {
comms.send(commsKey, {key: key, value: value}, false);
comms.send(commsKey, payload, false);
// }
// for (connection in comms.connections) {
// connection.send(commsKey, {key: key, value: value});
Expand All @@ -47,6 +54,10 @@ class MapBroadcaster<K, T> implements IBroadcaster {
}
}

public function addGuard(guard:(id:Dynamic, value:Dynamic) -> Bool):Void {
guards.push(untyped guard);
}

function get_value():Map<K, T> {
return map.value;
}
Expand Down
10 changes: 10 additions & 0 deletions src/comms/broadcaster/NotifierBroadcaster.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class NotifierBroadcaster<T> implements IBroadcaster {
public var id:String;
public var value(get, null):Dynamic;

var guards:Array<(id:String, value:T) -> Bool> = [];

public function new(comms:Comms, notifier:Notifier<T>, id:String) {
this.comms = comms;
this.id = id;
Expand All @@ -19,11 +21,19 @@ class NotifierBroadcaster<T> implements IBroadcaster {
}

public function setCurrentValue():Void {
for (guard in guards) {
if (!guard(id, notifier.value))
return;
}
// if (!comms.PAUSE_BROADCAST) {
comms.send(id, notifier.value);
// }
}

public function addGuard(guard:(id:Dynamic, value:Dynamic) -> Bool) {
guards.push(guard);
}

function get_value():Dynamic {
return notifier.value;
}
Expand Down

0 comments on commit dee3ddf

Please sign in to comment.