Created
May 14, 2018 02:27
-
-
Save devoncarew/1b1846729a9ff2c8e44ba21f05211921 to your computer and use it in GitHub Desktop.
EventBus
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
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
library core.event_bus; | |
import 'dart:async'; | |
/** | |
* An event bus class. Clients can listen for classes of events, optionally | |
* filtered by a string type. This can be used to decouple events sources and | |
* event listeners. | |
*/ | |
class EventBus { | |
final StreamController<BusEvent> _controller = | |
new StreamController.broadcast(); | |
EventBus(); | |
/** | |
* Listen for events on the event bus. Clients can pass in an optional [type], | |
* which filters the events to only those specific ones. | |
*/ | |
Stream<BusEvent> onEvent([String type]) { | |
if (type == null) { | |
return _controller.stream; | |
} else { | |
return _controller.stream.where((e) => e.type == type); | |
} | |
} | |
/** | |
* Add an event to the event bus. | |
*/ | |
void addEvent(BusEvent event) => _controller.add(event); | |
/** | |
* Close (destroy) this [EventBus]. This is generally not used outside of a | |
* testing context. All Stream listeners will be closed and the bus will not | |
* fire any more events. | |
*/ | |
Future close() => _controller.close(); | |
} | |
/** | |
* An event type for use with [EventBus]. | |
*/ | |
class BusEvent { | |
/// The type of the event. | |
final String type; | |
/// Any args associated with the event. | |
final Map args; | |
BusEvent(this.type, [this.args = const {}]); | |
String toString() => '[${type}: ${args}]'; | |
} |
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
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
library dart_pad.event_bus_test; | |
import 'dart:async'; | |
import 'package:dart_pad/core/event_bus.dart'; | |
import 'package:test/test.dart'; | |
main() => defineTests(); | |
void defineTests() { | |
group('event_bus', () { | |
test('fire one event', () { | |
EventBus bus = new EventBus(); | |
Future<List> f = bus.onEvent('file-save').toList(); | |
_fireEvents(bus); | |
bus.close(); | |
return f.then((List l) { | |
expect(l.length, 1); | |
}); | |
}); | |
test('fire two events', () { | |
EventBus bus = new EventBus(); | |
Future<List> f = bus.onEvent('file-modified').toList(); | |
_fireEvents(bus); | |
bus.close(); | |
return f.then((List l) { | |
expect(l.length, 2); | |
expect(l[0].args['file'], 'a'); | |
expect(l[1].args['file'], 'b'); | |
}); | |
}); | |
test('receive all events', () { | |
EventBus bus = new EventBus(); | |
Future<List> f = bus.onEvent().toList(); | |
_fireEvents(bus); | |
bus.close(); | |
return f.then((List l) { | |
expect(l.length, 3); | |
}); | |
}); | |
}); | |
} | |
void _fireEvents(EventBus bus) { | |
bus.addEvent(new BusEvent('file-save', {'file': 'a'})); | |
bus.addEvent(new BusEvent('file-modified', {'file': 'a'})); | |
bus.addEvent(new BusEvent('file-modified', {'file': 'b'})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment