Skip to content

Commit

Permalink
rework toString
Browse files Browse the repository at this point in the history
  • Loading branch information
krisselden committed Mar 28, 2018
1 parent c9cf71e commit 559aac5
Show file tree
Hide file tree
Showing 16 changed files with 3,981 additions and 3,104 deletions.
71 changes: 48 additions & 23 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import { assert, isTesting } from 'ember-debug';
import { DEBUG } from 'ember-env-flags';
import {
libraries,
run
run,
processAllNamespaces,
setNamespaceSearchDisabled
} from 'ember-metal';
import {
Namespace,
setNamespaceSearchDisabled,
runLoadHooks,
_loaded,
RSVP
} from 'ember-runtime';
import { runLoadHooks, _loaded, RSVP } from 'ember-runtime';
import { EventDispatcher, jQuery, jQueryDisabled } from 'ember-views';
import {
Route,
Expand Down Expand Up @@ -361,7 +357,8 @@ const Application = Engine.extend({
*/
_applicationInstances: null,

init(options) { // eslint-disable-line no-unused-vars
init() {
// eslint-disable-line no-unused-vars
this._super(...arguments);

if (!this.$) {
Expand Down Expand Up @@ -575,8 +572,14 @@ const Application = Engine.extend({
@public
*/
deferReadiness() {
assert('You must call deferReadiness on an instance of Application', this instanceof Application);
assert('You cannot defer readiness since the `ready()` hook has already been called.', this._readinessDeferrals > 0);
assert(
'You must call deferReadiness on an instance of Application',
this instanceof Application
);
assert(
'You cannot defer readiness since the `ready()` hook has already been called.',
this._readinessDeferrals > 0
);
this._readinessDeferrals++;
},

Expand All @@ -590,7 +593,10 @@ const Application = Engine.extend({
@public
*/
advanceReadiness() {
assert('You must call advanceReadiness on an instance of Application', this instanceof Application);
assert(
'You must call advanceReadiness on an instance of Application',
this instanceof Application
);
this._readinessDeferrals--;

if (this._readinessDeferrals === 0) {
Expand All @@ -615,7 +621,9 @@ const Application = Engine.extend({
@return {Promise<Application,Error>}
*/
boot() {
if (this._bootPromise) { return this._bootPromise; }
if (this._bootPromise) {
return this._bootPromise;
}

try {
this._bootSync();
Expand All @@ -641,13 +649,15 @@ const Application = Engine.extend({
@private
*/
_bootSync() {
if (this._booted) { return; }
if (this._booted) {
return;
}

// Even though this returns synchronously, we still need to make sure the
// boot promise exists for book-keeping purposes: if anything went wrong in
// the boot process, we need to store the error as a rejection on the boot
// promise so that a future caller of `boot()` can tell what failed.
let defer = this._bootResolver = RSVP.defer();
let defer = (this._bootResolver = RSVP.defer());
this._bootPromise = defer.promise;

try {
Expand Down Expand Up @@ -736,10 +746,13 @@ const Application = Engine.extend({
@public
*/
reset() {
assert(`Calling reset() on instances of \`Application\` is not
assert(
`Calling reset() on instances of \`Application\` is not
supported when globals mode is disabled; call \`visit()\` to
create new \`ApplicationInstance\`s and dispose them
via their \`destroy()\` method instead.`, this._globalsMode && this.autoboot);
via their \`destroy()\` method instead.`,
this._globalsMode && this.autoboot
);

let instance = this.__deprecatedInstance__;

Expand All @@ -766,7 +779,7 @@ const Application = Engine.extend({
// TODO: Is this still needed for _globalsMode = false?
if (!isTesting()) {
// Eagerly name all classes that are already loaded
Namespace.processAll();
processAllNamespaces();
setNamespaceSearchDisabled(true);
}

Expand Down Expand Up @@ -815,7 +828,9 @@ const Application = Engine.extend({
@event ready
@public
*/
ready() { return this; },
ready() {
return this;
},

// This method must be moved to the application instance object
willDestroy() {
Expand Down Expand Up @@ -1036,7 +1051,8 @@ const Application = Engine.extend({
return this.boot().then(() => {
let instance = this.buildInstance();

return instance.boot(options)
return instance
.boot(options)
.then(() => instance.visit(url))
.catch(error => {
run(instance, 'destroy');
Expand Down Expand Up @@ -1072,7 +1088,8 @@ Application.reopenClass({
@return {Ember.Registry} the built registry
@private
*/
buildRegistry(application, options = {}) { // eslint-disable-line no-unused-vars
buildRegistry() {
// eslint-disable-line no-unused-vars
let registry = this._super(...arguments);

commonSetupRegistry(registry);
Expand All @@ -1085,7 +1102,11 @@ Application.reopenClass({

function commonSetupRegistry(registry) {
registry.register('router:main', Router.extend());
registry.register('-view-registry:main', { create() { return dictionary(null); } });
registry.register('-view-registry:main', {
create() {
return dictionary(null);
}
});

registry.register('route:basic', Route);
registry.register('event_dispatcher:main', EventDispatcher);
Expand All @@ -1097,7 +1118,11 @@ function commonSetupRegistry(registry) {
registry.register('location:history', HistoryLocation);
registry.register('location:none', NoneLocation);

registry.register(P`-bucket-cache:main`, { create() { return new BucketCache(); } });
registry.register(P`-bucket-cache:main`, {
create() {
return new BucketCache();
}
});

if (EMBER_ROUTING_ROUTER_SERVICE) {
registry.register('service:router', RouterService);
Expand Down
68 changes: 41 additions & 27 deletions packages/ember-application/lib/system/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
*/

import { dictionary } from 'ember-utils';
import { get } from 'ember-metal';
import { get, findNamespace } from 'ember-metal';
import { assert, info } from 'ember-debug';
import {
String as StringUtils,
Object as EmberObject,
Namespace
} from 'ember-runtime';
import { String as StringUtils, Object as EmberObject } from 'ember-runtime';
import validateType from '../utils/validate-type';
import { getTemplate } from 'ember-glimmer';
import { DEBUG } from 'ember-env-flags';
Expand All @@ -22,13 +18,13 @@ export const Resolver = EmberObject.extend({
@property namespace
*/
namespace: null,
normalize: null, // required
resolve: null, // required
parseName: null, // required
normalize: null, // required
resolve: null, // required
parseName: null, // required
lookupDescription: null, // required
makeToString: null, // required
resolveOther: null, // required
_logLookup: null // required
makeToString: null, // required
resolveOther: null, // required
_logLookup: null // required
});

/**
Expand Down Expand Up @@ -115,18 +111,17 @@ const DefaultResolver = EmberObject.extend({
},

normalize(fullName) {
let [ type, name ] = fullName.split(':');
let [type, name] = fullName.split(':');

assert(
'Tried to normalize a container name without a colon (:) in it. ' +
'You probably tried to lookup a name that did not contain a type, ' +
'a colon, and a name. A proper lookup name would be `view:post`.',
'You probably tried to lookup a name that did not contain a type, ' +
'a colon, and a name. A proper lookup name would be `view:post`.',
fullName.split(':').length === 2
);

if (type !== 'template') {
let result = name
.replace(/(\.|_|-)./g, m => m.charAt(1).toUpperCase());
let result = name.replace(/(\.|_|-)./g, m => m.charAt(1).toUpperCase());

return `${type}:${result}`;
} else {
Expand Down Expand Up @@ -179,13 +174,14 @@ const DefaultResolver = EmberObject.extend({
*/

parseName(fullName) {
return this._parseNameCache[fullName] || (
return (
this._parseNameCache[fullName] ||
(this._parseNameCache[fullName] = this._parseName(fullName))
);
},

_parseName(fullName) {
let [ type, fullNameWithoutType ] = fullName.split(':');
let [type, fullNameWithoutType] = fullName.split(':');

let name = fullNameWithoutType;
let namespace = get(this, 'namespace');
Expand All @@ -197,18 +193,21 @@ const DefaultResolver = EmberObject.extend({
let parts = name.split('/');
name = parts[parts.length - 1];
let namespaceName = StringUtils.capitalize(parts.slice(0, -1).join('.'));
root = Namespace.byName(namespaceName);
root = findNamespace(namespaceName);

assert(
`You are looking for a ${name} ${type} in the ${namespaceName} namespace, but the namespace could not be found`,
root
);
}

let resolveMethodName = fullNameWithoutType === 'main' ? 'Main' : StringUtils.classify(type);
let resolveMethodName =
fullNameWithoutType === 'main' ? 'Main' : StringUtils.classify(type);

if (!(name && type)) {
throw new TypeError(`Invalid fullName: \`${fullName}\`, must be of the form \`type:name\` `);
throw new TypeError(
`Invalid fullName: \`${fullName}\`, must be of the form \`type:name\` `
);
}

return {
Expand Down Expand Up @@ -237,10 +236,15 @@ const DefaultResolver = EmberObject.extend({
let description;

if (parsedName.type === 'template') {
return `template at ${parsedName.fullNameWithoutType.replace(/\./g, '/')}`;
return `template at ${parsedName.fullNameWithoutType.replace(
/\./g,
'/'
)}`;
}

description = `${parsedName.root}.${StringUtils.classify(parsedName.name).replace(/\./g, '')}`;
description = `${parsedName.root}.${StringUtils.classify(
parsedName.name
).replace(/\./g, '')}`;

if (parsedName.type !== 'model') {
description += StringUtils.classify(parsedName.type);
Expand Down Expand Up @@ -280,7 +284,10 @@ const DefaultResolver = EmberObject.extend({
resolveTemplate(parsedName) {
let templateName = parsedName.fullNameWithoutType.replace(/\./g, '/');

return getTemplate(templateName) || getTemplate(StringUtils.decamelize(templateName));
return (
getTemplate(templateName) ||
getTemplate(StringUtils.decamelize(templateName))
);
},

/**
Expand Down Expand Up @@ -357,7 +364,9 @@ const DefaultResolver = EmberObject.extend({
@protected
*/
resolveOther(parsedName) {
let className = StringUtils.classify(parsedName.name) + StringUtils.classify(parsedName.type);
let className =
StringUtils.classify(parsedName.name) +
StringUtils.classify(parsedName.type);
let factory = get(parsedName.root, className);
return factory;
},
Expand Down Expand Up @@ -436,7 +445,12 @@ if (DEBUG) {
padding = new Array(60 - parsedName.fullName.length).join('.');
}

info(symbol, parsedName.fullName, padding, this.lookupDescription(parsedName.fullName));
info(
symbol,
parsedName.fullName,
padding,
this.lookupDescription(parsedName.fullName)
);
}
});
}
Loading

0 comments on commit 559aac5

Please sign in to comment.