Skip to content

Commit

Permalink
partially go back to old Marker system, better for code splitting for…
Browse files Browse the repository at this point in the history
… PlainDate
  • Loading branch information
arshaw committed Mar 2, 2024
1 parent 6aceb19 commit 235b938
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 251 deletions.
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/classApi/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { DurationBag } from '../internal/fields'
import { LocalesArg } from '../internal/intlFormatUtils'
import { formatDurationIso } from '../internal/isoFormat'
import { parseDuration, parseRelativeToSlots } from '../internal/isoParse'
import { RelativeToSlots } from '../internal/markerSystem'
import {
DurationRoundOptions,
RelativeToOptions,
TotalUnitOptionsWithRel,
} from '../internal/optionsRefine'
import { RelativeToSlots } from '../internal/relativeSystem'
import {
BrandingSlots,
DurationBranding,
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/bagRefine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
isoMonthsInYear,
} from './isoMath'
import { parseOffsetNano } from './isoParse'
import { RelativeToSlotsNoCalendar } from './markerSystem'
import { OffsetDisambig, Overflow } from './options'
import {
OverflowOptions,
Expand All @@ -89,7 +90,6 @@ import {
refineOverflowOptions,
refineZonedFieldOptions,
} from './optionsRefine'
import { RelativeToSlotsNoCalendar } from './relativeSystem'
import {
DurationSlots,
PlainDateSlots,
Expand Down
23 changes: 9 additions & 14 deletions packages/temporal-polyfill/src/internal/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { durationFieldNamesAsc } from './durationFields'
import { getLargestDurationUnit } from './durationMath'
import * as errorMessages from './errorMessages'
import { IsoDateFields, IsoDateTimeFields, IsoTimeFields } from './isoFields'
import { moveRelativeMarker } from './move'
import { RelativeToOptions, normalizeOptions } from './optionsRefine'
import {
RelativeToSlots,
createRelativeSystem,
relativeMarkerToEpochNano,
} from './relativeSystem'
anyMarkerToEpochNano,
createMarkerSystem,
createMoveMarker,
} from './markerSystem'
import { RelativeToOptions, normalizeOptions } from './optionsRefine'
import {
DurationSlots,
IdLike,
Expand Down Expand Up @@ -89,21 +89,16 @@ export function compareDurations<RA, C, T>(
throw new RangeError(errorMessages.missingRelativeTo)
}

const [marker, calendarOps, timeZoneOps] = createRelativeSystem(
const [marker, calendarOps, timeZoneOps] = createMarkerSystem(
getCalendarOps,
getTimeZoneOps,
relativeToSlots,
)
const moveMarker = createMoveMarker(calendarOps, timeZoneOps)

return compareBigNanos(
relativeMarkerToEpochNano(
moveRelativeMarker(durationSlots0, marker, calendarOps, timeZoneOps),
timeZoneOps,
),
relativeMarkerToEpochNano(
moveRelativeMarker(durationSlots1, marker, calendarOps, timeZoneOps),
timeZoneOps,
),
anyMarkerToEpochNano(moveMarker(marker, durationSlots0)),
anyMarkerToEpochNano(moveMarker(marker, durationSlots1)),
)
}

Expand Down
62 changes: 23 additions & 39 deletions packages/temporal-polyfill/src/internal/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import {
isoTimeFieldNamesAsc,
} from './isoFields'
import { isoMonthsInYear } from './isoMath'
import { moveByIsoDays, moveToMonthStart } from './move'
import { MarkerToEpochNano, MoveMarker } from './markerSystem'
import {
moveByIsoDays,
moveDateEfficient,
moveDateTime,
moveToMonthStart,
moveZonedEpochSlots,
} from './move'
import { RoundingMode } from './options'
import { DiffOptions, copyOptions, refineDiffOptions } from './optionsRefine'
import { RelativeMarkerSlots } from './relativeSystem'
import {
computeNanoInc,
roundBigNano,
Expand All @@ -43,6 +49,7 @@ import {
ZonedDateTimeSlots,
ZonedEpochSlots,
createDurationSlots,
extractEpochNano,
isIdLikeEqual,
} from './slots'
import {
Expand Down Expand Up @@ -144,10 +151,10 @@ export function diffZonedDateTimes<C extends IdLike, T extends IdLike>(
smallestUnit,
roundingInc,
roundingMode,
// RelativeSystem...
// MarkerMoveSystem...
zonedDateTimeSlots0,
calendarOps,
timeZoneOps,
extractEpochNano as MarkerToEpochNano,
bindArgs(moveZonedEpochSlots, calendarOps, timeZoneOps) as MoveMarker,
)
}
}
Expand Down Expand Up @@ -208,9 +215,10 @@ export function diffPlainDateTimes<C extends IdLike>(
smallestUnit,
roundingInc,
roundingMode,
// RelativeSystem...
// MarkerMoveSystem...
plainDateTimeSlots0,
calendarOps,
isoToEpochNano as MarkerToEpochNano,
bindArgs(moveDateTime, calendarOps) as MoveMarker,
)
}
}
Expand Down Expand Up @@ -323,9 +331,10 @@ function diffDateLike(
smallestUnit,
roundingInc,
roundingMode,
// RelativeSystem...
{ ...startIsoFields, ...isoTimeFieldDefaults },
calendarOps,
// MarkerMoveSystem...
startIsoFields,
isoToEpochNano as MarkerToEpochNano,
bindArgs(moveDateEfficient, calendarOps) as MoveMarker,
)
}
}
Expand Down Expand Up @@ -364,39 +373,14 @@ export function diffPlainTimes(
)
}

