Skip to content

Commit

Permalink
Merge pull request #1101 from NullVoxPopuli/require-an-owner
Browse files Browse the repository at this point in the history
Require an owner to exist to be wired through to the resource.
  • Loading branch information
NullVoxPopuli authored Jan 10, 2024
2 parents 7422ff0 + 443e4dc commit 1eab27f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
7 changes: 6 additions & 1 deletion ember-resources/src/function-based/immediate-invocation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-ignore
import { createCache, getValue } from '@glimmer/tracking/primitives/cache';
import { assert } from '@ember/debug';
import { associateDestroyableChild } from '@ember/destroyable';
// @ts-ignore
import { capabilities as helperCapabilities, invokeHelper, setHelperManager } from '@ember/helper';
Expand Down Expand Up @@ -178,4 +179,8 @@ type ResourceBlueprint<Value, Args> =
// semicolon

// Provide a singleton manager.
const ResourceInvokerFactory = (owner: Owner) => new ResourceInvokerManager(owner);
const ResourceInvokerFactory = (owner: Owner | undefined) => {
assert(`Cannot create resource without an owner`, owner);

return new ResourceInvokerManager(owner);
};
6 changes: 5 additions & 1 deletion ember-resources/src/function-based/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ function isReactive<Value>(maybe: unknown): maybe is Reactive<Value> {
return typeof maybe === 'object' && maybe !== null && CURRENT in maybe;
}

export const ResourceManagerFactory = (owner: Owner) => new FunctionResourceManager(owner);
export const ResourceManagerFactory = (owner: Owner | undefined) => {
assert(`Cannot create resource without an owner`, owner);

return new FunctionResourceManager(owner);
};
17 changes: 17 additions & 0 deletions test-app/tests/core/function-resource/clock-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ import { hash } from '@ember/helper';
import { clearRender, find, render, settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest, setupTest } from 'ember-qunit';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

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

import type Owner from '@ember/owner';

let setOwner: (context: unknown, owner: Owner) => void;

if (macroCondition(dependencySatisfies('ember-source', '>=4.12.0'))) {
// In no version of ember where `@ember/owner` tried to be imported did it exist
// if (macroCondition(false)) {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/application') as any).setOwner;
}

