startback 0.11.4 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/startback/bus.rb DELETED
@@ -1,94 +0,0 @@
1
- require 'startback/event'
2
- module Startback
3
- #
4
- # Sync and async bus abstraction allowing to register listeners and
5
- # emitting events towards them.
6
- #
7
- # This bus actually decorates two busses, one in synchronous and the
8
- # other one is asynchronous (optional).
9
- #
10
- # * A synchronous bus MUST call the listeners as part of emitting
11
- # process, and MUST re-raise any error occuring during that process.
12
- # See, e.g. Startback::Bus::Memory::Sync
13
- #
14
- # * An asynchronous bus MAY call the listeners later, but MUST hide
15
- # errors to the emitter.
16
- # See, e.g. Startback::Bus::Memory::Async
17
- #
18
- # This bus facade emits events to both sync and async busses (if any),
19
- # and listen on the sync one by default.
20
- #
21
- # For emitters:
22
- #
23
- # # This will synchronously call every listeners who `listen`
24
- # # on the synchronous bus (& reraise exceptions) then call
25
- # # (possibly later) all listeners who `listen` on the
26
- # # asynchronous bus if any (& hide exceptions).
27
- # bus.emit(event)
28
- #
29
- # # This only reaches sync listeners
30
- # bus.sync.emit(event)
31
- #
32
- # # This only reaches async listeners (an async bus must be set)
33
- # bus.async.emit(event)
34
- #
35
- # Please note that there is currently no way to reach sync listeners
36
- # without having to implement error handling on the emitter side.
37
- #
38
- # For listeners:
39
- #
40
- # # This will listen synchronously and make the emitter fail if
41
- # # anything goes wrong with the callback:
42
- # bus.listen(event_type) do |event|
43
- # ...
44
- # end
45
- #
46
- # # It is a shortcut for:
47
- # bus.sync.listen(event_type) do |event| ... end
48
- #
49
- # This will listen asynchronously and could not make the emitter
50
- # fail if something goes wrong with the callback.
51
- # bus.async.listen(event_type) do |event|
52
- # ...
53
- # end
54
- #
55
- # Feel free to access the sync and async busses directly for specific
56
- # cases though.
57
- #
58
- class Bus
59
- include Support::Robustness
60
-
61
- def initialize(sync = Memory::Sync.new, async = nil)
62
- @sync = sync
63
- @async = async
64
- end
65
- attr_reader :sync, :async
66
-
67
- # Emits a particular event to the listeners.
68
- #
69
- # @arg event an event, should be an Event instance (through duck
70
- # typing is allowed)
71
- def emit(event)
72
- monitor({
73
- op: "Startback::Bus#emit",
74
- op_data: {
75
- event: { type: event.type }
76
- }
77
- }, event.context) do
78
- sync.emit(event)
79
- async.emit(event) if async
80
- end
81
- end
82
-
83
- # Registers `listener` as being interested in receiving events of
84
- # a specific type.
85
- #
86
- # @arg type: Symbol, the type of event the listener is interested in.
87
- # @arg listener: Proc, the listener itself.
88
- def listen(type, processor = nil, listener = nil, &bl)
89
- sync.listen(type, processor, listener, &bl)
90
- end
91
-
92
- end # class Bus
93
- end # module Klaro
94
- require_relative 'bus/memory'
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
- module Startback
3
- describe Bus::Memory do
4
-
5
- subject{
6
- Bus::Memory::Async.new
7
- }
8
-
9
- it 'allows emiting an receiving' do
10
- seen = nil
11
- subject.listen("user_changed") do |evt|
12
- seen = evt
13
- end
14
- subject.emit(Event.new("user_changed", {id: 12}))
15
- expect(seen).to be_a(Event)
16
- expect(seen.type).to eql("user_changed")
17
- expect(seen.data.to_h).to eql({id: 12})
18
- end
19
-
20
- it 'allows mixin Symbol vs. String for event type' do
21
- seen = nil
22
- subject.listen(:user_changed) do |evt|
23
- seen = evt
24
- end
25
- subject.emit(Event.new(:user_changed, {id: 12}))
26
- expect(seen).to be_a(Event)
27
- expect(seen.type).to eql("user_changed")
28
- expect(seen.data.to_h).to eql({id: 12})
29
- end
30
-
31
- it 'does not raise errors synchronously' do
32
- subject.listen("user_changed") do |evt|
33
- raise "An error occured"
34
- end
35
- expect {
36
- subject.emit(Event.new("user_changed", {id: 12}))
37
- }.not_to raise_error
38
- end
39
-
40
- end
41
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
- module Startback
3
- describe Bus::Memory do
4
-
5
- subject{
6
- Bus::Memory::Sync.new
7
- }
8
-
9
- it 'allows emiting an receiving' do
10
- seen = nil
11
- subject.listen("user_changed") do |evt|
12
- seen = evt
13
- end
14
- subject.emit(Event.new("user_changed", {id: 12}))
15
- expect(seen).to be_a(Event)
16
- expect(seen.type).to eql("user_changed")
17
- expect(seen.data.to_h).to eql({id: 12})
18
- end
19
-
20
- it 'allows mixin Symbol vs. String for event type' do
21
- seen = nil
22
- subject.listen(:user_changed) do |evt|
23
- seen = evt
24
- end
25
- subject.emit(Event.new(:user_changed, {id: 12}))
26
- expect(seen).to be_a(Event)
27
- expect(seen.type).to eql("user_changed")
28
- expect(seen.data.to_h).to eql({id: 12})
29
- end
30
-
31
- it 'raises emit errors synchronously' do
32
- subject.listen("user_changed") do |evt|
33
- raise "An error occured"
34
- end
35
- expect {
36
- subject.emit(Event.new("user_changed", {id: 12}))
37
- }.to raise_error("An error occured")
38
- end
39
-
40
- end
41
- end