Skip to content

Commit 8d80f58

Browse files
committed
fix: progression system
1 parent 5f5ba03 commit 8d80f58

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

s/browser/behemoth-opfs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {Behemoth} from "../core/behemoth.js"
33
import {Hash, SetOptions} from "../core/types.js"
44
import {hashBlob} from "../core/tools/hash-blob.js"
5-
import {progression} from "../core/utils/progression.js"
5+
import {Progression} from "../core/utils/progression.js"
66
import {writeBlobToOpfs} from "./utils/write-blob-to-opfs.js"
77
import {getOpfsFileHandle} from "./utils/get-opfs-file-handle.js"
88

@@ -35,14 +35,14 @@ export class BehemothOpfs extends Behemoth {
3535
}
3636

3737
async set(blob: Blob, o?: SetOptions) {
38-
const progress = progression(blob.size * 2, o?.onProgress)
38+
const progress = Progression.blobStorage(blob.size, o?.onProgress)
3939

40-
const hash = await hashBlob(blob, progress.add)
40+
const hash = await hashBlob(blob, progress.hashing.set)
4141

4242
if (!await this.has(hash))
43-
await writeBlobToOpfs(blob, this.#directory, hash, progress.add)
43+
await writeBlobToOpfs(blob, this.#directory, hash, progress.storing.set)
4444

45-
progress.done()
45+
progress.finish()
4646
return hash
4747
}
4848

s/core/behemoth-memory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {GMap} from "@e280/stz"
33
import {Behemoth} from "./behemoth.js"
44
import {SetOptions, Hash} from "./types.js"
55
import {hashBlob} from "./tools/hash-blob.js"
6-
import {progression} from "./utils/progression.js"
6+
import {Progression} from "./utils/progression.js"
77

88
export class BehemothMemory extends Behemoth {
99
#map = new GMap<Hash, Blob>()
@@ -21,14 +21,14 @@ export class BehemothMemory extends Behemoth {
2121
}
2222

2323
async set(blob: Blob, o?: SetOptions) {
24-
const progress = progression(blob.size * 2, o?.onProgress)
24+
const progress = Progression.blobStorage(blob.size, o?.onProgress)
2525

26-
const hash = await hashBlob(blob, progress.add)
26+
const hash = await hashBlob(blob, progress.hashing.set)
2727

2828
if (!await this.has(hash))
2929
this.#map.set(hash, blob)
3030

31-
progress.done()
31+
progress.finish()
3232
return hash
3333
}
3434

s/core/utils/progression.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11

22
import {Progress} from "../types.js"
33

4-
export const progression = (
5-
total: number,
6-
onProgress: ((p: Progress) => void) = (() => {}),
7-
) => {
4+
export class Progression {
5+
static blobStorage(blobSize: number, onProgress: ((p: Progress) => void) = (() => {})) {
6+
let total = blobSize * 2
7+
let done = 0
8+
onProgress({total, done})
89

9-
let done = 0
10-
onProgress({total, done})
11-
12-
return {
13-
add: (n: number) => {
14-
done += n
10+
function report() {
11+
done = hashing.done + storing.done
1512
onProgress({total, done})
16-
},
13+
}
1714

18-
done: () => {
15+
function finish() {
1916
onProgress({total, done: total})
20-
},
17+
}
18+
19+
const hashing = new Progression(blobSize, report)
20+
const storing = new Progression(blobSize, report)
21+
return {hashing, storing, finish}
22+
}
23+
24+
done = 0
25+
26+
constructor(
27+
public total: number,
28+
public onProgress: ((p: Progress) => void) = (() => {}),
29+
) {}
30+
31+
#report() {
32+
const {total, done} = this
33+
this.onProgress({total, done})
34+
}
35+
36+
add = (n: number) => {
37+
this.done += n
38+
this.#report()
39+
}
40+
41+
set = (n: number) => {
42+
this.done = n
43+
this.#report()
44+
}
45+
46+
finish = () => {
47+
this.done = this.total
48+
this.#report()
2149
}
2250
}
2351

s/node/behemoth-disk.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {readBlob} from "../core/tools/readers.js"
77
import {Hash, SetOptions} from "../core/types.js"
88
import {hashBlob} from "../core/tools/hash-blob.js"
99
import {writeToFile} from "./utils/write-to-file.js"
10-
import {progression} from "../core/utils/progression.js"
10+
import {Progression} from "../core/utils/progression.js"
1111

1212
export class BehemothDisk extends Behemoth {
1313
static async mkdir(path: string) {
@@ -37,14 +37,14 @@ export class BehemothDisk extends Behemoth {
3737
}
3838

3939
async set(blob: Blob, o?: SetOptions) {
40-
const progress = progression(blob.size * 2, o?.onProgress)
40+
const progress = Progression.blobStorage(blob.size, o?.onProgress)
4141

42-
const hash = await hashBlob(blob, progress.add)
42+
const hash = await hashBlob(blob, progress.hashing.set)
4343

4444
if (!await this.has(hash))
45-
await writeToFile(this.#path(hash), readBlob(blob), progress.add)
45+
await writeToFile(this.#path(hash), readBlob(blob), progress.storing.set)
4646

47-
progress.done()
47+
progress.finish()
4848
return hash
4949
}
5050

0 commit comments

Comments
 (0)