module('Examples | resource | Clock', function (hooks) {
let wait = (ms = 1_100) => new Promise((resolve) => setTimeout(resolve, ms));

Expand Down Expand Up @@ -58,6 +73,8 @@ module('Examples | resource | Clock', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

let timeA = foo.now;

await wait();
Expand Down
33 changes: 32 additions & 1 deletion test-app/tests/core/function-resource/js-test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { tracked } from '@glimmer/tracking';
import { setOwner } from '@ember/application';
import { destroy } from '@ember/destroyable';
import Service from '@ember/service';
import { settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

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

import type Owner from '@ember/owner';
import type QUnit from 'qunit';

let setOwner: (context: unknown, owner: Owner) => void;

if (macroCondition(dependencySatisfies('ember-source', '>=4.12.0'))) {
// In no version of ember where `@ember/owner` tried to be imported did it exist
// if (macroCondition(false)) {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/application') as any).setOwner;
}

module('Utils | (function) resource | js', function (hooks) {
setupTest(hooks);

Expand All @@ -31,6 +44,8 @@ module('Utils | (function) resource | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 0);
await settled();

Expand Down Expand Up @@ -66,6 +81,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('basics', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

destroy(foo);
Expand All @@ -77,6 +94,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('reactivity', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

foo.count = 2;
Expand Down Expand Up @@ -110,6 +129,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('async reactivity', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

foo.count = 2;
Expand Down Expand Up @@ -174,6 +195,8 @@ module('Utils | (function) resource | js', function (hooks) {

let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

foo.count = 2;
Expand Down Expand Up @@ -235,6 +258,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('basics', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

destroy(foo);
Expand All @@ -246,6 +271,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('reactivity', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

foo.count = 2;
Expand All @@ -265,6 +292,8 @@ module('Utils | (function) resource | js', function (hooks) {
test('async reactivity', async function (assert) {
let foo = new Test(assert);

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 1);

foo.count = 2;
Expand Down Expand Up @@ -310,6 +339,8 @@ module('Utils | (function) resource | js', function (hooks) {

setOwner(test, this.owner);

setOwner(test, this.owner);

assert.strictEqual(test.data, 1);

testService.count = 2;
Expand Down
23 changes: 23 additions & 0 deletions test-app/tests/core/function-resource/js-use-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ import { destroy, isDestroyed, registerDestructor } from '@ember/destroyable';
import { settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

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

import type Owner from '@ember/owner';

let setOwner: (context: unknown, owner: Owner) => void;

if (macroCondition(dependencySatisfies('ember-source', '>=4.12.0'))) {
// In no version of ember where `@ember/owner` tried to be imported did it exist
// if (macroCondition(false)) {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/application') as any).setOwner;
}

// not testing in template, because that's the easy part
module('Core | (function) resource + use | js', function (hooks) {
setupTest(hooks);
Expand All @@ -33,6 +46,8 @@ module('Core | (function) resource + use | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

assert.strictEqual(foo.data, 4);
});

Expand All @@ -56,6 +71,8 @@ module('Core | (function) resource + use | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

assert.strictEqual(foo.data.current, 0);

foo.count = 3;
Expand Down Expand Up @@ -86,6 +103,8 @@ module('Core | (function) resource + use | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

// destruction only occurs if the resource is constructor, which would be upon access
foo.data.current;

Expand Down Expand Up @@ -126,6 +145,8 @@ module('Core | (function) resource + use | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

foo.data.current;
foo.count = 4;
foo.data.current;
Expand Down Expand Up @@ -153,6 +174,8 @@ module('Core | (function) resource + use | js', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

assert.strictEqual(foo.data1, 'hello');
assert.strictEqual(foo.data2.current, 'hello');
});
Expand Down
38 changes: 37 additions & 1 deletion test-app/tests/core/function-resource/rendering-test.gts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { tracked } from '@glimmer/tracking';
import { setOwner } from '@ember/application';
import { destroy } from '@ember/destroyable';
import Service from '@ember/service';
import { clearRender, render, settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

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

import type Owner from '@ember/owner';

let setOwner: (context: unknown, owner: Owner) => void;

if (macroCondition(dependencySatisfies('ember-source', '>=4.12.0'))) {
// In no version of ember where `@ember/owner` tried to be imported did it exist
// if (macroCondition(false)) {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/owner') as any).setOwner;
} else {
// Using 'any' here because importSync can't lookup types correctly
setOwner = (importSync('@ember/application') as any).setOwner;
}

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

Expand All @@ -27,6 +41,9 @@ module('Utils | (function) resource | rendering', function (hooks) {
}

let foo = new Test();

setOwner(foo, this.owner);

// reminder that destruction is async
let steps: string[] = [];
let step = (msg: string) => {
Expand Down Expand Up @@ -71,6 +88,9 @@ module('Utils | (function) resource | rendering', function (hooks) {
}

let foo = new Test();

setOwner(foo, this.owner);

// reminder that destruction is async
let steps: string[] = [];
let step = (msg: string) => {
Expand Down Expand Up @@ -120,6 +140,8 @@ module('Utils | (function) resource | rendering', function (hooks) {
let inc = 0;
let foo = new Test();

setOwner(foo, this.owner);

let theResource = resource(({ on }) => {
let i = inc;

Expand Down Expand Up @@ -166,6 +188,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

let theResource = resource(({ on }) => {
let i = foo.num;

Expand Down Expand Up @@ -212,6 +236,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

let theResource = (_num: number) =>
resource(({ on }) => {
let i = foo.num;
Expand Down Expand Up @@ -278,6 +304,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

await render(<template><out>{{foo.theResource}}</out></template>);

assert.dom('out').containsText('0');
Expand Down Expand Up @@ -321,6 +349,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

await render(<template>
{{#if foo.show}}
<out>{{foo.theResource}}</out>
Expand Down Expand Up @@ -377,6 +407,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

await render(<template>
{{#if foo.show}}
<out>{{foo.theResource}}</out>
Expand Down Expand Up @@ -426,6 +458,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let foo = new Test();

setOwner(foo, this.owner);

await render(<template>
{{#let (Wrapper foo.num) as |state|}}
<out>{{state}}</out>
Expand Down Expand Up @@ -483,6 +517,8 @@ module('Utils | (function) resource | rendering', function (hooks) {

let testData = new Test();

setOwner(testData, this.owner);

setOwner(testData, this.owner);

await render(<template><out>{{testData.data}}</out></template>);
Expand Down

0 comments on commit 1eab27f

Please sign in to comment.