-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb6fb4a
commit 45c6d5e
Showing
25 changed files
with
707 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import type { TodoistApiClient } from "../api"; | ||
import type { Label, LabelId } from "../api/domain/label"; | ||
import type { Project, ProjectId } from "../api/domain/project"; | ||
import type { Section, SectionId } from "../api/domain/section"; | ||
import { Repository } from "./repository"; | ||
import type { Task } from "./task"; | ||
import type { Task as ApiTask, CreateTaskParams, TaskId } from "../api/domain/task"; | ||
|
||
type UnsubscribeCallback = () => void; | ||
type Refresh = () => Promise<void>; | ||
|
||
type SubscriptionId = number; | ||
|
||
type DataAccessor = { | ||
projects: { | ||
all: () => IterableIterator<Project>, | ||
byId: (id: ProjectId) => Project | undefined, | ||
}, | ||
sections: { | ||
all: () => IterableIterator<Section>, | ||
byId: (id: SectionId) => Section | undefined, | ||
} | ||
labels: { | ||
all: () => IterableIterator<Label>, | ||
byId: (id: LabelId) => Label | undefined, | ||
} | ||
} | ||
|
||
type Actions = { | ||
closeTask: (id: TaskId) => Promise<void>, | ||
createTask: (content: string, params: CreateTaskParams) => Promise<void>, | ||
}; | ||
|
||
export class TodoistAdapter { | ||
|
||
public data: DataAccessor = { | ||
projects: { | ||
all: () => this.projects.iter(), | ||
byId: (id) => this.projects.getById(id), | ||
}, | ||
sections: { | ||
all: () => this.sections.iter(), | ||
byId: (id) => this.sections.getById(id), | ||
}, | ||
labels: { | ||
all: () => this.labels.iter(), | ||
byId: (id) => this.labels.getById(id), | ||
} | ||
} | ||
|
||
public actions: Actions = { | ||
closeTask: async (id) => await this.api.closeTask(id), | ||
createTask: async (content, params) => await this.api.createTask(content, params), | ||
} | ||
|
||
private api: TodoistApiClient | undefined; | ||
|
||
private readonly projects: Repository<ProjectId, Project>; | ||
private readonly sections: Repository<SectionId, Section>; | ||
private readonly labels: Repository<LabelId, Label>; | ||
|
||
private readonly subscriptions: Map<SubscriptionId, Refresh> = new Map(); | ||
private subscriptionIdGen: Generator<SubscriptionId> = subscriptionIdGenerator(); | ||
|
||
constructor() { | ||
this.projects = new Repository(() => this.api.getProjects()); | ||
this.sections = new Repository(() => this.api.getSections()); | ||
this.labels = new Repository(() => this.api.getLabels()); | ||
} | ||
|
||
public async initialize(api: TodoistApiClient) { | ||
this.api = api; | ||
await this.sync(); | ||
} | ||
|
||
public isReady(): boolean { | ||
return this.api !== undefined; | ||
} | ||
|
||
public async sync(): Promise<void> { | ||
if (!this.isReady()) { | ||
return; | ||
} | ||
|
||
this.projects.sync(); | ||
this.sections.sync(); | ||
this.labels.sync(); | ||
|
||
for (const refresh of this.subscriptions.values()) { | ||
await refresh(); | ||
} | ||
} | ||
|
||
public subscribe(query: string, callback: (tasks: Task[]) => void): [UnsubscribeCallback, Refresh] { | ||
const id = this.subscriptionIdGen.next().value; | ||
const refresh = this.buildRefresher(query, callback); | ||
this.subscriptions.set(id, refresh); | ||
|
||
return [ | ||
() => { this.subscriptions.delete(id) }, | ||
refresh | ||
]; | ||
} | ||
|
||
private buildRefresher(query: string, callback: (tasks: Task[]) => void): Refresh { | ||
return async () => { | ||
if (!this.isReady()) { | ||
return; | ||
} | ||
try { | ||
const data = await this.api.getTasks(query); | ||
const hydrated = data.map(t => this.hydrate(t)); | ||
|
||
callback(hydrated); | ||
} | ||
catch (error: any) { | ||
// TODO: Error handling | ||
} | ||
}; | ||
} | ||
|
||
private hydrate(apiTask: ApiTask): Task { | ||
const project = this.projects.getById(apiTask.projectId); | ||
const section = apiTask.sectionId ? this.sections.getById(apiTask.sectionId) : undefined; | ||
|
||
return { | ||
id: apiTask.id, | ||
|
||
content: apiTask.content, | ||
description: apiTask.description, | ||
|
||
project: project, | ||
section: section, | ||
parentId: apiTask.parentId, | ||
|
||
labels: apiTask.labels, | ||
priority: apiTask.priority, | ||
|
||
due: apiTask.due, | ||
order: apiTask.order | ||
}; | ||
} | ||
} | ||
|
||
function* subscriptionIdGenerator(): Generator<SubscriptionId> { | ||
let next = 0; | ||
|
||
while (true) { | ||
yield next++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export class Repository<T, U extends { id: T }> { | ||
private readonly data: Map<T, U> = new Map(); | ||
private readonly fetchData: () => Promise<U[]>; | ||
|
||
constructor(refreshData: () => Promise<U[]>) { | ||
this.fetchData = refreshData; | ||
} | ||
|
||
public async sync(): Promise<void> { | ||
try { | ||
const items = await this.fetchData(); | ||
|
||
this.data.clear(); | ||
for (const elem of items) { | ||
this.data.set(elem.id, elem); | ||
} | ||
} catch (error: any) { | ||
// TODO: Error handling | ||
} | ||
} | ||
|
||
public getById(id: T): U | undefined { | ||
return this.data.get(id); | ||
} | ||
|
||
public iter(): IterableIterator<U> { | ||
return this.data.values(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Project } from "../api/domain/project"; | ||
import type { Section } from "../api/domain/section"; | ||
import type { DueDate, Priority, TaskId } from "../api/domain/task"; | ||
|
||
export type Task = { | ||
id: TaskId, | ||
|
||
content: string, | ||
description: string, | ||
|
||
project?: Project, | ||
section?: Section, | ||
parentId?: TaskId, | ||
|
||
labels: string[], | ||
priority: Priority, | ||
|
||
due?: DueDate, | ||
order: number, | ||
}; |
Oops, something went wrong.