-
-
Save nelstrom/96c335f188a0d1543fb9ae3a040cfbe8 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, 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(); | |
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* fetch(url, { | |
signal, | |
}); | |
const result = yield* response.json(); | |
console.log({ response, result }); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment