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

[CHORE]: Eliminate default injections in favor of re-export #6450

Merged
merged 9 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 0 additions & 2 deletions packages/-ember-data/addon/setup-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,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/-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@ember-data/adapter/json-api';
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';
1 change: 1 addition & 0 deletions packages/-ember-data/app/adapters/-rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@ember-data/adapter/rest';
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/serializers/-default.
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module('integration/store - serializerFor', function(hooks) {
if (registrationName === 'serializer:-default') {
return undefined;
}
if (registrationName === 'serializer:-json-api') {
return undefined;
}
return lookup.call(owner, registrationName);
};
/*
Expand Down
50 changes: 46 additions & 4 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ type PendingSaveItem = {
let globalClientIdCounter = 1;

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

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;
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 +3220,27 @@ 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');
} else if (normalizedModelName === '-rest') {
const Adapter = require('@ember-data/adapter/rest').default;
owner.register(`adapter:-rest`, Adapter);
adapter = owner.lookup(`adapter:-rest`);
deprecateTestRegistration('adapter', '-rest');
} else if (normalizedModelName === '-default') {
const Adapter = require('@ember-data/adapter/json-api').default;
owner.register(`adapter:-default`, Adapter);
adapter = owner.lookup(`adapter:-default`);
adapter && deprecateTestRegistration('adapter', '-default');
}
}

if (adapter !== undefined) {
set(adapter, 'store', this);
_adapterCache[normalizedModelName] = adapter;
Expand All @@ -3239,6 +3260,27 @@ 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');
} else if (adapterName === '-rest') {
const Adapter = require('@ember-data/adapter/rest').default;
owner.register(`adapter:-rest`, Adapter);
adapter = owner.lookup(`adapter:-rest`);
deprecateTestRegistration('adapter', '-rest');
} else if (adapterName === '-default') {
const Adapter = require('@ember-data/adapter/json-api').default;
owner.register(`adapter:-default`, Adapter);
adapter = owner.lookup(`adapter:-default`);
adapter && deprecateTestRegistration('adapter', '-default');
}
}

if (adapter !== undefined) {
set(adapter, 'store', this);
_adapterCache[normalizedModelName] = adapter;
Expand All @@ -3250,7 +3292,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