-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
167 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
export interface Disposable { | ||
dispose: () => void | Promise<void> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,72 @@ | ||
import { ExtensionContext } from 'vscode' | ||
import { ExtensionContext, window } from 'vscode' | ||
import { Disposable } from '../disposable' | ||
import TypedEventEmitter from '../events' | ||
import { EXTENSION_ID } from '../extension' | ||
import { TaskItem } from './task-data-provider' | ||
|
||
export class Favorites { | ||
interface LocalEventTypes { | ||
'change': [] | ||
} | ||
|
||
const storageKey = `${EXTENSION_ID}-favorites` | ||
|
||
export class Favorites extends TypedEventEmitter<LocalEventTypes> implements Disposable { | ||
|
||
private context: ExtensionContext | ||
|
||
private items: Set<string> = new Set() | ||
|
||
constructor(context: ExtensionContext) { | ||
super() | ||
|
||
this.context = context | ||
|
||
this.init() | ||
} | ||
|
||
private notify(): void { | ||
this.emit('change') | ||
} | ||
|
||
private save() { | ||
const ids: string[] = [] | ||
this.items.forEach(v => ids.push(v)) | ||
this.context.workspaceState.update(storageKey, ids) | ||
} | ||
|
||
init(): void { | ||
const items = this.context.workspaceState.get<string[]>(storageKey) | ||
if (Array.isArray(items)) { | ||
this.items = new Set(items) | ||
console.log(`loaded ${items.length} favorites`) | ||
|
||
this.notify() | ||
} | ||
} | ||
|
||
dispose(): void { | ||
this.emitter.removeAllListeners() | ||
this.save() | ||
} | ||
|
||
list(): Set<string> { | ||
return this.items | ||
} | ||
|
||
add(item: TaskItem): void { | ||
this.items.add(item.id) | ||
this.notify() | ||
this.save() | ||
} | ||
|
||
remove(item: TaskItem): void { | ||
const id = item.id.substring(9) | ||
if (this.items.delete(id)) { | ||
this.notify() | ||
this.save() | ||
} else { | ||
window.showWarningMessage(`item with ID "${item.id}" was not in Set!`) | ||
} | ||
} | ||
|
||
} |
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,3 @@ | ||
export function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { | ||
return value !== null && value !== undefined | ||
} |