Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fc5f954
Query entries from node
cafca Apr 9, 2021
f934b87
Use camelCase api and some clean up
cafca Apr 9, 2021
ae3446e
Split up p2panda-api
cafca Apr 9, 2021
0b338eb
Refactor p2panda-api
cafca Apr 9, 2021
fd3ebf3
Store nextEntryArgs after publish
cafca Apr 9, 2021
7083bf6
Restrict length of chat log
cafca Apr 9, 2021
44a1e6a
Use the full message fields format when creating entries
cafca Apr 10, 2021
117c8ff
Cleanup and docs
cafca Apr 14, 2021
d555ab0
Fix folder name of Chatlog component
adzialocha Apr 15, 2021
a3a6e1e
Remove unused dependency
adzialocha Apr 15, 2021
292331e
More cleanup
adzialocha Apr 15, 2021
c36bd1d
Merge branch 'main' into query-entries
adzialocha Apr 15, 2021
b79c652
Move variables into own file and use them in Instructions
adzialocha Apr 15, 2021
8e61f28
Fix disappearing messages by introducing author to react key
adzialocha Apr 15, 2021
bd438fd
Fix slicing of last 10 messages
adzialocha Apr 15, 2021
26d79cc
Introduce date in message schema
adzialocha Apr 15, 2021
400c09a
Fix typo
cafca Apr 16, 2021
afd3830
Don't wait for session to be ready
cafca Apr 16, 2021
8fe0c9a
Allow toggling message sync
cafca Apr 16, 2021
e9b593f
Add a title
adzialocha Apr 16, 2021
ab0a51c
Correct variable name in Instructions
adzialocha Apr 16, 2021
32f8ee8
Log created message fields
cafca Apr 17, 2021
f2ed259
Don't reencode key pair
cafca Apr 17, 2021
9c23a9a
Merge branch 'query-entries' of github.com:p2panda/beep-boop into que…
cafca Apr 17, 2021
0f4ddf2
Fix sorting entries by date
adzialocha Apr 17, 2021
6772745
Make log entry clickable, minor css changes, nicer hashes
adzialocha Apr 17, 2021
23ab8cb
Fix memory leak
cafca May 13, 2021
2dbb1d2
Surface only untagged message field values
cafca May 13, 2021
6473f49
Use untagged messagefields form
cafca May 13, 2021
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
Prev Previous commit
Next Next commit
Store nextEntryArgs after publish
  • Loading branch information
cafca committed Apr 9, 2021
commit fd3ebf3ef1fe0cf4a1d8bc7a95038bffc204b5c3
10 changes: 6 additions & 4 deletions src/p2panda-api/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ const create = async (
fields: { message: string },
{ keyPair, schema, session }: InstanceArgs,
): Promise<void> => {
await session.init();

const {
MessageFields,
encodeCreateMessage,
signEncodeEntry,
KeyPair,
} = session.p2panda;
} = await session.loadWasm();

// Hard coded field type for now
const FIELD_TYPE = 'text';
Expand Down Expand Up @@ -53,7 +51,11 @@ const create = async (
);

// Publish entry and store returned entryArgs for next entry
await session.publishEntry(entryEncoded, encodedMessage);
const nextEntryArgs = await session.publishEntry(
entryEncoded,
encodedMessage,
);
session.setNextEntryArgs(keyPair.publicKey(), schema, nextEntryArgs);
};

export default { create };
58 changes: 38 additions & 20 deletions src/p2panda-api/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,62 @@ import p2panda from 'p2panda-js';
import { Resolved } from '~/typescript/helpers';

import { log } from '.';
import { EntryArgs, EntryRecord, EntryRecordEncoded } from './types';
import { Entry, EntryArgs, EntryRecord, EntryRecordEncoded } from './types';

export default class Session {
endpoint: string = null;
private client = null;
p2panda: Resolved<typeof p2panda> = null;
nextEntryArgs: { [cacheKey: string]: EntryArgs } = {};

constructor(endpoint: Session['endpoint']) {
this.endpoint = endpoint;
const transport = new HTTPTransport(endpoint);
this.client = new Client(new RequestManager([transport]));
this.loadWasm();
}

// Load the WebAssembly p2panda library. Always await this function before
// using this.p2panda. Unfortunately this cannot be handled in the
// constructor as that cannot be asynchronous.
async init(): Promise<void> {
if (this.p2panda != null) return;
this.p2panda = await p2panda;
// Load and return the WebAssembly p2panda library.
//
// Always await this function before
// using `this.p2panda`. Unfortunately this cannot be handled in the
// constructor as the contructor cannot be async.
async loadWasm(): Promise<Session['p2panda']> {
if (this.p2panda == null) {
this.p2panda = await p2panda;
}
return this.p2panda;
}

async getNextEntryArgs(author: string, schema: string): Promise<EntryArgs> {
// do rpc call
const result = await this.client.request({
method: 'panda_getEntryArguments',
params: { author, schema },
});
log('panda_getEntryArguments', result);
return result;
const cacheKey = `${author}/${schema}`;
let rv = this.nextEntryArgs[cacheKey];
if (rv) {
// use cache
delete this.nextEntryArgs[cacheKey];
log('panda_getEntryArguments [cached]', rv);
return rv;
} else {
// do rpc call
rv = await this.client.request({
method: 'panda_getEntryArguments',
params: { author, schema },
});
log('panda_getEntryArguments', rv);
}

return rv;
}

setNextEntryArgs(author: string, schema: string, entryArgs: EntryArgs): void {
const cacheKey = `${author}/${schema}`;
this.nextEntryArgs[cacheKey] = entryArgs;
}

async publishEntry(
entryEncoded: string,
messageEncoded: string,
): Promise<void> {
): Promise<EntryArgs> {
const result = await this.client.request({
method: 'panda_publishEntry',
params: { entryEncoded, messageEncoded },
Expand All @@ -57,16 +78,13 @@ export default class Session {
}

async queryEntries(schema: string): Promise<EntryRecord[]> {
await this.init();
const { decodeEntry } = await this.loadWasm();

const result = await this.queryEntriesEncoded(schema);
return Promise.all(
result.map(async (entry) => ({
...entry,
decoded: await this.p2panda.decodeEntry(
entry.entryBytes,
entry.payloadBytes,
),
decoded: await decodeEntry(entry.entryBytes, entry.payloadBytes),
})),
);
}
Expand Down