Skip to content

Commit

Permalink
immutableEntryStore
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Mar 19, 2024
1 parent 96516ef commit f653c92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 61 deletions.
7 changes: 1 addition & 6 deletions packages/store/src/document-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
immutableEntryStore,
liveLinksStore,
pipe,
retryUntilSuccess,
uniquify,
} from '@holochain-open-dev/stores';
import {
Expand Down Expand Up @@ -39,11 +38,7 @@ export function sliceStrings<K extends string, V>(
export class DocumentStore<S, E> {
constructor(public synStore: SynStore, public documentHash: AnyDhtHash) { }

record = retryUntilSuccess(async () => {
const document = await this.synStore.client.getDocument(this.documentHash);
if (!document) throw new Error('Document not found yet');
return document;
});
record = immutableEntryStore(async () => this.synStore.client.getDocument(this.documentHash));

/**
* Keeps an up to date map of all the workspaces for this document
Expand Down
106 changes: 51 additions & 55 deletions packages/store/src/workspace-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
AsyncStatus,
derived,
deriveStore,
immutableEntryStore,
liveLinksStore,
pipe,
retryUntilSuccess,
sliceAndJoin,
toPromise,
uniquify,
Expand All @@ -32,15 +32,11 @@ export class WorkspaceStore<S, E> {
constructor(
public documentStore: DocumentStore<S, E>,
public workspaceHash: EntryHash
) {}
) { }

record = retryUntilSuccess(async () => {
const workspace = await this.documentStore.synStore.client.getWorkspace(
this.workspaceHash
);
if (!workspace) throw new Error('Workspace not found yet');
return workspace;
});
record = immutableEntryStore(async () => this.documentStore.synStore.client.getWorkspace(
this.workspaceHash
));

name = pipe(this.record, workspace => workspace.entry.name);

Expand Down Expand Up @@ -116,54 +112,54 @@ export class WorkspaceStore<S, E> {
derived(
this.session,
s =>
({ status: 'complete', value: s } as AsyncStatus<
SessionStore<S, E> | undefined
>)
({ status: 'complete', value: s } as AsyncStatus<
SessionStore<S, E> | undefined
>)
),
session =>
session
? session.currentTip
: pipe(
liveLinksStore(
this.documentStore.synStore.client,
this.workspaceHash,

() =>
this.documentStore.synStore.client.getWorkspaceTips(
this.workspaceHash
),
'WorkspaceToTip'
),
async commitsLinks => {
const tipsLinks = new HoloHashMap<ActionHash, Link>();

const tipsPrevious = new HoloHashMap<ActionHash, boolean>();

for (const commitLink of commitsLinks) {
tipsLinks.set(commitLink.target, commitLink);
const previousCommitsHashes: Array<ActionHash> = decode(
commitLink.tag
) as Array<ActionHash>;

for (const previousCommitHash of previousCommitsHashes) {
tipsPrevious.set(previousCommitHash, true);
}
liveLinksStore(
this.documentStore.synStore.client,
this.workspaceHash,

() =>
this.documentStore.synStore.client.getWorkspaceTips(
this.workspaceHash
),
'WorkspaceToTip'
),
async commitsLinks => {
const tipsLinks = new HoloHashMap<ActionHash, Link>();

const tipsPrevious = new HoloHashMap<ActionHash, boolean>();

for (const commitLink of commitsLinks) {
tipsLinks.set(commitLink.target, commitLink);
const previousCommitsHashes: Array<ActionHash> = decode(
commitLink.tag
) as Array<ActionHash>;

for (const previousCommitHash of previousCommitsHashes) {
tipsPrevious.set(previousCommitHash, true);
}
}

for (const overwrittenTip of tipsPrevious.keys()) {
tipsLinks.delete(overwrittenTip);
}
const tipsHashes = Array.from(tipsLinks.keys());
if (tipsHashes.length === 0) return undefined;
for (const overwrittenTip of tipsPrevious.keys()) {
tipsLinks.delete(overwrittenTip);
}
const tipsHashes = Array.from(tipsLinks.keys());
if (tipsHashes.length === 0) return undefined;

if (tipsHashes.length === 1) return tipsHashes[0];
if (tipsHashes.length === 1) return tipsHashes[0];

const newCommit = await this.merge(tipsHashes);
return newCommit.actionHash;
},
commit =>
commit ? this.documentStore.commits.get(commit) : undefined
)
const newCommit = await this.merge(tipsHashes);
return newCommit.actionHash;
},
commit =>
commit ? this.documentStore.commits.get(commit) : undefined
)
);

/**
Expand All @@ -173,9 +169,9 @@ export class WorkspaceStore<S, E> {
commit
? (stateFromCommit(commit.entry) as S)
: pipe(
this.documentStore.record,
document => stateFromDocument(document.entry) as S
)
this.documentStore.record,
document => stateFromDocument(document.entry) as S
)
);

/**
Expand All @@ -187,10 +183,10 @@ export class WorkspaceStore<S, E> {
return derived(
session.state,
s =>
({
status: 'complete',
value: s,
} as AsyncStatus<S>)
({
status: 'complete',
value: s,
} as AsyncStatus<S>)
);

return this.latestSnapshot;
Expand Down

0 comments on commit f653c92

Please sign in to comment.