-
-
Save cowboyd/0afe663a10276e05a74a04c06d56d37b to your computer and use it in GitHub Desktop.
Messing around with Effection. Trying to create a scope, which is torn down with the component. And trying to run a task in that scope in response to user interaction.
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 { registerDestructor } from '@ember/destroyable'; | |
import { action } from '@ember/object'; | |
import { tracked } from '@glimmer/tracking'; | |
import { createScope, expect, useAbortSignal } from 'effection'; | |
export default class ProductSearchComponent extends Component { | |
@tracked query = ''; | |
constructor(...args) { | |
super(...args); | |
console.log('Creating ProductSearch'); | |
let [_scope, destroy] = createScope(); | |
this.scope = _scope; | |
registerDestructor(this, () => { | |
destroy().finally(() => console.log('destroyed')); | |
console.log('Destroying ProductSearch'); | |
}); | |
} | |
@action | |
doSearch(e) { | |
this.query = e.target.value; | |
const url = `https://dummyjson.com/products/search?q=${this.query}`; | |
this.scope.run(function* () { | |
console.log('Running in scope...', { url }); | |
let signal = yield* useAbortSignal(); | |
const response = yield* expect(fetch(url, { | |
signal, | |
})); | |
const result = yield* expect(response.json()); | |
console.log({ response, result }); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment