Skip to content

Commit

Permalink
better way of doing resetOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Jan 27, 2023
1 parent 813d6f9 commit 9a1b7d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
19 changes: 7 additions & 12 deletions lib/src/full-calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Calendar, CalendarOptions } from '@fullcalendar/core';
import { CustomRendering, CustomRenderingStore } from '@fullcalendar/core/internal';
import { OPTION_INPUT_NAMES, OPTION_IS_DEEP } from './options';
import { CalendarOption, CalendarTemplateRef } from './private-types';
import { deepCopy, shallowCopy, mapHash } from './utils/obj';
import { deepCopy, mapHash } from './utils/obj';
import { deepEqual } from './utils/fast-deep-equal';

@Component({
Expand Down Expand Up @@ -110,7 +110,7 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
...this.buildInputOptions(),
};
const newProcessedOptions: Record<string, any> = {};
let anyChanges = false;
const changedOptionNames: string[] = []

// detect adds and updates (and update snapshot)
for (const optionName in newOptions) {
Expand All @@ -120,17 +120,12 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
if (deepChangeDetection && OPTION_IS_DEEP[optionName]) {
if (!deepEqual(optionSnapshot[optionName], optionVal)) {
optionSnapshot[optionName] = deepCopy(optionVal);
anyChanges = true;

// trick FC into knowing about a nested change.
// TODO: future versions won't need this.
// can't use the previously-made deep copy because it blows away prototype-association.
optionVal = shallowCopy(optionVal);
changedOptionNames.push(optionName);
}
} else {
if (optionSnapshot[optionName] !== optionVal) {
optionSnapshot[optionName] = optionVal;
anyChanges = true;
changedOptionNames.push(optionName);
}
}

Expand All @@ -144,16 +139,16 @@ export class FullCalendarComponent implements AfterViewInit, DoCheck, AfterConte
for (const optionName of oldOptionNames) {
if (!(optionName in newOptions)) { // doesn't exist in new options?
delete optionSnapshot[optionName];
anyChanges = true;
changedOptionNames.push(optionName);
}
}

if (anyChanges) {
if (changedOptionNames.length) {
this.calendar.pauseRendering();
this.calendar.resetOptions({
...newProcessedOptions,
...this.buildExtraOptions(),
});
}, changedOptionNames);
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions lib/src/utils/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ export function deepCopy(input: any): any {
}


export function shallowCopy(val: any): any {
if (typeof val === 'object') {
if (Array.isArray(val)) {
val = Array.prototype.slice.call(val);
} else if (val) { // non-null
val = { ...val };
}
}
return val;
}


export function mapHash(input: any, func: any): any {
const output: { [key: string]: any } = {};

Expand Down

0 comments on commit 9a1b7d7

Please sign in to comment.