Big changes and fixes. Uses action history. Improve role selection. Remove unused logs
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { AppendInvitationParameters, Engine, FindSuitableResourcesParameters } from '@xo-cash/engine';
|
||||
import type { XOInvitation, XOInvitationInput, XOInvitationOutput, XOInvitationVariable } from '@xo-cash/types';
|
||||
import type { XOInvitation, XOInvitationCommit, XOInvitationInput, XOInvitationOutput, XOInvitationVariable } from '@xo-cash/types';
|
||||
import type { UnspentOutputData } from '@xo-cash/state';
|
||||
|
||||
import type { SSEvent } from '../utils/sse-client.js';
|
||||
@@ -50,18 +50,12 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
throw new Error(`Template not found: ${invitation.templateIdentifier}`);
|
||||
}
|
||||
|
||||
console.log('Invitation:', invitation);
|
||||
|
||||
// Create the invitation
|
||||
const invitationInstance = new Invitation(invitation, dependencies);
|
||||
|
||||
console.log('Invitation instance:', invitationInstance);
|
||||
|
||||
// Start the invitation and its tracking
|
||||
await invitationInstance.start();
|
||||
|
||||
console.log('Invitation started:', invitationInstance);
|
||||
|
||||
return invitationInstance;
|
||||
}
|
||||
|
||||
@@ -114,21 +108,28 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
|
||||
async start(): Promise<void> {
|
||||
// Connect to the sync server and get the invitation (in parallel)
|
||||
console.time(`connectAndGetInvitation-${this.data.invitationIdentifier}`);
|
||||
const [_, invitation] = await Promise.all([
|
||||
this.syncServer.connect(),
|
||||
this.syncServer.getInvitation(this.data.invitationIdentifier),
|
||||
]);
|
||||
console.timeEnd(`connectAndGetInvitation-${this.data.invitationIdentifier}`);
|
||||
|
||||
// There is a chance we get SSE messages before the invitation is returned, so we want to combine any commits
|
||||
const sseCommits = this.data.commits;
|
||||
|
||||
// Set the invitation data with the combined commits
|
||||
this.data = { ...this.data, ...invitation, commits: [...sseCommits, ...(invitation?.commits ?? [])] };
|
||||
console.time(`mergeCommits-${this.data.invitationIdentifier}`);
|
||||
// Merge the commits
|
||||
const combinedCommits = this.mergeCommits(sseCommits, invitation?.commits ?? []);
|
||||
console.timeEnd(`mergeCommits-${this.data.invitationIdentifier}`);
|
||||
|
||||
console.log('Invitation data:', this.data);
|
||||
console.time(`setInvitationData-${this.data.invitationIdentifier}`);
|
||||
// Set the invitation data with the combined commits
|
||||
this.data = { ...this.data, ...invitation, commits: combinedCommits };
|
||||
|
||||
// Store the invitation in the storage
|
||||
await this.storage.set(this.data.invitationIdentifier, this.data);
|
||||
console.timeEnd(`setInvitationData-${this.data.invitationIdentifier}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,17 +144,16 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
const data = JSON.parse(event.data) as { topic?: string; data?: unknown };
|
||||
if (data.topic === 'invitation-updated') {
|
||||
const invitation = decodeExtendedJsonObject(data.data) as XOInvitation;
|
||||
console.log('Invitation updated:', invitation);
|
||||
|
||||
if (invitation.invitationIdentifier !== this.data.invitationIdentifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('New commits:', invitation.commits);
|
||||
|
||||
// Filter out commits that already exist (probably a faster way to do this. This is n^2)
|
||||
const newCommits = invitation.commits.filter(commit => !this.data.commits.some(c => c.commitIdentifier === commit.commitIdentifier));
|
||||
this.data.commits.push(...newCommits);
|
||||
const newCommits = this.mergeCommits(this.data.commits, invitation.commits);
|
||||
|
||||
// Set the new commits
|
||||
this.data = { ...this.data, commits: newCommits };
|
||||
|
||||
// Calculate the new status of the invitation
|
||||
this.updateStatus();
|
||||
@@ -163,6 +163,28 @@ export class Invitation extends EventEmitter<InvitationEventMap> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge the commits
|
||||
* @param initial - The initial commits
|
||||
* @param additional - The additional commits
|
||||
* @returns The merged commits
|
||||
*/
|
||||
private mergeCommits(initial: XOInvitationCommit[], additional: XOInvitationCommit[]): XOInvitationCommit[] {
|
||||
// Create a map of the initial commits
|
||||
const initialMap = new Map<string, XOInvitationCommit>();
|
||||
for(const commit of initial) {
|
||||
initialMap.set(commit.commitIdentifier, commit);
|
||||
}
|
||||
|
||||
// Merge the additional commits
|
||||
// TODO: They are immutable? So, it should be fine to "ovewrite" existing commits as it should be the same data, right?
|
||||
for(const commit of additional) {
|
||||
initialMap.set(commit.commitIdentifier, commit);
|
||||
}
|
||||
|
||||
// Return the merged commits
|
||||
return Array.from(initialMap.values());
|
||||
}
|
||||
/**
|
||||
* Update the status of the invitation based on the filled in information
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user