Skip to content

Commit 1efc411

Browse files
Merge pull request #1129 from NullVoxPopuli/issue-1128
Fix use types
2 parents 1b5940d + 98e4a85 commit 1efc411

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

ember-resources/src/function-based/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface InternalFunctionResourceConfig<Value = unknown> {
1010
[INTERNAL]: true;
1111
}
1212

13-
export const CURRENT = Symbol('ember-resources::CURRENT');
13+
export const CURRENT = Symbol('ember-resources::CURRENT') as unknown as 'CURRENT';
1414

1515
export interface GlintRenderable {
1616
/**
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import Component from '@glimmer/component';
2+
import { render } from '@ember/test-helpers';
3+
import { module, test } from 'qunit';
4+
import { setupRenderingTest } from 'ember-qunit';
5+
6+
import { cell, resource, use } from 'ember-resources';
7+
8+
import type { Reactive} from 'ember-resources';
9+
10+
type Cell<T> = ReturnType<typeof cell<T>>;
11+
12+
export type ClockNakedSignature = {
13+
percentage: Cell<number>;
14+
counter: Cell<number>;
15+
};
16+
17+
export type ClockSignature = Reactive<ClockNakedSignature>;
18+
19+
interface Signature {
20+
Args: {};
21+
Blocks: {
22+
default: [ClockNakedSignature];
23+
};
24+
Element: HTMLDivElement;
25+
}
26+
27+
const Clock = resource(() => {
28+
const counter = cell(0);
29+
const percentage = cell(0);
30+
31+
return { percentage, counter };
32+
});
33+
34+
35+
// use (the function) exposes a .current property, like a Cell
36+
class Refresher extends Component<Signature> {
37+
clock = use(this, Clock);
38+
39+
<template>{{yield this.clock.current}}</template>
40+
}
41+
42+
// with use (the decorator) the .current access is absorbed in an underlying getter
43+
class Refresher2 extends Component<Signature> {
44+
@use clock = Clock;
45+
46+
<template>{{yield this.clock}}</template>
47+
}
48+
49+
const keys = (o: Record<string, unknown>) => Object.keys(o).join(',');
50+
51+
module('issues/1128', function(hooks) {
52+
setupRenderingTest(hooks);
53+
54+
test('it works', async function (assert) {
55+
await render(<template>
56+
<Refresher as |r|>
57+
<output id="one-keys">{{keys r}}</output>
58+
<output id="one">{{r.percentage.current}}</output>
59+
</Refresher>
60+
<Refresher2 as |r|>
61+
<output id="two-keys">{{keys r}}</output>
62+
<output id="two">{{r.percentage.current}}</output>
63+
</Refresher2>
64+
</template>);
65+
66+
assert.dom('#one-keys').hasText('percentage,counter');
67+
assert.dom('#two-keys').hasText('percentage,counter');
68+
assert.dom('#one').hasText('0');
69+
assert.dom('#two').hasText('0');
70+
});
71+
72+
});

0 commit comments

Comments
 (0)