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

🐛Fix bind data when start and end are the same #21566

Merged
merged 5 commits into from
Mar 26, 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
15 changes: 7 additions & 8 deletions extensions/amp-date-picker/0.1/amp-date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {createDeferred} from './react-utils';
import {createSingleDatePicker} from './single-date-picker';
import {dashToCamelCase} from '../../../src/string';
import {dev, user, userAssert} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {dict, map} from '../../../src/utils/object';
import {escapeCssSelectorIdent} from '../../../src/css';
import {once} from '../../../src/utils/function';
import {requireExternal} from '../../../src/module';
Expand Down Expand Up @@ -78,10 +78,10 @@ class BindDatesDetails {
this['dates'] = dates;

/** @const */
this['start'] = dates[0];
this['start'] = map(dates[0]);

/** @const */
this['end'] = dates[dates.length - 1];
this['end'] = map(dates[dates.length - 1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we verify that dates.length > 0 anywhere? Also, why did the original cause a circular reference error?

Copy link
Contributor Author

@cvializ cvializ Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your first question, getBindDates_ always takes at least one date, so the array will always have length 1 as currently written. But it could be good defensive programming practice to add a dev assert.
For your second question, when Bind attempts to deepMerge the new state with the old state, the deepMerge function marks objects in the new state as seen as it iterates through the state tree. If there is an object reference assigned to 2 properties of the new state object, it gets marked as seen and is encountered again, emitting the error. Not sure if this is WAI, but it's easy to work around.

amphtml/src/utils/object.js

Lines 105 to 107 in 391a431

if (seen.includes(s)) {
throw new Error('Source object has a circular reference.');
}

}
}

Expand Down Expand Up @@ -1453,14 +1453,13 @@ export class AmpDatePicker extends AMP.BaseElement {
*/
iterateDateRange_(startDate, endDate, cb) {
const normalizedEndDate = endDate || startDate;
if (!normalizedEndDate.isAfter(startDate)) {
if (!normalizedEndDate.isSameOrAfter(startDate)) {
return;
}

const index = startDate.clone();
while (!index.isAfter(normalizedEndDate)) {
cb(index.clone());
index.add(1, 'days');
const days = normalizedEndDate.diff(startDate, 'days') + 1;
for (let i = 0; i < days; i++) {
cb(startDate.clone().add(i + 1, 'days'));
}
}

Expand Down
13 changes: 13 additions & 0 deletions third_party/moment/moment.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ moment.prototype.isAfter = function(other) {};
*/
moment.prototype.isBetween = function(first, second) {};

/**
* @param {!moment} other
* @return {boolean}
*/
moment.prototype.isSameOrAfter = function(other) {};

/**
* @param {!moment} other
* @param {string} type
* @return {number}
*/
moment.prototype.diff = function(other, type) {};

/**
* @param {string} format
* @return {string}
Expand Down