Skip to content

Commit f76baf0

Browse files
feat(plugins): better api by moving some bootstrapper code directly into aurelia
1 parent f970c65 commit f76baf0

File tree

3 files changed

+178
-18
lines changed

3 files changed

+178
-18
lines changed

src/aurelia.js

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ function loadResources(container, resourcesToLoad, appResources){
5858
return viewEngine.importViewResources(importIds, names, appResources);
5959
}
6060

61+
function runTasks(aurelia, tasks){
62+
let current, next = () => {
63+
if(current = tasks.shift()){
64+
return Promise.resolve(current(aurelia)).then(next);
65+
}
66+
67+
return Promise.resolve();
68+
};
69+
70+
return next();
71+
}
72+
6173
/**
6274
* The framework core that provides the main Aurelia object.
6375
*
@@ -74,11 +86,13 @@ export class Aurelia {
7486
use:Plugins;
7587

7688
constructor(loader?:Loader, container?:Container, resources?:ResourceRegistry){
89+
this.resourcesToLoad = {};
90+
this.preStartTasks = [];
91+
this.postStartTasks = [];
7792
this.loader = loader || new window.AureliaLoader();
7893
this.container = container || new Container();
7994
this.resources = resources || new ResourceRegistry();
8095
this.use = new Plugins(this);
81-
this.resourcesToLoad = {};
8296

8397
this.withInstance(Aurelia, this);
8498
this.withInstance(Loader, this.loader);
@@ -167,6 +181,30 @@ export class Aurelia {
167181
return this;
168182
}
169183

184+
/**
185+
* Adds an async function that runs before the plugins are run.
186+
*
187+
* @method addPreStartTask
188+
* @param {Function} task The function to run before start.
189+
* @return {Aurelia} Returns the current Aurelia instance.
190+
*/
191+
addPreStartTask(task:Function):Aurelia{
192+
this.preStartTasks.push(task);
193+
return this;
194+
}
195+
196+
/**
197+
* Adds an async function that runs after the plugins are run.
198+
*
199+
* @method addPostStartTask
200+
* @param {Function} task The function to run after start.
201+
* @return {Aurelia} Returns the current Aurelia instance.
202+
*/
203+
addPostStartTask(task:Function):Aurelia{
204+
this.postStartTasks.push(task);
205+
return this;
206+
}
207+
170208
/**
171209
* Loads plugins, then resources, and then starts the Aurelia instance.
172210
*
@@ -183,22 +221,26 @@ export class Aurelia {
183221

184222
preventActionlessFormSubmit();
185223

186-
return this.use._process().then(() => {
187-
if(!this.container.hasHandler(BindingLanguage)){
188-
var message = 'You must configure Aurelia with a BindingLanguage implementation.';
189-
logger.error(message);
190-
throw new Error(message);
191-
}
192-
193-
if(!this.container.hasHandler(Animator)){
194-
Animator.configureDefault(this.container);
195-
}
196-
197-
return loadResources(this.container, this.resourcesToLoad, this.resources).then(() => {
198-
logger.info('Aurelia Started');
199-
var evt = new window.CustomEvent('aurelia-started', { bubbles: true, cancelable: true });
200-
document.dispatchEvent(evt);
201-
return this;
224+
return runTasks(this, this.preStartTasks).then(() => {
225+
return this.use._process().then(() => {
226+
if(!this.container.hasHandler(BindingLanguage)){
227+
var message = 'You must configure Aurelia with a BindingLanguage implementation.';
228+
logger.error(message);
229+
throw new Error(message);
230+
}
231+
232+
if(!this.container.hasHandler(Animator)){
233+
Animator.configureDefault(this.container);
234+
}
235+
236+
return loadResources(this.container, this.resourcesToLoad, this.resources);
237+
}).then(() => {
238+
return runTasks(this, this.postStartTasks).then(() => {
239+
logger.info('Aurelia Started');
240+
var evt = new window.CustomEvent('aurelia-started', { bubbles: true, cancelable: true });
241+
document.dispatchEvent(evt);
242+
return this;
243+
});
202244
});
203245
});
204246
}

src/plugins.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class Plugins {
3333
this.aurelia = aurelia;
3434
this.info = [];
3535
this.processed = false;
36+
37+
aurelia.addPreStartTask(() => System.normalize('aurelia-bootstrapper').then(bootstrapperName => this.bootstrapperName = bootstrapperName));
3638
}
3739

3840
/**
@@ -55,6 +57,122 @@ export class Plugins {
5557
return this;
5658
}
5759

60+
/**
61+
* Plugs in the default binding language from aurelia-templating-binding.
62+
*
63+
* @method defaultBindingLanguage
64+
* @return {Plugins} Returns the current Plugins instance.
65+
*/
66+
defaultBindingLanguage():Plugins{
67+
this.aurelia.addPreStartTask(() => {
68+
return System.normalize('aurelia-templating-binding', this.bootstrapperName).then(name => {
69+
this.aurelia.use.plugin(name);
70+
});
71+
});
72+
73+
return this;
74+
};
75+
76+
/**
77+
* Plugs in the router from aurelia-templating-router.
78+
*
79+
* @method router
80+
* @return {Plugins} Returns the current Plugins instance.
81+
*/
82+
router():Plugins{
83+
this.aurelia.addPreStartTask(() => {
84+
return System.normalize('aurelia-templating-router', this.bootstrapperName).then(name => {
85+
this.aurelia.use.plugin(name);
86+
});
87+
});
88+
89+
return this;
90+
}
91+
92+
/**
93+
* Plugs in the default history implementation from aurelia-history-browser.
94+
*
95+
* @method history
96+
* @return {Plugins} Returns the current Plugins instance.
97+
*/
98+
history():Plugins{
99+
this.aurelia.addPreStartTask(() => {
100+
return System.normalize('aurelia-history-browser', this.bootstrapperName).then(name => {
101+
this.aurelia.use.plugin(name);
102+
});
103+
});
104+
105+
return this;
106+
}
107+
108+
/**
109+
* Plugs in the default templating resources (if, repeat, show, compose, etc.) from aurelia-templating-resources.
110+
*
111+
* @method defaultResources
112+
* @return {Plugins} Returns the current Plugins instance.
113+
*/
114+
defaultResources():Plugins{
115+
this.aurelia.addPreStartTask(() => {
116+
return System.normalize('aurelia-templating-resources', this.bootstrapperName).then(name => {
117+
System.map['aurelia-templating-resources'] = name;
118+
this.aurelia.use.plugin(name);
119+
});
120+
});
121+
122+
return this;
123+
}
124+
125+
/**
126+
* Plugs in the event aggregator from aurelia-event-aggregator.
127+
*
128+
* @method eventAggregator
129+
* @return {Plugins} Returns the current Plugins instance.
130+
*/
131+
eventAggregator():Plugins{
132+
this.aurelia.addPreStartTask(() => {
133+
return System.normalize('aurelia-event-aggregator', this.bootstrapperName).then(name => {
134+
System.map['aurelia-event-aggregator'] = name;
135+
this.aurelia.use.plugin(name);
136+
});
137+
});
138+
139+
return this;
140+
}
141+
142+
/**
143+
* Sets up the Aurelia configuration. This is equivalent to calling `.defaultBindingLanguage().defaultResources().history().router().eventAggregator();`
144+
*
145+
* @method standardConfiguration
146+
* @return {Plugins} Returns the current Plugins instance.
147+
*/
148+
standardConfiguration():Plugins{
149+
return this.aurelia.use
150+
.defaultBindingLanguage()
151+
.defaultResources()
152+
.history()
153+
.router()
154+
.eventAggregator();
155+
}
156+
157+
/**
158+
* Plugs in the ConsoleAppender and sets the log level to debug.
159+
*
160+
* @method developmentLogging
161+
* @return {Plugins} Returns the current Plugins instance.
162+
*/
163+
developmentLogging():Plugins{
164+
this.aurelia.addPreStartTask(() => {
165+
return System.normalize('aurelia-logging-console', this.bootstrapperName).then(name => {
166+
return this.aurelia.loader.loadModule(name).then(m => {
167+
TheLogManager.addAppender(new m.ConsoleAppender());
168+
TheLogManager.setLevel(TheLogManager.logLevel.debug);
169+
});
170+
});
171+
});
172+
173+
return this;
174+
}
175+
58176
_process(){
59177
var aurelia = this.aurelia,
60178
loader = aurelia.loader,

test/plugin.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('the plugin loader', () => {
66
aureliaMock;
77

88
beforeEach(() => {
9-
aureliaMock = jasmine.createSpyObj('aureliaMock', ['loader']);
9+
aureliaMock = jasmine.createSpyObj('aureliaMock', ['loader', 'addPreStartTask']);
1010
plugins = new Plugins(aureliaMock);
1111
});
1212

0 commit comments

Comments
 (0)