generated from obsidianmd/obsidian-sample-plugin
-
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.
- Loading branch information
Showing
13 changed files
with
165 additions
and
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ImagePicker } from '../ImagePicker' | ||
|
||
import { BackgrounderLane } from './BackgrounderLane' | ||
import { BackgrounderLaneProps } from './types' | ||
|
||
/** | ||
* General purpose background job runner :) | ||
* | ||
* This is used mostly to alleviate the main thread from | ||
* doing too much work at once. Things like indexing, | ||
* image processing, or other long-running tasks can be | ||
* run in the background. | ||
* | ||
* It's a FIFO queue, so jobs are run in the order they | ||
* are enqueued. | ||
*/ | ||
export class Backgrounder { | ||
public lanes: Record<string, BackgrounderLane> = {} | ||
|
||
constructor(public plugin: ImagePicker) {} | ||
|
||
log = (...args: unknown[]) => { | ||
this.plugin.log('Backgrounder -> ', ...args) | ||
} | ||
|
||
createLane = (lane: Omit<BackgrounderLaneProps, 'queue'>) => { | ||
this.lanes[lane.type] = new BackgrounderLane(this.plugin, lane) | ||
} | ||
|
||
deleteLane = (type: string) => { | ||
delete this.lanes[type] | ||
} | ||
|
||
stop = (type: string) => { | ||
if (this.lanes[type]) { | ||
this.lanes[type].clear() | ||
this.log('Stopped lane:', type) | ||
} else { | ||
this.log('Lane not found:', type) | ||
} | ||
} | ||
|
||
stopAll = () => { | ||
for (const lane of Object.values(this.lanes)) { | ||
lane.clear() | ||
} | ||
this.log('Stopped all lanes') | ||
} | ||
} |
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,2 @@ | ||
export * from './Backgrounder' | ||
export * from './types' |
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,46 @@ | ||
export interface BackgrounderJob { | ||
/** | ||
* Internally used UUID for the job | ||
*/ | ||
id: string | ||
/** | ||
* The type of job, in a small string. | ||
* | ||
* This is used as a "unique" identifier for the job. | ||
* If you run it in a lane with `unique: true`, only | ||
* one job of this type will be in the queue at a time. | ||
*/ | ||
type: string | ||
/** | ||
* The action that will be run when the job is executed | ||
*/ | ||
action: () => void | Promise<void> | ||
/** | ||
* If true, the job will be run immediately if the lane is free | ||
*/ | ||
eager?: boolean | ||
} | ||
|
||
export type BackgrounderQueue = BackgrounderJob[] | ||
|
||
export interface BackgrounderLaneProps { | ||
type: string | ||
queue: BackgrounderQueue | ||
/** | ||
* The time to wait between jobs in milliseconds | ||
*/ | ||
sleep: number | ||
/** | ||
* If true, only one job of this type can be in the queue at a time | ||
*/ | ||
unique: boolean | ||
/** | ||
* Determines which job to keep when enqueuing a unique job | ||
* | ||
* 'first' keeps the existing job and ignores the new one | ||
* 'last' keeps the new job and removes the existing one | ||
* | ||
* @default 'first' | ||
*/ | ||
uniqueKeep: 'first' | 'last' | ||
} |
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,18 @@ | ||
import Dexie from 'dexie' | ||
|
||
import { IndexerNode, Thumbnail } from './types' | ||
|
||
export class IndexerDB extends Dexie { | ||
index: Dexie.Table<IndexerNode, string> | ||
thumbnails: Dexie.Table<Thumbnail, string> | ||
|
||
constructor() { | ||
super('ImagePicker') | ||
this.version(1).stores({ | ||
index: 'path', | ||
thumbnails: 'id', | ||
}) | ||
this.index = this.table('index') | ||
this.thumbnails = this.table('thumbnails') | ||
} | ||
} |
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,2 @@ | ||
export * from './Indexer' | ||
export * from './types' |
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,24 @@ | ||
import { type TFile } from 'obsidian' | ||
|
||
export interface IndexerRoot { | ||
[path: string]: IndexerNode | ||
} | ||
|
||
export interface IndexerNode | ||
extends Pick<TFile, 'basename' | 'extension' | 'stat' | 'path' | 'name'> { | ||
uri: string | ||
thumbnail?: string | ||
} | ||
|
||
export interface AbstractIndexerRoot { | ||
[path: string]: AbstractIndexerNode | ||
} | ||
|
||
export interface AbstractIndexerNode extends Omit<IndexerNode, 'thumbnail'> { | ||
thumbnail: Thumbnail | ||
} | ||
|
||
export interface Thumbnail { | ||
id: string | ||
data: string | ||
} |
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 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