export function diffRelativeMarkers(
largestUnit: Unit,
// RelativeSystem (w/ two points)...
slots0: RelativeMarkerSlots,
slots1: RelativeMarkerSlots,
calendarOps: DiffOps,
timeZoneOps?: TimeZoneOps,
): DurationFields {
if (timeZoneOps) {
return diffZonedEpochSlotsExact(
calendarOps,
timeZoneOps,
slots0 as ZonedEpochSlots<unknown, unknown>,
slots1 as ZonedEpochSlots<unknown, unknown>,
largestUnit,
)
}
return diffDateTimesExact(
calendarOps,
slots0 as IsoDateTimeFields,
slots1 as IsoDateTimeFields,
largestUnit,
)
}

// Exact Diffing
// -----------------------------------------------------------------------------

export function diffZonedEpochSlotsExact(
calendarOps: DiffOps,
timeZoneOps: TimeZoneOps,
slots0: ZonedEpochSlots<unknown, unknown>,
slots1: ZonedEpochSlots<unknown, unknown>,
slots0: ZonedEpochSlots,
slots1: ZonedEpochSlots,
largestUnit: Unit,
origOptions?: DiffOptions,
): DurationFields {
Expand Down Expand Up @@ -463,8 +447,8 @@ function diffZonedEpochNanoViaCalendar(
calendarOps: DiffOps,
timeZoneOps: TimeZoneOps,
sign: NumberSign,
slots0: ZonedEpochSlots<unknown, unknown>,
slots1: ZonedEpochSlots<unknown, unknown>,
slots0: ZonedEpochSlots,
slots1: ZonedEpochSlots,
largestUnit: Unit,
origOptions?: DiffOptions,
): DurationFields {
Expand Down
64 changes: 26 additions & 38 deletions packages/temporal-polyfill/src/internal/durationMath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BigNano, addBigNanos, bigNanoToNumber } from './bigNano'
import { DiffOps } from './calendarOps'
import { diffRelativeMarkers } from './diff'
import {
DurationFields,
DurationTimeFields,
Expand All @@ -10,20 +9,21 @@ import {
durationFieldNamesAsc,
} from './durationFields'
import * as errorMessages from './errorMessages'
import { moveRelativeMarker } from './move'
import {
DiffMarkers,
Marker,
MarkerToEpochNano,
MoveMarker,
RelativeToSlots,
createMarkerDiffSystem,
} from './markerSystem'
import { Overflow } from './options'
import {
DurationRoundOptions,
RelativeToOptions,
normalizeOptions,
refineDurationRoundOptions,
} from './optionsRefine'
import {
RelativeMarkerSlots,
RelativeToSlots,
createRelativeSystem,
relativeMarkerToEpochNano,
} from './relativeSystem'
import { roundDayTimeDuration, roundRelativeDuration } from './round'
import { DurationSlots, createDurationSlots } from './slots'
import { TimeZoneOps } from './timeZoneOps'
Expand All @@ -48,36 +48,20 @@ export function spanDuration(
durationFields0: DurationFields,
durationFields1: DurationFields | undefined, // HACKy
largestUnit: Unit, // TODO: more descrimination?
// RelativeSystem...
marker: RelativeMarkerSlots,
calendarOps: DiffOps,
timeZoneOps?: TimeZoneOps,
// MarkerDiffSystem...
marker: Marker,
markerToEpochNano: MarkerToEpochNano,
moveMarker: MoveMarker,
diffMarkers: DiffMarkers,
): [DurationFields, BigNano] {
let endMarker = moveRelativeMarker(
durationFields0,
marker,
calendarOps,
timeZoneOps,
)
let endMarker = moveMarker(marker, durationFields0)

if (durationFields1) {
endMarker = moveRelativeMarker(
durationFields1,
endMarker,
calendarOps,
timeZoneOps,
)
endMarker = moveMarker(endMarker, durationFields1)
}

const balancedDuration = diffRelativeMarkers(
largestUnit,
marker,
endMarker,
calendarOps,
timeZoneOps,
)

return [balancedDuration, relativeMarkerToEpochNano(endMarker, timeZoneOps)]
const balancedDuration = diffMarkers(marker, endMarker, largestUnit)
return [balancedDuration, markerToEpochNano(endMarker)]
}

// Adding
Expand Down Expand Up @@ -130,7 +114,11 @@ export function addDurations<RA, C, T>(
slots,
otherSlots,
largestUnit,
...createRelativeSystem(getCalendarOps, getTimeZoneOps, relativeToSlots),
...createMarkerDiffSystem(
getCalendarOps,
getTimeZoneOps,
relativeToSlots,
),
)[0],
)
}
Expand All @@ -155,7 +143,7 @@ function addDayTimeDurations(
}
}

