99 gSchema$EventBase ,
1010 gSchema$EventInstance ,
1111} from "@core/types/gcal" ;
12- import dayjs from "@core/util/date/dayjs" ;
12+ import dayjs , { Dayjs } from "@core/util/date/dayjs" ;
1313import { diffRRuleOptions } from "@core/util/event/event.util" ;
1414import {
1515 getGcalEventDateFormat ,
@@ -22,6 +22,9 @@ export class GcalEventRRule extends RRule {
2222 #dateKey: "date" | "dateTime" ;
2323 #dateFormat: string ;
2424 #durationMs! : number ;
25+ #startDate! : Dayjs ;
26+ #endDate! : Dayjs ;
27+ #timezone! : string ;
2528
2629 constructor ( event : gSchema$EventBase , options : Partial < Options > = { } ) {
2730 super ( GcalEventRRule . #initOptions( event , options ) ) ;
@@ -30,17 +33,18 @@ export class GcalEventRRule extends RRule {
3033 this . #isAllDay = "date" in this . #event. start ! ;
3134 this . #dateKey = this . #isAllDay ? "date" : "dateTime" ;
3235 this . #dateFormat = getGcalEventDateFormat ( this . #event. start ) ;
36+ this . #timezone = this . #event. start ?. timeZone ?? dayjs . tz . guess ( ) ;
3337
3438 const { start, end } = this . #event;
35- const startDate = parseGCalEventDate ( start ) ;
36- const endDate = parseGCalEventDate ( end ) ;
3739
38- this . #durationMs = endDate . diff ( startDate , "milliseconds" ) ;
40+ this . #startDate = parseGCalEventDate ( start ) ;
41+ this . #endDate = parseGCalEventDate ( end ) ;
42+ this . #durationMs = this . #endDate. diff ( this . #startDate, "milliseconds" ) ;
3943 }
4044
4145 static #initOptions(
4246 event : gSchema$EventBase ,
43- options : Partial < Options > = { } ,
47+ _options : Partial < Options > = { } ,
4448 ) : Partial < Options > {
4549 const startDate = parseGCalEventDate ( event . start ) ;
4650 const dtstart = startDate . local ( ) . toDate ( ) ;
@@ -49,11 +53,14 @@ export class GcalEventRRule extends RRule {
4953 const recurrence = event . recurrence ?. join ( "\n" ) . trim ( ) ;
5054 const valid = recurrence ?. length > 0 ;
5155 const rruleSet = valid ? rrulestr ( recurrence ! , opts ) : { origOptions : { } } ;
52- const rruleOptions = { ...rruleSet . origOptions , ...options } ;
53- const rawCount = rruleOptions . count ?? GCAL_MAX_RECURRENCES ;
54- const count = Math . min ( rawCount , GCAL_MAX_RECURRENCES ) ;
56+ const rruleOptions = { ...rruleSet . origOptions , ..._options } ;
57+ const options = { ...rruleOptions , dtstart, tzid } ;
5558
56- return { ...rruleOptions , count, dtstart, tzid } ;
59+ if ( options . until instanceof Date ) {
60+ options . count = undefined as unknown as number ;
61+ }
62+
63+ return options ;
5764 }
5865
5966 diffOptions ( rrule : GcalEventRRule ) : Array < [ keyof ParsedOptions , unknown ] > {
@@ -64,6 +71,19 @@ export class GcalEventRRule extends RRule {
6471 return this . toString ( ) . split ( "\n" ) ;
6572 }
6673
74+ override all (
75+ iterator : ( d : Date , len : number ) => boolean = ( _ , index ) =>
76+ index < GCAL_MAX_RECURRENCES ,
77+ ) : Date [ ] {
78+ const dates = super . all ( iterator ) ;
79+ const firstInstance = dates [ 0 ] ;
80+ const firstInstanceStartDate = dayjs ( firstInstance ) . tz ( this . #timezone) ;
81+ const includesDtStart = this . #startDate. isSame ( firstInstanceStartDate ) ;
82+ const rDates = includesDtStart ? [ ] : [ this . #startDate. toDate ( ) ] ;
83+
84+ return rDates . concat ( dates ) ;
85+ }
86+
6787 /**
6888 * instances
6989 *
@@ -73,9 +93,7 @@ export class GcalEventRRule extends RRule {
7393 */
7494 instances ( ) : gSchema$EventInstance [ ] {
7595 return this . all ( ) . map ( ( date ) => {
76- const timezone = dayjs . tz . guess ( ) ;
77- const tzid = this . #event. start ?. timeZone ?? timezone ;
78- const startDate = dayjs ( date ) . tz ( tzid ) ;
96+ const startDate = dayjs ( date ) . tz ( this . #timezone) ;
7997 const endDate = startDate . add ( this . #durationMs, "milliseconds" ) ;
8098
8199 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -85,11 +103,11 @@ export class GcalEventRRule extends RRule {
85103 recurringEventId : this . #event. id ! ,
86104 start : {
87105 [ this . #dateKey] : startDate ?. format ( this . #dateFormat) ,
88- timeZone : this . #event . start ?. timeZone ?? timezone ,
106+ timeZone : this . #timezone,
89107 } ,
90108 end : {
91109 [ this . #dateKey] : endDate . format ( this . #dateFormat) ,
92- timeZone : this . #event . end ?. timeZone ?? timezone ,
110+ timeZone : this . #timezone,
93111 } ,
94112 } ;
95113
0 commit comments