Skip to content

Commit

Permalink
feat: new default template
Browse files Browse the repository at this point in the history
The default template for all createMachine components is now
`{{yield this.state this.send}}` to remove common boilerplate
  • Loading branch information
NullVoxPopuli committed Apr 23, 2021
1 parent a7439eb commit e3be573
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export default createMachine({
},
});
```
```hbs
{{!-- app/components/toggle.hbs --}}
{{yield this.state this.send}}
```

Usage of this "toggle" component:

Expand All @@ -48,6 +44,13 @@ Usage of this "toggle" component:
</Toggle>
```

The default template for every `createMachine(..)` is
```hbs
{{yield this.state this.send}}
```
but that can be overriden to suit your needs by defining your own template.
the `this` is an instance of the [XState Interpreter](https://xstate.js.org/api/classes/interpreter.html)

### API

#### `@config`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { setComponentManager } from '@ember/component';
import { setComponentManager, setComponentTemplate } from '@ember/component';
import { hbs } from 'ember-cli-htmlbars';

import ComponentManager from 'ember-statechart-component/-private/statechart-manager';
import { StateNode } from 'xstate';

// Managers are managed globally, and not per app instance
setComponentManager((owner) => ComponentManager.create(owner), StateNode.prototype);
setComponentTemplate(hbs`{{yield this.state this.send}}`, StateNode.prototype);

export function initialize(): void {}
export function initialize(): void {
/* intentionally empty */
}

export default {
initialize,
Expand Down
99 changes: 39 additions & 60 deletions tests/integration/usage-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { setComponentTemplate } from '@ember/component';
import { clearRender, render } from '@ember/test-helpers';
import click from '@ember/test-helpers/dom/click';
import { hbs } from 'ember-cli-htmlbars';
Expand All @@ -15,18 +14,13 @@ module('Usage', function (hooks) {
setupRenderingTest(hooks);

test('it works', async function (assert) {
let toggle = setComponentTemplate(
hbs`
{{yield this.state this.send}}
`,
createMachine({
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } },
},
})
);
let toggle = createMachine({
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } },
},
});

this.owner.register('component:toggle-machine', toggle);

Expand All @@ -49,18 +43,13 @@ module('Usage', function (hooks) {
});

test('can pass config', async function (assert) {
let toggle = setComponentTemplate(
hbs`
{{yield this.state this.send}}
`,
createMachine({
initial: 'inactive',
states: {
inactive: { entry: 'increment', on: { TOGGLE: 'active' } },
active: { entry: 'increment', on: { TOGGLE: 'inactive' } },
},
})
);
let toggle = createMachine({
initial: 'inactive',
states: {
inactive: { entry: 'increment', on: { TOGGLE: 'active' } },
active: { entry: 'increment', on: { TOGGLE: 'inactive' } },
},
});

this.owner.register('component:toggle-machine', toggle);

Expand Down Expand Up @@ -92,31 +81,26 @@ module('Usage', function (hooks) {
});

test('can pass context', async function (assert) {
let toggle = setComponentTemplate(
hbs`
{{yield this.state this.send}}
`,
createMachine({
initial: 'inactive',
context: {
numCalled: 0,
let toggle = createMachine({
initial: 'inactive',
context: {
numCalled: 0,
},
states: {
inactive: {
entry: assign({
numCalled: (ctx: any) => ctx.numCalled + 1,
}),
on: { TOGGLE: 'active' },
},
states: {
inactive: {
entry: assign({
numCalled: (ctx: any) => ctx.numCalled + 1,
}),
on: { TOGGLE: 'active' },
},
active: {
entry: assign({
numCalled: (ctx: any) => ctx.numCalled + 1,
}),
on: { TOGGLE: 'inactive' },
},
active: {
entry: assign({
numCalled: (ctx: any) => ctx.numCalled + 1,
}),
on: { TOGGLE: 'inactive' },
},
})
);
},
});

this.owner.register('component:toggle-machine', toggle);

Expand Down Expand Up @@ -150,18 +134,13 @@ module('Usage', function (hooks) {
});

test('can pass initial state', async function (assert) {
let toggle = setComponentTemplate(
hbs`
{{yield this.state this.send}}
`,
createMachine({
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } },
},
})
);
let toggle = createMachine({
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } },
},
});

let previousState: State<unknown> | null = null;

Expand Down

0 comments on commit e3be573

Please sign in to comment.