Last active
October 4, 2023 05:15
-
-
Save lejeunerenard/0502d82bb2e58a9200da711bc29af261 to your computer and use it in GitHub Desktop.
Autobase remote only example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Corestore from 'corestore' | |
import RAM from 'random-access-memory' | |
import Autobase from 'autobase' | |
// Create two chat users, each with their own Hypercores. | |
const st = new Corestore(RAM) | |
const userAInput = st.get({ name: 'UserAInput' }) | |
const userAOutput = st.get({ name: 'UserAOutput', valueEncoding: 'json' }) | |
const userBInput = st.get({ name: 'UserBInput' }) | |
// Make two Autobases with those two users as inputs. | |
const baseA = new Autobase({ | |
inputs: [userAInput, userBInput], | |
localInput: userAInput, | |
outputs: [userAOutput], | |
localOutput: userAOutput, | |
eagerUpdate: false // ensure it doesn't compute view automatically | |
}) | |
const baseB = new Autobase({ | |
inputs: [userAInput, userBInput], | |
localInput: userBInput, | |
outputs: [userAOutput], | |
eagerUpdate: false // ensure it doesn't compute view automatically | |
}) | |
// Append chat messages and read them out again, using the default options. | |
// This simulates two peers who are always completely up-to-date with each others messages. | |
await baseA.append('A0: hello!') | |
await baseB.append('B0: hi! good to hear from you') | |
await baseA.append('A1: likewise. fun exercise huh?') | |
await baseB.append('B1: yep. great time.') | |
// Local Linearization | |
baseA.start({ | |
unwrap: true, | |
apply: async (view, batch) => { | |
batch = batch.map(({ value }) => Buffer.from(value.toString('utf-8').toUpperCase(), 'utf-8')) | |
await view.append(batch) | |
} | |
}) | |
for await (const node of baseA.createCausalStream()) { | |
console.log(node.value.toString()) | |
} | |
// Local Linearization | |
baseB.start({ | |
unwrap: true, | |
apply: async (view, batch) => { | |
console.log('baseB append called') | |
batch = batch.map(({ value }) => Buffer.from(value.toString('utf-8').toUpperCase(), 'utf-8')) | |
await view.append(batch) | |
} | |
}) | |
console.log('\nREADING VIEW A \n') | |
const viewA = baseA.view | |
await viewA.update() | |
for (let i = 0; i < viewA.length; i++) { | |
const node = await viewA.get(i) | |
console.log(node.value.toString()) | |
} | |
console.log('\nREADING VIEW B \n') | |
const viewB = baseA.view | |
await viewB.update() | |
for (let i = 0; i < viewB.length; i++) { | |
const node = await viewB.get(i) | |
console.log(node.value.toString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment