Skip to content

Instantly share code, notes, and snippets.

@josephg
Last active Aug 3, 2021
Embed
What would you like to do?
automerge output flat
CACHE = {
_root: {
text: {
elems: [
{
elemId: '2@3cb54fecd444446cbe579bd128017c42',
pred: [ '2@3cb54fecd444446cbe579bd128017c42' ],
value: 'a'
},
{
elemId: '3@3cb54fecd444446cbe579bd128017c42',
pred: [ '3@3cb54fecd444446cbe579bd128017c42' ],
value: 'b'
},
{
elemId: '4@3cb54fecd444446cbe579bd128017c42',
pred: [ '4@3cb54fecd444446cbe579bd128017c42' ],
value: 'c'
}
]
}
},
'1@3cb54fecd444446cbe579bd128017c42': {
elems: [
{
elemId: '2@3cb54fecd444446cbe579bd128017c42',
pred: [ '2@3cb54fecd444446cbe579bd128017c42' ],
value: 'a'
},
{
elemId: '3@3cb54fecd444446cbe579bd128017c42',
pred: [ '3@3cb54fecd444446cbe579bd128017c42' ],
value: 'b'
},
{
elemId: '4@3cb54fecd444446cbe579bd128017c42',
pred: [ '4@3cb54fecd444446cbe579bd128017c42' ],
value: 'c'
}
]
}
}
CONFLICTS = {
text: {
'1@3cb54fecd444446cbe579bd128017c42': {
elems: [
{
elemId: '2@3cb54fecd444446cbe579bd128017c42',
pred: [ '2@3cb54fecd444446cbe579bd128017c42' ],
value: 'a'
},
{
elemId: '3@3cb54fecd444446cbe579bd128017c42',
pred: [ '3@3cb54fecd444446cbe579bd128017c42' ],
value: 'b'
},
{
elemId: '4@3cb54fecd444446cbe579bd128017c42',
pred: [ '4@3cb54fecd444446cbe579bd128017c42' ],
value: 'c'
}
]
}
}
}
STATE = {
seq: 4,
maxOp: 4,
requests: [],
clock: { '3cb54fecd444446cbe579bd128017c42': 4 },
deps: [],
backendState: {
state: {
opSet: {
states: {
'3cb54fecd444446cbe579bd128017c42': [
'c76f124cc443e45a653e763f5356776f1aae8e540520d645f597cf91660bb129',
'4d493936bf6224f93aabd0d2266f57af1b9607acdb032bc78dbfa16398985818',
'4191aa7393bc40d1c2155f0002b1510129e79b478be1f833235b0e6d94871efb',
'a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e'
]
},
history: [
'c76f124cc443e45a653e763f5356776f1aae8e540520d645f597cf91660bb129',
'4d493936bf6224f93aabd0d2266f57af1b9607acdb032bc78dbfa16398985818',
'4191aa7393bc40d1c2155f0002b1510129e79b478be1f833235b0e6d94871efb',
'a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e'
],
byObject: {
_root: {
_keys: {
text: [
{
obj: '_root',
key: 'text',
action: 'makeText',
insert: false,
pred: [],
opId: '1@3cb54fecd444446cbe579bd128017c42'
}
]
}
},
'1@3cb54fecd444446cbe579bd128017c42': {
_init: {
obj: '_root',
key: 'text',
action: 'makeText',
insert: false,
pred: [],
opId: '1@3cb54fecd444446cbe579bd128017c42'
},
_inbound: [
{
obj: '_root',
key: 'text',
action: 'makeText',
insert: false,
pred: [],
opId: '1@3cb54fecd444446cbe579bd128017c42'
}
],
_keys: {
'2@3cb54fecd444446cbe579bd128017c42': [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '_head',
action: 'set',
insert: true,
value: 'a',
pred: [],
opId: '2@3cb54fecd444446cbe579bd128017c42'
}
],
'3@3cb54fecd444446cbe579bd128017c42': [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '2@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'b',
pred: [],
opId: '3@3cb54fecd444446cbe579bd128017c42'
}
],
'4@3cb54fecd444446cbe579bd128017c42': [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '3@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'c',
pred: [],
opId: '4@3cb54fecd444446cbe579bd128017c42'
}
]
},
_elemIds: {
length: 3,
_nodes: {
null: {
key: null,
value: null,
level: 2,
prevKey: [],
nextKey: [
'2@3cb54fecd444446cbe579bd128017c42',
'2@3cb54fecd444446cbe579bd128017c42'
],
prevCount: [],
nextCount: [ 1, 1 ]
},
'2@3cb54fecd444446cbe579bd128017c42': {
key: '2@3cb54fecd444446cbe579bd128017c42',
value: 'a',
level: 2,
prevKey: [ null, null ],
nextKey: [
'3@3cb54fecd444446cbe579bd128017c42',
'4@3cb54fecd444446cbe579bd128017c42'
],
prevCount: [ 1, 1 ],
nextCount: [ 1, 2 ]
},
'3@3cb54fecd444446cbe579bd128017c42': {
key: '3@3cb54fecd444446cbe579bd128017c42',
value: 'b',
level: 1,
prevKey: [ '2@3cb54fecd444446cbe579bd128017c42' ],
nextKey: [ '4@3cb54fecd444446cbe579bd128017c42' ],
prevCount: [ 1 ],
nextCount: [ 1 ]
},
'4@3cb54fecd444446cbe579bd128017c42': {
key: '4@3cb54fecd444446cbe579bd128017c42',
value: 'c',
level: 2,
prevKey: [
'3@3cb54fecd444446cbe579bd128017c42',
'2@3cb54fecd444446cbe579bd128017c42'
],
nextKey: [ null, null ],
prevCount: [ 1, 2 ],
nextCount: [ 1, 1 ]
}
},
_randomSource: { next: [Function: next] }
},
_following: {
_head: [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '_head',
action: 'set',
insert: true,
value: 'a',
pred: [],
opId: '2@3cb54fecd444446cbe579bd128017c42'
}
],
'2@3cb54fecd444446cbe579bd128017c42': [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '2@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'b',
pred: [],
opId: '3@3cb54fecd444446cbe579bd128017c42'
}
],
'3@3cb54fecd444446cbe579bd128017c42': [
{
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '3@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'c',
pred: [],
opId: '4@3cb54fecd444446cbe579bd128017c42'
}
]
},
_insertion: {
'2@3cb54fecd444446cbe579bd128017c42': {
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '_head',
action: 'set',
insert: true,
value: 'a',
pred: [],
opId: '2@3cb54fecd444446cbe579bd128017c42'
},
'3@3cb54fecd444446cbe579bd128017c42': {
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '2@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'b',
pred: [],
opId: '3@3cb54fecd444446cbe579bd128017c42'
},
'4@3cb54fecd444446cbe579bd128017c42': {
obj: '1@3cb54fecd444446cbe579bd128017c42',
elemId: '3@3cb54fecd444446cbe579bd128017c42',
action: 'set',
insert: true,
value: 'c',
pred: [],
opId: '4@3cb54fecd444446cbe579bd128017c42'
}
}
}
},
hashes: {
c76f124cc443e45a653e763f5356776f1aae8e540520d645f597cf91660bb129: {
actor: '3cb54fecd444446cbe579bd128017c42',
seq: 1,
startOp: 1,
change: Uint8Array(75) [
133, 111, 74, 131, 199, 111, 18, 76, 1, 65, 0, 16,
60, 181, 79, 236, 212, 68, 68, 108, 190, 87, 155, 209,
40, 1, 124, 66, 1, 1, 230, 199, 157, 133, 6, 14,
73, 110, 105, 116, 105, 97, 108, 105, 122, 97, 116, 105,
111, 110, 0, 5, 21, 6, 52, 1, 66, 2, 86, 2,
112, 2, 127, 4, 116, 101, 120, 116, 1, 127, 4, 127,
0, 127, 0
],
maxOpId: 1,
depsPast: [],
depsFuture: [
'4d493936bf6224f93aabd0d2266f57af1b9607acdb032bc78dbfa16398985818'
]
},
'4d493936bf6224f93aabd0d2266f57af1b9607acdb032bc78dbfa16398985818': {
actor: '3cb54fecd444446cbe579bd128017c42',
seq: 2,
startOp: 2,
change: Uint8Array(101) [
133, 111, 74, 131, 77, 73, 57, 54, 1, 91, 1, 199,
111, 18, 76, 196, 67, 228, 90, 101, 62, 118, 63, 83,
86, 119, 111, 26, 174, 142, 84, 5, 32, 214, 69, 245,
151, 207, 145, 102, 11, 177, 41, 16, 60, 181, 79, 236,
212, 68, 68, 108, 190, 87, 155, 209, 40, 1, 124, 66,
2, 2, 230, 199, 157, 133, 6, 0, 0, 8, 1, 2,
2, 2, 19, 2, 52, 2, 66, 2, 86, 2, 87, 1,
112, 2, 127, 0, 127, 1, 127, 0, 0, 1, 127, 1,
127, 22, 97, 127,
... 1 more item
],
maxOpId: 2,
depsPast: [
'c76f124cc443e45a653e763f5356776f1aae8e540520d645f597cf91660bb129'
],
depsFuture: [
'4191aa7393bc40d1c2155f0002b1510129e79b478be1f833235b0e6d94871efb'
]
},
'4191aa7393bc40d1c2155f0002b1510129e79b478be1f833235b0e6d94871efb': {
actor: '3cb54fecd444446cbe579bd128017c42',
seq: 3,
startOp: 3,
change: Uint8Array(105) [
133, 111, 74, 131, 65, 145, 170, 115, 1, 95, 1, 77,
73, 57, 54, 191, 98, 36, 249, 58, 171, 208, 210, 38,
111, 87, 175, 27, 150, 7, 172, 219, 3, 43, 199, 141,
191, 161, 99, 152, 152, 88, 24, 16, 60, 181, 79, 236,
212, 68, 68, 108, 190, 87, 155, 209, 40, 1, 124, 66,
3, 3, 230, 199, 157, 133, 6, 0, 0, 9, 1, 2,
2, 2, 17, 2, 19, 2, 52, 2, 66, 2, 86, 2,
87, 1, 112, 2, 127, 0, 127, 1, 127, 0, 127, 2,
0, 1, 127, 1,
... 5 more items
],
maxOpId: 3,
depsPast: [
'4d493936bf6224f93aabd0d2266f57af1b9607acdb032bc78dbfa16398985818'
],
depsFuture: [
'a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e'
]
},
a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e: {
actor: '3cb54fecd444446cbe579bd128017c42',
seq: 4,
startOp: 4,
change: Uint8Array(105) [
133, 111, 74, 131, 167, 103, 37, 61, 1, 95, 1, 65,
145, 170, 115, 147, 188, 64, 209, 194, 21, 95, 0, 2,
177, 81, 1, 41, 231, 155, 71, 139, 225, 248, 51, 35,
91, 14, 109, 148, 135, 30, 251, 16, 60, 181, 79, 236,
212, 68, 68, 108, 190, 87, 155, 209, 40, 1, 124, 66,
4, 4, 230, 199, 157, 133, 6, 0, 0, 9, 1, 2,
2, 2, 17, 2, 19, 2, 52, 2, 66, 2, 86, 2,
87, 1, 112, 2, 127, 0, 127, 1, 127, 0, 127, 3,
0, 1, 127, 1,
... 5 more items
],
maxOpId: 4,
depsPast: [
'4191aa7393bc40d1c2155f0002b1510129e79b478be1f833235b0e6d94871efb'
],
depsFuture: []
}
},
deps: [
'a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e'
],
maxOp: 4,
queue: []
},
objectIds: {}
},
heads: [
'a767253de1aa18b15d0d40c124f97a72f9a4cdb397e1e8719b3b88381624139e'
]
},
lastLocalChange: Uint8Array(105) [
133, 111, 74, 131, 167, 103, 37, 61, 1, 95, 1, 65,
145, 170, 115, 147, 188, 64, 209, 194, 21, 95, 0, 2,
177, 81, 1, 41, 231, 155, 71, 139, 225, 248, 51, 35,
91, 14, 109, 148, 135, 30, 251, 16, 60, 181, 79, 236,
212, 68, 68, 108, 190, 87, 155, 209, 40, 1, 124, 66,
4, 4, 230, 199, 157, 133, 6, 0, 0, 9, 1, 2,
2, 2, 17, 2, 19, 2, 52, 2, 66, 2, 86, 2,
87, 1, 112, 2, 127, 0, 127, 1, 127, 0, 127, 3,
0, 1, 127, 1,
... 5 more items
]
}
const automerge = require('automerge')
const constants = require('automerge/frontend/constants')
const consoleLib = require('console')
globalThis.console = new consoleLib.Console({
stdout: process.stdout, stderr: process.stderr,
inspectOptions: {depth: null}
})
let state = automerge.from({text: new automerge.Text("")})
state = automerge.change(state, doc => {
doc.text.insertAt(0, 'a')
})
state = automerge.change(state, doc => {
doc.text.insertAt(1, 'b')
})
state = automerge.change(state, doc => {
doc.text.insertAt(2, 'c')
})
const flatten = obj => {
if (obj == null || typeof obj !== 'object') return obj
else if (obj.toJS) return flatten(obj.toJS())
//else if (obj instanceof Uint8Array) return `[Uint8Array (${obj.byteLength})]`
else if (obj instanceof Uint8Array) return obj
else if (Array.isArray(obj)) return obj.map(flatten)
else return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, flatten(v)]))
}
// console.log(flatten(state[constants.STATE]))
console.log('CACHE =', flatten(state[constants.CACHE]))
console.log('CONFLICTS =', flatten(state[constants.CONFLICTS]))
console.log('STATE =', flatten(state[constants.STATE]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment