Skip to content

Commit

Permalink
[@xstate/lit] Defining onNext as an instance field
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarmarina committed Jun 26, 2024
1 parent fdddbda commit d44d4a1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { constants } = require('jest-config');
module.exports = {
prettierPath: null,
setupFilesAfterEnv: ['@xstate-repo/jest-utils/setup'],
transformIgnorePatterns: [
'node_modules/(?!(@open-wc|lit-html|lit-element|lit|@lit)/)'
],
transform: {
[constants.DEFAULT_JS_PATTERN]: 'babel-jest',
'^.+\\.vue$': '@vue/vue3-jest',
Expand Down
4 changes: 2 additions & 2 deletions packages/xstate-lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
},
"peerDependencies": {
"lit": "^3.1.2",
"xstate": "^5.9.1"
"xstate": "^5.14.0"
},
"devDependencies": {
"@open-wc/testing-helpers": "^3.0.0",
"@testing-library/dom": "^9.3.4",
"@types/jest": "^29.5.10",
"lit": "^3.1.2",
"xstate": "5.9.1"
"xstate": "5.14.0"
}
}
5 changes: 2 additions & 3 deletions packages/xstate-lit/src/UseMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class UseMachine<TMachine extends AnyStateMachine>
this.options = options;
this.callback = callback;
this.currentSnapshot = this.snapshot;
this.onNext = this.onNext.bind(this);

(this.host = host).addController(this);
}
Expand All @@ -57,13 +56,13 @@ export class UseMachine<TMachine extends AnyStateMachine>
this.subs.unsubscribe();
}

protected onNext(snapshot: SnapshotFrom<TMachine>) {
protected onNext = (snapshot: SnapshotFrom<TMachine>) => {
if (this.currentSnapshot !== snapshot) {
this.currentSnapshot = snapshot;
this.callback?.(snapshot);
this.host.requestUpdate();
}
}
};

private startService() {
this.actorRef = createActor(this.machine, this.options);
Expand Down
9 changes: 9 additions & 0 deletions scripts/jest-utils/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ afterEach(() => {
Object.keys(consoleSpies).forEach((method) => {
const spy = consoleSpies[method];

if (spy.mock.calls.length) {
spy.mock.calls = spy.mock.calls.filter(
(args) =>
!args.includes(
'Lit is in dev mode. Not recommended for production! See https://lit.dev/msg/dev-mode for more information.'
)
);
}

if (spy.mock.calls.length) {
const calls = spy.mock.calls;

Expand Down

0 comments on commit d44d4a1

Please sign in to comment.