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

Refactor CoreObject to leverage native JS semantics. #16436

Merged
merged 4 commits into from
Mar 29, 2018
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
69 changes: 47 additions & 22 deletions packages/ember-application/lib/system/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import {
schedule,
bind,
once,
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 @@ -365,7 +361,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 @@ -579,8 +576,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 @@ -594,7 +597,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 @@ -619,7 +625,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 @@ -645,13 +653,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 @@ -740,10 +750,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 @@ -770,7 +783,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 @@ -819,7 +832,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 @@ -1040,7 +1055,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 @@ -1076,7 +1092,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 @@ -1089,7 +1106,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 @@ -1101,7 +1122,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