Skip to content

Commit

Permalink
[CHORE] RFC-0329 - adding deprecation about using evented in ember-data
Browse files Browse the repository at this point in the history
  • Loading branch information
pete committed Apr 24, 2019
1 parent 67bf0b0 commit bacce7a
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 27 deletions.
75 changes: 75 additions & 0 deletions packages/store/addon/-private/system/deprecated-evented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import Evented from '@ember/object/evented';
import Mixin from '@ember/object/mixin';
import { deprecate } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

let INSTANCE_DEPRECATIONS;
let lookupDeprecations;
let DeprecatedEvented;

if (DEBUG) {
INSTANCE_DEPRECATIONS = new WeakMap();

lookupDeprecations = function lookupInstanceDrecations(instance) {
let deprecations = INSTANCE_DEPRECATIONS.get(instance);

if (!deprecations) {
deprecations = {};
INSTANCE_DEPRECATIONS.set(instance, deprecations);
}

return deprecations;
};

DeprecatedEvented = Mixin.create(Evented, {
/**
* Provides a way to call Evented without logging deprecation warnings
* @param {String} name
*/
_has(name) {
return Evented.mixins[0].properties.has.call(this, name);
},

_deprecateEvented(eventName) {
let deprecations = lookupDeprecations(this);
const _deprecationData = this._getDeprecatedEventedInfo
? `on ${this._getDeprecatedEventedInfo()}`
: '';
const deprecationMessage = _deprecationData
? `Called ${eventName} ${_deprecationData}`
: eventName;
deprecate(deprecationMessage, deprecations[eventName], {
id: 'ember-evented',
until: '3.12',
});
deprecations[eventName] = true;
},

has(name) {
this._deprecateEvented(name);
return this._super(...arguments);
},

off(name, target, method) {
this._deprecateEvented(name);
return this._super(...arguments);
},

on(name, target, method) {
this._deprecateEvented(name);
return this._super(...arguments);
},

one(name, target, method) {
this._deprecateEvented(name);
return this._super(...arguments);
},

trigger(name) {
this._deprecateEvented(name);
return this._super(...arguments);
},
});
}

export default (DEBUG ? DeprecatedEvented : Evented);
7 changes: 4 additions & 3 deletions packages/store/addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/
import { all } from 'rsvp';

