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 dc8e1a8 commit 1b552a2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 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
8 changes: 8 additions & 0 deletions addon/addon/template-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 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 = {};
8 changes: 8 additions & 0 deletions test-app/tests/unit/resolvers/classic/-setup-resolver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Resolver, { ModuleRegistry } from 'ember-resolver';
import { setOwner } from '@ember/owner';

export let resolver;
export let loader;

export function setupResolver(options = {}) {
let owner = options.owner;
delete options.owner;

if (!options.namespace) {
options.namespace = { modulePrefix: 'appkit' };
}
Expand All @@ -22,4 +26,8 @@ export function setupResolver(options = {}) {
};

resolver = Resolver.create(options);

if (owner) {
setOwner(resolver, owner);
}
}
4 changes: 2 additions & 2 deletions test-app/tests/unit/resolvers/classic/basic-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */

import Ember from 'ember';
import { module, test } from 'qunit';
import { setupResolver, resolver, loader } from './-setup-resolver';
import { TEMPLATES } from 'ember-resolver/template-cache';

let originalConsoleInfo;

Expand Down Expand Up @@ -434,7 +434,7 @@ module('ember-resolver/resolvers/classic', function (hooks) {
});

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

Expand Down
12 changes: 8 additions & 4 deletions test-app/tests/unit/resolvers/classic/logging-test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import Ember from 'ember';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

import { setupResolver, resolver, loader } from './-setup-resolver';

let originalConsoleInfo, logCalls;

module('Logging', function (hooks) {
setupTest(hooks);

hooks.beforeEach(function () {
originalConsoleInfo = console ? console.info : null;
logCalls = [];
console.info = function (arg) {
logCalls.push(arg);
};
setupResolver();
setupResolver({ owner: this.owner });
});

hooks.afterEach(function () {
Expand All @@ -26,7 +28,8 @@ module('Logging', function (hooks) {
return 'is logged';
});

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

resolver.resolve('fruit:orange');

Expand All @@ -38,7 +41,8 @@ module('Logging', function (hooks) {
return 'is not logged';
});

Ember.ENV.LOG_MODULE_RESOLVER = false;
let env = this.owner.resolveRegistration('config:environment');
env.LOG_MODULE_RESOLVER = false;

resolver.resolve('fruit:orange');

Expand Down

0 comments on commit 1b552a2

Please sign in to comment.