|
|
|
@ -7,66 +7,70 @@ import { make_reactive } from './utils.js';
|
|
|
|
* we could check all of these edge-cases but I think that might become complicated very soon and introduce more bugs
|
|
|
|
* we could check all of these edge-cases but I think that might become complicated very soon and introduce more bugs
|
|
|
|
* also there is the possibility of these behaviors to change as well,
|
|
|
|
* also there is the possibility of these behaviors to change as well,
|
|
|
|
* so creating a new date and applying the change is a better idea I guess
|
|
|
|
* so creating a new date and applying the change is a better idea I guess
|
|
|
|
* @param {Date} current_datetime
|
|
|
|
* @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]>} options
|
|
|
|
* @param {Date} new_datetime
|
|
|
|
* @param {unknown[]} params
|
|
|
|
* @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]>["notify_read_properties"]} notify_read_properties
|
|
|
|
|
|
|
|
* @return {boolean} - returns true if any changes happened
|
|
|
|
* @return {boolean} - returns true if any changes happened
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
const notify_datetime_changes = (current_datetime, new_datetime, notify_read_properties) => {
|
|
|
|
const notify_datetime_changes = (options, ...params) => {
|
|
|
|
let had_time_changes = false;
|
|
|
|
let is_time_changed = false;
|
|
|
|
let had_date_changes = false;
|
|
|
|
let is_date_changed = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getFullYear() !== new_datetime.getFullYear()) {
|
|
|
|
const new_datetime = new Date(options.value);
|
|
|
|
notify_read_properties(['getFullYear', 'getUTCFullYear']);
|
|
|
|
|
|
|
|
had_date_changes = true;
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
new_datetime[options.property](...params);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (options.value.getFullYear() !== new_datetime.getFullYear()) {
|
|
|
|
|
|
|
|
options.notify_read_properties(['getFullYear', 'getUTCFullYear']);
|
|
|
|
|
|
|
|
is_date_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
// @ts-expect-error
|
|
|
|
if (current_datetime.getYear && current_datetime.getYear() !== new_datetime.getYear()) {
|
|
|
|
if (options.value.getYear && options.value.getYear() !== new_datetime.getYear()) {
|
|
|
|
// @ts-expect-error
|
|
|
|
// @ts-expect-error
|
|
|
|
notify_read_properties(['getYear']);
|
|
|
|
options.notify_read_properties(['getYear']);
|
|
|
|
had_date_changes = true;
|
|
|
|
is_date_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getMonth() !== new_datetime.getMonth()) {
|
|
|
|
if (options.value.getMonth() !== new_datetime.getMonth()) {
|
|
|
|
notify_read_properties(['getMonth', 'getUTCMonth']);
|
|
|
|
options.notify_read_properties(['getMonth', 'getUTCMonth']);
|
|
|
|
had_date_changes = true;
|
|
|
|
is_date_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getDay() !== new_datetime.getDay()) {
|
|
|
|
if (options.value.getDay() !== new_datetime.getDay()) {
|
|
|
|
notify_read_properties(['getDay', 'getUTCDay']);
|
|
|
|
options.notify_read_properties(['getDay', 'getUTCDay']);
|
|
|
|
had_date_changes = true;
|
|
|
|
is_date_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getHours() !== new_datetime.getHours()) {
|
|
|
|
if (options.value.getHours() !== new_datetime.getHours()) {
|
|
|
|
notify_read_properties(['getHours', 'getUTCHours']);
|
|
|
|
options.notify_read_properties(['getHours', 'getUTCHours']);
|
|
|
|
had_time_changes = true;
|
|
|
|
is_time_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getMinutes() !== new_datetime.getMinutes()) {
|
|
|
|
if (options.value.getMinutes() !== new_datetime.getMinutes()) {
|
|
|
|
notify_read_properties(['getMinutes', 'getUTCMinutes']);
|
|
|
|
options.notify_read_properties(['getMinutes', 'getUTCMinutes']);
|
|
|
|
had_time_changes = true;
|
|
|
|
is_time_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getSeconds() !== new_datetime.getSeconds()) {
|
|
|
|
if (options.value.getSeconds() !== new_datetime.getSeconds()) {
|
|
|
|
notify_read_properties(['getSeconds', 'getUTCSeconds']);
|
|
|
|
options.notify_read_properties(['getSeconds', 'getUTCSeconds']);
|
|
|
|
had_time_changes = true;
|
|
|
|
is_time_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (current_datetime.getMilliseconds() !== new_datetime.getMilliseconds()) {
|
|
|
|
if (options.value.getMilliseconds() !== new_datetime.getMilliseconds()) {
|
|
|
|
notify_read_properties(['getMilliseconds', 'getUTCMilliseconds']);
|
|
|
|
options.notify_read_properties(['getMilliseconds', 'getUTCMilliseconds']);
|
|
|
|
had_time_changes = true;
|
|
|
|
is_time_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (had_time_changes) {
|
|
|
|
if (is_time_changed) {
|
|
|
|
notify_read_properties(['toTimeString', 'toLocaleTimeString']);
|
|
|
|
options.notify_read_properties(['toTimeString', 'toLocaleTimeString']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (had_date_changes) {
|
|
|
|
if (is_date_changed) {
|
|
|
|
notify_read_properties(['toDateString', 'toLocaleDateString']);
|
|
|
|
options.notify_read_properties(['toDateString', 'toLocaleDateString']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return had_date_changes || had_time_changes;
|
|
|
|
return is_date_changed || is_time_changed;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const ReactiveDate = make_reactive(Date, {
|
|
|
|
export const ReactiveDate = make_reactive(Date, {
|
|
|
|
@ -116,108 +120,49 @@ export const ReactiveDate = make_reactive(Date, {
|
|
|
|
],
|
|
|
|
],
|
|
|
|
interceptors: {
|
|
|
|
interceptors: {
|
|
|
|
setDate: (options, ...params) => {
|
|
|
|
setDate: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setDate(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setFullYear: (options, ...params) => {
|
|
|
|
setFullYear: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setFullYear(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setHours: (options, ...params) => {
|
|
|
|
setHours: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setHours(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[3])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setMilliseconds: (options, ...params) => {
|
|
|
|
setMilliseconds: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setMilliseconds(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setMinutes: (options, ...params) => {
|
|
|
|
setMinutes: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setMinutes(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setMonth: (options, ...params) => {
|
|
|
|
setMonth: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setMonth(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setSeconds: (options, ...params) => {
|
|
|
|
setSeconds: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setSeconds(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setTime: (options, ...params) => {
|
|
|
|
setTime: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setTime(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCDate: (options, ...params) => {
|
|
|
|
setUTCDate: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCDate(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCFullYear: (options, ...params) => {
|
|
|
|
setUTCFullYear: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCFullYear(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCHours: (options, ...params) => {
|
|
|
|
setUTCHours: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCHours(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[3])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCMilliseconds: (options, ...params) => {
|
|
|
|
setUTCMilliseconds: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCMilliseconds(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCMinutes: (options, ...params) => {
|
|
|
|
setUTCMinutes: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCMinutes(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[2])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCMonth: (options, ...params) => {
|
|
|
|
setUTCMonth: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCMonth(/**@type {number}*/ (params[0]), /**@type {number | undefined}*/ (params[1]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setUTCSeconds: (options, ...params) => {
|
|
|
|
setUTCSeconds: (options, ...params) => {
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
tmp.setUTCSeconds(
|
|
|
|
|
|
|
|
/**@type {number}*/ (params[0]),
|
|
|
|
|
|
|
|
/**@type {number | undefined}*/ (params[1])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// @ts-expect-error - deprecated method
|
|
|
|
// @ts-expect-error - deprecated method
|
|
|
|
setYear: (options, ...params) => {
|
|
|
|
setYear: (options, ...params) => {
|
|
|
|
@ -225,10 +170,7 @@ export const ReactiveDate = make_reactive(Date, {
|
|
|
|
if (!options.value.getYear) {
|
|
|
|
if (!options.value.getYear) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const tmp = new Date(options.value);
|
|
|
|
return notify_datetime_changes(options, ...params);
|
|
|
|
// @ts-expect-error
|
|
|
|
|
|
|
|
tmp.setYear(/**@type {number}*/ (params[0]));
|
|
|
|
|
|
|
|
return notify_datetime_changes(options.value, tmp, options.notify_read_properties);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|