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] Move PromiseBelongsTo, PromiseManyArray and ManyArray to model package #6822

Merged
merged 4 commits into from
Nov 30, 2019
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
3 changes: 1 addition & 2 deletions packages/-ember-data/addon/-private/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export { Snapshot } from '@ember-data/store/-private';
export {
AdapterPopulatedRecordArray,
InternalModel,
ManyArray,
PromiseArray,
PromiseManyArray,
PromiseObject,
RecordArray,
RecordArrayManager,
Expand All @@ -20,4 +18,5 @@ export {
normalizeModelName,
coerceId,
} from '@ember-data/store/-private';
export { ManyArray, PromiseManyArray } from '@ember-data/model/-private';
export { RecordData, Relationship } from '@ember-data/record-data/-private';
2 changes: 1 addition & 1 deletion packages/-ember-data/node-tests/fixtures/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ module.exports = {
'(public) @ember-data/serializer Serializer#store',
'(public) @ember-data/serializer Transform#deserialize',
'(public) @ember-data/serializer Transform#serialize',
'(public) @ember-data/store @ember-data/store#normalizeModelName',
'(public) @ember-data/store BelongsToReference#id',
'(public) @ember-data/store BelongsToReference#load',
'(public) @ember-data/store BelongsToReference#push',
Expand Down Expand Up @@ -296,7 +297,6 @@ module.exports = {
'(public) @ember-data/store RecordArrayManager#createAdapterPopulatedRecordArray',
'(public) @ember-data/store RecordArrayManager#createRecordArray',
'(public) @ember-data/store RecordArrayManager#liveRecordArrayFor',
'(public) @ember-data/store RecordArrayManager#normalizeModelName',
'(public) @ember-data/store RecordArrayManager#unregisterRecordArray',
'(public) @ember-data/store RecordReference#id',
'(public) @ember-data/store RecordReference#load',
Expand Down
3 changes: 3 additions & 0 deletions packages/model/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export { default as hasMany } from './has-many';
export { default as Model } from './model';
export { default as Errors } from './errors';

export { default as ManyArray } from './system/many-array';
export { default as PromiseBelongsTo } from './system/promise-belongs-to';
export { default as PromiseManyArray } from './system/promise-many-array';
export { default as _modelForMixin } from './system/model-for-mixin';
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import { all } from 'rsvp';

//import Evented from '@ember/object/evented';
import DeprecatedEvent from './deprecated-evented';
import MutableArray from '@ember/array/mutable';
import EmberArray from '@ember/array';
import EmberObject, { get } from '@ember/object';
import { assert } from '@ember/debug';
import { PromiseArray } from './promise-proxies';
import { _objectIsAlive } from './store/common';
import diffArray from './diff-array';
import recordDataFor from './record-data-for';
import { DeprecatedEvented, PromiseArray, diffArray, _objectIsAlive, recordDataFor } from '@ember-data/store/-private';
import { CUSTOM_MODEL_CLASS, FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';

/**
Expand Down Expand Up @@ -55,9 +51,9 @@ import { CUSTOM_MODEL_CLASS, FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/can

@class ManyArray
@extends EmberObject
@uses Ember.MutableArray, EmberData.DeprecatedEvent
@uses Ember.MutableArray, DeprecatedEvented
*/
export default EmberObject.extend(MutableArray, DeprecatedEvent, {
export default EmberObject.extend(MutableArray, DeprecatedEvented, {
// here to make TS happy
_inverseIsAsync: false,
isLoaded: false,
Expand Down
40 changes: 40 additions & 0 deletions packages/model/addon/-private/system/promise-belongs-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
import { PromiseObject } from '@ember-data/store/-private';

/**
@module @ember-data/model
*/

/**
A PromiseBelongsTo is a PromiseObject that also proxies certain method calls
to the underlying belongsTo model.
Right now we proxy:

* `reload()`

@class PromiseBelongsTo
@extends PromiseObject
@private
*/
const PromiseBelongsTo = PromiseObject.extend({
// we don't proxy meta because we would need to proxy it to the relationship state container
// however, meta on relationships does not trigger change notifications.
// if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()`
meta: computed(function() {
assert(
'You attempted to access meta on the promise for the async belongsTo relationship ' +
`${this.get('_belongsToState').modelName}:${this.get('_belongsToState').key}'.` +
'\nUse `record.belongsTo(relationshipName).meta()` instead.',
false
);
}),

reload(options) {
assert('You are trying to reload an async belongsTo before it has been created', this.get('content') !== undefined);
let { key, store, originatingInternalModel } = this._belongsToState;
return store.reloadBelongsTo(this, originatingInternalModel, key, options).then(() => this);
},
});

export default PromiseBelongsTo;
56 changes: 56 additions & 0 deletions packages/model/addon/-private/system/promise-many-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { get } from '@ember/object';
import { reads } from '@ember/object/computed';
import { Promise } from 'rsvp';
import { assert } from '@ember/debug';
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';
import { PromiseArray } from '@ember-data/store/-private';

/**
@module @ember-data/model
*/

/**
A PromiseManyArray is a PromiseArray that also proxies certain method calls
to the underlying manyArray.
Right now we proxy:

* `reload()`
* `createRecord()`
* `on()`
* `one()`
* `trigger()`
* `off()`
* `has()`

@class PromiseManyArray
@extends Ember.ArrayProxy
@private
*/
const PromiseManyArray = PromiseArray.extend({
links: FULL_LINKS_ON_RELATIONSHIPS ? reads('content.links') : undefined,
reload(options) {
assert('You are trying to reload an async manyArray before it has been created', get(this, 'content'));
this.set('promise', this.get('content').reload(options));
return this;
},
createRecord: proxyToContent('createRecord'),
on: proxyToContent('on'),
one: proxyToContent('one'),
trigger: proxyToContent('trigger'),
off: proxyToContent('off'),
has: proxyToContent('has'),
});

export default PromiseManyArray;

export function promiseManyArray(promise, label) {
return PromiseManyArray.create({
promise: Promise.resolve(promise, label),
});
}

function proxyToContent(method) {
return function() {
return get(this, 'content')[method](...arguments);
};
}
3 changes: 1 addition & 2 deletions packages/store/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export { errorsHashToArray, errorsArrayToHash } from './system/errors-utils';
export { default as RootState } from './system/model/states';
export { default as InternalModel } from './system/model/internal-model';

export { PromiseArray, PromiseObject, PromiseManyArray } from './system/promise-proxies';
export { PromiseArray, PromiseObject } from './system/promise-proxies';

export { RecordArray, AdapterPopulatedRecordArray } from './system/record-arrays';

export { default as ManyArray } from './system/many-array';
export { default as RecordArrayManager } from './system/record-array-manager';

// // Used by tests
Expand Down
42 changes: 32 additions & 10 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import { DEBUG } from '@glimmer/env';
import { assert, inspect } from '@ember/debug';
import RootState from './states';
import Snapshot from '../snapshot';
import ManyArray from '../many-array';
import { PromiseBelongsTo, PromiseManyArray } from '../promise-proxies';
import Store from '../ds-model-store';
import { errorsHashToArray } from '../errors-utils';
import RecordArray from '../record-arrays/record-array';
// import { PromiseArray } from '../promise-proxies';

import { RecordReference, BelongsToReference, HasManyReference } from '../references';
import { RecordData } from '../../ts-interfaces/record-data';
Expand All @@ -35,12 +34,22 @@ import { internalModelFactoryFor, setRecordIdentifier } from '../store/internal-
import CoreStore from '../core-store';
import coerceId from '../coerce-id';
import recordDataFor from '../record-data-for';
import { HAS_MODEL_PACKAGE } from '@ember-data/private-build-infra';

type DefaultRecordData = import('@ember-data/record-data/-private').RecordData;
type RecordArray = InstanceType<typeof RecordArray>;
type RelationshipRecordData = import('@ember-data/record-data/-private/ts-interfaces/relationship-record-data').RelationshipRecordData;
type Relationships = import('@ember-data/record-data/-private/relationships/state/create').default;

// move to TS hacks module that we can delete when this is no longer a necessary recast
type ManyArray = InstanceType<typeof import('@ember-data/model/-private').ManyArray>;
type PromiseBelongsTo = InstanceType<typeof import('@ember-data/model/-private').PromiseBelongsTo>;
type PromiseManyArray = InstanceType<typeof import('@ember-data/model/-private').PromiseManyArray>;

/**
@module @ember-data/store
*/

// once the presentation logic is moved into the Model package we can make
// eliminate these lossy and redundant helpers
function relationshipsFor(instance: InternalModel): Relationships {
Expand All @@ -55,14 +64,24 @@ function relationshipStateFor(instance: InternalModel, propertyName: string) {

const { hasOwnProperty } = Object.prototype;

/**
@module @ember-data/store
*/

// move to TS hacks module that we can delete when this is no longer a necessary recast
type ManyArray = InstanceType<typeof ManyArray>;
type PromiseBelongsTo = InstanceType<typeof PromiseBelongsTo>;
type PromiseManyArray = InstanceType<typeof PromiseManyArray>;
let ManyArray: ManyArray;
let PromiseBelongsTo: PromiseBelongsTo;
let PromiseManyArray: PromiseManyArray;

let _found = false;
let _getModelPackage: () => boolean;
if (HAS_MODEL_PACKAGE) {
_getModelPackage = function() {
if (!_found) {
let modelPackage = require('@ember-data/model/-private');
({ ManyArray, PromiseBelongsTo, PromiseManyArray } = modelPackage);
if (ManyArray && PromiseBelongsTo && PromiseManyArray) {
_found = true;
}
}
return _found;
};
}

// TODO this should be integrated with the code removal so we can use it together with the if condition
// and not alongside it
Expand Down Expand Up @@ -149,6 +168,9 @@ export default class InternalModel {
error: any;

constructor(public store: CoreStore | Store, public identifier: StableRecordIdentifier) {
if (HAS_MODEL_PACKAGE) {
_getModelPackage();
}
this._id = identifier.id;
this.modelName = identifier.type;
this.clientId = identifier.lid;
Expand Down
4 changes: 2 additions & 2 deletions packages/store/addon/-private/system/normalize-model-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { dasherize } from '@ember/string';
This method normalizes a modelName into the format Ember Data uses
internally.

@method normalizeModelName
@public
@function normalizeModelName
@for @ember-data/store
@param {String} modelName
@return {String} normalizedModelName
*/
Expand Down
78 changes: 2 additions & 76 deletions packages/store/addon/-private/system/promise-proxies.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import ObjectProxy from '@ember/object/proxy';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import ArrayProxy from '@ember/array/proxy';
import { get, computed } from '@ember/object';
import { reads } from '@ember/object/computed';
import { Promise } from 'rsvp';
import { assert } from '@ember/debug';
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';

/**
@module @ember-data/store
*/

/*
/**
A `PromiseArray` is an object that acts like both an `Ember.Array`
and a promise. When the promise is resolved the resulting value
will be set to the `PromiseArray`'s `content` property. This makes
Expand Down Expand Up @@ -43,7 +40,7 @@ export const PromiseArray = ArrayProxy.extend(PromiseProxyMixin, {
meta: reads('content.meta'),
});

/*
/**
A `PromiseObject` is an object that acts like both an `EmberObject`
and a promise. When the promise is resolved, then the resulting value
will be set to the `PromiseObject`'s `content` property. This makes
Expand Down Expand Up @@ -84,74 +81,3 @@ export function promiseArray(promise, label) {
promise: Promise.resolve(promise, label),
});
}

export const PromiseBelongsTo = PromiseObject.extend({
// we don't proxy meta because we would need to proxy it to the relationship state container
// however, meta on relationships does not trigger change notifications.
// if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()`
meta: computed(function() {
assert(
'You attempted to access meta on the promise for the async belongsTo relationship ' +
`${this.get('_belongsToState').modelName}:${this.get('_belongsToState').key}'.` +
'\nUse `record.belongsTo(relationshipName).meta()` instead.',
false
);
}),

reload(options) {
assert('You are trying to reload an async belongsTo before it has been created', this.get('content') !== undefined);
let { key, store, originatingInternalModel } = this._belongsToState;

return store.reloadBelongsTo(this, originatingInternalModel, key, options).then(() => this);
},
});

export function proxyToContent(method) {
return function() {
return get(this, 'content')[method](...arguments);
};
}

/*
A PromiseManyArray is a PromiseArray that also proxies certain method calls
to the underlying manyArray.
Right now we proxy:

* `reload()`
* `createRecord()`
* `on()`
* `one()`
* `trigger()`
* `off()`
* `has()`

@class PromiseManyArray
@extends Ember.ArrayProxy
*/
export const PromiseManyArray = PromiseArray.extend({
links: FULL_LINKS_ON_RELATIONSHIPS ? reads('content.links') : undefined,

reload(options) {
assert('You are trying to reload an async manyArray before it has been created', get(this, 'content'));
this.set('promise', this.get('content').reload(options));
return this;
},

createRecord: proxyToContent('createRecord'),

on: proxyToContent('on'),

one: proxyToContent('one'),

trigger: proxyToContent('trigger'),

off: proxyToContent('off'),

has: proxyToContent('has'),
});

export function promiseManyArray(promise, label) {
return PromiseManyArray.create({
promise: Promise.resolve(promise, label),
});
}