| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import { io } from './utils' |
|
|
| function createNow(originalNow: typeof Date.now) { |
| return { |
| now: function now() { |
| io('`Date.now()`', 'time') |
| return originalNow() |
| }, |
| }['now'.slice() as 'now'].bind(null) |
| } |
|
|
| function createDate(originalConstructor: typeof Date): typeof Date { |
| const properties = Object.getOwnPropertyDescriptors(originalConstructor) |
| properties.now.value = createNow(originalConstructor.now) |
|
|
| const apply = Reflect.apply |
| const construct = Reflect.construct |
|
|
| const newConstructor = Object.defineProperties( |
| |
| function Date() { |
| if (new.target === undefined) { |
| io('`Date()`', 'time') |
| return apply(originalConstructor, undefined, arguments) |
| } |
| if (arguments.length === 0) { |
| io('`new Date()`', 'time') |
| } |
| return construct(originalConstructor, arguments, new.target) |
| }, |
| properties |
| ) |
| Object.defineProperty(originalConstructor.prototype, 'constructor', { |
| value: newConstructor, |
| }) |
| return newConstructor as typeof Date |
| } |
|
|
| try { |
| |
| Date = createDate(Date) |
| } catch { |
| console.error( |
| 'Failed to install `Date` class extension. When using `experimental.cacheComponents`, APIs that read the current time will not correctly trigger dynamic behavior.' |
| ) |
| } |
|
|