opal-async 1.1.1 → 1.2.0

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
  SHA256:
3
- metadata.gz: 050f56171be0bacefeff4c68f9ba1e517b81adf3017b4013dcbeff44e8bbc331
4
- data.tar.gz: 242811c68a22d384c2b6ce5e916fef95b729460b1dfab5a13e7891405c16d0e7
3
+ metadata.gz: e4ac70e28d27ce5551acbba060dba2956e791e33ab7788673ffd6ebfc08fc35f
4
+ data.tar.gz: c02be17a79bab91865aa0898afc97a024376bf0c7686dd85854193fd4174516a
5
5
  SHA512:
6
- metadata.gz: 1265a3c27ccbbe5e9a78f2f734a16697b5d4cff3cfe7294c071b04c246f5530f7cca397592ff76683f4bfbc3682116d6c335bc4ca51dd4382b722cdd85ffbe2d
7
- data.tar.gz: c260fc81573eea00cd1be33410655dbbbffec077d88fad31bb9f7f1c4941f04351dbaa1bb385e45e6fda67e0d4ee41d1f68a49f3c55e0639480e76383cddd01b
6
+ metadata.gz: 1cd290ff3f7b6173e7d6cf6440bca1819e53294a2a7859a016bf0e2632cc7b43b5a18f5563d21be0b755e7018cf5449f5f1e6e18a3217a44d950d580cd4eac57
7
+ data.tar.gz: 0bc9487d71ce6343d0b708c56c6878bb445390db5043a5972221723ffec12059286f03486424f0b480441a46bb4d9af0407028444f0a119a2dd3b566031ba7e7
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.2.0
4
+
5
+ - Asynchronous `Array#cycle` method that is web browser event loop friendly
6
+
3
7
  ## 1.1.1
4
8
 
5
9
  - Added `Thread` class extension to enable using `Async::Task` as `Thread` in Opal
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Add this line to your application's Gemfile:
7
7
 
8
- gem 'opal-async', '~> 1.1.1'
8
+ gem 'opal-async', '~> 1.2.0'
9
9
 
10
10
  And then execute:
11
11
 
@@ -161,21 +161,51 @@ Async::Interval.new 3000 do
161
161
  end
162
162
  ```
163
163
 
164
- ### Thread
164
+ ### Ruby Extensions
165
+
166
+ [opal-async](https://rubygems.org/gems/opal-async) ships with some Opal Ruby extensions that enhance Ruby classes with asynchronous capabilities.
167
+
168
+ You may activate all the Ruby extensions via this require statement:
169
+
170
+ ```ruby
171
+ require 'async/ext'
172
+ ```
173
+
174
+ #### Thread
165
175
 
166
176
  You may use the `Async::Task` class as a `Thread` class in Opal to perform asynchronous work with an extra `require` statement.
167
177
 
168
178
  ```ruby
169
- require 'async/ext/thread'
179
+ require 'async/ext/thread' # not needed if you called `require 'async/ext'`
170
180
 
171
181
  Thread.new do
172
182
  puts "hello world"
173
183
  end
174
184
  ```
175
185
 
186
+ #### Array
187
+
188
+ `Array#cycle` has been amended to work asynchronously via `Async::Task` when triggered inside another `Async::Task` (auto-detects it)
189
+
190
+ This makes it not block the web browser event loop, thus allowing other tasks to update the DOM unhindered while `Array#cycle` is running.
191
+
192
+ ```ruby
193
+ require 'async/ext/array' # not needed if you called `require 'async/ext'`
194
+
195
+ Async::Task.new do
196
+ [1,2,3,4].cycle do |n|
197
+ puts n
198
+ Async::Task.new do
199
+ # make a DOM update
200
+ end
201
+ sleep(1) # this does not block the event loop since it is transparently happening inside an Async::Task
202
+ end
203
+ end
204
+ ```
205
+
176
206
  ## In The Wild
177
207
 
178
- opal-async is currently used in the following projects:
208
+ opal-async is currently used in:
179
209
  - [Glimmer DSL for Opal](https://github.com/AndyObtiva/glimmer-dsl-opal)
180
210
 
181
211
  ## Change Log
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
@@ -0,0 +1,2 @@
1
+ require 'async/ext/thread'
2
+ require 'async/ext/array'
@@ -0,0 +1,25 @@
1
+ require 'async/task'
2
+
3
+ class Array
4
+ alias cycle_without_opal_async cycle
5
+ def cycle(n=nil, &block)
6
+ if Async::Task.started? && block_given?
7
+ array = self * n unless n.nil?
8
+ index = 0
9
+ looper = lambda do
10
+ if n.nil?
11
+ block.call(self[index])
12
+ index += 1
13
+ index = index % self.size
14
+ Async::Task.new(&looper)
15
+ else
16
+ block.call(array.shift)
17
+ Async::Task.new(&looper) unless array.empty?
18
+ end
19
+ end
20
+ Async::Task.new(&looper)
21
+ else
22
+ cycle_without_opal_async(n, &block)
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,23 @@
1
1
  module Async
2
2
  class Task
3
+ class << self
4
+ # returns general status of whether a task is running.
5
+ # Useful to code running in the task block to determine if it is running
6
+ # as part of a task or not and take special actions accordingly.
7
+ def started?
8
+ @@started = false unless defined? :@@started
9
+ @@started
10
+ end
11
+
12
+ def start
13
+ @@started = true
14
+ end
15
+
16
+ def stop
17
+ @@started = false
18
+ end
19
+ end
20
+
3
21
  attr_accessor :delay, :times
4
22
  def initialize options={}, &block
5
23
  @options = options
@@ -15,6 +33,7 @@ module Async
15
33
  @delay = @options[:delay] || 0
16
34
  @times = @options[:times]
17
35
  @proc = Proc.new do
36
+ self.class.start
18
37
  if @times
19
38
  if @times.is_a?(Fixnum)
20
39
  @block.call(@countup, @countdown)
@@ -34,6 +53,7 @@ module Async
34
53
  @block.call
35
54
  @stopped = true
36
55
  end
56
+ self.class.stop
37
57
  end
38
58
  end
39
59
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-async
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravenstine
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-07-22 00:00:00.000000000 Z
12
+ date: 2020-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: opal
@@ -121,6 +121,8 @@ files:
121
121
  - opal/async.rb
122
122
  - opal/async/countdown.rb
123
123
  - opal/async/enumerator.rb
124
+ - opal/async/ext.rb
125
+ - opal/async/ext/array.rb
124
126
  - opal/async/ext/thread.rb
125
127
  - opal/async/interval.rb
126
128
  - opal/async/task.rb
@@ -150,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
152
  - !ruby/object:Gem::Version
151
153
  version: '0'
152
154
  requirements: []
153
- rubygems_version: 3.1.2
155
+ rubygems_version: 3.1.4
154
156
  signing_key:
155
157
  specification_version: 4
156
158
  summary: Provides non-blocking tasks and enumerators for Opal.