Skip to content

Commit 50a6240

Browse files
committed
Allow Event & AbortController to not exist
1 parent 7a880df commit 50a6240

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
},
2525
"scripts": {
26-
"prepack": "jest && npm run build",
26+
"prepack": "jest && npm run build && node tests/node.js",
2727
"dev": "parcel watch",
2828
"build": "parcel build",
2929
"test": "jest --watch"

src/index.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class InternalInstance {
248248
private globalHandlers = new Handlers()
249249
private resolved = null as Promise<Record<string, any>> | null
250250
private accumulations: Map<symbol | string, Array<symbol | string | Event>> = new Map();
251-
private eventAborter = new AbortController();
251+
private eventAborter = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
252252
child: null | InternalInstance = null
253253

254254
constructor(
@@ -334,14 +334,14 @@ class InternalInstance {
334334
// Not sure if we still need this if we have abort signals, and for what versions of Node/browsers?
335335
target.removeEventListener(event, this);
336336
}
337-
this.eventAborter.abort();
337+
this.eventAborter?.abort();
338338

339339
this.globalHandlers.reset();
340340
}
341341

342342
consume(stateGenerator: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)) {
343343
this.cleanup();
344-
this.eventAborter = new AbortController();
344+
this.eventAborter = typeof AbortController !== 'undefined' ? new AbortController() : undefined;
345345

346346
this.willEnter();
347347

@@ -374,7 +374,7 @@ class InternalInstance {
374374
}
375375

376376
for (const [event, target] of this.globalHandlers.eventsToListenTo) {
377-
target.addEventListener(event, this, { signal: this.eventAborter.signal } as AddEventListenerOptions);
377+
target.addEventListener(event, this, { signal: this.eventAborter?.signal } as AddEventListenerOptions);
378378
}
379379

380380
} else if (typeof initialReturn === 'function') {
@@ -474,7 +474,15 @@ class InternalInstance {
474474
}
475475
}
476476

477-
class MachineStateChangedEvent extends Event {
477+
const FallbackEvent = class Event {
478+
public readonly type: string;
479+
constructor(type: string, eventInitDict?: EventInit | undefined) {
480+
this.type = type;
481+
}
482+
} as typeof Event;
483+
const BaseEvent = typeof Event !== 'undefined' ? Event : FallbackEvent
484+
485+
class MachineStateChangedEvent extends BaseEvent {
478486
readonly value: string;
479487

480488
constructor(type: string, value: string) {
@@ -517,7 +525,7 @@ export function start(
517525
_aborter?.signal.dispatchEvent(new MachineStateChangedEvent('StateChanged', getCurrent() as string));
518526
},
519527
didChangeAccumulations() {
520-
_aborter?.signal.dispatchEvent(new Event('AccumulationsChanged'));
528+
_aborter?.signal.dispatchEvent(new BaseEvent('AccumulationsChanged'));
521529
},
522530
sendEvent(event, snapshotCount) {
523531
if (typeof snapshotCount === "number" && snapshotCount !== _changeCount) {

tests/node.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require('assert');
2+
const { start, on } = require("../dist/yieldmachine");
3+
4+
function Switch() {
5+
function* OFF() {
6+
yield on("FLICK", ON);
7+
}
8+
function* ON() {
9+
yield on("FLICK", OFF);
10+
}
11+
12+
return OFF;
13+
}
14+
15+
const machine = start(Switch);
16+
assert.ok(machine);
17+
assert.strictEqual(machine.current, "OFF");
18+
19+
machine.next("FLICK");
20+
assert.strictEqual(machine.current, "ON");
21+
assert.strictEqual(machine.changeCount, 1);
22+
23+
machine.next("FLICK");
24+
assert.strictEqual(machine.current, "OFF");
25+
assert.strictEqual(machine.changeCount, 2);

0 commit comments

Comments
 (0)