Skip to content

Commit

Permalink
[CHORE]: Eliminate default injections in favor of re-export (#6450)
Browse files Browse the repository at this point in the history
* [CHORE]: Eliminate default injections in favor of re-export

* remove registration in setup container

* add debug blocks

* Fix tests

* no need for these defaults

* overload deprecate tests

* undo serializer test addition

* update comment
  • Loading branch information
snewcomer authored and runspired committed Sep 18, 2019
1 parent 0fe5c00 commit b3d5da4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
3 changes: 0 additions & 3 deletions packages/-ember-data/addon/setup-container.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DebugAdapter } from './-private';
import Store from '@ember-data/store';
import JSONAPIAdapter from '@ember-data/adapter/json-api';

function hasRegistration(application, registrationName) {
// fallback our ember-data tests necessary
Expand Down Expand Up @@ -28,8 +27,6 @@ function initializeStore(application) {
registerOptionsForType.call(application, 'serializer', { singleton: false });
registerOptionsForType.call(application, 'adapter', { singleton: false });

application.register('adapter:-json-api', JSONAPIAdapter);

if (!hasRegistration(application, 'service:store')) {
application.register('service:store', Store);
}
Expand Down
1 change: 1 addition & 0 deletions packages/-ember-data/app/adapters/-json-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@ember-data/adapter/json-api';
20 changes: 10 additions & 10 deletions packages/-ember-data/tests/integration/store/adapter-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ module('integration/store - adapterFor', function(hooks) {

test('when no adapter is available we throw an error', async function(assert) {
let { owner } = this;
/*
ensure our store instance does not specify a fallback
we use an empty string as that would cause `owner.lookup` to blow up if not guarded properly
whereas `null` `undefined` `false` would not.
*/
store.adapter = '';
/*
adapter:-json-api is the "last chance" fallback and is
registered automatically.
unregistering it will cause adapterFor to return `undefined`.
the json-api adapter which is re-exported as app/adapters/-json-api.
here we override to ensure adapterFor will return `undefined`.
*/
owner.unregister('adapter:-json-api');
const lookup = owner.lookup;
owner.lookup = registrationName => {
if (registrationName === 'adapter:-json-api') {
return undefined;
}
return lookup.call(owner, registrationName);
};

assert.expectAssertion(() => {
store.adapterFor('person');
}, /No adapter was found for 'person' and no 'application', store\.adapter = 'adapter-fallback-name', or '-json-api' adapter were found as fallbacks\./);
}, /Assertion Failed: No adapter was found for 'person' and no 'application' adapter was found as a fallback/);
});

test('we find and instantiate the application adapter', async function(assert) {
Expand Down
29 changes: 26 additions & 3 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ type PendingSaveItem = {
let globalClientIdCounter = 1;

const HAS_SERIALIZER_PACKAGE = has('@ember-data/serializer');
const HAS_ADAPTER_PACKAGE = has('@ember-data/adapter');

function deprecateTestRegistration(factoryType: 'adapter', factoryName: '-json-api'): void;
function deprecateTestRegistration(factoryType: 'serializer', factoryName: '-json-api' | '-rest' | '-default'): void;
// TODO add adapter here and deprecate those registrations as well after refactoring them to re-exports
function deprecateTestRegistration(factoryType: 'serializer', factoryName: '-json-api' | '-rest' | '-default'): void {
function deprecateTestRegistration(factoryType: 'serializer' | 'adapter', factoryName: '-json-api' | '-rest' | '-default'): void {
deprecate(
`You looked up the ${factoryType} "${factoryName}" but it was not found. Likely this means you are using a legacy ember-qunit moduleFor helper. Add "needs: ['${factoryType}:${factoryName}']", "integration: true", or refactor to modern syntax to resolve this deprecation.`,
false,
Expand Down Expand Up @@ -3220,6 +3221,17 @@ abstract class CoreStore extends Service {
let owner = getOwner(this);

adapter = owner.lookup(`adapter:${normalizedModelName}`);

// in production this is handled by the re-export
if (DEBUG && HAS_ADAPTER_PACKAGE && adapter === undefined) {
if (normalizedModelName === '-json-api') {
const Adapter = require('@ember-data/adapter/json-api').default;
owner.register(`adapter:-json-api`, Adapter);
adapter = owner.lookup(`adapter:-json-api`);
deprecateTestRegistration('adapter', '-json-api');
}
}

if (adapter !== undefined) {
set(adapter, 'store', this);
_adapterCache[normalizedModelName] = adapter;
Expand All @@ -3239,6 +3251,17 @@ abstract class CoreStore extends Service {
// property defined on the store
let adapterName = this.adapter || '-json-api';
adapter = adapterName ? _adapterCache[adapterName] || owner.lookup(`adapter:${adapterName}`) : undefined;

// in production this is handled by the re-export
if (DEBUG && HAS_ADAPTER_PACKAGE && adapter === undefined) {
if (adapterName === '-json-api') {
const Adapter = require('@ember-data/adapter/json-api').default;
owner.register(`adapter:-json-api`, Adapter);
adapter = owner.lookup(`adapter:-json-api`);
deprecateTestRegistration('adapter', '-json-api');
}
}

if (adapter !== undefined) {
set(adapter, 'store', this);
_adapterCache[normalizedModelName] = adapter;
Expand All @@ -3250,7 +3273,7 @@ abstract class CoreStore extends Service {
// `adapter` property on store: use json-api adapter
adapter = _adapterCache['-json-api'] || owner.lookup('adapter:-json-api');
assert(
`No adapter was found for '${modelName}' and no 'application', store.adapter = 'adapter-fallback-name', or '-json-api' adapter were found as fallbacks.`,
`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`,
adapter !== undefined
);
set(adapter, 'store', this);
Expand Down

0 comments on commit b3d5da4

Please sign in to comment.