Skip to content

add fs.flush() that saves superblock #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ declare module '@isomorphic-git/lightning-fs' {
* @returns The size of a file or directory in bytes.
*/
du(filepath: string): Promise<number>
/**
* Function that saves anything that need to be saved in IndexedBD or custom IDB object. Right now it saves SuperBlock so it's save to dump the object as dump it into a file (e.g. from a Browser)
* @returns Promise that resolves when super block is saved to file
*/
flush(): Promise<void>
}

export interface Options {
Expand Down Expand Up @@ -245,8 +250,19 @@ declare module '@isomorphic-git/lightning-fs' {
* @default false
*/
defer?: boolean

db: FS.IDB
}
export interface IDB {
constructor(dbname: string, storename: string): IDB
saveSuperblock(sb: Uint8Array): TypeOrPromise<void>
loadSuperblock(): TypeOrPromise<FS.SuperBlock>
loadFile(inode: number): TypeOrPromise<Uint8Array>
writeFile(inode: number, data: Uint8Array): TypeOrPromise<void>
wipe(): TypeOrPromise<void>
close(): TypeOrPromise<void>
}
type TypeOrPromise<T> = T | Promise<T>
export type SuperBlock = Map<string | number, any>
export interface MKDirOptions {
/**
* Posix mode permissions
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"files": [
"**/*.js",
"index.d.ts",
"!__tests__",
"!coverage"
],
Expand Down
5 changes: 4 additions & 1 deletion src/DefaultBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const path = require("./path.js");
module.exports = class DefaultBackend {
constructor() {
this.saveSuperblock = debounce(() => {
this._saveSuperblock();
this.flush();
}, 500);
}
async init (name, {
Expand Down Expand Up @@ -180,4 +180,7 @@ module.exports = class DefaultBackend {
du(filepath) {
return this._cache.du(filepath);
}
flush() {
return this._saveSuperblock();
}
}
3 changes: 3 additions & 0 deletions src/PromisifiedFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,7 @@ module.exports = class PromisifiedFS {
async du(filepath) {
return this._backend.du(filepath);
}
async flush() {
return this._backend.flush();
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = class FS {
this.symlink = this.symlink.bind(this)
this.backFile = this.backFile.bind(this)
this.du = this.du.bind(this)
this.flush = this.flush.bind(this)
}
init(name, options) {
return this.promises.init(name, options)
Expand Down Expand Up @@ -85,4 +86,8 @@ module.exports = class FS {
const [resolve, reject] = wrapCallback(cb);
this.promises.du(filepath).then(resolve).catch(reject);
}
flush(cb) {
const [resolve, reject] = wrapCallback(cb);
this.promises.flush().then(resolve).catch(reject);
}
}