Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Forked from nelstrom/product-search.hbs
Last active October 30, 2023 17:30
Show Gist options
  • Save cowboyd/0afe663a10276e05a74a04c06d56d37b to your computer and use it in GitHub Desktop.
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.
<div>
<h1>Product search</h1>
<input {{on 'input' this.doSearch}} value={{this.query}} />
</div>
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