Skip to content

Instantly share code, notes, and snippets.

@yarigpopov
Last active October 16, 2021 15:16
Show Gist options
  • Save yarigpopov/da9bc61d1e60c52d96d47977ab16d9f1 to your computer and use it in GitHub Desktop.
Save yarigpopov/da9bc61d1e60c52d96d47977ab16d9f1 to your computer and use it in GitHub Desktop.
<Async @trigger={{this.asyncThing}} as |async|>
<div>{{async.current.value}}</div>
<button
type="button"
class="
p-2 bg-gray-200 cursor-pointer rounded-md focus:outline-none
{{if async.isBusy "cursor-not-allowed"}}
"
{{on "click" async.trigger}}
>
click
</button>
</Async>
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { useMachine } from 'ember-statecharts';
import { use } from 'ember-usable';
import async from './async/-machine';
function noop() {}
export default class Async extends Component {
@use statechart = useMachine(async).withContext({
trigger: this.args.trigger,
onSuccess: this.args.onSuccess || noop,
onError: this.args.onError || noop,
});
get isIdle() {
return this.statechart.state.matches('idle');
}
get isBusy() {
return this.statechart.state.matches('busy');
}
get didSucceed() {
return this.statechart.state.matches('success');
}
get didError() {
return this.statechart.state.matches('error');
}
@action
trigger() {
this.statechart.send('DO');
}
}
{{yield (hash
trigger=this.trigger
current=this.statechart.state
isBusy=this.isBusy
isIdle=this.isIdle
didSucceed=this.didSucceed
didError=this.didError
)}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment