Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LevelbossMike/1bc630348f9890e68de73d551e396238 to your computer and use it in GitHub Desktop.
Save LevelbossMike/1bc630348f9890e68de73d551e396238 to your computer and use it in GitHub Desktop.
Child components
import Component from '@glimmer/component';
import { action } from '@ember/object';
export default class extends Component {
get context() {
return JSON.stringify(this.args.machine.machine.context)
}
@action
updateContext() {
this.args.machine
.onTransition((state) => console.log('CHILD STATE: ', state))
.send("UPDATE_CONTEXT")
}
}
import Component from '@glimmer/component';
import { matchesState, useMachine } from 'ember-statecharts';
import { createMachine, send, assign, spawn } from 'xstate'
import { action } from '@ember/object'
import { use } from 'ember-usable';
const counterMachine = createMachine({
id: 'timer',
initial: 'active',
context: {
count: 0,
},
states: {
active: {
entry: [send('INC', { delay: 1000 })],
on: {
INC: {
target: 'active',
actions: [
assign({
count: (context) => context.count + 1,
}),
],
},
},
},
},
});
const parentMachine = createMachine({
id: 'parent',
initial: 'active',
context: {
counterRef: null,
},
states: {
active: {
entry: [
assign({
counterRef: () => spawn(counterMachine, { sync: true }),
}),
],
},
},
});
export default class extends Component {
@use statechart = useMachine(parentMachine).onTransition((state) => console.log("PARENT STATE: ", state));
get parentCount() {
const counterRef = this.statechart.state.context.counterRef;
if (counterRef) {
return counterRef.state.context.count;
} else {
return 0;
}
}
}
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
<ParentMachine />
<br>
<br>
<h2>CHILD MACHINE</h2>
<div>
{{this.context}}
</div>
<button {{on "click" this.updateContext}}>UPDATE CONTEXT WITH NEW VALUE</button>
<h1>Parent Machine</h1>
<div>Child Count: {{this.parentCount}}</div>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-statecharts": "0.13.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment