Skip to content

Commit 9d0f1ff

Browse files
committed
perf: retrieve a range of months at once
1 parent 92a8073 commit 9d0f1ff

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
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: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,40 @@ export class FcDatePicker extends DatePicker {
4646
}
4747
};
4848

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

5379
const _updateMonthStyles = function(element) {
5480
const month = element.month;
55-
const key = month.toISOString().substr(0,7);
5681
_clearStyles.call(element);
57-
_getStyle(key).then(styles=>setTimeout(()=>{
82+
_getStyle(month).then(styles=>setTimeout(()=>{
5883
if (element.month===month) {
5984
for (let i in styles) {
6085
_setStyleForDay.call(element,i,styles[i]);

0 commit comments

Comments
 (0)