-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #357 from ajthinking/FileStorage
Add FileStorage
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { GetLinkItemsParams, ItemValue, ObserverStorage } from '@data-story/core'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
|
||
type FileStorageData = { | ||
linkCounts: Record<string, number>; | ||
linkItems: Record<string, ItemValue[]>; | ||
nodes: Record<string, 'BUSY' | 'COMPLETE'>; | ||
} | ||
|
||
export class FileStorage implements ObserverStorage { | ||
constructor() { | ||
this.write({ | ||
linkCounts: {}, | ||
linkItems: {}, | ||
nodes: {} | ||
}); | ||
} | ||
|
||
async close() {} | ||
|
||
async getLinkCount(linkId: string): Promise<number | undefined> { | ||
const data = this.read(); | ||
return data.linkCounts[linkId]; | ||
} | ||
|
||
async setLinkCount(linkId: string, count: number): Promise<void> { | ||
const data = this.read(); | ||
data.linkCounts[linkId] = count; | ||
this.write(data); | ||
} | ||
|
||
async getLinkItems({linkId, offset, limit}: GetLinkItemsParams): Promise<ItemValue[] | undefined> { | ||
const data = this.read(); | ||
return data.linkItems[linkId].slice(offset, offset + limit); | ||
} | ||
|
||
async setLinkItems(linkId: string, items: ItemValue[]): Promise<void> { | ||
const data = this.read(); | ||
data.linkItems[linkId] = items; | ||
this.write(data); | ||
} | ||
|
||
async appendLinkItems(linkId: string, items: ItemValue[]): Promise<void> { | ||
const data = this.read(); | ||
data.linkItems[linkId].push(...items); | ||
this.write(data); | ||
} | ||
|
||
async getNodeStatus(nodeId: string): Promise<'BUSY' | 'COMPLETE' | undefined> { | ||
const data = this.read(); | ||
return data.nodes[nodeId]; | ||
} | ||
|
||
async setNodeStatus(nodeId: string, status: 'BUSY' | 'COMPLETE'): Promise<void> { | ||
const data = this.read(); | ||
data.nodes[nodeId] = status; | ||
this.write(data); | ||
} | ||
|
||
private getFilePath() { | ||
const workspacePath = vscode.workspace.workspaceFolders![0].uri.fsPath; | ||
const datastoryDir = path.join(workspacePath, '.datastory'); | ||
return path.join(datastoryDir, 'execution.json'); | ||
} | ||
|
||
private read(): FileStorageData { | ||
return JSON.parse(fs.readFileSync(this.getFilePath(), 'utf-8')); | ||
} | ||
|
||
private write(data: FileStorageData) { | ||
fs.writeFileSync(this.getFilePath(), JSON.stringify(data, null, 2)); | ||
} | ||
} |