tra 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a84b53b34c31d8bb637f5ee409bf0d3aaec9d25
4
- data.tar.gz: c38aa19122fec744ec828a5ce3c64b4ad3a7545d
3
+ metadata.gz: feb23af2174e0f8e50c07497804c47b6442929e7
4
+ data.tar.gz: f620d3dbff3a2c91360351216904a2721e926e79
5
5
  SHA512:
6
- metadata.gz: 4180e1fdb9f22d1248f93a4185e61d058df415e6b20edb1614566528d450fea658081cff936e0b3c926afd5d8b77f81390725d2918cbf05b515a43371dc92d55
7
- data.tar.gz: 258cfcac05bfecc92dd59a79da29514f61853a3658c194ee6772ce9c6e911ce4e72f71cd8a8d34f1847bc6659e16208511d612fc6d32962eea9e3249a172aa7c
6
+ metadata.gz: 91800584d8ec4999a3cf5e8a49cfff2157a539cbbaaf0cf7e5b54367639f03e5b912fc1c6150657f38a5d19531538031e17fe9f8262745e7d23fd3122be9df29
7
+ data.tar.gz: 18cb9b1a4a50243bc4627bca9354523eba341fb56bdabd7166f1206707206d27d318d0223f6d39634a93dd74a50ecb97d2698515793d9d37d1b9b8443a5ced32
data/lib/tra.rb CHANGED
@@ -5,10 +5,8 @@ require_relative 'tra/mailbox'
5
5
  require_relative 'tra/fork'
6
6
 
7
7
  module Tra
8
- QUEUE = Queue.new
9
-
10
- DIRECTORY = "/tmp/tra"
11
- FILE = -> pid { "#{DIRECTORY}/#{pid}" }
8
+ QUEUE, PATTERNS = Queue.new, {}
9
+ DIRECTORY, FILE = '/tmp/tra', -> pid { "#{DIRECTORY}/#{pid}" }
12
10
 
13
11
  class << self
14
12
  def enumerator
@@ -20,6 +18,10 @@ module Tra
20
18
  extend Forwardable
21
19
  delegate [:next, :take] => :enumerator
22
20
 
21
+ def on pattern, &action
22
+ PATTERNS[pattern] = action
23
+ end
24
+
23
25
  def run
24
26
  Mailbox.receive
25
27
 
@@ -1,7 +1,7 @@
1
1
  module Tra
2
2
  module Fork
3
3
  def fork
4
- super { QUEUE.clear; Mailbox.receive; yield }
4
+ super { QUEUE.clear; PATTERNS.clear; Mailbox.receive; yield }
5
5
  end
6
6
  end
7
7
  end
@@ -7,7 +7,18 @@ module Tra
7
7
 
8
8
  Thread.new do
9
9
  Socket.unix_server_loop file do |socket|
10
- QUEUE << (Marshal.load socket.read size_of_message_from socket)
10
+ message = Marshal.load socket.read size_of_message_from socket
11
+ patterns = PATTERNS.select { |pattern, _| pattern === message }
12
+
13
+ if patterns.empty?
14
+ QUEUE << message
15
+ else
16
+ Thread.new do
17
+ patterns.each do |_, action|
18
+ Thread.new { action.call message }
19
+ end
20
+ end
21
+ end
11
22
  end
12
23
  end
13
24
 
data/test.rb CHANGED
@@ -15,3 +15,37 @@ p Tra.next
15
15
  =end
16
16
  Tra.take(5).each { |message| p message }
17
17
  child.put [:my, :message, "with\n newlines\n"]
18
+
19
+
20
+ # sloppy patterns' test goes underneath
21
+ class A
22
+ end
23
+
24
+ class B
25
+ end
26
+
27
+ As = []
28
+ Bs = []
29
+
30
+ Tra.on A do |message|
31
+ As << message
32
+ end
33
+
34
+ Tra.on B do |message|
35
+ Bs << message
36
+ end
37
+
38
+ fork do
39
+ Process.ppid.put A.new
40
+ Process.ppid.put :asd
41
+ Process.ppid.put B.new
42
+ Process.ppid.put B.new
43
+ Process.ppid.put B.new
44
+ Process.ppid.put A.new
45
+ Process.ppid.put 42
46
+ end
47
+
48
+ sleep 1
49
+ p As.size #2
50
+ p Bs.size #3
51
+ p Tra.take 2
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |g|
2
2
  g.name = 'tra'
3
3
  g.files = `git ls-files`.split($/)
4
- g.version = '0.0.2'
4
+ g.version = '0.0.3'
5
5
  g.summary = 'Erlang-style messaging for Ruby processes'
6
6
  g.authors = ['Anatoly Chernow']
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-21 00:00:00.000000000 Z
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: