Skip to content

Commit

Permalink
fix(lifecycle): support resources without a setup method
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jun 23, 2021
1 parent 899c2c0 commit d11e6fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion addon/-private/resources/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ function setupInstance(
let instance = new Class(owner, args);

associateDestroyableChild(cache, instance);
instance.setup();

if ('setup' in instance) {
instance.setup();
}

if ('teardown' in instance) {
registerDestructor(instance, () => instance.teardown());
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/use-resource-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ module('useResource', function () {

assert.verifySteps(['setup', 'update', 'update', 'teardown']);
});

test('setup is optional', async function (assert) {
class Doubler extends LifecycleResource<{ positional: [number] }> {
get num() {
return this.args.positional[0] * 2;
}
}

class Test {
@tracked count = 0;

data = useResource(this, Doubler, () => [this.count]);
}

let foo = new Test();

assert.equal(foo.data.num, 0);

foo.count = 3;
await settled();

assert.equal(foo.data.num, 6);

foo.count = 4;
await settled();

assert.equal(foo.data.num, 8);
});
});

module('in templates', function (hooks) {
Expand Down

0 comments on commit d11e6fc

Please sign in to comment.