-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement persistence of last used using stateDB
- Loading branch information
1 parent
40d90ff
commit 4282865
Showing
3 changed files
with
44 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,52 @@ | ||
import { ILauncher } from '@jupyterlab/launcher'; | ||
import { IStateDB } from '@jupyterlab/statedb'; | ||
|
||
export interface ILastUsedDatabase { | ||
get(item: ILauncher.IItemOptions): Date | null; | ||
recordAsUsedNow(item: ILauncher.IItemOptions): void; | ||
recordAsUsedNow(item: ILauncher.IItemOptions): Promise<void>; | ||
} | ||
|
||
type DatabaseLayout = Record<string, string>; | ||
|
||
export class LastUsedDatabase implements ILastUsedDatabase { | ||
constructor() { | ||
// TODO: use settings registry, or state db, or server to persist this info | ||
this._db = new Map(); | ||
constructor(options: { stateDB: IStateDB; fetchInterval: number }) { | ||
this._stateDB = options.stateDB; | ||
this._updateDB(); | ||
window.setInterval(this._updateDB, options.fetchInterval); | ||
} | ||
get(item: ILauncher.IItemOptions) { | ||
const date = this._db.get(this._itemKey(item)); | ||
if (!this._db) { | ||
return null; | ||
} | ||
const date = this._db[this._itemKey(item)]; | ||
return date ? new Date(date) : null; | ||
} | ||
recordAsUsedNow(item: ILauncher.IItemOptions) { | ||
this._db.set(this._itemKey(item), new Date().toUTCString()); | ||
async recordAsUsedNow(item: ILauncher.IItemOptions) { | ||
const db = await this._fetch(); | ||
this._db = db; | ||
db[this._itemKey(item)] = new Date().toUTCString(); | ||
await this._stateDB.save(this._stateDBKey, db); | ||
} | ||
private _updateDB = () => { | ||
this._fetch() | ||
.then(db => { | ||
this._db = db; | ||
}) | ||
.catch(console.warn); | ||
}; | ||
private async _fetch(): Promise<DatabaseLayout> { | ||
const db = (await this._stateDB.fetch(this._stateDBKey)) as | ||
| DatabaseLayout | ||
| undefined; | ||
if (typeof db === 'undefined') { | ||
return {}; | ||
} | ||
return db; | ||
} | ||
private _itemKey(item: ILauncher.IItemOptions): string { | ||
return item.command + '_' + JSON.stringify(item.args); | ||
} | ||
private _db: Map<string, string>; | ||
private _db: DatabaseLayout | null = null; | ||
private _stateDB: IStateDB; | ||
private _stateDBKey = 'new-launcher:last-used'; | ||
} |
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