Skip to content

Commit b86383a

Browse files
authored
Merge pull request #92 from isomorphic-git/fs-flush
add fs.flush() that saves superblock
2 parents 5b69f1c + e905450 commit b86383a

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

index.d.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ declare module '@isomorphic-git/lightning-fs' {
206206
* @returns The size of a file or directory in bytes.
207207
*/
208208
du(filepath: string): Promise<number>
209+
/**
210+
* 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)
211+
* @returns Promise that resolves when super block is saved to file
212+
*/
213+
flush(): Promise<void>
209214
}
210215

211216
export interface Options {
@@ -245,8 +250,19 @@ declare module '@isomorphic-git/lightning-fs' {
245250
* @default false
246251
*/
247252
defer?: boolean
248-
253+
db: FS.IDB
254+
}
255+
export interface IDB {
256+
constructor(dbname: string, storename: string): IDB
257+
saveSuperblock(sb: Uint8Array): TypeOrPromise<void>
258+
loadSuperblock(): TypeOrPromise<FS.SuperBlock>
259+
loadFile(inode: number): TypeOrPromise<Uint8Array>
260+
writeFile(inode: number, data: Uint8Array): TypeOrPromise<void>
261+
wipe(): TypeOrPromise<void>
262+
close(): TypeOrPromise<void>
249263
}
264+
type TypeOrPromise<T> = T | Promise<T>
265+
export type SuperBlock = Map<string | number, any>
250266
export interface MKDirOptions {
251267
/**
252268
* Posix mode permissions

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"files": [
4949
"**/*.js",
50+
"index.d.ts",
5051
"!__tests__",
5152
"!coverage"
5253
],

src/DefaultBackend.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const path = require("./path.js");
1313
module.exports = class DefaultBackend {
1414
constructor() {
1515
this.saveSuperblock = debounce(() => {
16-
this._saveSuperblock();
16+
this.flush();
1717
}, 500);
1818
}
1919
async init (name, {
@@ -180,4 +180,7 @@ module.exports = class DefaultBackend {
180180
du(filepath) {
181181
return this._cache.du(filepath);
182182
}
183+
flush() {
184+
return this._saveSuperblock();
185+
}
183186
}

src/PromisifiedFS.js

+3
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,7 @@ module.exports = class PromisifiedFS {
198198
async du(filepath) {
199199
return this._backend.du(filepath);
200200
}
201+
async flush() {
202+
return this._backend.flush();
203+
}
201204
}

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = class FS {
2929
this.symlink = this.symlink.bind(this)
3030
this.backFile = this.backFile.bind(this)
3131
this.du = this.du.bind(this)
32+
this.flush = this.flush.bind(this)
3233
}
3334
init(name, options) {
3435
return this.promises.init(name, options)
@@ -85,4 +86,8 @@ module.exports = class FS {
8586
const [resolve, reject] = wrapCallback(cb);
8687
this.promises.du(filepath).then(resolve).catch(reject);
8788
}
89+
flush(cb) {
90+
const [resolve, reject] = wrapCallback(cb);
91+
this.promises.flush().then(resolve).catch(reject);
92+
}
8893
}

0 commit comments

Comments
 (0)