Skip to content

Commit

Permalink
Ensure that "fresh" ActivityItemStreams returned by `addActivityIte…
Browse files Browse the repository at this point in the history
…mStream()` retain their specialization type

i.e., either `ActivityItemOutputStream` or `ActivityItemErrorStream`
  • Loading branch information
DavisVaughan committed Sep 27, 2024
1 parent 9a48061 commit a7bfa6b
Showing 1 changed file with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ export class ActivityItemStream {

/**
* Constructor.
*
* Never to be called directly, as the result won't be fully specialized.
* Internally, use `newActivityItemStream()` instead.
* Externally, use `ActivityItemOutputStream` or `ActivityItemErrorStream` constructors instead.
*
* @param id The identifier.
* @param parentId The parent identifier.
* @param when The date.
* @param text The text.
*/
constructor(
protected constructor(
readonly id: string,
readonly parentId: string,
readonly when: Date,
Expand Down Expand Up @@ -95,7 +100,7 @@ export class ActivityItemStream {
const remainderText = activityItemStream.text.substring(newlineIndex + 1);

// Add an ActivityItemStream with the text containing the newline.
this.activityItemStreams.push(new ActivityItemStream(
this.activityItemStreams.push(this.newActivityItemStream!(
activityItemStream.id,
activityItemStream.parentId,
activityItemStream.when,
Expand All @@ -116,7 +121,7 @@ export class ActivityItemStream {
}

// Create the remainder ActivityItemStream.
activityItemStream = new ActivityItemStream(
activityItemStream = this.newActivityItemStream!(
activityItemStream.id,
activityItemStream.parentId,
activityItemStream.when,
Expand All @@ -138,6 +143,25 @@ export class ActivityItemStream {

//#endregion Public Methods

//#region Protected Methods

/**
* Constructor for typed ActivityItemStream instances
*
* Must be overriden by specializations.
*
* Used to ensure that newly created ActivityItemStreams returned by `addActivityItemStream()`
* retain their original type, i.e. either ActivityItemOutputStream or ActivityItemErrorStream.
*
* @param id The identifier.
* @param parentId The parent identifier.
* @param when The date.
* @param text The text.
*/
protected newActivityItemStream?(id: string, parentId: string, when: Date, text: string): ActivityItemStream;

//#endregion Protected Methods

//#region Private Methods

/**
Expand All @@ -159,9 +183,25 @@ export class ActivityItemStream {
/**
* ActivityItemOutputStream class.
*/
export class ActivityItemOutputStream extends ActivityItemStream { }
export class ActivityItemOutputStream extends ActivityItemStream {
constructor(id: string, parentId: string, when: Date, text: string) {
super(id, parentId, when, text);
}

protected override newActivityItemStream(id: string, parentId: string, when: Date, text: string): ActivityItemStream {
return new ActivityItemOutputStream(id, parentId, when, text);
}
}

/**
* ActivityItemErrorStream class.
*/
export class ActivityItemErrorStream extends ActivityItemStream { }
export class ActivityItemErrorStream extends ActivityItemStream {
constructor(id: string, parentId: string, when: Date, text: string) {
super(id, parentId, when, text);
}

protected override newActivityItemStream(id: string, parentId: string, when: Date, text: string): ActivityItemStream {
return new ActivityItemErrorStream(id, parentId, when, text);
}
}

0 comments on commit a7bfa6b

Please sign in to comment.