| 40 | * ``` |
| 41 | */ |
| 42 | export class SvelteDate extends Date { |
| 43 | #time = state(super.getTime()); |
| 44 | |
| 45 | /** @type {Map<keyof Date, Source<unknown>>} */ |
| 46 | #deriveds = new Map(); |
| 47 | |
| 48 | #reaction = active_reaction; |
| 49 | |
| 50 | /** @param {any[]} params */ |
| 51 | constructor(...params) { |
| 52 | // @ts-ignore |
| 53 | super(...params); |
| 54 | |
| 55 | if (DEV) { |
| 56 | tag(this.#time, 'SvelteDate.#time'); |
| 57 | } |
| 58 | |
| 59 | if (!inited) this.#init(); |
| 60 | } |
| 61 | |
| 62 | #init() { |
| 63 | inited = true; |
| 64 | |
| 65 | var proto = SvelteDate.prototype; |
| 66 | var date_proto = Date.prototype; |
| 67 | |
| 68 | var methods = /** @type {Array<keyof Date & string>} */ ( |
| 69 | Object.getOwnPropertyNames(date_proto) |
| 70 | ); |
| 71 | |
| 72 | for (const method of methods) { |
| 73 | if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') { |
| 74 | // @ts-ignore |
| 75 | proto[method] = function (...args) { |
| 76 | // don't memoize if there are arguments |
| 77 | // @ts-ignore |
| 78 | if (args.length > 0) { |
| 79 | get(this.#time); |
| 80 | // @ts-ignore |
| 81 | return date_proto[method].apply(this, args); |
| 82 | } |
| 83 | |
| 84 | var d = this.#deriveds.get(method); |
| 85 | |
| 86 | if (d === undefined) { |
| 87 | // lazily create the derived, but as though it were being |
| 88 | // created at the same time as the class instance |
| 89 | const reaction = active_reaction; |
| 90 | set_active_reaction(this.#reaction); |
| 91 | |
| 92 | d = derived(() => { |
| 93 | get(this.#time); |
| 94 | // @ts-ignore |
| 95 | return date_proto[method].apply(this, args); |
| 96 | }); |
| 97 | |
| 98 | if (DEV) { |
| 99 | tag(d, `SvelteDate.${method}()`); |