-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to use Streams instead of Events
- Loading branch information
Nelson Silva
committed
Dec 11, 2013
1 parent
5aa5b92
commit 8cba48d
Showing
6 changed files
with
58 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,20 @@ | ||
library events; | ||
|
||
typedef void Listener(event); | ||
import "dart:async"; | ||
|
||
class Event { | ||
String type; | ||
Event(this.type); | ||
} | ||
|
||
class Events { | ||
class Emitter { | ||
|
||
Map<String, ListenerList> _listeners; | ||
final _evtController = new StreamController<Event>.broadcast(); | ||
|
||
Events() : _listeners = <String, ListenerList>{}; | ||
Stream<Event> operator[] (type) => _evtController.stream.where((e) => e.type == type); | ||
|
||
ListenerList operator [](String type) => _listeners.putIfAbsent(type, () { | ||
return new ListenerList(type); | ||
}); | ||
|
||
removeAllListeners() => _listeners = {}; | ||
} | ||
|
||
abstract class Emitter<E extends Events>{ | ||
|
||
E on; | ||
|
||
} | ||
|
||
class ListenerList { | ||
|
||
final String _type; | ||
|
||
final List<Listener> _listeners; | ||
|
||
ListenerList(this._type) : _listeners = <Listener>[]; | ||
|
||
ListenerList add(Listener listener) { | ||
_add(listener); | ||
return this; | ||
dispatch(evtOrType) { | ||
var evt = (evtOrType is String) ? new Event(evtOrType) : evtOrType; | ||
_evtController.add(evt); | ||
} | ||
|
||
ListenerList remove(Listener listener) { | ||
_remove(listener); | ||
return this; | ||
} | ||
|
||
bool dispatch([evt]) { | ||
//assert(evt.type == _type); | ||
_listeners.forEach((l) => l(evt)); | ||
} | ||
|
||
void _add(Listener listener) { | ||
_listeners.add(listener); | ||
} | ||
|
||
void _remove(Listener listener) { | ||
_listeners.removeRange(_listeners.indexOf(listener), 1); | ||
} | ||
|
||
int get length => _listeners.length; | ||
|
||
bool get isEmpty => _listeners.isEmpty; | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
part of sockjs_client; | ||
|
||
class ReceiverEvents extends event.Events { | ||
event.ListenerList get message => this["message"]; | ||
event.ListenerList get close => this["close"]; | ||
} | ||
|
||
class Receiver implements event.Emitter<ReceiverEvents> { | ||
ReceiverEvents on = new ReceiverEvents(); | ||
class Receiver extends Object with event.Emitter { | ||
Stream get message => this["message"]; | ||
Stream get close => this["close"]; | ||
} | ||
|
||
typedef Receiver ReceiverFactory(String recvUrl, AjaxObjectFactory xhrFactory); |