forked from Azure/BatchExplorer
-
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.
Fix bugs related to the settings and remove dependency on electron-js…
…on-storage (Azure#662) * Fix settings crash when no setting file * REmove * remove package * Fix * remove types * fix
- Loading branch information
1 parent
3d07c4d
commit da82b52
Showing
12 changed files
with
97 additions
and
158 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
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
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,47 +1,59 @@ | ||
import { Injectable } from "@angular/core"; | ||
import * as storage from "electron-json-storage"; | ||
import { AsyncSubject, Observable } from "rxjs"; | ||
import * as path from "path"; | ||
import { Observable } from "rxjs"; | ||
|
||
import { log } from "app/utils"; | ||
import { FileSystemService } from "./fs.service"; | ||
|
||
/** | ||
* This service is used to read/write files to the user data folder. | ||
* Prefer this for writing big data over localStorage. | ||
* Each key is a different file under userData. | ||
*/ | ||
@Injectable() | ||
export class LocalFileStorage { | ||
|
||
constructor(private fs: FileSystemService) { } | ||
/** | ||
* @param filename Name of the file | ||
* @param key Key where the data is store | ||
* @returns Observable which will resolve the data contained in the file if successfull or reject if any error | ||
*/ | ||
public get<T>(filename: string): Observable<T> { | ||
const sub = new AsyncSubject<T>(); | ||
storage.get(filename, (error, data) => { | ||
this._errorToSub(sub, error, data); | ||
public get<T>(key: string): Observable<T> { | ||
return this.read(key).map((content) => { | ||
if (!content) { | ||
return {}; | ||
} | ||
|
||
try { | ||
return JSON.parse(content); | ||
} catch (e) { | ||
log.error("Loading file from storage has invalid json", { key, content }); | ||
return {}; | ||
} | ||
}); | ||
return sub.asObservable(); | ||
} | ||
|
||
/** | ||
* Store the given data into the given file. | ||
* @param filename Filename to store the data | ||
* @param filename Key to store the data(Corespond to a file under userdata) | ||
* @param data Javascript object(JSON format) to store | ||
* @returns observable that will resolve if saving is sucessfull or reject if any error | ||
*/ | ||
public set<T>(filename: string, data: T): Observable<{}> { | ||
const sub = new AsyncSubject(); | ||
storage.set(filename, data, (error) => { | ||
this._errorToSub(sub, error); | ||
}); | ||
return sub; | ||
public set<T>(key: string, data: T): Observable<{}> { | ||
const content = JSON.stringify(data); | ||
return this.write(key, content); | ||
} | ||
|
||
private _errorToSub(subject: AsyncSubject<any>, error: any, data?: any) { | ||
if (error) { | ||
subject.error(error); | ||
} else { | ||
if (data) { | ||
subject.next(data); | ||
} | ||
subject.complete(); | ||
} | ||
public read(key: string): Observable<string> { | ||
return Observable.fromPromise(this.fs.readFile(this._getFile(key)).catch(() => null)); | ||
} | ||
|
||
public write(key: string, content: string): Observable<string> { | ||
return Observable.fromPromise(this.fs.saveFile(this._getFile(key), content)); | ||
} | ||
|
||
private _getFile(key: string) { | ||
const filename = key.endsWith(".json") ? key : `${key}.json`; | ||
return path.join(this.fs.commonFolders.userData, filename); | ||
} | ||
} |
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
Oops, something went wrong.