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

[BUGFIX release] Fix ownerInjection when used to create services directly #18780

Merged
merged 1 commit into from
Feb 28, 2020
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
13 changes: 13 additions & 0 deletions packages/@ember/-internals/container/tests/container_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OWNER } from '@ember/-internals/owner';
import { assign } from '@ember/polyfills';
import { EMBER_MODULE_UNIFICATION } from '@ember/canary-features';
import Service from '@ember/service';
import { DEBUG } from '@glimmer/env';
import { Registry } from '..';
import { factory, moduleFor, AbstractTestCase, runTask } from 'internal-test-helpers';
Expand Down Expand Up @@ -501,6 +502,18 @@ moduleFor(
assert.equal(result[OWNER], owner, 'owner is properly included');
}

['@test ownerInjection should be usable to create a service for testing'](assert) {
assert.expect(0);

let owner = {};
let registry = new Registry();
let container = registry.container({ owner });

let result = container.ownerInjection();

Service.create(result);
}

['@test lookup passes options through to expandlocallookup'](assert) {
let registry = new Registry();
let container = registry.container();
Expand Down
37 changes: 20 additions & 17 deletions packages/@ember/-internals/runtime/lib/system/core_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const reopen = Mixin.prototype.reopen;
const wasApplied = new WeakSet();

const factoryMap = new WeakMap();
let debugOwnerMap;

if (DEBUG) {
debugOwnerMap = new WeakMap();
}

const prototypeMixinMap = new WeakMap();

Expand Down Expand Up @@ -283,19 +288,13 @@ class CoreObject {
assert(
`An EmberObject based class, ${this.constructor}, was not instantiated correctly. You may have either used \`new\` instead of \`.create()\`, or not passed arguments to your call to super in the constructor: \`super(...arguments)\`. If you are trying to use \`new\`, consider using native classes without extending from EmberObject.`,
(() => {
if (passedFromCreate === PASSED_FROM_CREATE) {
return true;
}

if (initFactory === undefined) {
return false;
}
let owner = debugOwnerMap.get(this.constructor);
debugOwnerMap.delete(this.constructor);

if (passedFromCreate === initFactory.owner) {
return true;
}

return false;
return (
passedFromCreate !== undefined &&
(passedFromCreate === PASSED_FROM_CREATE || passedFromCreate === owner)
);
})()
);

Expand Down Expand Up @@ -762,11 +761,15 @@ class CoreObject {
owner = getOwner(props);
}

if (owner === undefined) {
// fallback to passing the special PASSED_FROM_CREATE symbol
// to avoid an error when folks call things like Controller.extend().create()
// we should do a subsequent deprecation pass to ensure this isn't allowed
owner = PASSED_FROM_CREATE;
if (DEBUG) {
if (owner === undefined) {
// fallback to passing the special PASSED_FROM_CREATE symbol
// to avoid an error when folks call things like Controller.extend().create()
// we should do a subsequent deprecation pass to ensure this isn't allowed
owner = PASSED_FROM_CREATE;
} else {
debugOwnerMap.set(this, owner);
}
}

instance = new C(owner);
Expand Down