simple-event-bus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/event-bus.rb +3 -0
  3. data/lib/event_bus.rb +71 -0
  4. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6f7d8ed22f4a348783da3d0f2075e1c1716ceb9dee5cb42a189430554675468
4
+ data.tar.gz: aad5322c057cd3b959dbff07afab489ef537962f3e28ce1775a010e611eb571c
5
+ SHA512:
6
+ metadata.gz: 00dc1d904dff9addde61c3f7524c91c59770b8f37a89342d88c326830a84fc1b1e3b7001a7f70f87dbf90970238f8ec99302caf03b1f8c051d8f838070648369
7
+ data.tar.gz: ace377fb20d19e88dc71e97f9e29275cc641ac75f10691507c026f87c534244f0e4c3b430295e3033defba6c160b531d93f4c39bba0c6b8f2dcd7aae5e3b3a5e
data/lib/event-bus.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'event_bus'
data/lib/event_bus.rb ADDED
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ class SimpleEventBus
6
+ VERSION = '0.1.0'
7
+
8
+ class << self
9
+ extend Forwardable
10
+
11
+ def [](*events)
12
+ Module.new.tap do |m|
13
+ m.define_singleton_method(:included) do |base|
14
+ events.each { |e| SimpleEventBus.subscribe(e, base) }
15
+ end
16
+ end
17
+ end
18
+
19
+ def instance
20
+ @instance ||= new
21
+ end
22
+
23
+ def_delegators :instance, :emit, :subscribe, :unsubscribe
24
+ end
25
+
26
+ def initialize
27
+ @subscribers = handler_hash
28
+ @once = handler_hash
29
+ end
30
+
31
+ def emit(event, *args)
32
+ caller = handler_caller(event, args)
33
+
34
+ @subscribers[event].each(&caller)
35
+ @once[event].delete_if(&caller)
36
+
37
+ nil
38
+ end
39
+
40
+ def subscribe(event, obj_hlr = nil, once: false, &hlr)
41
+ handler = hlr || obj_hlr
42
+
43
+ raise ArgumentError, 'object or proc handler required' if handler.nil?
44
+
45
+ (once ? @once : @subscribers)[event][handler.object_id] = handler
46
+
47
+ handler.object_id
48
+ end
49
+
50
+ def unsubscribe(event, id)
51
+ @subscribers[event].delete(id)
52
+ end
53
+
54
+ private
55
+
56
+ def handler_hash
57
+ Hash.new { |h, k| h[k] = {} }
58
+ end
59
+
60
+ def handler_caller(event, args)
61
+ proc do |_id, handler|
62
+ if handler.is_a?(Proc)
63
+ handler.call(*args)
64
+ else
65
+ handler.send(event, *args)
66
+ end
67
+
68
+ true
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-event-bus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Slyusarev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.60'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.60'
41
+ description:
42
+ email:
43
+ - slyusarevmikhail@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/event-bus.rb
49
+ - lib/event_bus.rb
50
+ homepage: https://github.com/mikhailvs/simple-event-bus
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.7.7
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Simple event bus. Easily emit and handle events.
74
+ test_files: []