-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
37 lines (29 loc) · 1.35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { AsyncFeed, Feed } from './lib/AsyncFeed'
import BlockStorage from './lib/BlockStorage'
import { MergeHandler } from './lib/MergeHandler'
import Transaction from './lib/Transaction'
import * as Errors from './lib/Errors'
type RWFunction = (index: number, data: Buffer) => Buffer
type ConstructOpts = { valueEncoding?: string, onWrite?: RWFunction, onRead?: RWFunction }
type IndexNode = { id: number, children: Array<number>, content: Array<number>, index?: number }
export class HyperObjects {
readonly feed: AsyncFeed
readonly valueEncoding: string
readonly storage: BlockStorage
constructor(feed: Feed, opts?: ConstructOpts) {
opts = opts || {}
this.feed = new AsyncFeed(feed)
this.storage = new BlockStorage(this.feed, opts.onWrite, opts.onRead)
this.valueEncoding = opts.valueEncoding || 'binary'
}
async transaction<T>(executor: (tr: Transaction) => Promise<T>, mergeHandler?: MergeHandler) : Promise<T> {
await this.storage.ready()
const head = await this.feed.length()
const tr = new Transaction(this.storage, head, {valueEncoding: this.valueEncoding, mergeHandler: mergeHandler})
await tr.ready()
const retval = await executor(tr)
await tr.commit()
return retval
}
}
export {Transaction, MergeHandler, AsyncFeed, Feed, BlockStorage, Errors}