Last active
March 23, 2020 16:28
-
-
Save stravid/d6f266719b1a7fd9fe205b070ae7e79c to your computer and use it in GitHub Desktop.
Statechart Parent Child Activity Example
This file contains hidden or 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
const { Machine } = XState; | |
export default Machine({ | |
id: 'child', | |
initial: 'present', | |
context: { | |
name: undefined | |
}, | |
states: { | |
present: { | |
activities: ['nameShowing'] | |
} | |
} | |
}); |
This file contains hidden or 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 Ember from 'ember'; | |
export default Ember.Component.extend({ | |
didInsertElement() { | |
// The child machine is already interpreted, how can I define the activity | |
// `nameShowing` so the statechart drives the Ember component. | |
// Returns the passed in child service that got spawned in the parent. | |
console.log(this.get('childService')); | |
} | |
}); |
This file contains hidden or 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 Ember from 'ember'; | |
import parentMachine from '../parent-machine'; | |
const { interpret } = XState; | |
export default Ember.Component.extend({ | |
didInsertElement() { | |
const actualMachine = parentMachine.withConfig({ | |
activities: { | |
emptyListShowing: () => { | |
this.set('emberStateEmptyListShowing', true); | |
return () => { this.set('emberStateEmptyListShowing', false) }; | |
}, | |
listShowing: (context, event) => { | |
this.set('emberStateListShowing', true); | |
this.set('emberChildren', context.children); | |
return () => { this.set('emberStateListShowing', false) }; | |
}, | |
inputShowing: () => { | |
this.set('emberStateInputShowing', true); | |
return () => { this.set('emberStateInputShowing', false) }; | |
}, | |
} | |
}); | |
const parentService = interpret(actualMachine); | |
parentService.start(); | |
this.set('parentService', parentService); | |
}, | |
actions: { | |
emberSubmit() { | |
this.get('parentService').send({ | |
type: 'SUBMIT', | |
data: { name: this.get('emberChildName') } | |
}); | |
this.set('emberChildName', ''); | |
} | |
} | |
}); |
This file contains hidden or 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 childMachine from './child-machine'; | |
const { Machine, assign, spawn } = XState; | |
export default Machine({ | |
id: 'parent', | |
type: 'parallel', | |
context: { | |
children: [] | |
}, | |
states: { | |
list: { | |
initial: 'sad', | |
states: { | |
sad: { | |
on: { | |
SUBMIT: { target: 'temp', actions: ['addChild'] } | |
}, | |
activities: ['emptyListShowing'] | |
}, | |
happy: { | |
on: { | |
SUBMIT: { target: 'temp', actions: ['addChild'] } | |
}, | |
activities: ['listShowing'] | |
}, | |
temp: { | |
on: { | |
'': 'happy' | |
} | |
} | |
} | |
}, | |
form: { | |
initial: 'input', | |
states: { | |
input: { | |
activities: ['inputShowing'] | |
} | |
} | |
} | |
} | |
}, { | |
actions: { | |
addChild: assign({ | |
children: (context, event) => { | |
return context.children.concat({ | |
name: event.data.name, | |
ref: spawn(childMachine.withContext({ name: event.data.name })) | |
}); | |
} | |
}) | |
} | |
}); |
This file contains hidden or 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 { run } from '@ember/runloop'; | |
export default function destroyApp(application) { | |
run(application, 'destroy'); | |
} |
This file contains hidden or 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 Ember from 'ember'; | |
import Application from '../../app'; | |
import config from '../../config/environment'; | |
const { run } = Ember; | |
const assign = Ember.assign || Ember.merge; | |
export default function startApp(attrs) { | |
let application; | |
let attributes = assign({rootElement: "#test-root"}, config.APP); | |
attributes.autoboot = true; | |
attributes = assign(attributes, attrs); // use defaults, but you can override; | |
run(() => { | |
application = Application.create(attributes); | |
application.setupForTesting(); | |
application.injectTestHelpers(); | |
}); | |
return application; | |
} |
This file contains hidden or 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 Application from '../app'; | |
import config from '../config/environment'; | |
import { setApplication } from '@ember/test-helpers'; | |
import { start } from 'ember-qunit'; | |
import { assign } from '@ember/polyfills'; | |
let attributes = assign({ rootElement: '#main' }, config.APP); | |
setApplication(Application.create(attributes)); | |
start(); |
This file contains hidden or 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.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3", | |
"xstate": "https://unpkg.com/xstate@4/dist/xstate.js" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment