Skip to content

Commit 5a66563

Browse files
committed
Merge pull request #13526 from dgeb/engine-parent-dependencies
Clone shared dependencies between an engine instance and its parent.
2 parents 0e796e7 + f026757 commit 5a66563

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

packages/ember-application/lib/system/engine-instance.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import EmberError from 'ember-metal/error';
88
import Registry from 'container/registry';
99
import ContainerProxy from 'ember-runtime/mixins/container_proxy';
1010
import RegistryProxy from 'ember-runtime/mixins/registry_proxy';
11+
import { privatize as P } from 'container/registry';
1112
import { getEngineParent, setEngineParent } from 'ember-application/system/engine-parent';
1213
import { assert } from 'ember-metal/debug';
1314
import run from 'ember-metal/run_loop';
@@ -97,6 +98,10 @@ const EngineInstance = EmberObject.extend(RegistryProxy, ContainerProxy, {
9798

9899
assert('An engine instance\'s parent must be set via `setEngineParent(engine, parent)` prior to calling `engine.boot()`.', getEngineParent(this));
99100

101+
if (isEnabled('ember-application-engines')) {
102+
this.cloneParentDependencies();
103+
}
104+
100105
this.base.runInstanceInitializers(this);
101106

102107
this._booted = true;
@@ -154,6 +159,32 @@ if (isEnabled('ember-application-engines')) {
154159
setEngineParent(engineInstance, this);
155160

156161
return engineInstance;
162+
},
163+
164+
/**
165+
Clone dependencies shared between an engine instance and its parent.
166+
167+
@private
168+
@method cloneParentDependencies
169+
*/
170+
cloneParentDependencies() {
171+
let parent = getEngineParent(this);
172+
173+
[
174+
'route:basic',
175+
'event_dispatcher:main',
176+
P`-bucket-cache:main`,
177+
'service:-routing'
178+
].forEach((key) => {
179+
this.register(key, parent.resolveRegistration(key));
180+
});
181+
182+
[
183+
'router:main',
184+
'-view-registry:main'
185+
].forEach((key) => {
186+
this.register(key, parent.lookup(key), { instantiate: false });
187+
});
157188
}
158189
});
159190
}

packages/ember-application/tests/system/application_instance_test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import Engine from 'ember-application/system/engine';
12
import Application from 'ember-application/system/application';
23
import ApplicationInstance from 'ember-application/system/application-instance';
34
import run from 'ember-metal/run_loop';
45
import jQuery from 'ember-views/system/jquery';
56
import factory from 'container/tests/test-helpers/factory';
7+
import isEnabled from 'ember-metal/features';
8+
import { privatize as P } from 'container/registry';
69

710
let app, appInstance;
811

@@ -148,3 +151,46 @@ QUnit.test('unregistering a factory clears all cached instances of that factory'
148151

149152
assert.notStrictEqual(postController1, postController2, 'lookup creates a brand new instance, because the previous one was reset');
150153
});
154+
155+
if (isEnabled('ember-application-engines')) {
156+
QUnit.test('can build and boot a registered engine', function(assert) {
157+
assert.expect(7);
158+
159+
let ChatEngine = Engine.extend();
160+
let chatEngineInstance;
161+
162+
app.register('engine:chat', ChatEngine);
163+
164+
run(function() {
165+
appInstance = ApplicationInstance.create({ application: app });
166+
chatEngineInstance = appInstance.buildChildEngineInstance('chat');
167+
});
168+
169+
return chatEngineInstance.boot()
170+
.then(() => {
171+
assert.ok(true, 'boot successful');
172+
173+
[
174+
'route:basic',
175+
'event_dispatcher:main',
176+
P`-bucket-cache:main`,
177+
'service:-routing'
178+
].forEach((key) => {
179+
assert.strictEqual(
180+
chatEngineInstance.resolveRegistration(key),
181+
appInstance.resolveRegistration(key),
182+
`Engine and parent app share registrations for '${key}'`);
183+
});
184+
185+
[
186+
'router:main',
187+
'-view-registry:main'
188+
].forEach((key) => {
189+
assert.strictEqual(
190+
chatEngineInstance.lookup(key),
191+
appInstance.lookup(key),
192+
`Engine and parent app share singleton '${key}'`);
193+
});
194+
});
195+
});
196+
}

packages/ember-application/tests/system/engine_instance_initializers_test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ let MyEngine,
99

1010
function buildEngineInstance(EngineClass) {
1111
let engineInstance = EngineClass.buildInstance();
12-
setEngineParent(engineInstance, {});
12+
setEngineParent(engineInstance, {
13+
lookup() { return {}; },
14+
resolveRegistration() { return {}; }
15+
});
1316
return engineInstance;
1417
}
1518

packages/ember-application/tests/system/engine_instance_test.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,31 @@ QUnit.test('unregistering a factory clears all cached instances of that factory'
5757
assert.notStrictEqual(postComponent1, postComponent2, 'lookup creates a brand new instance because previous one was reset');
5858
});
5959

60-
QUnit.test('can be booted when its parent has been set', function(assert) {
61-
run(function() {
62-
engineInstance = EngineInstance.create({ base: engine });
63-
});
60+
if (isEnabled('ember-application-engines')) {
61+
QUnit.test('can be booted when its parent has been set', function(assert) {
62+
assert.expect(3);
63+
64+
run(function() {
65+
engineInstance = EngineInstance.create({ base: engine });
66+
});
67+
68+
expectAssertion(function() {
69+
engineInstance._bootSync();
70+
}, 'An engine instance\'s parent must be set via `setEngineParent(engine, parent)` prior to calling `engine.boot()`.');
6471

65-
expectAssertion(function() {
66-
engineInstance._bootSync();
67-
}, 'An engine instance\'s parent must be set via `setEngineParent(engine, parent)` prior to calling `engine.boot()`.');
72+
setEngineParent(engineInstance, {});
6873

69-
setEngineParent(engineInstance, {});
74+
// Stub `cloneParentDependencies`, the internals of which are tested along
75+
// with application instances.
76+
engineInstance.cloneParentDependencies = function() {
77+
assert.ok(true, 'parent dependencies are cloned');
78+
};
7079

71-
return engineInstance.boot().then(() => {
72-
assert.ok(true, 'boot successful');
80+
return engineInstance.boot().then(() => {
81+
assert.ok(true, 'boot successful');
82+
});
7383
});
74-
});
7584

76-
if (isEnabled('ember-application-engines')) {
7785
QUnit.test('can build a child instance of a registered engine', function(assert) {
7886
let ChatEngine = Engine.extend();
7987
let chatEngineInstance;

0 commit comments

Comments
 (0)