Skip to content

Commit

Permalink
Fix rendering a cell directly returned from a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jul 10, 2023
1 parent 257e664 commit bfc432b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
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

0 comments on commit bfc432b

Please sign in to comment.