Class: PromiseV2
Instance Attribute Summary collapse
-
#next ⇒ Object
readonly
Returns the value of attribute next.
-
#prev ⇒ Object
readonly
Returns the value of attribute prev.
Class Method Summary collapse
- .all_resolved(*promises) ⇒ Object
- .allocate ⇒ Object
- .any(*promises) ⇒ Object
- .error ⇒ Object
- .race(*promises) ⇒ Object
- .reject(value = nil) ⇒ Object
- .resolve(value = nil) ⇒ Object
- .value ⇒ Object
- .when(*promises) ⇒ Object (also: all)
Instance Method Summary collapse
- #always(&block) ⇒ Object (also: #finally, #ensure)
- #always!(&block) ⇒ Object (also: #finally!, #ensure!)
- #and(*promises) ⇒ Object
- #error ⇒ Object
- #fail(&block) ⇒ Object (also: #rescue, #catch)
- #fail!(&block) ⇒ Object (also: #rescue!, #catch!)
- #gen_tracing_proc(passing, &block) ⇒ Object
-
#initialize {|_self| ... } ⇒ PromiseV2
constructor
A new instance of PromiseV2.
- #inspect ⇒ Object
-
#light_nativity_check! ⇒ Object
Raise an exception when a non-JS-native method is called on a JS-native promise but permits some typed promises.
-
#native? ⇒ Boolean
Is this promise native to JavaScript? This means, that methods like resolve or reject won't be available.
-
#nativity_check! ⇒ Object
Raise an exception when a non-JS-native method is called on a JS-native promise.
- #realized? ⇒ Boolean
- #reject(value = nil) ⇒ Object (also: #reject!)
- #rejected? ⇒ Boolean
- #resolve(value = nil) ⇒ Object (also: #resolve!)
- #resolved? ⇒ Boolean
- #then(&block) ⇒ Object (also: #do)
- #then!(&block) ⇒ Object (also: #do!)
-
#there_can_be_only_one! ⇒ Object
Allow only one chain to be present, as needed by the previous implementation.
- #trace(depth = nil, &block) ⇒ Object
- #trace!(*args, &block) ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ PromiseV2
Returns a new instance of PromiseV2.
359 360 361 |
# File 'opal/stdlib/promise/v2.rb', line 359 def initialize(&block) yield self if block_given? end |
Instance Attribute Details
#next ⇒ Object (readonly)
Returns the value of attribute next.
167 168 169 |
# File 'opal/stdlib/promise/v2.rb', line 167 def next @next end |
#prev ⇒ Object (readonly)
Returns the value of attribute prev.
167 168 169 |
# File 'opal/stdlib/promise/v2.rb', line 167 def prev @prev end |
Class Method Details
.all_resolved(*promises) ⇒ Object
127 128 129 130 131 132 |
# File 'opal/stdlib/promise/v2.rb', line 127 def all_resolved(*promises) promises = Array(promises.length == 1 ? promises.first : promises) `Promise.allResolved(#{promises})`.tap do |prom| prom.instance_variable_set(:@type, :all_resolved) end end |
.allocate ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'opal/stdlib/promise/v2.rb', line 108 def allocate ok, fail = nil, nil prom = `new self.$$constructor(function(_ok, _fail) { #{ok} = _ok; #{fail} = _fail; })` prom.instance_variable_set(:@type, :opal) prom.instance_variable_set(:@resolve_proc, ok) prom.instance_variable_set(:@reject_proc, fail) prom end |
.any(*promises) ⇒ Object
134 135 136 137 138 139 |
# File 'opal/stdlib/promise/v2.rb', line 134 def any(*promises) promises = Array(promises.length == 1 ? promises.first : promises) `Promise.any(#{promises})`.tap do |prom| prom.instance_variable_set(:@type, :any) end end |
.error ⇒ Object
164 165 166 167 168 169 170 |
# File 'opal/stdlib/promise/v2.rb', line 164 def reject(value = nil) `Promise.reject(#{value})`.tap do |prom| prom.instance_variable_set(:@type, :reject) prom.instance_variable_set(:@realized, :reject) prom.instance_variable_set(:@value, value) end end |
.race(*promises) ⇒ Object
141 142 143 144 145 146 |
# File 'opal/stdlib/promise/v2.rb', line 141 def race(*promises) promises = Array(promises.length == 1 ? promises.first : promises) `Promise.race(#{promises})`.tap do |prom| prom.instance_variable_set(:@type, :race) end end |
.reject(value = nil) ⇒ Object
157 158 159 160 161 162 163 |
# File 'opal/stdlib/promise/v2.rb', line 157 def reject(value = nil) `Promise.reject(#{value})`.tap do |prom| prom.instance_variable_set(:@type, :reject) prom.instance_variable_set(:@realized, :reject) prom.instance_variable_set(:@value, value) end end |
.resolve(value = nil) ⇒ Object
148 149 150 151 152 153 154 |
# File 'opal/stdlib/promise/v2.rb', line 148 def resolve(value = nil) `Promise.resolve(#{value})`.tap do |prom| prom.instance_variable_set(:@type, :resolve) prom.instance_variable_set(:@realized, :resolve) prom.instance_variable_set(:@value, value) end end |
.value ⇒ Object
155 156 157 158 159 160 161 |
# File 'opal/stdlib/promise/v2.rb', line 155 def resolve(value = nil) `Promise.resolve(#{value})`.tap do |prom| prom.instance_variable_set(:@type, :resolve) prom.instance_variable_set(:@realized, :resolve) prom.instance_variable_set(:@value, value) end end |
Instance Method Details
#always(&block) ⇒ Object Also known as: finally, ensure
266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'opal/stdlib/promise/v2.rb', line 266 def always(&block) prom = nil blk = gen_tracing_proc(block) do |val| prom.instance_variable_set(:@realized, :resolve) prom.instance_variable_set(:@value, val) end prom = `self.finally(#{blk})` prom.instance_variable_set(:@prev, self) prom.instance_variable_set(:@type, :always) (@next ||= []) << prom prom end |
#always!(&block) ⇒ Object Also known as: finally!, ensure!
279 280 281 282 |
# File 'opal/stdlib/promise/v2.rb', line 279 def always!(&block) there_can_be_only_one! always(&block) end |
#and(*promises) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'opal/stdlib/promise/v2.rb', line 346 def and(*promises) promises = promises.map do |i| if PromiseV2 === i i else PromiseV2.value(i) end end PromiseV2.when(self, *promises).then do |a, *b| [*a, *b] end end |
#error ⇒ Object
341 342 343 344 |
# File 'opal/stdlib/promise/v2.rb', line 341 def error light_nativity_check! @value if rejected? end |
#fail(&block) ⇒ Object Also known as: rescue, catch
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'opal/stdlib/promise/v2.rb', line 243 def fail(&block) prom = nil blk = gen_tracing_proc(block) do |val| prom.instance_variable_set(:@realized, :resolve) prom.instance_variable_set(:@value, val) end prom = `self.catch(#{blk})` prom.instance_variable_set(:@prev, self) prom.instance_variable_set(:@type, :fail) (@next ||= []) << prom prom end |
#fail!(&block) ⇒ Object Also known as: rescue!, catch!
256 257 258 259 |
# File 'opal/stdlib/promise/v2.rb', line 256 def fail!(&block) there_can_be_only_one! fail(&block) end |
#gen_tracing_proc(passing, &block) ⇒ Object
194 195 196 197 198 199 200 |
# File 'opal/stdlib/promise/v2.rb', line 194 def gen_tracing_proc(passing, &block) proc do |i| res = passing.call(i) yield(res) res end end |
#inspect ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'opal/stdlib/promise/v2.rb', line 365 def inspect result = "#<#{self.class}" if @type result += ":#{@type}" unless %i[opal resolve reject].include? @type else result += ':native' end result += ":#{@realized}" if @realized result += "(#{object_id})" if @next && @next.any? result += " >> #{@next.inspect}" end result += ": #{@value.inspect}" if @value result += '>' result end |
#light_nativity_check! ⇒ Object
Raise an exception when a non-JS-native method is called on a JS-native promise but permits some typed promises
182 183 184 185 |
# File 'opal/stdlib/promise/v2.rb', line 182 def light_nativity_check! return if %i[reject resolve trace always fail then].include? @type raise ArgumentError, 'this promise is native to JavaScript' if native? end |
#native? ⇒ Boolean
Is this promise native to JavaScript? This means, that methods like resolve or reject won't be available.
171 172 173 |
# File 'opal/stdlib/promise/v2.rb', line 171 def native? @type != :opal end |
#nativity_check! ⇒ Object
Raise an exception when a non-JS-native method is called on a JS-native promise
176 177 178 |
# File 'opal/stdlib/promise/v2.rb', line 176 def nativity_check! raise ArgumentError, 'this promise is native to JavaScript' if native? end |
#realized? ⇒ Boolean
326 327 328 329 |
# File 'opal/stdlib/promise/v2.rb', line 326 def realized? light_nativity_check! !@realized.nil? end |
#reject(value = nil) ⇒ Object Also known as: reject!
212 213 214 215 216 217 218 219 |
# File 'opal/stdlib/promise/v2.rb', line 212 def reject(value = nil) nativity_check! raise ArgumentError, 'this promise was already resolved' if @realized @value = value @realized = :reject @reject_proc.call(value) self end |
#rejected? ⇒ Boolean
321 322 323 324 |
# File 'opal/stdlib/promise/v2.rb', line 321 def rejected? light_nativity_check! @realized == :reject end |
#resolve(value = nil) ⇒ Object Also known as: resolve!
202 203 204 205 206 207 208 209 |
# File 'opal/stdlib/promise/v2.rb', line 202 def resolve(value = nil) nativity_check! raise ArgumentError, 'this promise was already resolved' if @realized @value = value @realized = :resolve @resolve_proc.call(value) self end |
#resolved? ⇒ Boolean
316 317 318 319 |
# File 'opal/stdlib/promise/v2.rb', line 316 def resolved? light_nativity_check! @realized == :resolve end |
#then(&block) ⇒ Object Also known as: do
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'opal/stdlib/promise/v2.rb', line 222 def then(&block) prom = nil blk = gen_tracing_proc(block) do |val| prom.instance_variable_set(:@realized, :resolve) prom.instance_variable_set(:@value, val) end prom = `self.then(#{blk})` prom.instance_variable_set(:@prev, self) prom.instance_variable_set(:@type, :then) (@next ||= []) << prom prom end |
#then!(&block) ⇒ Object Also known as: do!
235 236 237 238 |
# File 'opal/stdlib/promise/v2.rb', line 235 def then!(&block) there_can_be_only_one! self.then(&block) end |
#there_can_be_only_one! ⇒ Object
Allow only one chain to be present, as needed by the previous implementation. This isn't a strict check - it's always possible on the JS side to chain a given block.
190 191 192 |
# File 'opal/stdlib/promise/v2.rb', line 190 def there_can_be_only_one! raise ArgumentError, 'a promise has already been chained' if @next && @next.any? end |
#trace(depth = nil, &block) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'opal/stdlib/promise/v2.rb', line 289 def trace(depth = nil, &block) prom = self.then do values = [] prom = self while prom && (!depth || depth > 0) val = nil begin val = prom.value rescue ArgumentError val = :native end values.unshift(val) depth -= 1 if depth prom = prom.prev end yield(*values) end prom.instance_variable_set(:@type, :trace) prom end |
#trace!(*args, &block) ⇒ Object
311 312 313 314 |
# File 'opal/stdlib/promise/v2.rb', line 311 def trace!(*args, &block) there_can_be_only_one! trace(*args, &block) end |