// Rounding (with RelativeSystem)
// Rounding
// -----------------------------------------------------------------------------

export function roundDuration<RA, C, T>(
Expand Down Expand Up @@ -199,7 +187,7 @@ export function roundDuration<RA, C, T>(
throw new RangeError(errorMessages.missingRelativeTo)
}

const relativeSystem = createRelativeSystem(
const diffSystem = createMarkerDiffSystem(
getCalendarOps,
getTimeZoneOps,
relativeToSlots,
Expand All @@ -215,7 +203,7 @@ export function roundDuration<RA, C, T>(
slots,
undefined,
largestUnit,
...relativeSystem,
...diffSystem,
)

const origSign = slots.sign
Expand All @@ -235,7 +223,7 @@ export function roundDuration<RA, C, T>(
smallestUnit,
roundingInc,
roundingMode,
...relativeSystem,
...diffSystem,
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/isoParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
isIsoDateFieldsValid,
isoEpochFirstLeapYear,
} from './isoMath'
import { RelativeToSlots } from './markerSystem'
import { moveToMonthStart } from './move'
import { EpochDisambig, OffsetDisambig, Overflow } from './options'
import { ZonedFieldOptions, refineZonedFieldOptions } from './optionsRefine'
import { RelativeToSlots } from './relativeSystem'
import {
DateSlots,
DurationSlots,
Expand Down
Loading

0 comments on commit 235b938

Please sign in to comment.