0.400.0-dev.0 • Published 4 days ago

@holochain-syn/store v0.400.0-dev.0

Weekly downloads
-
License
-
Repository
-
Last release
4 days ago

@holochain-syn/store

Reactive store that holds the state for the syn Holochain zome.

High-level design

These are the high level concepts that syn implements:

  • Each network that includes syn can manage multiple documents.
  • Each document is identified by its root commit hash.
  • Each document has multiple workspaces which can evolve independently of each other, and also fork and merge (eg. "main", "proposal").
  • Each workspace has a latest "tip" commit, which represents the latest snapshot of the state of the document in that workspace.
  • Finally, each workspace has a session, which you can join to edit the state of the workspace collaboratively with other agents.

And at the level of code, these concepts translate to these classes:

  • SynStore: to create and fetch the documents in this network.
  • DocumentStore: to create and fetch the workspaces for the given document, and also its commits.
  • WorkspaceStore: to fetch the latest snaphshot and also the previous commits for the given workspace.
  • SessionStore: to edit the state of the given workspace in a real-time collaborative session.

Initialization

You can initialize a new document like this:

import { AppWebsocket, AppWebsocket } from '@holochain/client';
import { SynStore, DocumentStore, WorkspaceStore } from '@holochain-syn/store';
import { SynClient } from '@holochain-syn/client';

const appWs = await AppWebsocket.connect(url);
const client = await AppWebsocket.connect(appWs, 'YOUR_APP_ID')

const synStore = new SynStore(new SynClient(client, 'YOUR_ROLE_NAME', 'YOUR_ZOME_NAME'));

// Create a new document
const documentStore = await synStore.createDocument(
  // Initial state of the document
  { applicationDefinedField: 'somevalue' },
  // This is an optional object to be able to store arbitrary information in the commit
  { meta: 'value'}
);
// Tag the document as "active" to allow other peers to discover it
await synStore.client.tagDocument(documentHash, "active")

// Create the workspace for the document
const workspaceStore = new documentStore.createWorkspace(
  'main',
  // Commit hash that will act as the initial tip for the workspace
  // Passing undefined means the workspace will be initialized with the document's initial state
  undefined
);

At this point, no synchronization is happening yet. This is because you haven't joined the session for the newly created workspace. Let's join the session:

const sessionStore: SessionStore = await sessionStore.joinSession();

If you want another peer to discover that document and join the same session, you can do this:

import { AnyDhtHash } from '@holochain/client'
import { Commit } from '@holochain-syn/client';
import { EntryRecord, EntryHashMap } from '@holochain-open-dev/utils';
import { DocumentStore, WorkspaceStore } from '@holochain-syn/store';
import { toPromise, joinAsyncMap, pipe } from '@holochain-open-dev/stores';

// Fetch all the active documents
const documentsHashes: Array<AnyDhtHash> = await synStore.client.getDocumentsWithTag("active");

// Build the documentStore for the document with the first document
const documentStore = synStore.documents.get(documentsHashes[0]);

// Fetch all workspaces for that document
const workspaces: ReadonlyMap<EntryHash, WorkspaceStore> = await toPromise(documentStore.allWorkspaces);

// Find the workspace
const workspaceStore = Array.from(workspaces.entries())[0];

// Join the session for the workspace
const sessionStore = await workspaceStore.joinSession();

State and state changes

Now you are connected to all the peers in that same workspace, and can subscribe to the current state for the workspace and also request changes to the state:

sessionStore.state.subscribe(state => console.log('New State!', state));

// The input for the function needs to be a function that mutates the given javascript object state 
sessionStore.change(state => {
  state.applicationDefinedField = 'Updated content!';
});

Alternatively, you can also get information about the current state of the workspace without joining the session:

workspaceStore.tip.subscribe(tip => {
  if (tip.status === 'complete') { // "status" can also be "pending" or "error"
    console.log('current tip of the workspace: ', tip);
  }
});

workspaceStore.latestSnapshot.subscribe(latestSnapshot => {
  if (latestSnapshot.status === 'complete') { // "status" can also be "pending" or "error"
    console.log('current state of the workspace: ', latestSnapshot);
  }
});

workspaceStore.sessionParticipants.subscribe(participants => {
  if (participants.status === 'complete') { // "status" can also be "pending" or "error"
    console.log('current participants of the workspace session: ', participants);
  }
});

This is useful to display information about the current state of the workspace without having to join the session.

Leaving the session

When you are done with those changes, you need to explicitly leave the session:

await sessionStore.leaveSession();

If you don't, all other participants in the session will try to keep synchronizing with you.

Committing

Changes are committed every 10 seconds by default, and also when the last participant for the workspaces leaves the workspace. You can also commit the changes manually:

await sessionStore.commitChanges(
    // This is an optional object to be able to store arbitrary information in the commit
  { applicationDefinedField: 'somevalue'} 
);
0.400.0-dev.0

4 days ago

0.300.0-dev.1

12 days ago

0.300.0-dev.0

2 months ago

0.11.4

3 months ago

0.11.5

3 months ago

0.11.3

3 months ago

0.11.2

3 months ago

0.11.1

4 months ago

0.11.0

5 months ago

0.10.0

5 months ago

0.9.0

5 months ago

0.8.0

6 months ago

0.7.1

6 months ago

0.6.2

6 months ago

0.5.0

8 months ago

0.4.1

10 months ago

0.7.0

6 months ago

0.6.1

6 months ago

0.6.0

7 months ago

0.3.0

1 year ago

0.4.0

1 year ago

0.3.0-alpha.6

1 year ago

0.3.0-alpha.7

1 year ago

0.3.0-alpha.2

1 year ago

0.3.0-alpha.3

1 year ago

0.3.0-alpha.4

1 year ago

0.3.0-alpha.5

1 year ago

0.2.5

1 year ago

0.3.0-alpha.1

1 year ago

0.2.4

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.4

2 years ago

0.0.5

2 years ago

0.1.3

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago