Skip to content

Commit 9cc4d1e

Browse files
committed
perf: retrieve a range of months at once
1 parent 567228b commit 9cc4d1e

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/main/java/com/flowingcode/addons/ycalendar/DatePickerEx.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,26 @@ public void refreshAll() {
7474
}
7575

7676
@ClientCallable
77-
private JsonObject fetchStyles(String yearMonthStr) {
78-
YearMonth yearMonth = YearMonth.parse(yearMonthStr);
77+
private JsonObject fetchStyles(String minStr, String maxStr) {
78+
JsonObject result = Json.createObject();
79+
YearMonth min = YearMonth.parse(minStr);
80+
YearMonth max = YearMonth.parse(maxStr);
81+
for (YearMonth m = min; m.compareTo(max) <= 0; m = m.plusMonths(1)) {
82+
String key = m.toString();
83+
getStyles(m).ifPresent(styles -> result.put(key, styles));
84+
}
85+
return result;
86+
}
87+
88+
private Optional<JsonObject> getStyles(YearMonth yearMonth) {
7989
JsonObject monthStyles = Json.createObject();
8090
for (int i = 1, n = yearMonth.lengthOfMonth(); i <= n; i++) {
8191
LocalDate date = yearMonth.atDay(i);
8292
Optional.ofNullable(classNameGenerator).map(g -> g.apply(date)).ifPresent(className -> {
8393
monthStyles.put(Integer.toString(date.getDayOfMonth()), className);
8494
});
8595
}
86-
return monthStyles;
96+
return monthStyles.keys().length > 0 ? Optional.of(monthStyles) : Optional.empty();
8797
}
8898

8999
}

src/main/resources/META-INF/frontend/fc-date-picker/fc-date-picker.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,43 @@ export class FcDatePicker extends DatePicker {
4747
}
4848
};
4949

50-
const _getStyle = key => {
51-
return this._styles[key] || (this._styles[key] = this.$server.fetchStyles(key));
50+
const _getStyle = month => {
51+
const tostr = date => date.toISOString().substr(0,7);
52+
const add = (date, delta) => new Date(date.getFullYear(), date.getMonth()+delta, 1);
53+
54+
const key = tostr(month);
55+
56+
if (!this._styles[key]) {
57+
let min = month;
58+
let max = month;
59+
const N = 50;
60+
for (let i=0;i<N;i++) {
61+
min=add(min,-1);
62+
if(this._styles[tostr(min)]) {min=add(min,+1); break;}
63+
}
64+
for (let i=0;i<N;i++) {
65+
max=add(max,+1);
66+
if(this._styles[tostr(max)]) {max=add(max,-1); break;}
67+
}
68+
69+
const fetched = this.$server.fetchStyles(tostr(min), tostr(max)).catch(err => {
70+
console.error('fetchStyles '+tostr(min)+' '+tostr(max)+' failed', err);
71+
return {};
72+
});
73+
for (let m=min;m<=max;m=add(m,1)) {
74+
const k = tostr(m);
75+
this._styles[k] = new Promise(resolve => {
76+
fetched.then(styles=>resolve(styles[k] || {}));
77+
});
78+
}
79+
}
80+
return this._styles[key];
5281
}
5382

5483
const _updateMonthStyles = function(element) {
5584
const month = element.month;
56-
const key = month.toISOString().substr(0,7);
5785
_clearStyles.call(element);
58-
_getStyle(key).then(styles=>setTimeout(()=>{
86+
_getStyle(month).then(styles=>setTimeout(()=>{
5987
if (element.month===month) {
6088
for (let i in styles) {
6189
_setStyleForDay.call(element,i,styles[i]);
@@ -77,7 +105,7 @@ export class FcDatePicker extends DatePicker {
77105
setTimeout(()=> _updateMonthStyles(calendar));
78106
}
79107
});
80-
return calendar;
108+
return calendar;
81109
}
82110
}
83111
};

0 commit comments

Comments
 (0)