Skip to content

Commit

Permalink
Remove actor.onDone(...) (#3991)
Browse files Browse the repository at this point in the history
* Replace a lot of .onDone with .subscribe({ complete })

* Get rid of remaining .onDone() calls

* Fix types

* Remove onStop

* Remove stopListeners

* Add changeset
  • Loading branch information
davidkpiano committed May 19, 2023
1 parent fe6db14 commit 98db493
Show file tree
Hide file tree
Showing 22 changed files with 562 additions and 414 deletions.
14 changes: 14 additions & 0 deletions .changeset/smart-laws-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'xstate': major
---

The `actor.onDone(...)` method is removed. Use `actor.subscribe({ complete() {... } })` instead.

```diff
- actor.onDone(() => { ... })
+ actor.subscribe({
+ complete() {
+ // ...
+ }
+})
```
7 changes: 3 additions & 4 deletions docs/fr/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,9 @@ const pingPongMachine = createMachine({
}
}
});

interpret(pingPongMachine)
.onDone(() => done())
.start();
const actor = interpret(pingPongMachine);
actor.subscribe({ complete: () => done() });
actor.start();
```

## Invoking Observables <Badge text="4.6"/>
Expand Down
6 changes: 3 additions & 3 deletions docs/fr/guides/delays.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ const dynamicDelayMachine = createMachine({
}
});

const dynamicDelayService = interpret(dynamicDelayMachine)
.onDone(() => console.log('done!'))
.start();
const dynamicDelayService = interpret(dynamicDelayMachine);
dynamicDelayService.subscribe({ complete: () => console.log('done!') });
dynamicDelayService.start();

dynamicDelayService.send({
type: 'ACTIVATE',
Expand Down
7 changes: 3 additions & 4 deletions docs/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,9 @@ const pingPongMachine = createMachine({
}
}
});

interpret(pingPongMachine)
.onDone(() => done())
.start();
const actor = interpret(pingPongMachine);
actor.subscribe({ complete: () => done() });
actor.start();
```

## Invoking Observables <Badge text="4.6"/>
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/delays.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ const dynamicDelayMachine = createMachine({
}
});

const dynamicDelayService = interpret(dynamicDelayMachine)
.onDone(() => console.log('done!'))
.start();
const dynamicDelayService = interpret(dynamicDelayMachine);
dynamicDelayService.subscribe({ complete: () => console.log('done!') });
dynamicDelayService.start();

dynamicDelayService.send({
type: 'ACTIVATE',
Expand Down
7 changes: 3 additions & 4 deletions docs/zh/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,9 @@ const pingPongMachine = createMachine({
}
}
});

interpret(pingPongMachine)
.onDone(() => done())
.start();
const actor = interpret(pingPongMachine);
actor.subscribe({ complete: () => done() });
actor.start();
```

## 调用 Observables <Badge text="4.6"/>
Expand Down
6 changes: 3 additions & 3 deletions docs/zh/guides/delays.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ const dynamicDelayMachine = createMachine({
}
});

const dynamicDelayService = interpret(dynamicDelayMachine)
.onDone(() => console.log('done!'))
.start();
const dynamicDelayService = interpret(dynamicDelayMachine);
dynamicDelayService.subscribe({ complete: () => console.log('done!') });
dynamicDelayService.start();

dynamicDelayService.send({
type: 'ACTIVATE',
Expand Down
20 changes: 0 additions & 20 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,6 @@ export class Interpreter<
};
}

/**
* Adds a state listener that is notified when the statechart has reached its final state.
* @param listener The state listener
*/
public onDone(listener: EventListener<DoneEvent>): this {
if (this.status === ActorStatus.Stopped && this._doneEvent) {
listener(this._doneEvent);
} else {
this.observers.add({
complete: () => {
if (this._doneEvent) {
listener(this._doneEvent);
}
}
});
}

return this;
}

/**
* Starts the interpreter from the initial state
*/
Expand Down
26 changes: 14 additions & 12 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,13 +1372,15 @@ describe('entry/exit actions', () => {
}
});

interpret(parentMachine)
.onDone(() => {
const actor = interpret(parentMachine);
actor.subscribe({
complete: () => {
expect(exitCalled).toBeTruthy();
expect(childExitCalled).toBeTruthy();
done();
})
.start();
}
});
actor.start();
});
});

Expand Down Expand Up @@ -2443,9 +2445,9 @@ describe('forwardTo()', () => {
}
});

const service = interpret(parent)
.onDone(() => done())
.start();
const service = interpret(parent);
service.subscribe({ complete: () => done() });
service.start();

service.send({ type: 'EVENT', value: 42 });
});
Expand Down Expand Up @@ -2493,9 +2495,9 @@ describe('forwardTo()', () => {
}
});

const service = interpret(parent)
.onDone(() => done())
.start();
const service = interpret(parent);
service.subscribe({ complete: () => done() });
service.start();

service.send({ type: 'EVENT', value: 42 });
});
Expand Down Expand Up @@ -3112,7 +3114,7 @@ describe('raise', () => {

const service = interpret(machine).start();

service.onDone(() => done());
service.subscribe({ complete: () => done() });

// Ensures that the delayed self-event is sent when in the `b` state
service.send({ type: 'TO_B' });
Expand Down Expand Up @@ -3193,7 +3195,7 @@ describe('raise', () => {

const service = interpret(machine).start();

service.onDone(() => done());
service.subscribe({ complete: () => done() });

setTimeout(() => {
// didn't transition yet
Expand Down
Loading

0 comments on commit 98db493

Please sign in to comment.