Skip to content

Commit

Permalink
fix: update favorite handling
Browse files Browse the repository at this point in the history
  • Loading branch information
axelrindle committed May 24, 2024
1 parent cb97e22 commit 44e448d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/services/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Favorites extends TypedEventEmitter<LocalEventTypes> implements Dis
}

remove(item: TaskItem): void {
const id = item.id.substring(9)
const id = item.id.startsWith('favorite') ? item.id.substring(9) : item.id
if (this.items.delete(id)) {
this.notify()
this.save()
Expand Down
27 changes: 15 additions & 12 deletions src/services/task-data-provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Mutex } from 'async-mutex'
import { createHash } from 'crypto'
import { stat } from 'fs/promises'
import { join } from 'path'
import { groupBy, identity } from 'remeda'
import { Command, Event, EventEmitter, ExtensionContext, ProgressLocation, ProviderResult, Task, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, tasks, window } from 'vscode'
import { EXTENSION_ID } from '../extension'
import { Command, Event, EventEmitter, ProgressLocation, ProviderResult, Task, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, tasks, window } from 'vscode'
import { notEmpty } from '../util'
import Config from './config'
import { Favorites } from './favorites'
import { Mutex } from 'async-mutex'

const groupKeyFavorites = 'favorites'

Expand Down Expand Up @@ -120,12 +119,15 @@ export class TaskItem extends TreeItem {

export class FavoriteItem extends TaskItem {

readonly originalId: string

constructor(
task: Task,
command: Command,
) {
super(task, command, true)

this.originalId = makeTaskId(task, false)
this.setIconPath()
}

Expand Down Expand Up @@ -153,9 +155,7 @@ export default class TaskDataProvider implements TreeDataProvider<TreeItem> {
private _onDidChangeTreeData: EventEmitter<TreeItem | undefined | void> = new EventEmitter<TreeItem | undefined | void>()
readonly onDidChangeTreeData: Event<TreeItem | undefined | void> = this._onDidChangeTreeData.event

constructor(config: Config, context: ExtensionContext, favorites: Favorites) {
const { subscriptions } = context

constructor(config: Config, favorites: Favorites) {
this.config = config
this.favorites = favorites

Expand All @@ -165,10 +165,6 @@ export default class TaskDataProvider implements TreeDataProvider<TreeItem> {
this.favorites.on('change', () => this.refresh())

this.refresh()

subscriptions.push(
window.registerTreeDataProvider(EXTENSION_ID, this)
)
}

getTreeItem(element: TreeItem): TreeItem {
Expand Down Expand Up @@ -221,9 +217,16 @@ export default class TaskDataProvider implements TreeDataProvider<TreeItem> {
return undefined
})
.filter(notEmpty)
.sort((a, b) => a.task.name.localeCompare(b.task.name)) as TaskItem[]
.sort((a, b) => a.task.name.localeCompare(b.task.name)) as FavoriteItem[]

for (const task of result) {
const index = favorites.findIndex(fav => fav.originalId == task.id)
if (index != -1) {
task.contextValue = 'favoriteItem'
}
}

return favorites.concat(result)
return [...favorites, ...result]
}

async refresh(): Promise<void> {
Expand Down

0 comments on commit 44e448d

Please sign in to comment.