Skip to content

Commit

Permalink
Merge pull request #1129 from NullVoxPopuli/issue-1128
Browse files Browse the repository at this point in the history
Fix use types
  • Loading branch information
NullVoxPopuli authored May 3, 2024
2 parents 1b5940d + 98e4a85 commit 1efc411
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ember-resources/src/function-based/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface InternalFunctionResourceConfig<Value = unknown> {
[INTERNAL]: true;
}

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

export interface GlintRenderable {
/**
Expand Down
72 changes: 72 additions & 0 deletions test-app/tests/core/issue-1128-test.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Component from '@glimmer/component';
import { render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';

import { cell, resource, use } from 'ember-resources';

import type { Reactive} from 'ember-resources';

type Cell<T> = ReturnType<typeof cell<T>>;

export type ClockNakedSignature = {
percentage: Cell<number>;
counter: Cell<number>;
};

export type ClockSignature = Reactive<ClockNakedSignature>;

interface Signature {
Args: {};
Blocks: {
default: [ClockNakedSignature];
};
Element: HTMLDivElement;
}

const Clock = resource(() => {
const counter = cell(0);
const percentage = cell(0);

return { percentage, counter };
});


// use (the function) exposes a .current property, like a Cell
class Refresher extends Component<Signature> {
clock = use(this, Clock);

<template>{{yield this.clock.current}}</template>
}

// with use (the decorator) the .current access is absorbed in an underlying getter
class Refresher2 extends Component<Signature> {
@use clock = Clock;

<template>{{yield this.clock}}</template>
}

const keys = (o: Record<string, unknown>) => Object.keys(o).join(',');

module('issues/1128', function(hooks) {
setupRenderingTest(hooks);

test('it works', async function (assert) {
await render(<template>
<Refresher as |r|>
<output id="one-keys">{{keys r}}</output>
<output id="one">{{r.percentage.current}}</output>
</Refresher>
<Refresher2 as |r|>
<output id="two-keys">{{keys r}}</output>
<output id="two">{{r.percentage.current}}</output>
</Refresher2>
</template>);

assert.dom('#one-keys').hasText('percentage,counter');
assert.dom('#two-keys').hasText('percentage,counter');
assert.dom('#one').hasText('0');
assert.dom('#two').hasText('0');
});

});

0 comments on commit 1efc411

Please sign in to comment.