braid-org / braidjs

Monorepo for Braid Projects in Javascript
https://braid.org
262 stars 15 forks source link

braidjs bug? #29

Closed iacore closed 4 weeks ago

iacore commented 1 month ago

The output doesn't match what the readme says.

> var sequence = require('./antimatter').sequence_crdt
undefined
> var root_node = sequence.create_node('alice1', 'hello')
undefined
> console.log(sequence.generate_braid(root_node, 'alice1', x => false)) // outputs [0, 0, "hello"]
[]
undefined
dglittle commented 4 weeks ago

Thanks! This is definitely an issue. Part of the issue is with the readme. The readme claims that sequence.generate_braid will generate [0, 0, 'hello']. There are two problems with this. First, it should generate an array within an array: [[0, 0, 'hello']]. Second, as it stands, it returns []; this can be fixed with this new code:

var sequence = require('./antimatter').sequence_crdt

// modified line:
var root_node = sequence.create_node('root', '')

// new line:
sequence.add_version(root_node, 'alice1', [[0, 0, 'hello']], null, x => true)

console.log(sequence.generate_braid(root_node, 'alice1', x => false))

This code adds a line with add_version. This creates a tree with two nodes: "root" and "alice1", whereas the original readme code creates a tree with just one node "alice1".

This issue is that generate_braid treats the root node in a special way, and in particular, never includes the root node text in the returned splices.

I say "part of the issue" is with the readme because another part may be with this decision to treat the root node in a special way.. I'll need to ponder that a bit. If it makes sense to not treat the root node in a special way, then the readme can stay the same, except for the array-within-an-array [[...]] bit which I have just fixed.

dglittle commented 4 weeks ago

So, I've done a couple things to hopefully satisfy this issue for the moment.

  1. I updated the readme to point to https://braid.org/antimatter, since it is more up-to-date, being generated dynamically based on the source code itself.
  2. I updated the sequence.generate_braid example to work (even tested it): now has add_version line, in addition to returning an array-of-arrays.