-
-
Save LevelbossMike/1bc630348f9890e68de73d551e396238 to your computer and use it in GitHub Desktop.
Child components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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