From 4e48f377fa5ebedb9d7d04e987b3b7c9a0ed523c Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:16:33 -0500 Subject: [PATCH] Allow link to receive owner directly --- pnpm-lock.yaml | 15 +++++++++------ reactiveweb/src/link.ts | 10 ++++++++++ tests/test-app/package.json | 4 ++-- tests/test-app/tests/utils/link-test.ts | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 82ec798..7eea2da 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -364,6 +364,9 @@ importers: msw: specifier: ^1.3.2 version: 1.3.2(typescript@5.2.2) + reactiveweb: + specifier: workspace:* + version: file:reactiveweb(@ember/test-waiters@3.1.0)(@glimmer/component@1.1.2)(@glimmer/tracking@1.1.2)(@glint/template@1.2.1)(ember-concurrency@3.1.1)(ember-source@5.4.0) devDependencies: '@babel/core': specifier: ^7.23.2 @@ -512,9 +515,6 @@ importers: qunit-dom: specifier: ^3.0.0 version: 3.0.0 - reactiveweb: - specifier: workspace:* - version: file:reactiveweb(@ember/test-waiters@3.1.0)(@glimmer/component@1.1.2)(@glimmer/tracking@1.1.2)(@glint/template@1.2.1)(ember-concurrency@3.1.1)(ember-source@5.4.0) stylelint: specifier: ^15.11.0 version: 15.11.0(typescript@5.2.2) @@ -6993,6 +6993,7 @@ packages: ember-source: 5.4.0(@babel/core@7.23.7)(@glimmer/component@1.1.2)(@glint/template@1.2.1)(webpack@5.89.0) transitivePeerDependencies: - supports-color + dev: false /ember-async-data@1.0.3(ember-source@5.5.0): resolution: {integrity: sha512-54OtoQwNi+/ZvPOVuT4t8fcHR9xL8N7kBydzcZSo6BIEsLYeXPi3+jUR8niWjfjXXhKlJ8EWXR0lTeHleTrxbw==} @@ -7775,6 +7776,7 @@ packages: transitivePeerDependencies: - '@babel/core' - supports-color + dev: false /ember-functions-as-helper-polyfill@2.1.2(ember-source@3.28.12): resolution: {integrity: sha512-yvW6xykvZEIYzzwlrC/g9yu6LtLkkj5F+ho6U+BDxN1uREMgoMOZnji7sSILn5ITVpaJ055DPcO+utEFD7IZOA==} @@ -7984,6 +7986,7 @@ packages: ember-source: 5.4.0(@babel/core@7.23.7)(@glimmer/component@1.1.2)(@glint/template@1.2.1)(webpack@5.89.0) transitivePeerDependencies: - supports-color + dev: false /ember-resources@6.4.2(@ember/test-waiters@3.1.0)(@glimmer/component@1.1.2)(@glimmer/tracking@1.1.2)(@glint/template@1.2.1)(ember-source@5.5.0): resolution: {integrity: sha512-B1OLdSpekdiyk/yfHc+aJF3ovI6rskwJOLzd+iX60RTyicO9Nsjw8LU9XI95uk4AOqO0MbSXogQ/MsN/mIPzOw==} @@ -15687,7 +15690,7 @@ packages: id: file:reactiveweb name: reactiveweb peerDependencies: - '@ember/test-waiters': ^3.1.0 + '@ember/test-waiters': '>= 3.1.0' ember-source: '*' dependencies: '@ember/test-waiters': 3.1.0 @@ -15708,7 +15711,7 @@ packages: id: file:reactiveweb name: reactiveweb peerDependencies: - '@ember/test-waiters': ^3.1.0 + '@ember/test-waiters': '>= 3.1.0' ember-source: '*' dependencies: '@ember/test-waiters': 3.1.0 @@ -15722,4 +15725,4 @@ packages: - '@glint/template' - ember-concurrency - supports-color - dev: true + dev: false diff --git a/reactiveweb/src/link.ts b/reactiveweb/src/link.ts index cd776bd..d0cca57 100644 --- a/reactiveweb/src/link.ts +++ b/reactiveweb/src/link.ts @@ -111,6 +111,16 @@ function directLink(child: object, parent: object) { let owner = getOwner(parent); + // the parent *is* the owner + // These checks are based on the `@public` interface of what + // an owner is + if (!owner && 'lookup' in parent && 'register' in parent && 'ownerInjection' in parent) { + // SAFETY: there is no JS-mode way to narrow an object + // to an owner. + // SEE: @ember/-internal/owner.d.ts + owner = parent as any; + } + if (owner) { setOwner(child, owner); } diff --git a/tests/test-app/package.json b/tests/test-app/package.json index 29da892..5733b88 100644 --- a/tests/test-app/package.json +++ b/tests/test-app/package.json @@ -24,7 +24,7 @@ "lint:prettier": "prettier -c .", "lint:prettier:fix": "prettier -w .", "_syncPnpm": "pnpm sync-dependencies-meta-injected", - "start": "concurrently 'ember serve' 'pnpm _syncPnpm --watch' --names 'tests serve,tests sync deps'", + "start": "concurrently 'ember serve' 'pnpm sync-dependencies-meta-injected --watch' --names 'tests serve,tests sync deps'", "test:ember": "pnpm _syncPnpm && ember test --test-port 0" }, "dependenciesMeta": { @@ -82,7 +82,6 @@ "prettier": "^3.0.3", "qunit": "^2.20.0", "qunit-dom": "^3.0.0", - "reactiveweb": "workspace:*", "stylelint": "^15.11.0", "stylelint-config-standard": "^34.0.0", "stylelint-prettier": "^4.0.2", @@ -101,6 +100,7 @@ "@nullvoxpopuli/eslint-configs": "^3.2.2", "ember-concurrency": "^3.1.1", "ember-resources": "^6.4.2", + "reactiveweb": "workspace:*", "msw": "^1.3.2" }, "msw": { diff --git a/tests/test-app/tests/utils/link-test.ts b/tests/test-app/tests/utils/link-test.ts index d1649eb..7b9ee5a 100644 --- a/tests/test-app/tests/utils/link-test.ts +++ b/tests/test-app/tests/utils/link-test.ts @@ -69,4 +69,18 @@ module('link', function (hooks) { assert.strictEqual(demo.foo.foo, 2); }); + + test('can be passed the owner', async function (assert) { + this.owner.register('service:foo', FooService); + + class Demo { + @service declare foo: FooService; + } + + let demo = new Demo(); + + link(demo, this.owner); + + assert.strictEqual(demo.foo.foo, 2); + }); });