Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering a cell directly returned from a resource #941

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/five-hounds-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"ember-resources": patch
---

Fix an issue with a new (not yet used feature) where Resources could directly return a `Cell`, and it would have its `.current` method automatically called when resolving the value of a Resource.

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

export const Now = resource(({ on }) => {
const now = cell(Date.now());
const timer = setInterval(() => now.set(Date.now()));

on.cleanup(() => clearInterval(timer));

return now;
});


<template>
It is: <time>{{Now}}</time>
</template>
```
5 changes: 5 additions & 0 deletions ember-resources/src/core/function-based/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { invokeHelper } from '@ember/helper';
import { capabilities as helperCapabilities } from '@ember/helper';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

import { Cell } from '../../util/cell';
import { INTERNAL } from './types';

import type {
Expand Down Expand Up @@ -130,6 +131,10 @@ class FunctionResourceManager {
return maybeValue();
}

if (maybeValue instanceof Cell) {
return maybeValue.current;
}

return maybeValue;
}

Expand Down
2 changes: 1 addition & 1 deletion ember-resources/src/util/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface GlintRenderable {
toHTML(): string;
}

class Cell<Value = unknown> implements GlintRenderable {
export class Cell<Value = unknown> implements GlintRenderable {
@tracked declare current: Value;

toHTML(): string {
Expand Down
12 changes: 10 additions & 2 deletions test-app/tests/core/function-resource/rendering-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { clearRender, render, settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';

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

module('Utils | (function) resource | rendering', function (hooks) {
setupRenderingTest(hooks);

test(`returning a cell renders the cell's value`, async function (assert) {
const StuckClock = resource(() => cell(2));

await render(<template>{{StuckClock}}</template>);

assert.dom().hasText('2');
});

module('lifecycle', function () {
module('direct rendering', function () {
test('when consuming tracked data', async function (assert) {
Expand Down Expand Up @@ -345,7 +353,7 @@ module('Utils | (function) resource | rendering', function (hooks) {
assert.verifySteps(['destroy 0']);
});

test('when gated by an if and conusming tracked data', async function (assert) {
test('when gated by an if and consuming tracked data', async function (assert) {
// reminder that destruction is async
let steps: string[] = [];
let step = (msg: string) => {
Expand Down