Skip to content

Commit 8c30866

Browse files
committed
simplified tmp date creation for notify_datetime_changes
1 parent 2dad487 commit 8c30866

File tree

1 file changed

+55
-113
lines changed
  • packages/svelte/src/reactivity

1 file changed

+55
-113
lines changed

packages/svelte/src/reactivity/date.js

Lines changed: 55 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,70 @@ import { make_reactive } from './utils.js';
77
* we could check all of these edge-cases but I think that might become complicated very soon and introduce more bugs
88
* also there is the possibility of these behaviors to change as well,
99
* so creating a new date and applying the change is a better idea I guess
10-
* @param {Date} current_datetime
11-
* @param {Date} new_datetime
12-
* @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]>["notify_read_properties"]} notify_read_properties
10+
* @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]>} options
11+
* @param {unknown[]} params
1312
* @return {boolean} - returns true if any changes happened
1413
*/
15-
const notify_datetime_changes = (current_datetime, new_datetime, notify_read_properties) => {
16-
let had_time_changes = false;
17-
let had_date_changes = false;
14+
const notify_datetime_changes = (options, ...params) => {
15+
let is_time_changed = false;
16+
let is_date_changed = false;
1817

19-
if (current_datetime.getFullYear() !== new_datetime.getFullYear()) {
20-
notify_read_properties(['getFullYear', 'getUTCFullYear']);
21-
had_date_changes = true;
18+
const new_datetime = new Date(options.value);
19+
20+
// @ts-ignore
21+
new_datetime[options.property](...params);
22+
23+
if (options.value.getFullYear() !== new_datetime.getFullYear()) {
24+
options.notify_read_properties(['getFullYear', 'getUTCFullYear']);
25+
is_date_changed = true;
2226
}
2327

2428
// @ts-expect-error
25-
if (current_datetime.getYear && current_datetime.getYear() !== new_datetime.getYear()) {
29+
if (options.value.getYear && options.value.getYear() !== new_datetime.getYear()) {
2630
// @ts-expect-error
27-
notify_read_properties(['getYear']);
28-
had_date_changes = true;
31+
options.notify_read_properties(['getYear']);
32+
is_date_changed = true;
2933
}
3034

31-
if (current_datetime.getMonth() !== new_datetime.getMonth()) {
32-
notify_read_properties(['getMonth', 'getUTCMonth']);
33-
had_date_changes = true;
35+
if (options.value.getMonth() !== new_datetime.getMonth()) {
36+
options.notify_read_properties(['getMonth', 'getUTCMonth']);
37+
is_date_changed = true;
3438
}
3539

36-
if (current_datetime.getDay() !== new_datetime.getDay()) {
37-
notify_read_properties(['getDay', 'getUTCDay']);
38-
had_date_changes = true;
40+
if (options.value.getDay() !== new_datetime.getDay()) {
41+
options.notify_read_properties(['getDay', 'getUTCDay']);
42+
is_date_changed = true;
3943
}
4044

41-
if (current_datetime.getHours() !== new_datetime.getHours()) {
42-
notify_read_properties(['getHours', 'getUTCHours']);
43-
had_time_changes = true;
45+
if (options.value.getHours() !== new_datetime.getHours()) {
46+
options.notify_read_properties(['getHours', 'getUTCHours']);
47+
is_time_changed = true;
4448
}
4549

46-
if (current_datetime.getMinutes() !== new_datetime.getMinutes()) {
47-
notify_read_properties(['getMinutes', 'getUTCMinutes']);
48-
had_time_changes = true;
50+
if (options.value.getMinutes() !== new_datetime.getMinutes()) {
51+
options.notify_read_properties(['getMinutes', 'getUTCMinutes']);
52+
is_time_changed = true;
4953
}
5054

51-
if (current_datetime.getSeconds() !== new_datetime.getSeconds()) {
52-
notify_read_properties(['getSeconds', 'getUTCSeconds']);
53-
had_time_changes = true;
55+
if (options.value.getSeconds() !== new_datetime.getSeconds()) {
56+
options.notify_read_properties(['getSeconds', 'getUTCSeconds']);
57+
is_time_changed = true;
5458
}
5559

56-
if (current_datetime.getMilliseconds() !== new_datetime.getMilliseconds()) {
57-
notify_read_properties(['getMilliseconds', 'getUTCMilliseconds']);
58-
had_time_changes = true;
60+
if (options.value.getMilliseconds() !== new_datetime.getMilliseconds()) {
61+
options.notify_read_properties(['getMilliseconds', 'getUTCMilliseconds']);
62+
is_time_changed = true;
5963
}
6064

61-
if (had_time_changes) {
62-
notify_read_properties(['toTimeString', 'toLocaleTimeString']);
65+
if (is_time_changed) {
66+
options.notify_read_properties(['toTimeString', 'toLocaleTimeString']);
6367
}
6468

65-
if (had_date_changes) {
66-
notify_read_properties(['toDateString', 'toLocaleDateString']);
69+
if (is_date_changed) {
70+
options.notify_read_properties(['toDateString', 'toLocaleDateString']);
6771
}
6872

69-
return had_date_changes || had_time_changes;
73+
return is_date_changed || is_time_changed;
7074
};
7175

7276
export const ReactiveDate = make_reactive(Date, {
@@ -116,119 +120,57 @@ export const ReactiveDate = make_reactive(Date, {
116120
],
117121
interceptors: {
118122
setDate: (options, ...params) => {
119-
const tmp = new Date(options.value);
120-
tmp.setDate(/**@type {number}*/ (params[0]));
121-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
123+
return notify_datetime_changes(options, ...params);
122124
},
123125
setFullYear: (options, ...params) => {
124-
const tmp = new Date(options.value);
125-
tmp.setFullYear(
126-
/**@type {number}*/ (params[0]),
127-
/**@type {number | undefined}*/ (params[1]),
128-
/**@type {number | undefined}*/ (params[2])
129-
);
130-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
126+
return notify_datetime_changes(options, ...params);
131127
},
132128
setHours: (options, ...params) => {
133-
const tmp = new Date(options.value);
134-
tmp.setHours(
135-
/**@type {number}*/ (params[0]),
136-
/**@type {number | undefined}*/ (params[1]),
137-
/**@type {number | undefined}*/ (params[2]),
138-
/**@type {number | undefined}*/ (params[3])
139-
);
140-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
129+
return notify_datetime_changes(options, ...params);
141130
},
142131
setMilliseconds: (options, ...params) => {
143-
const tmp = new Date(options.value);
144-
tmp.setMilliseconds(/**@type {number}*/ (params[0]));
145-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
132+
return notify_datetime_changes(options, ...params);
146133
},
147134
setMinutes: (options, ...params) => {
148-
const tmp = new Date(options.value);
149-
tmp.setMinutes(
150-
/**@type {number}*/ (params[0]),
151-
/**@type {number | undefined}*/ (params[1]),
152-
/**@type {number | undefined}*/ (params[2])
153-
);
154-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
135+
return notify_datetime_changes(options, ...params);
155136
},
156137
setMonth: (options, ...params) => {
157-
const tmp = new Date(options.value);
158-
tmp.setMonth(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
159-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
138+
return notify_datetime_changes(options, ...params);
160139
},
161140
setSeconds: (options, ...params) => {
162-
const tmp = new Date(options.value);
163-
tmp.setSeconds(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
164-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
141+
return notify_datetime_changes(options, ...params);
165142
},
166143
setTime: (options, ...params) => {
167-
const tmp = new Date(options.value);
168-
tmp.setTime(/**@type {number}*/ (params[0]));
169-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
144+
return notify_datetime_changes(options, ...params);
170145
},
171146
setUTCDate: (options, ...params) => {
172-
const tmp = new Date(options.value);
173-
tmp.setUTCDate(/**@type {number}*/ (params[0]));
174-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
147+
return notify_datetime_changes(options, ...params);
175148
},
176149
setUTCFullYear: (options, ...params) => {
177-
const tmp = new Date(options.value);
178-
tmp.setUTCFullYear(
179-
/**@type {number}*/ (params[0]),
180-
/**@type {number | undefined}*/ (params[1]),
181-
/**@type {number | undefined}*/ (params[2])
182-
);
183-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
150+
return notify_datetime_changes(options, ...params);
184151
},
185152
setUTCHours: (options, ...params) => {
186-
const tmp = new Date(options.value);
187-
tmp.setUTCHours(
188-
/**@type {number}*/ (params[0]),
189-
/**@type {number | undefined}*/ (params[1]),
190-
/**@type {number | undefined}*/ (params[2]),
191-
/**@type {number | undefined}*/ (params[3])
192-
);
193-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
153+
return notify_datetime_changes(options, ...params);
194154
},
195155
setUTCMilliseconds: (options, ...params) => {
196-
const tmp = new Date(options.value);
197-
tmp.setUTCMilliseconds(/**@type {number}*/ (params[0]));
198-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
156+
return notify_datetime_changes(options, ...params);
199157
},
200158
setUTCMinutes: (options, ...params) => {
201-
const tmp = new Date(options.value);
202-
tmp.setUTCMinutes(
203-
/**@type {number}*/ (params[0]),
204-
/**@type {number | undefined}*/ (params[1]),
205-
/**@type {number | undefined}*/ (params[2])
206-
);
207-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
159+
return notify_datetime_changes(options, ...params);
208160
},
209161
setUTCMonth: (options, ...params) => {
210-
const tmp = new Date(options.value);
211-
tmp.setUTCMonth(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
212-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
162+
return notify_datetime_changes(options, ...params);
213163
},
214164
setUTCSeconds: (options, ...params) => {
215-
const tmp = new Date(options.value);
216-
tmp.setUTCSeconds(
217-
/**@type {number}*/ (params[0]),
218-
/**@type {number | undefined}*/ (params[1])
219-
);
220-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
165+
return notify_datetime_changes(options, ...params);
221166
},
222167
// @ts-expect-error - deprecated method
223168
setYear: (options, ...params) => {
224169
// it might be removed from browsers
225170
if (!options.value.getYear) {
226171
return false;
227172
}
228-
const tmp = new Date(options.value);
229-
// @ts-expect-error
230-
tmp.setYear(/**@type {number}*/ (params[0]));
231-
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
173+
return notify_datetime_changes(options, ...params);
232174
}
233175
}
234176
});

0 commit comments

Comments
 (0)