Skip to content

Commit

Permalink
Do not use global ember -- internalize template resolution cache
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jun 6, 2024
1 parent fe73b7d commit 3a6ed80
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
10 changes: 7 additions & 3 deletions addon/addon/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* globals requirejs, require */

import Ember from 'ember';
import { assert, deprecate, warn } from '@ember/debug';
import EmberObject from '@ember/object';
import { dasherize, classify, underscore } from './string';
import { DEBUG } from '@glimmer/env';
import classFactory from './utils/class-factory';
import { getOwner } from '@ember/owner';

import { TEMPLATES } from './template-cache';

if (typeof requirejs.entries === 'undefined') {
requirejs.entries = requirejs._eak_seen;
Expand Down Expand Up @@ -295,7 +297,7 @@ class Resolver extends EmberObject {
resolveTemplate(parsedName) {
let resolved = this.resolveOther(parsedName);
if (resolved == null) {
resolved = Ember.TEMPLATES[parsedName.fullNameWithoutType];
resolved = TEMPLATES[parsedName.fullNameWithoutType];
}
return resolved;
}
Expand Down Expand Up @@ -459,7 +461,9 @@ class Resolver extends EmberObject {

// only needed until 1.6.0-beta.2 can be required
_logLookup(found, parsedName, description) {
if (!Ember.ENV.LOG_MODULE_RESOLVER && !parsedName.root.LOG_RESOLVER) {
let owner = getOwner(this);
let env = owner?.resolveRegistration('config:environment');
if (!env?.LOG_MODULE_RESOLVER && !parsedName.root.LOG_RESOLVER) {
return;
}

Expand Down
30 changes: 30 additions & 0 deletions addon/addon/template-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Originally: https://github.com/emberjs/ember.js/blob/28444d536fa20debef2a67e2f18c5eb11113a4b5/packages/%40ember/-internals/glimmer/lib/template_registry.ts#L9
*
* import { TEMPLATES } from 'ember';
*
* Removed for RFC 1003
*/
export let TEMPLATES = {};

export function setTemplates(templates) {
TEMPLATES = templates;
}

export function getTemplates() {
return TEMPLATES;
}

export function getTemplate(name) {
if (Object.prototype.hasOwnProperty.call(TEMPLATES, name)) {
return TEMPLATES[name];
}
}

export function hasTemplate(name) {
return Object.prototype.hasOwnProperty.call(TEMPLATES, name);
}

export function setTemplate(name, template) {
return (TEMPLATES[name] = template);
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions test-app/tests/unit/resolvers/classic/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Ember from 'ember';
import { module, test } from 'qunit';
import Resolver, { ModuleRegistry } from 'ember-resolver';
import { TEMPLATES } from 'ember-resolver/template-cache';

let originalConsoleInfo, logCalls, resolver, loader;

Expand Down Expand Up @@ -456,7 +457,7 @@ test('can lookup templates with mixed naming moduleName', function (assert) {
});

test('can lookup templates via Ember.TEMPLATES', function (assert) {
Ember.TEMPLATES['application'] = function () {
TEMPLATES['application'] = function () {
return '<h1>herp</h1>';
};

Expand Down Expand Up @@ -695,7 +696,9 @@ test('logs lookups when logging is enabled', function (assert) {
return 'is logged';
});

Ember.ENV.LOG_MODULE_RESOLVER = true;
this.owner.resolveRegistration(
'config:environment'
).LOG_MODULE_RESOLVER = true;

resolver.resolve('fruit:orange');

Expand Down

0 comments on commit 3a6ed80

Please sign in to comment.