standard-procedure-plumbing 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,61 +0,0 @@
1
- require "async/task"
2
- require "async/semaphore"
3
-
4
- module Plumbing
5
- class EventDispatcher
6
- class Fiber < EventDispatcher
7
- def initialize limit: 4
8
- super()
9
- @semaphore = Async::Semaphore.new(limit)
10
- @queue = Set.new
11
- @paused = false
12
- end
13
-
14
- def dispatch event
15
- @queue << event
16
- dispatch_events unless @paused
17
- end
18
-
19
- def pause
20
- @paused = true
21
- end
22
-
23
- def resume
24
- @paused = false
25
- dispatch_events
26
- end
27
-
28
- def queue_size
29
- @queue.size
30
- end
31
-
32
- def shutdown
33
- super
34
- @queue.clear
35
- end
36
-
37
- private
38
-
39
- def dispatch_events
40
- @semaphore.async do |task|
41
- events = @queue.dup
42
- @queue.clear
43
- events.each do |event|
44
- dispatch_event event, task
45
- end
46
- end
47
- end
48
-
49
- def dispatch_event event, task
50
- @observers.collect do |observer|
51
- task.async do
52
- observer.call event
53
- rescue => ex
54
- puts ex
55
- ex
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,35 +0,0 @@
1
- module Plumbing
2
- class EventDispatcher
3
- def initialize observers: []
4
- @observers = observers
5
- end
6
-
7
- def add_observer observer = nil, &block
8
- observer ||= block.to_proc
9
- raise Plumbing::InvalidObserver.new "observer_does_not_respond_to_call" unless observer.respond_to? :call
10
- @observers << observer
11
- observer
12
- end
13
-
14
- def remove_observer observer
15
- @observers.delete observer
16
- end
17
-
18
- def is_observer? observer
19
- @observers.include? observer
20
- end
21
-
22
- def dispatch event
23
- @observers.collect do |observer|
24
- observer.call event
25
- rescue => ex
26
- puts ex
27
- ex
28
- end
29
- end
30
-
31
- def shutdown
32
- @observers = []
33
- end
34
- end
35
- end