All files / src/reactivity date.js

100% Statements 111/111
100% Branches 9/9
100% Functions 6/6
100% Lines 111/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 28x 28x 60x 60x 60x 28x 28x 1x 1x 16x 16x 12x 12x 12x 12x 12x 12x 12x 16x 16x 1x 1x 1x 1x 8x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import { DEV } from 'esm-env';
import { INSPECT_SYMBOL } from '../internal/client/constants.js';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
 
/** @type {Array<keyof Date>} */
const read = [
	'getDate',
	'getDay',
	'getFullYear',
	'getHours',
	'getMilliseconds',
	'getMinutes',
	'getMonth',
	'getSeconds',
	'getTime',
	'getTimezoneOffset',
	'getUTCDate',
	'getUTCDay',
	'getUTCFullYear',
	'getUTCHours',
	'getUTCMilliseconds',
	'getUTCMinutes',
	'getUTCMonth',
	'getUTCSeconds',
	// @ts-expect-error this is deprecated
	'getYear',
	'toDateString',
	'toISOString',
	'toJSON',
	'toLocaleDateString',
	'toLocaleString',
	'toLocaleTimeString',
	'toString',
	'toTimeString',
	'toUTCString'
];
 
/** @type {Array<keyof Date>} */
const write = [
	'setDate',
	'setFullYear',
	'setHours',
	'setMilliseconds',
	'setMinutes',
	'setMonth',
	'setSeconds',
	'setTime',
	'setUTCDate',
	'setUTCFullYear',
	'setUTCHours',
	'setUTCMilliseconds',
	'setUTCMinutes',
	'setUTCMonth',
	'setUTCSeconds',
	// @ts-expect-error this is deprecated
	'setYear'
];
 
var inited = false;
 
export class ReactiveDate extends Date {
	#raw_time = source(super.getTime());
 
	// We init as part of the first instance so that we can treeshake this class
	#init() {
		if (!inited) {
			inited = true;
			const proto = ReactiveDate.prototype;
			const date_proto = Date.prototype;
 
			for (const method of read) {
				// @ts-ignore
				proto[method] = function (...args) {
					get(this.#raw_time);
					// @ts-ignore
					return date_proto[method].apply(this, args);
				};
			}
 
			for (const method of write) {
				// @ts-ignore
				proto[method] = function (...args) {
					// @ts-ignore
					const v = date_proto[method].apply(this, args);
					const time = date_proto.getTime.call(this);
					if (time !== this.#raw_time.v) {
						set(this.#raw_time, time);
					}
					return v;
				};
			}
 
			if (DEV) {
				// @ts-ignore
				proto[INSPECT_SYMBOL] = function () {
					get(this.#raw_time);
				};
			}
		}
	}
 
	/**
	 * @param {any[]} values
	 */
	constructor(...values) {
		// @ts-ignore
		super(...values);
		this.#init();
	}
}