Created
December 15, 2023 03:56
-
-
Save lejeunerenard/c6ff1548a77296ea61f48694dca389cc to your computer and use it in GitHub Desktop.
Replication & `.ready()` Timing
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' | |
function makeStore (seed) { | |
return new Corestore(RAM, { primaryKey: Buffer.alloc(32).fill(seed) }) | |
} | |
const a = makeStore('a') | |
const aInput = a.get({ name: 'input', valueEncoding: 'json' }) | |
aInput.on('ready', () => console.log('a ready')) | |
await aInput.ready() | |
aInput.append('a1') | |
aInput.append('a2') | |
const b = makeStore('b') | |
const bInput = b.get({ name: 'input', valueEncoding: 'json' }) | |
bInput.on('ready', () => console.log('b ready')) | |
await bInput.ready() | |
bInput.append('b1') | |
bInput.append('b2') | |
console.log('aInput.length', aInput.length) | |
const s1 = a.replicate(true) | |
const s2 = b.replicate(false) | |
const prefixConsoleError = (prefix) => | |
(...args) => console.error(prefix, ...args) | |
s1.on('error', prefixConsoleError('a')) | |
s2.on('error', prefixConsoleError('b')) | |
s1.pipe(s2).pipe(s1) | |
const bsAInput = b.get(aInput.key) | |
bsAInput.on('ready', () => console.log('bsAInput ready')) | |
await bsAInput.ready() | |
console.log('bsAInput.length', bsAInput.length) | |
const asBInput = a.get(bInput.key) | |
asBInput.on('ready', () => console.log('asBInput ready')) | |
await asBInput.ready() | |
console.log('asBInput.length', asBInput.length) | |
console.log('bsAInput.length', bsAInput.length) | |
/* Logs: | |
a ready | |
b ready | |
aInput.length 2 | |
bsAInput ready | |
bsAInput.length 0 | |
asBInput ready | |
asBInput.length 0 | |
bsAInput.length 2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment