Skip to content

Commit

Permalink
Merge pull request #357 from ajthinking/FileStorage
Browse files Browse the repository at this point in the history
Add FileStorage
  • Loading branch information
ajthinking authored Jan 3, 2025
2 parents 8e2e6dc + 98c3dab commit 331d147
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ds-ext/src/DiagramEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { observeLinkUpdate } from './messageHandlers/observeLinkUpdate';
import { getDataFromStorage } from './messageHandlers/getDataFromStorage';
import { cancelObservation } from './messageHandlers/cancelObservation';
import { DuckDBStorage } from './duckDBStorage';
import { FileStorage } from './fileStorage';

export class DiagramEditorProvider implements vscode.CustomEditorProvider<DiagramDocument> {
private readonly _onDidChangeCustomDocument = new vscode.EventEmitter<vscode.CustomDocumentEditEvent<DiagramDocument>>();
Expand Down Expand Up @@ -44,6 +45,7 @@ export class DiagramEditorProvider implements vscode.CustomEditorProvider<Diagra
private async init(): Promise<void> {
try {
this.observerStorage = new DuckDBStorage();
// this.observerStorage = new FileStorage();
} catch (error) {
this.observerStorage = new DiagramObserverStorage();
}
Expand Down
75 changes: 75 additions & 0 deletions packages/ds-ext/src/fileStorage.ts
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));
}
}

0 comments on commit 331d147

Please sign in to comment.