mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 09:01:19 +00:00
Manages per-user TflSource instances with individual line configuration. Implements FeedSourceProvider so it can be wired into FeedEngineService. Adds TflSource.setLines() so line config can be mutated in place, keeping engine references valid. Also exports ITflApi from @aris/source-tfl for testability. Co-authored-by: Ona <no-reply@ona.com>
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { TflSource, type ITflApi, type TflLineId } from "@aris/source-tfl"
|
|
|
|
import type { FeedSourceProvider } from "../feed/service.ts"
|
|
|
|
import { UserNotFoundError } from "../lib/error.ts"
|
|
|
|
/**
|
|
* Manages per-user TflSource instances with individual line configuration.
|
|
*/
|
|
export class TflService implements FeedSourceProvider {
|
|
private sources = new Map<string, TflSource>()
|
|
|
|
constructor(private readonly api: ITflApi) {}
|
|
|
|
feedSourceForUser(userId: string): TflSource {
|
|
let source = this.sources.get(userId)
|
|
if (!source) {
|
|
source = new TflSource({ client: this.api })
|
|
this.sources.set(userId, source)
|
|
}
|
|
return source
|
|
}
|
|
|
|
/**
|
|
* Update monitored lines for a user. Mutates the existing TflSource
|
|
* so that references held by FeedEngine remain valid.
|
|
* @throws {UserNotFoundError} If no source exists for the user
|
|
*/
|
|
updateLinesOfInterest(userId: string, lines: TflLineId[]): void {
|
|
const source = this.sources.get(userId)
|
|
if (!source) {
|
|
throw new UserNotFoundError(userId)
|
|
}
|
|
source.setLinesOfInterest(lines)
|
|
}
|
|
|
|
removeUser(userId: string): void {
|
|
this.sources.delete(userId)
|
|
}
|
|
}
|