13
13
package org .scalajs .testsuite .jsinterop
14
14
15
15
import scala .scalajs .js
16
+ import scala .scalajs .js .|
16
17
import scala .scalajs .js .annotation ._
17
18
18
19
import org .junit .Assert ._
@@ -251,6 +252,17 @@ class JSSymbolTest {
251
252
assertArrayEquals(Array (532 ), content.result())
252
253
}
253
254
255
+ @ Test def sjsdefinedAsyncIterable (): Unit = {
256
+ PromiseMock .withMockedPromise { processQueue =>
257
+ val obj = new SJSDefinedAsyncIterable
258
+
259
+ val content = Array .newBuilder[Int ]
260
+ iterateAsyncIterable(obj)(content += _)
261
+ processQueue()
262
+ assertArrayEquals(Array (278 ), content.result())
263
+ }
264
+ }
265
+
254
266
@ Test def inOperatorWithSymbols (): Unit = {
255
267
val obj = mkObject(sym1 -> " foo" )
256
268
@@ -304,26 +316,40 @@ object JSSymbolTest {
304
316
def callSymbol (sym : js.Symbol )(args : js.Any * ): Any = js.native
305
317
}
306
318
307
- def singletonIterator (singleton : Any ): js.Dynamic = {
308
- var first = true
319
+ def singletonIterator (singleton : Any ): js.Dynamic =
320
+ js.Dynamic .literal(next = singletonIteratorLogic(singleton))
321
+
322
+ def singletonAsyncIterator (singleton : Any ): js.Dynamic = {
323
+ val logic = singletonIteratorLogic(singleton)
309
324
js.Dynamic .literal(
310
- next = { () =>
311
- if (first) {
312
- first = false
313
- js.Dynamic .literal(value = singleton.asInstanceOf [js.Any ],
314
- done = false )
315
- } else {
316
- js.Dynamic .literal(value = (), done = true )
317
- }
318
- }
325
+ next = { () =>
326
+ new js.Promise [js.Dynamic ]({
327
+ (resolve : js.Function1 [js.Dynamic | js.Thenable [js.Dynamic ], _], reject : js.Function1 [Any , _]) =>
328
+ resolve(logic())
329
+ })
330
+ }
319
331
)
320
332
}
321
333
334
+ def singletonIteratorLogic (singleton : Any ): () => js.Dynamic = {
335
+ var first = true
336
+
337
+ { () =>
338
+ if (first) {
339
+ first = false
340
+ js.Dynamic .literal(value = singleton.asInstanceOf [js.Any ], done = false )
341
+ } else {
342
+ js.Dynamic .literal(value = (), done = true )
343
+ }
344
+ }
345
+ }
346
+
322
347
def iterableToArray [A ](iterable : JSIterable [A ]): js.Array [A ] =
323
348
js.constructorOf[js.Array [_]].from(iterable).asInstanceOf [js.Array [A ]]
324
349
325
350
def iterateIterable [A ](iterable : JSIterable [A ])(f : A => Any ): Unit = {
326
351
val iterator = iterable.iterator()
352
+
327
353
def loop (): Unit = {
328
354
import js .DynamicImplicits .truthValue
329
355
@@ -333,6 +359,26 @@ object JSSymbolTest {
333
359
loop()
334
360
}
335
361
}
362
+
363
+ loop()
364
+ }
365
+
366
+ def iterateAsyncIterable [A ](
367
+ iterable : JSAsyncIterable [A ])(f : A => Any ): Unit = {
368
+ val iterator = iterable.asyncIterator()
369
+
370
+ def loop (): Unit = {
371
+ import js .DynamicImplicits .truthValue
372
+
373
+ val pEntry = iterator.next().asInstanceOf [js.Promise [js.Dynamic ]]
374
+ pEntry.`then`[Unit ] { (entry : js.Dynamic ) =>
375
+ if (! entry.done) {
376
+ f(entry.value.asInstanceOf [A ])
377
+ loop()
378
+ }
379
+ }
380
+ }
381
+
336
382
loop()
337
383
}
338
384
@@ -512,4 +558,14 @@ object JSSymbolTest {
512
558
@ JSName (js.Symbol .iterator)
513
559
def iterator (): js.Dynamic = singletonIterator(532 )
514
560
}
561
+
562
+ trait JSAsyncIterable [+ A ] extends js.Object {
563
+ @ JSName (js.Symbol .asyncIterator)
564
+ def asyncIterator (): js.Dynamic
565
+ }
566
+
567
+ class SJSDefinedAsyncIterable extends JSAsyncIterable [Int ] {
568
+ @ JSName (js.Symbol .asyncIterator)
569
+ def asyncIterator (): js.Dynamic = singletonAsyncIterator(278 )
570
+ }
515
571
}
0 commit comments