-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.ts
44 lines (35 loc) · 1.28 KB
/
adapter.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
38
39
40
41
42
43
44
import type { Address, Expression, ExpressionAdapter, PublicSharing, HolochainLanguageDelegate, LanguageContext } from "@perspect3vism/ad4m";
import { IpfsPutAdapter } from "./putAdapter";
import { DNA_NICK } from "./dna";
const _appendBuffer = (buffer1, buffer2) => {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
const uint8ArrayConcat = (chunks) => {
return chunks.reduce(_appendBuffer);
};
export default class Adapter implements ExpressionAdapter {
#holochain: HolochainLanguageDelegate;
putAdapter: PublicSharing;
constructor(context: LanguageContext) {
this.#holochain = context.Holochain as HolochainLanguageDelegate;
this.putAdapter = new IpfsPutAdapter(context);
}
async get(address: Address): Promise<Expression> {
const { expressions } = await this.#holochain.call(
DNA_NICK,
"anchored-expression",
"get_expressions",
{ key: address }
);
if (expressions.length === 0) return null;
const expression = expressions.pop();
for(const prop of Object.keys(expression.data)) {
if(expression.data[prop] === null)
delete expression.data[prop]
}
return expression;
}
}