-
Notifications
You must be signed in to change notification settings - Fork 8
Added support for decrement #28
base: main
Are you sure you want to change the base?
Conversation
Here is the modified test in
|
When running this test in
|
I can't make the I fixed the But now I have another error in my web console. |
@@ -14,7 +14,12 @@ class CounterIndex { | |||
updateIndex(oplog) { | |||
if(this._index) { | |||
const createCounter = e => Counter.from(e.payload.value) | |||
const mergeToIndex = e => this._index.merge(e) | |||
const mergeToIndex = e => { | |||
const other = new PNCounter(e.uid, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createCounter
should return a PNCounter, is there a reason to not keep it as this._index.merge(e)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand why, but when I do I end up with this error:
TypeError: Cannot convert undefined or null to object
at Function.entries (<anonymous>)
at GCounter.merge (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/node_modules/crdts/src/G-Counter.js:39:12)
at PNCounter.merge (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/node_modules/crdts/src/PN-Counter.js:25:12)
at mergeToIndex (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/src/CounterIndex.js:17:45)
at Array.forEach (<anonymous>)
at CounterIndex.updateIndex (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/src/CounterIndex.js:20:10)
at CounterStore._updateIndex (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/node_modules/orbit-db-store/src/Store.js:415:23)
at Replicator.onLoadCompleted (/Users/jerome.bernard/Documents/Personnel/Development/orbit-db-counterstore/node_modules/orbit-db-store/src/Store.js:108:22)
I remember having seen at one point some mismatch in counters
vs _counters
, but I'm not sure if it's for that specific change.
Anyway, the opens a saved counter
test fails when reverting this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you're right it was due to toJSON
method not serializing the counters correctly. This should be fixed now (here) and we can leave the line as this._index.merge(e)
const counter = new Counter(this.uid, Object.assign({}, this._index.get()._counters)) | ||
const counter = new PNCounter(this.uid, | ||
new GCounter(this.uid, this._index.get().p._counters), | ||
new GCounter(this.uid, this._index.get().n._counters)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for highlighting this! static from
in PNCounter.js should be changed to static from (json) { return new PNCounter(json.id, new GCounter(json.id, json.p.counters), new GCounter(json.id, json.n.counters)) }
If the above is changed, this._index.get().p
should return a GCounter, that way we can have this be const counter = new Counter(this.uid, this._index.get().p, this._index.get().n)
, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely a better solution as the CRDT lib may be used for something different than OrbitDB libs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed the constructor in PNCounter to expect objects instead of instances of GCounter.
This way this line can be const counter = new PNCounter(this.uid, this._index.get().p._counters, this.index.get().n._counters)
, what do you think?
|
||
dec(amount) { | ||
const counter = new PNCounter(this.uid, | ||
new GCounter(this.uid, this._index.get().p._counters), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same remark on my side as well :)
Thank you @jeje, this looks great! I've left a just a few comments Re: the browser example, I think the library is out of date and isn't creating the keystore in a browser-friendly manner. Mind if we take care of that in a separate PR? As for the warnings, I believe those are from the ordering of property declarations in Entry but this should be fixed in the latest update. Thanks for adding tests! Care to add those as a PR too? |
Sorry for coming back to you that late and thanks for your review!
No problem. I tried to enrich the example as well but failed.
Great. Should I do something on my side or wait for this update?
You mean as a PR to the orbit-db lib, I guess? |
As a side note, it would be easier to be able to test this module without having to use a forked version of the Otherwise some unit tests may help a bit (with mocks) but won't be enough, I guess. |
@shamb0t any feedback on my last questions? |
Apologies for my very late response @jeje! Thank you for revisiting this! I left a couple more comments about some changes to PNCounter that I think make this easier now
The warnings should stop with that update so no need for anything from your end.
Yes, I mean't as a PR to orbit-db :)
Do you need to duplicate the IPFS set up code? I'm currently running
This way your local orbit-db repo will point to your local counterstore repo and you won't need to duplicate code (but do need a local orbit-db repo), is that helpful? Lmk if I misunderstood your question |
Potential pull-request for #10.