Skip to content

Commit 6ec93a6

Browse files
Merge pull request #38 from NullVoxPopuli/make-setup-optional
fix(lifecycle): support resources without a setup method
2 parents 899c2c0 + d11e6fc commit 6ec93a6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

addon/-private/resources/lifecycle.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ function setupInstance(
7070
let instance = new Class(owner, args);
7171

7272
associateDestroyableChild(cache, instance);
73-
instance.setup();
73+
74+
if ('setup' in instance) {
75+
instance.setup();
76+
}
7477

7578
if ('teardown' in instance) {
7679
registerDestructor(instance, () => instance.teardown());

tests/unit/use-resource-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,34 @@ module('useResource', function () {
8686

8787
assert.verifySteps(['setup', 'update', 'update', 'teardown']);
8888
});
89+
90+
test('setup is optional', async function (assert) {
91+
class Doubler extends LifecycleResource<{ positional: [number] }> {
92+
get num() {
93+
return this.args.positional[0] * 2;
94+
}
95+
}
96+
97+
class Test {
98+
@tracked count = 0;
99+
100+
data = useResource(this, Doubler, () => [this.count]);
101+
}
102+
103+
let foo = new Test();
104+
105+
assert.equal(foo.data.num, 0);
106+
107+
foo.count = 3;
108+
await settled();
109+
110+
assert.equal(foo.data.num, 6);
111+
112+
foo.count = 4;
113+
await settled();
114+
115+
assert.equal(foo.data.num, 8);
116+
});
89117
});
90118

91119
module('in templates', function (hooks) {

0 commit comments

Comments
 (0)