import Evented from '@ember/object/evented';
//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';
Expand Down Expand Up @@ -54,9 +55,9 @@ import recordDataFor from './record-data-for';
@class ManyArray
@namespace DS
@extends EmberObject
@uses Ember.MutableArray, Ember.Evented
@uses Ember.MutableArray, EmberData.DeprecatedEvent
*/
export default EmberObject.extend(MutableArray, Evented, {
export default EmberObject.extend(MutableArray, DeprecatedEvent, {
init() {
this._super(...arguments);

Expand Down
32 changes: 16 additions & 16 deletions packages/store/addon/-private/system/model/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapBy, not } from '@ember/object/computed';
import Evented from '@ember/object/evented';
import DeprecatedEvent from '../deprecated-evented';
import ArrayProxy from '@ember/array/proxy';
import { get, computed } from '@ember/object';
import { makeArray, A } from '@ember/array';
Expand Down Expand Up @@ -82,7 +82,7 @@ import { makeArray, A } from '@ember/array';
@extends Ember.ArrayProxy
@uses Ember.Evented
*/
export default ArrayProxy.extend(Evented, {
export default ArrayProxy.extend(DeprecatedEvent, {
/**
Register with target handler
Expand Down Expand Up @@ -191,22 +191,22 @@ export default ArrayProxy.extend(Evented, {
Example
```javascript
let errors = get(user, 'errors');
// add multiple errors
errors.add('password', [
'Must be at least 12 characters',
'Must contain at least one symbol',
'Cannot contain your name'
]);
errors.errorsFor('password');
// =>
// [
// { attribute: 'password', message: 'Must be at least 12 characters' },
// { attribute: 'password', message: 'Must contain at least one symbol' },
// { attribute: 'password', message: 'Cannot contain your name' },
// ]
// add a single error
errors.add('username', 'This field is required');
Expand Down Expand Up @@ -280,16 +280,16 @@ export default ArrayProxy.extend(Evented, {
```javascript
let errors = get('user', errors);
errors.add('phone', ['error-1', 'error-2']);
errors.errorsFor('phone');
// =>
// [
// { attribute: 'phone', message: 'error-1' },
// { attribute: 'phone', message: 'error-2' },
// ]
errors.remove('phone');
errors.errorsFor('phone');
// => undefined
```
Expand Down Expand Up @@ -331,35 +331,35 @@ export default ArrayProxy.extend(Evented, {
Manually clears all errors for the record.
This will transition the record into a `valid` state, and
will trigger the `becameValid` event and lifecycle method.
Example:
```javascript
let errors = get('user', errors);
errors.add('username', ['error-a']);
errors.add('phone', ['error-1', 'error-2']);
errors.errorsFor('username');
// =>
// [
// { attribute: 'username', message: 'error-a' },
// ]
errors.errorsFor('phone');
// =>
// [
// { attribute: 'phone', message: 'error-1' },
// { attribute: 'phone', message: 'error-2' },
// ]
errors.clear();
errors.errorsFor('username');
// => undefined
errors.errorsFor('phone');
// => undefined
errors.get('messages')
// => []
```
Expand Down
38 changes: 35 additions & 3 deletions packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ import { default as recordDataFor, relationshipStateFor } from '../record-data-f
import RecordDataDefault from './record-data';
import RecordData from '../../ts-interfaces/record-data';
import { JsonApiResource } from '../../ts-interfaces/record-data-json-api';
import { deprecate } from '@ember/application/deprecations';

let INSTANCE_DEPRECATIONS;
let lookupDeprecations;

if (DEBUG) {
INSTANCE_DEPRECATIONS = new WeakMap();

lookupDeprecations = function lookupInstanceDrecations(instance) {
let deprecations = INSTANCE_DEPRECATIONS.get(instance);

if (!deprecations) {
deprecations = {};
INSTANCE_DEPRECATIONS.set(instance, deprecations);
}

return deprecations;
};
}

/*
The TransitionChainMap caches the `state.enters`, `state.setups`, and final state reached
Expand Down Expand Up @@ -301,6 +320,19 @@ export default class InternalModel {
}

this._record = store._modelFactoryFor(this.modelName).create(createOptions);
if (DEBUG) {
let klass = this._record.constructor;
let deprecations = lookupDeprecations(klass);
debugger;
['becameError', 'becameInvalid', 'didCreate', 'didDelete', 'didLoad', 'didUpdate', 'ready', 'rolledBack'].forEach(methodName => {
if (typeof this._record[methodName] === 'function') {
deprecate(`Attempted to define ${methodName} on ${this._record.modelName}#${this._record.id}`, deprecations[methodName], {
id: 'ember-evented',
until: '3.12'
})
}
});
}

this._triggerDeferredTriggers();
}
Expand Down Expand Up @@ -503,9 +535,9 @@ export default class InternalModel {
let parentInternalModel = this;
let async = relationshipMeta.options.async;
let isAsync = typeof async === 'undefined' ? true : async;
let _belongsToState = {
key,
store,
let _belongsToState = {
key,
store,
originatingInternalModel: this,
modelName: relationshipMeta.type
}
Expand Down
11 changes: 8 additions & 3 deletions packages/store/addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isNone } from '@ember/utils';
import EmberError from '@ember/error';
import Evented from '@ember/object/evented';
//import Evented from '@ember/object/evented';
import DeprecatedEvented from '../deprecated-evented';
import EmberObject, { computed, get } from '@ember/object';
import { DEBUG } from '@glimmer/env';
import { assert, warn, deprecate } from '@ember/debug';
Expand Down Expand Up @@ -68,9 +69,9 @@ const retrieveFromCurrentState = computed('currentState', function(key) {
@class Model
@namespace DS
@extends EmberObject
@uses Ember.Evented
@uses EmberData.DeprecatedEvented
*/
const Model = EmberObject.extend(Evented, {
const Model = EmberObject.extend(DeprecatedEvented, {
/**
If this property is `true` the record is in the `empty`
state. Empty is the first state all records enter after they have
Expand Down Expand Up @@ -1193,6 +1194,10 @@ if (DEBUG) {
init() {
this._super(...arguments);

if (DEBUG) {
this._getDeprecatedEventedInfo = () => `${this._internalModel.modelName}#${this.id}`;
}

if (!this._internalModel) {
throw new EmberError(
'You should not call `create` on a model. Instead, call `store.createRecord` with the attributes you would like to set.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { A } from '@ember/array';
import { get } from '@ember/object';
import RecordArray from './record-array';
import cloneNull from '../clone-null';
import { DEBUG } from '@glimmer/env';

/**
Represents an ordered list of records whose order and membership is
Expand Down Expand Up @@ -50,6 +51,11 @@ export default RecordArray.extend({
this._super(...arguments);
this.query = this.query || null;
this.links = this.links || null;

if (DEBUG) {
this._getDeprecatedEventedInfo = () =>
`AdapterPopulatedRecordArray containing ${this.modelName} for query: ${this.query}`;
}
},

replace() {
Expand Down Expand Up @@ -84,6 +90,9 @@ export default RecordArray.extend({

this.manager._associateWithRecordArray(internalModels, this);

if (DEBUG && this._has('didLoad')) {
this._deprecateEvented('didLoad');
}
// TODO: should triggering didLoad event be the last action of the runLoop?
once(this, 'trigger', 'didLoad');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
@module ember-data
*/

import Evented from '@ember/object/evented';
//import Evented from '@ember/object/evented';
import DeprecatedEvented from '../deprecated-evented';

import ArrayProxy from '@ember/array/proxy';
import { set, get, computed } from '@ember/object';
import { Promise } from 'rsvp';
import { DEBUG } from '@glimmer/env';
import { PromiseArray } from '../promise-proxies';
import SnapshotRecordArray from '../snapshot-record-array';

Expand All @@ -23,10 +25,14 @@ import SnapshotRecordArray from '../snapshot-record-array';
@uses Ember.Evented
*/

export default ArrayProxy.extend(Evented, {
export default ArrayProxy.extend(DeprecatedEvented, {
init() {
this._super(...arguments);

if (DEBUG) {
this._getDeprecatedEventedInfo = () => `RecordArray containing ${this.modelName}`;
}

/**
The array of client ids backing the record array. When a
record is requested from the record array, the record
Expand Down

0 comments on commit bacce7a

Please sign in to comment.