| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { expect } from 'chai'; |
| 11 |
|
| 12 |
import { Signal, Stream } from '@lumino/signaling'; |
| 13 |
|
| 14 |
class TestObject { |
| 15 |
readonly one = new Signal<this, void>(this); |
| 16 |
|
| 17 |
readonly two = new Signal<this, number>(this); |
| 18 |
|
| 19 |
readonly three = new Stream<this, string[]>(this); |
| 20 |
} |
| 21 |
|
| 22 |
class ExtendedObject extends TestObject { |
| 23 |
notifyCount = 0; |
| 24 |
|
| 25 |
onNotify(): void { |
| 26 |
this.notifyCount++; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
class TestHandler { |
| 31 |
name = ''; |
| 32 |
|
| 33 |
oneCount = 0; |
| 34 |
|
| 35 |
twoValue = 0; |
| 36 |
|
| 37 |
twoSender: TestObject | null = null; |
| 38 |
|
| 39 |
onOne(): void { |
| 40 |
this.oneCount++; |
| 41 |
} |
| 42 |
|
| 43 |
onTwo(sender: TestObject, args: number): void { |
| 44 |
this.twoSender = sender; |
| 45 |
this.twoValue = args; |
| 46 |
} |
| 47 |
|
| 48 |
onThree(sender: TestObject, args: string[]): void { |
| 49 |
args.push(this.name); |
| 50 |
} |
| 51 |
|
| 52 |
onThrow(): void { |
| 53 |
throw new Error(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
describe('@lumino/signaling', () => { |
| 58 |
describe('Signal', () => { |
| 59 |
describe('#sender', () => { |
| 60 |
it('should be the sender of the signal', () => { |
| 61 |
let obj = new TestObject(); |
| 62 |
expect(obj.one.sender).to.equal(obj); |
| 63 |
expect(obj.two.sender).to.equal(obj); |
| 64 |
expect(obj.three.sender).to.equal(obj); |
| 65 |
}); |
| 66 |
}); |
| 67 |
|
| 68 |
describe('#connect()', () => { |
| 69 |
it('should return true on success', () => { |
| 70 |
let obj = new TestObject(); |
| 71 |
let handler = new TestHandler(); |
| 72 |
let c1 = obj.one.connect(handler.onOne, handler); |
| 73 |
expect(c1).to.equal(true); |
| 74 |
}); |
| 75 |
|
| 76 |
it('should return false on failure', () => { |
| 77 |
let obj = new TestObject(); |
| 78 |
let handler = new TestHandler(); |
| 79 |
let c1 = obj.one.connect(handler.onOne, handler); |
| 80 |
let c2 = obj.one.connect(handler.onOne, handler); |
| 81 |
expect(c1).to.equal(true); |
| 82 |
expect(c2).to.equal(false); |
| 83 |
}); |
| 84 |
|
| 85 |
it('should connect plain functions', () => { |
| 86 |
let obj = new TestObject(); |
| 87 |
let handler = new TestHandler(); |
| 88 |
let c1 = obj.one.connect(handler.onThrow); |
| 89 |
expect(c1).to.equal(true); |
| 90 |
}); |
| 91 |
|
| 92 |
it('should ignore duplicate connections', () => { |
| 93 |
let obj = new TestObject(); |
| 94 |
let handler = new TestHandler(); |
| 95 |
let c1 = obj.one.connect(handler.onOne, handler); |
| 96 |
let c2 = obj.one.connect(handler.onOne, handler); |
| 97 |
let c3 = obj.two.connect(handler.onTwo, handler); |
| 98 |
let c4 = obj.two.connect(handler.onTwo, handler); |
| 99 |
obj.one.emit(undefined); |
| 100 |
obj.two.emit(42); |
| 101 |
expect(c1).to.equal(true); |
| 102 |
expect(c2).to.equal(false); |
| 103 |
expect(c3).to.equal(true); |
| 104 |
expect(c4).to.equal(false); |
| 105 |
expect(handler.oneCount).to.equal(1); |
| 106 |
expect(handler.twoValue).to.equal(42); |
| 107 |
}); |
| 108 |
|
| 109 |
it('should handle connect after disconnect and emit', () => { |
| 110 |
let obj = new TestObject(); |
| 111 |
let handler = new TestHandler(); |
| 112 |
let c1 = obj.one.connect(handler.onOne, handler); |
| 113 |
expect(c1).to.equal(true); |
| 114 |
obj.one.disconnect(handler.onOne, handler); |
| 115 |
obj.one.emit(undefined); |
| 116 |
let c2 = obj.one.connect(handler.onOne, handler); |
| 117 |
expect(c2).to.equal(true); |
| 118 |
}); |
| 119 |
}); |
| 120 |
|
| 121 |
describe('#disconnect()', () => { |
| 122 |
it('should return true on success', () => { |
| 123 |
let obj = new TestObject(); |
| 124 |
let handler = new TestHandler(); |
| 125 |
obj.one.connect(handler.onOne, handler); |
| 126 |
let d1 = obj.one.disconnect(handler.onOne, handler); |
| 127 |
expect(d1).to.equal(true); |
| 128 |
}); |
| 129 |
|
| 130 |
it('should return false on failure', () => { |
| 131 |
let obj = new TestObject(); |
| 132 |
let handler = new TestHandler(); |
| 133 |
let d1 = obj.one.disconnect(handler.onOne, handler); |
| 134 |
expect(d1).to.equal(false); |
| 135 |
}); |
| 136 |
|
| 137 |
it('should disconnect plain functions', () => { |
| 138 |
let obj = new TestObject(); |
| 139 |
let handler = new TestHandler(); |
| 140 |
obj.one.connect(handler.onThrow); |
| 141 |
expect(obj.one.disconnect(handler.onThrow)).to.equal(true); |
| 142 |
expect(() => obj.one.emit(undefined)).to.not.throw(Error); |
| 143 |
}); |
| 144 |
|
| 145 |
it('should disconnect a specific signal', () => { |
| 146 |
let obj1 = new TestObject(); |
| 147 |
let obj2 = new TestObject(); |
| 148 |
let obj3 = new TestObject(); |
| 149 |
let handler1 = new TestHandler(); |
| 150 |
let handler2 = new TestHandler(); |
| 151 |
let handler3 = new TestHandler(); |
| 152 |
obj1.one.connect(handler1.onOne, handler1); |
| 153 |
obj2.one.connect(handler2.onOne, handler2); |
| 154 |
obj1.one.connect(handler3.onOne, handler3); |
| 155 |
obj2.one.connect(handler3.onOne, handler3); |
| 156 |
obj3.one.connect(handler3.onOne, handler3); |
| 157 |
let d1 = obj1.one.disconnect(handler1.onOne, handler1); |
| 158 |
let d2 = obj1.one.disconnect(handler1.onOne, handler1); |
| 159 |
let d3 = obj2.one.disconnect(handler3.onOne, handler3); |
| 160 |
obj1.one.emit(undefined); |
| 161 |
obj2.one.emit(undefined); |
| 162 |
obj3.one.emit(undefined); |
| 163 |
expect(d1).to.equal(true); |
| 164 |
expect(d2).to.equal(false); |
| 165 |
expect(d3).to.equal(true); |
| 166 |
expect(handler1.oneCount).to.equal(0); |
| 167 |
expect(handler2.oneCount).to.equal(1); |
| 168 |
expect(handler3.oneCount).to.equal(2); |
| 169 |
}); |
| 170 |
|
| 171 |
it('should handle disconnecting sender after receiver', () => { |
| 172 |
let obj = new TestObject(); |
| 173 |
let handler = new TestHandler(); |
| 174 |
obj.one.connect(handler.onOne, handler); |
| 175 |
Signal.disconnectReceiver(handler); |
| 176 |
Signal.disconnectSender(obj); |
| 177 |
obj.one.emit(undefined); |
| 178 |
expect(handler.oneCount).to.equal(0); |
| 179 |
}); |
| 180 |
|
| 181 |
it('should handle disconnecting receiver after sender', () => { |
| 182 |
let obj = new TestObject(); |
| 183 |
let handler = new TestHandler(); |
| 184 |
obj.one.connect(handler.onOne, handler); |
| 185 |
Signal.disconnectSender(obj); |
| 186 |
Signal.disconnectReceiver(handler); |
| 187 |
obj.one.emit(undefined); |
| 188 |
expect(handler.oneCount).to.equal(0); |
| 189 |
}); |
| 190 |
}); |
| 191 |
|
| 192 |
describe('#emit()', () => { |
| 193 |
it('should be a no-op if there are no connection', () => { |
| 194 |
let obj = new TestObject(); |
| 195 |
expect(() => { |
| 196 |
obj.one.emit(undefined); |
| 197 |
}).to.not.throw(Error); |
| 198 |
}); |
| 199 |
|
| 200 |
it('should pass the sender and args to the handlers', () => { |
| 201 |
let obj = new TestObject(); |
| 202 |
let handler1 = new TestHandler(); |
| 203 |
let handler2 = new TestHandler(); |
| 204 |
obj.two.connect(handler1.onTwo, handler1); |
| 205 |
obj.two.connect(handler2.onTwo, handler2); |
| 206 |
obj.two.emit(15); |
| 207 |
expect(handler1.twoSender).to.equal(obj); |
| 208 |
expect(handler2.twoSender).to.equal(obj); |
| 209 |
expect(handler1.twoValue).to.equal(15); |
| 210 |
expect(handler2.twoValue).to.equal(15); |
| 211 |
}); |
| 212 |
|
| 213 |
it('should invoke handlers in connection order', () => { |
| 214 |
let obj = new TestObject(); |
| 215 |
let handler1 = new TestHandler(); |
| 216 |
let handler2 = new TestHandler(); |
| 217 |
let handler3 = new TestHandler(); |
| 218 |
handler1.name = 'foo'; |
| 219 |
handler2.name = 'bar'; |
| 220 |
handler3.name = 'baz'; |
| 221 |
obj.three.connect(handler1.onThree, handler1); |
| 222 |
obj.one.connect(handler1.onOne, handler1); |
| 223 |
obj.three.connect(handler2.onThree, handler2); |
| 224 |
obj.three.connect(handler3.onThree, handler3); |
| 225 |
let names: string[] = []; |
| 226 |
obj.three.emit(names); |
| 227 |
obj.one.emit(undefined); |
| 228 |
expect(names).to.deep.equal(['foo', 'bar', 'baz']); |
| 229 |
expect(handler1.oneCount).to.equal(1); |
| 230 |
expect(handler2.oneCount).to.equal(0); |
| 231 |
}); |
| 232 |
|
| 233 |
it('should catch any exceptions in handlers', () => { |
| 234 |
let obj = new TestObject(); |
| 235 |
let handler1 = new TestHandler(); |
| 236 |
let handler2 = new TestHandler(); |
| 237 |
let handler3 = new TestHandler(); |
| 238 |
handler1.name = 'foo'; |
| 239 |
handler2.name = 'bar'; |
| 240 |
handler3.name = 'baz'; |
| 241 |
obj.three.connect(handler1.onThree, handler1); |
| 242 |
obj.three.connect(handler2.onThrow, handler2); |
| 243 |
obj.three.connect(handler3.onThree, handler3); |
| 244 |
let threw = false; |
| 245 |
let names1: string[] = []; |
| 246 |
try { |
| 247 |
obj.three.emit(names1); |
| 248 |
} catch (e) { |
| 249 |
threw = true; |
| 250 |
} |
| 251 |
expect(threw).to.equal(false); |
| 252 |
expect(names1).to.deep.equal(['foo', 'baz']); |
| 253 |
}); |
| 254 |
|
| 255 |
it('should not invoke signals added during emission', () => { |
| 256 |
let obj = new TestObject(); |
| 257 |
let handler1 = new TestHandler(); |
| 258 |
let handler2 = new TestHandler(); |
| 259 |
let handler3 = new TestHandler(); |
| 260 |
handler1.name = 'foo'; |
| 261 |
handler2.name = 'bar'; |
| 262 |
handler3.name = 'baz'; |
| 263 |
let adder = { |
| 264 |
add: () => { |
| 265 |
obj.three.connect(handler3.onThree, handler3); |
| 266 |
} |
| 267 |
}; |
| 268 |
obj.three.connect(handler1.onThree, handler1); |
| 269 |
obj.three.connect(handler2.onThree, handler2); |
| 270 |
obj.three.connect(adder.add, adder); |
| 271 |
let names1: string[] = []; |
| 272 |
obj.three.emit(names1); |
| 273 |
obj.three.disconnect(adder.add, adder); |
| 274 |
let names2: string[] = []; |
| 275 |
obj.three.emit(names2); |
| 276 |
expect(names1).to.deep.equal(['foo', 'bar']); |
| 277 |
expect(names2).to.deep.equal(['foo', 'bar', 'baz']); |
| 278 |
}); |
| 279 |
|
| 280 |
it('should not invoke signals removed during emission', () => { |
| 281 |
let obj = new TestObject(); |
| 282 |
let handler1 = new TestHandler(); |
| 283 |
let handler2 = new TestHandler(); |
| 284 |
let handler3 = new TestHandler(); |
| 285 |
handler1.name = 'foo'; |
| 286 |
handler2.name = 'bar'; |
| 287 |
handler3.name = 'baz'; |
| 288 |
let remover = { |
| 289 |
remove: () => { |
| 290 |
obj.three.disconnect(handler3.onThree, handler3); |
| 291 |
} |
| 292 |
}; |
| 293 |
obj.three.connect(handler1.onThree, handler1); |
| 294 |
obj.three.connect(handler2.onThree, handler2); |
| 295 |
obj.three.connect(remover.remove, remover); |
| 296 |
obj.three.connect(handler3.onThree, handler3); |
| 297 |
let names: string[] = []; |
| 298 |
obj.three.emit(names); |
| 299 |
expect(names).to.deep.equal(['foo', 'bar']); |
| 300 |
}); |
| 301 |
}); |
| 302 |
|
| 303 |
describe('.disconnectBetween()', () => { |
| 304 |
it('should clear all connections between a sender and receiver', () => { |
| 305 |
let obj = new TestObject(); |
| 306 |
let handler1 = new TestHandler(); |
| 307 |
let handler2 = new TestHandler(); |
| 308 |
obj.one.connect(handler1.onOne, handler1); |
| 309 |
obj.one.connect(handler2.onOne, handler2); |
| 310 |
obj.two.connect(handler1.onTwo, handler1); |
| 311 |
obj.two.connect(handler2.onTwo, handler2); |
| 312 |
obj.one.emit(undefined); |
| 313 |
expect(handler1.oneCount).to.equal(1); |
| 314 |
expect(handler2.oneCount).to.equal(1); |
| 315 |
obj.two.emit(42); |
| 316 |
expect(handler1.twoValue).to.equal(42); |
| 317 |
expect(handler2.twoValue).to.equal(42); |
| 318 |
Signal.disconnectBetween(obj, handler1); |
| 319 |
obj.one.emit(undefined); |
| 320 |
expect(handler1.oneCount).to.equal(1); |
| 321 |
expect(handler2.oneCount).to.equal(2); |
| 322 |
obj.two.emit(7); |
| 323 |
expect(handler1.twoValue).to.equal(42); |
| 324 |
expect(handler2.twoValue).to.equal(7); |
| 325 |
}); |
| 326 |
|
| 327 |
it('should be a no-op if the sender or receiver is not connected', () => { |
| 328 |
expect(() => Signal.disconnectBetween({}, {})).to.not.throw(Error); |
| 329 |
}); |
| 330 |
}); |
| 331 |
|
| 332 |
describe('.disconnectSender()', () => { |
| 333 |
it('should disconnect all signals from a specific sender', () => { |
| 334 |
let obj1 = new TestObject(); |
| 335 |
let obj2 = new TestObject(); |
| 336 |
let handler1 = new TestHandler(); |
| 337 |
let handler2 = new TestHandler(); |
| 338 |
obj1.one.connect(handler1.onOne, handler1); |
| 339 |
obj1.one.connect(handler2.onOne, handler2); |
| 340 |
obj2.one.connect(handler1.onOne, handler1); |
| 341 |
obj2.one.connect(handler2.onOne, handler2); |
| 342 |
Signal.disconnectSender(obj1); |
| 343 |
obj1.one.emit(undefined); |
| 344 |
obj2.one.emit(undefined); |
| 345 |
expect(handler1.oneCount).to.equal(1); |
| 346 |
expect(handler2.oneCount).to.equal(1); |
| 347 |
}); |
| 348 |
|
| 349 |
it('should be a no-op if the sender is not connected', () => { |
| 350 |
expect(() => Signal.disconnectSender({})).to.not.throw(Error); |
| 351 |
}); |
| 352 |
}); |
| 353 |
|
| 354 |
describe('.disconnectReceiver()', () => { |
| 355 |
it('should disconnect all signals from a specific receiver', () => { |
| 356 |
let obj1 = new TestObject(); |
| 357 |
let obj2 = new TestObject(); |
| 358 |
let handler1 = new TestHandler(); |
| 359 |
let handler2 = new TestHandler(); |
| 360 |
obj1.one.connect(handler1.onOne, handler1); |
| 361 |
obj1.one.connect(handler2.onOne, handler2); |
| 362 |
obj2.one.connect(handler1.onOne, handler1); |
| 363 |
obj2.one.connect(handler2.onOne, handler2); |
| 364 |
obj2.two.connect(handler1.onTwo, handler1); |
| 365 |
obj2.two.connect(handler2.onTwo, handler2); |
| 366 |
Signal.disconnectReceiver(handler1); |
| 367 |
obj1.one.emit(undefined); |
| 368 |
obj2.one.emit(undefined); |
| 369 |
obj2.two.emit(42); |
| 370 |
expect(handler1.oneCount).to.equal(0); |
| 371 |
expect(handler2.oneCount).to.equal(2); |
| 372 |
expect(handler1.twoValue).to.equal(0); |
| 373 |
expect(handler2.twoValue).to.equal(42); |
| 374 |
}); |
| 375 |
|
| 376 |
it('should be a no-op if the receiver is not connected', () => { |
| 377 |
expect(() => Signal.disconnectReceiver({})).to.not.throw(Error); |
| 378 |
}); |
| 379 |
}); |
| 380 |
|
| 381 |
describe('.disconnectAll()', () => { |
| 382 |
it('should clear all connections for an object', () => { |
| 383 |
let counter = 0; |
| 384 |
let onCount = () => { |
| 385 |
counter++; |
| 386 |
}; |
| 387 |
let ext1 = new ExtendedObject(); |
| 388 |
let ext2 = new ExtendedObject(); |
| 389 |
ext1.one.connect(ext1.onNotify, ext1); |
| 390 |
ext1.one.connect(ext2.onNotify, ext2); |
| 391 |
ext1.one.connect(onCount); |
| 392 |
ext2.one.connect(ext1.onNotify, ext1); |
| 393 |
ext2.one.connect(ext2.onNotify, ext2); |
| 394 |
ext2.one.connect(onCount); |
| 395 |
Signal.disconnectAll(ext1); |
| 396 |
ext1.one.emit(undefined); |
| 397 |
ext2.one.emit(undefined); |
| 398 |
expect(ext1.notifyCount).to.equal(0); |
| 399 |
expect(ext2.notifyCount).to.equal(1); |
| 400 |
expect(counter).to.equal(1); |
| 401 |
}); |
| 402 |
}); |
| 403 |
|
| 404 |
describe('.clearData()', () => { |
| 405 |
it('should clear all signal data associated with an object', () => { |
| 406 |
let counter = 0; |
| 407 |
let onCount = () => { |
| 408 |
counter++; |
| 409 |
}; |
| 410 |
let ext1 = new ExtendedObject(); |
| 411 |
let ext2 = new ExtendedObject(); |
| 412 |
ext1.one.connect(ext1.onNotify, ext1); |
| 413 |
ext1.one.connect(ext2.onNotify, ext2); |
| 414 |
ext1.one.connect(onCount); |
| 415 |
ext2.one.connect(ext1.onNotify, ext1); |
| 416 |
ext2.one.connect(ext2.onNotify, ext2); |
| 417 |
ext2.one.connect(onCount); |
| 418 |
Signal.clearData(ext1); |
| 419 |
ext1.one.emit(undefined); |
| 420 |
ext2.one.emit(undefined); |
| 421 |
expect(ext1.notifyCount).to.equal(0); |
| 422 |
expect(ext2.notifyCount).to.equal(1); |
| 423 |
expect(counter).to.equal(1); |
| 424 |
}); |
| 425 |
}); |
| 426 |
|
| 427 |
describe('.getExceptionHandler()', () => { |
| 428 |
it('should default to an exception handler', () => { |
| 429 |
expect(Signal.getExceptionHandler()).to.be.a('function'); |
| 430 |
}); |
| 431 |
}); |
| 432 |
|
| 433 |
describe('.setExceptionHandler()', () => { |
| 434 |
afterEach(() => { |
| 435 |
Signal.setExceptionHandler(console.error); |
| 436 |
}); |
| 437 |
|
| 438 |
it('should set the exception handler', () => { |
| 439 |
let handler = (err: Error) => { |
| 440 |
console.error(err); |
| 441 |
}; |
| 442 |
Signal.setExceptionHandler(handler); |
| 443 |
expect(Signal.getExceptionHandler()).to.equal(handler); |
| 444 |
}); |
| 445 |
|
| 446 |
it('should return the old exception handler', () => { |
| 447 |
let handler = (err: Error) => { |
| 448 |
console.error(err); |
| 449 |
}; |
| 450 |
let old1 = Signal.setExceptionHandler(handler); |
| 451 |
let old2 = Signal.setExceptionHandler(old1); |
| 452 |
expect(old1).to.equal(console.error); |
| 453 |
expect(old2).to.equal(handler); |
| 454 |
}); |
| 455 |
|
| 456 |
it('should invoke the exception handler on a slot exception', () => { |
| 457 |
let called = false; |
| 458 |
let obj = new TestObject(); |
| 459 |
let handler = new TestHandler(); |
| 460 |
obj.one.connect(handler.onThrow, handler); |
| 461 |
Signal.setExceptionHandler(() => { |
| 462 |
called = true; |
| 463 |
}); |
| 464 |
expect(called).to.equal(false); |
| 465 |
obj.one.emit(undefined); |
| 466 |
expect(called).to.equal(true); |
| 467 |
}); |
| 468 |
}); |
| 469 |
}); |
| 470 |
|
| 471 |
describe('Stream', () => { |
| 472 |
describe('#[Symbol.asyncIterator]()', () => { |
| 473 |
it('should yield emissions', async () => { |
| 474 |
const stream = new Stream<unknown, string>({}); |
| 475 |
const input = 'async'; |
| 476 |
const expected = 'aINTERRUPTEDsync'; |
| 477 |
const wait = Promise.resolve(); |
| 478 |
let emitted = ''; |
| 479 |
let once = true; |
| 480 |
stream.connect(() => { |
| 481 |
if (once) { |
| 482 |
once = false; |
| 483 |
stream.emit('I'); |
| 484 |
stream.emit('N'); |
| 485 |
stream.emit('T'); |
| 486 |
stream.emit('E'); |
| 487 |
stream.emit('R'); |
| 488 |
stream.emit('R'); |
| 489 |
stream.emit('U'); |
| 490 |
stream.emit('P'); |
| 491 |
stream.emit('T'); |
| 492 |
stream.emit('E'); |
| 493 |
stream.emit('D'); |
| 494 |
} |
| 495 |
}); |
| 496 |
input.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 497 |
wait.then(() => stream.stop()); |
| 498 |
for await (const letter of stream) { |
| 499 |
emitted = emitted.concat(letter); |
| 500 |
} |
| 501 |
expect(emitted).to.equal(expected); |
| 502 |
}); |
| 503 |
|
| 504 |
it('should return an async iterator', async () => { |
| 505 |
const stream = new Stream<unknown, string>({}); |
| 506 |
const input = 'iterator'; |
| 507 |
const expected = 'iAHEMterator'; |
| 508 |
const wait = Promise.resolve(); |
| 509 |
let emitted = ''; |
| 510 |
let once = true; |
| 511 |
stream.connect(() => { |
| 512 |
if (once) { |
| 513 |
once = false; |
| 514 |
stream.emit('A'); |
| 515 |
stream.emit('H'); |
| 516 |
stream.emit('E'); |
| 517 |
stream.emit('M'); |
| 518 |
} |
| 519 |
}); |
| 520 |
input.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 521 |
wait.then(() => stream.stop()); |
| 522 |
|
| 523 |
const it = stream[Symbol.asyncIterator](); |
| 524 |
let emission: IteratorResult<string, any>; |
| 525 |
while (!(emission = await it.next()).done) { |
| 526 |
emitted = emitted.concat(emission.value); |
| 527 |
} |
| 528 |
|
| 529 |
expect(emitted).to.equal(expected); |
| 530 |
}); |
| 531 |
}); |
| 532 |
|
| 533 |
describe('#stop()', () => { |
| 534 |
it('should stop emissions in the async interable', async () => { |
| 535 |
const stream = new Stream<unknown, string>({}); |
| 536 |
const one = 'alpha'; |
| 537 |
const two = 'beta'; |
| 538 |
const three = 'delta'; |
| 539 |
const expected = 'aINTERRUPTEDlphadelta'; |
| 540 |
const wait = Promise.resolve(); |
| 541 |
let emitted = ''; |
| 542 |
let once = true; |
| 543 |
|
| 544 |
stream.connect(() => { |
| 545 |
if (once) { |
| 546 |
once = false; |
| 547 |
stream.emit('I'); |
| 548 |
stream.emit('N'); |
| 549 |
stream.emit('T'); |
| 550 |
stream.emit('E'); |
| 551 |
stream.emit('R'); |
| 552 |
stream.emit('R'); |
| 553 |
stream.emit('U'); |
| 554 |
stream.emit('P'); |
| 555 |
stream.emit('T'); |
| 556 |
stream.emit('E'); |
| 557 |
stream.emit('D'); |
| 558 |
} |
| 559 |
}); |
| 560 |
|
| 561 |
one.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 562 |
wait.then(() => stream.stop()); |
| 563 |
|
| 564 |
// These should not be collected because the iterator has stopped. |
| 565 |
two.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 566 |
wait.then(() => stream.stop()); |
| 567 |
|
| 568 |
for await (const letter of stream) { |
| 569 |
emitted = emitted.concat(letter); |
| 570 |
} |
| 571 |
|
| 572 |
// These should be collected because there is a new iterator. |
| 573 |
three.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 574 |
wait.then(() => stream.stop()); |
| 575 |
|
| 576 |
for await (const letter of stream) { |
| 577 |
emitted = emitted.concat(letter); |
| 578 |
} |
| 579 |
|
| 580 |
expect(emitted).to.equal(expected); |
| 581 |
}); |
| 582 |
|
| 583 |
it('should resolve to `done` in an async iterator', async () => { |
| 584 |
const stream = new Stream<unknown, string>({}); |
| 585 |
const input = 'stopiterator'; |
| 586 |
const expected = 'sAHEMtopiterator'; |
| 587 |
const wait = Promise.resolve(); |
| 588 |
let emitted = ''; |
| 589 |
let once = true; |
| 590 |
stream.connect(() => { |
| 591 |
if (once) { |
| 592 |
once = false; |
| 593 |
stream.emit('A'); |
| 594 |
stream.emit('H'); |
| 595 |
stream.emit('E'); |
| 596 |
stream.emit('M'); |
| 597 |
} |
| 598 |
}); |
| 599 |
input.split('').forEach(x => wait.then(() => stream.emit(x))); |
| 600 |
wait.then(() => stream.stop()); |
| 601 |
|
| 602 |
const it = stream[Symbol.asyncIterator](); |
| 603 |
let emission: IteratorResult<string, any>; |
| 604 |
while (!(emission = await it.next()).done) { |
| 605 |
emitted = emitted.concat(emission.value); |
| 606 |
} |
| 607 |
|
| 608 |
expect(emitted).to.equal(expected); |
| 609 |
}); |
| 610 |
}); |
| 611 |
}); |
| 612 |
}); |