jellopy 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 968069bdeaeaeae6248edb6e2b141a20e0f14b77
4
+ data.tar.gz: 3c48261602c6c3547dc0645c2fa4d36ea059452b
5
+ SHA512:
6
+ metadata.gz: 5b402f16887feef90e55cf1877e542f9035907c678592ae997a374445d51321d2919bb959e7ea61dcd5fba2f3deefa9e6c6d673fdaad9bd55979974cd9663016
7
+ data.tar.gz: d8fce03fe1f5921cb3c9496fea4cea44ea86929af1d91d89931b26f8c67b8e0061cfed83271d3427b30db1cddb3a6a36a87f1b4c48b428a5fb836119a7c86b79
@@ -0,0 +1,4 @@
1
+ def asleep(sec)
2
+ $eloop.call_later(Fiber.current, sec, $eloop.item[:callback])
3
+ Fiber.yield
4
+ end
@@ -0,0 +1,34 @@
1
+ def async(obj)
2
+
3
+ def wrapper(func, *args)
4
+ local_fiber = Fiber.new do
5
+ if func.instance_of?UnboundMethod
6
+ # TODO: write unit tests with corner cases for this line
7
+ func = func.bind(self)
8
+ end
9
+ func.call(*args)
10
+ end
11
+ local_fiber
12
+ end
13
+
14
+ if instance_of?Object
15
+ original_fn = method(obj)
16
+ define_method(obj) { |*args| wrapper(original_fn, *args) }
17
+ elsif instance_of?Class
18
+
19
+ # handling class methods
20
+ if self.methods.include?obj
21
+ original_fn = self.method(obj)
22
+ class_eval {define_method(:a_class_method) { |*args| wrapper(original_fn, *args) }}
23
+
24
+ # handling instance methods
25
+ elsif self.instance_methods.include?obj
26
+ original_fn = instance_method(obj)
27
+ define_method(obj) { |*args| wrapper(original_fn, *args) }
28
+
29
+ else
30
+ raise 'Unhandled case'
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ def await(wait_for_obj)
2
+ $eloop.call_later(wait_for_obj, 0, Fiber.current)
3
+ Fiber.yield
4
+ end
@@ -0,0 +1,40 @@
1
+ require 'time'
2
+ require 'fiber'
3
+
4
+ class EventLoop
5
+
6
+ attr_accessor :item
7
+
8
+ def initialize
9
+ @queue = []
10
+ @item = {}
11
+ end
12
+
13
+ def create_task(fn)
14
+ self.call_later(fn, 0)
15
+ end
16
+
17
+ def call_later(fn, timeout, callback=nil)
18
+ @queue << {:fn => fn, :time => Time.now.to_f + timeout, :callback => callback}
19
+ end
20
+
21
+ def start
22
+ while @queue.size > 0
23
+ @queue.sort_by!{|i| i[:time].to_i}
24
+ @item = @queue[0]
25
+ ctime = Time.now.to_f
26
+ if ctime >= @item[:time]
27
+ @queue.delete_at 0
28
+ @item[:fn].resume
29
+ self.call_later(@item[:callback], 0) if @item[:callback] and not @item[:fn].alive?
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ def stop
36
+
37
+ end
38
+
39
+ alias_method :add_task, :create_task
40
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'event_loop'
2
+ require_relative 'async'
3
+ require_relative 'asleep'
4
+ require_relative 'await'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jellopy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrii Halenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Async / await syntax for Ruby Fiber.
14
+ email: andrew@galenko.org
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/asleep.rb
20
+ - lib/async.rb
21
+ - lib/await.rb
22
+ - lib/event_loop.rb
23
+ - lib/jellopy.rb
24
+ homepage: https://github.com/Bot123123/jellopy
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.5.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Jellopy!
48
+ test_files: []