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

Lazy snapshot.type #4885

Merged
merged 2 commits into from
Mar 31, 2017
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
235 changes: 121 additions & 114 deletions addon/-private/system/snapshot-record-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,149 +10,156 @@
@param {Array} snapshots An array of snapshots
@param {Object} meta
*/
export default function SnapshotRecordArray(recordArray, meta, options = {}) {
/**
An array of snapshots
@private
@property _snapshots
@type {Array}
*/
this._snapshots = null;
/**
An array of records
@private
@property _recordArray
@type {Array}
*/
this._recordArray = recordArray;
export default class SnapshotRecordArray {
constructor(recordArray, meta, options = {}) {
/**
An array of snapshots
@private
@property _snapshots
@type {Array}
*/
this._snapshots = null;

/**
An array of records
@private
@property _recordArray
@type {Array}
*/
this._recordArray = recordArray;

/**
Number of records in the array

Example

```app/adapters/post.js
import DS from 'ember-data'

export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotRecordArray) {
return !snapshotRecordArray.length;
},
});
```

/**
Number of records in the array
@property length
@type {Number}
*/
this.length = recordArray.get('length');

Example
this._type = null;

```app/adapters/post.js
import DS from 'ember-data'
/**
Meta objects for the record array.

export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotRecordArray) {
return !snapshotRecordArray.length;
},
});
```
Example

@property length
@type {Number}
*/
this.length = recordArray.get('length');
```app/adapters/post.js
import DS from 'ember-data'

/**
The type of the underlying records for the snapshots in the array, as a DS.Model
@property type
@type {DS.Model}
*/
this.type = recordArray.get('type');
export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotRecordArray) {
var lastRequestTime = snapshotRecordArray.meta.lastRequestTime;
var twentyMinutes = 20 * 60 * 1000;
return Date.now() > lastRequestTime + twentyMinutes;
},
});
```

/**
Meta objects for the record array.
@property meta
@type {Object}
*/
this.meta = meta;

Example
/**
A hash of adapter options passed into the store method for this request.

```app/adapters/post.js
import DS from 'ember-data'
Example

export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotRecordArray) {
var lastRequestTime = snapshotRecordArray.meta.lastRequestTime;
var twentyMinutes = 20 * 60 * 1000;
return Date.now() > lastRequestTime + twentyMinutes;
},
});
```
```app/adapters/post.js
import MyCustomAdapter from './custom-adapter';

@property meta
@type {Object}
*/
this.meta = meta;
export default MyCustomAdapter.extend({
findAll(store, type, sinceToken, snapshotRecordArray) {
if (snapshotRecordArray.adapterOptions.subscribe) {
// ...
}
// ...
}
});
```

/**
A hash of adapter options passed into the store method for this request.
@property adapterOptions
@type {Object}
*/
this.adapterOptions = options.adapterOptions;

Example
/**
The relationships to include for this request.

```app/adapters/post.js
import MyCustomAdapter from './custom-adapter';
Example

export default MyCustomAdapter.extend({
findAll(store, type, sinceToken, snapshotRecordArray) {
if (snapshotRecordArray.adapterOptions.subscribe) {
// ...
```app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
findAll(store, type, snapshotRecordArray) {
var url = `/${type.modelName}?include=${encodeURIComponent(snapshotRecordArray.include)}`;

return fetch(url).then((response) => response.json())
}
// ...
}
});
```
});

@property include
@type {String|Array}
*/
this.include = options.include;
}

@property adapterOptions
@type {Object}
/**
The type of the underlying records for the snapshots in the array, as a DS.Model
@property type
@type {DS.Model}
*/
this.adapterOptions = options.adapterOptions;
get type() {
return this._type || (this._type = this._recordArray.get('type'));
}

/**
The relationships to include for this request.
Get snapshots of the underlying record array

Example

```app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
findAll(store, type, snapshotRecordArray) {
var url = `/${type.modelName}?include=${encodeURIComponent(snapshotRecordArray.include)}`;
```app/adapters/post.js
import DS from 'ember-data'

return fetch(url).then((response) => response.json())
export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotArray) {
var snapshots = snapshotArray.snapshots();

return snapshots.any(function(ticketSnapshot) {
var timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
if (timeDiff > 20) {
return true;
} else {
return false;
}
});
}
});
```

@property include
@type {String|Array}
@method snapshots
@return {Array} Array of snapshots
*/
this.include = options.include;
}

/**
Get snapshots of the underlying record array

Example

```app/adapters/post.js
import DS from 'ember-data'

export default DS.JSONAPIAdapter.extend({
shouldReloadAll(store, snapshotArray) {
var snapshots = snapshotArray.snapshots();

return snapshots.any(function(ticketSnapshot) {
var timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
if (timeDiff > 20) {
return true;
} else {
return false;
}
});
snapshots() {
if (this._snapshots !== null) {
return this._snapshots;
}
});
```

@method snapshots
@return {Array} Array of snapshots
*/
SnapshotRecordArray.prototype.snapshots = function() {
if (this._snapshots !== null) {
this._snapshots = this._recordArray._takeSnapshot();

return this._snapshots;
}

this._snapshots = this._recordArray._takeSnapshot();

return this._snapshots;
};
}
24 changes: 13 additions & 11 deletions addon/-private/system/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ export default class Snapshot {
this.adapterOptions = options.adapterOptions;
this.include = options.include;

/**
The type of the underlying record for this snapshot, as a DS.Model.

@property type
@type {DS.Model}
*/
// TODO @runspired we should deprecate this in favor of modelClass but only once
// we've cleaned up the internals enough that a public change to follow suite is
// uncontroversial.
this.type = internalModel.modelClass;

/**
The name of the type of the underlying record for this snapshot, as a string.

Expand All @@ -87,6 +76,19 @@ export default class Snapshot {
this._changedAttributes = record.changedAttributes();
}

/**
The type of the underlying record for this snapshot, as a DS.Model.

@property type
@type {DS.Model}
*/
get type() {
// TODO @runspired we should deprecate this in favor of modelClass but only once
// we've cleaned up the internals enough that a public change to follow suite is
// uncontroversial.
return this._internalModel.modelClass;
}

/**
Returns the value of an attribute.

Expand Down
31 changes: 31 additions & 0 deletions tests/integration/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ test("snapshot.id, snapshot.type and snapshot.modelName returns correctly", func
});
});

test('snapshot.type loads the class lazily', function(assert) {
assert.expect(3);

let postClassLoaded = false;
let modelFor = env.store._modelFor;
env.store._modelFor = (name) => {
if (name === 'post') {
postClassLoaded = true;
}
return modelFor.call(env.store, name);
};

run(function() {
env.store.push({
data: {
type: 'post',
id: '1',
attributes: {
title: 'Hello World'
}
}
});
let postInternalModel = env.store._internalModelForId('post', 1);
let snapshot = postInternalModel.createSnapshot();

assert.equal(false, postClassLoaded, 'model class is not eagerly loaded');
assert.equal(snapshot.type, Post, 'type is correct');
assert.equal(true, postClassLoaded, 'model class is loaded');
});
});

test("snapshot.attr() does not change when record changes", function(assert) {
assert.expect(2);

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/system/snapshot-record-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,27 @@ test('#snapshot', function(assert) {
assert.equal(snapshot.snapshots(), snapshotTaken, 'should return the exact same snapshot');
assert.equal(didTakeSnapshot, 1, 'still only one snapshot should have been taken');
});

test('SnapshotRecordArray.type loads the class lazily', function(assert) {
let array = Ember.A([1, 2]);
let typeLoaded = false;

Object.defineProperty(array, 'type', {
get() {
typeLoaded = true;
return 'some type';
}
});

let meta = { };
let options = {
adapterOptions: 'some options',
include: 'include me'
};

let snapshot = new SnapshotRecordArray(array, meta, options);

assert.equal(false, typeLoaded, 'model class is not eager loaded');
assert.equal(snapshot.type, 'some type');
assert.equal(true, typeLoaded, 'model class is loaded');
});