Greetings,Has anyone tried to separate ShareDB-specific logic from application logic?We're working on a ShareDB application and trying to implement the Clean Architecture. We're having trouble identifying ways to refactor such that the client side code interfaces only indirectly with ShareDB, via some sort of "service layer" API that speaks the language of the application domain and encapsulates the ShareDB interaction.We're considering only using the ShareDB client on the server, and implementing a WebSockets-based service layer to the browser and frontend code. One fear I have with this approach is that we'd be reinventing the wheel, and might as well just use the ShareDB client in the browser, but wrap it in a service layer within the browser JS.
Just curious in general, has anyone tried or experienced something similar? Thank you.
--Best regards,Curran
You received this message because you are subscribed to the Google Groups "ShareJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharejs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
OT as used by ShareDB needs to run on the local client, 1) because it maintains all client document state, 2) because OT’s main role is to minimise client-server latency by allowing optimistic updates on the client.
Because of OT the ShareDB client library does a lot of heavy lifting - so yes, if you want abstraction then you should wrap it in a service later within the browser. You can use the underlying ottypes to create a fairly simple mockable persistence layer then build further business logic on top of that.
Seems reasonable. There’s a few different approaches which could be taken with React, depending on if you have stateful components and/or use Flux.
Most of the time I’d expect a ShareDB document can be represented by an object with just a submitOp(..) function and an on(‘op’, …) event emitter; I’ve had success using that for mocking.
When handling remote ops, you have the choice of using the ShareDB-managed snapshot available on doc.data, or using the ‘op’ event handler and applying the OT operation yourself using ottypes such as json0, which is not actually that hard - this is what the bindings do.
For Flux/Redux you could use doc.data as your top-level state, but be warned that ShareDB directly mutates it. Alternatively you could turn each ‘op’ event into an Action and use ottypes to update your central state. This is what I do but I don’t use deeply nested models so I can’t offer any specific pointers there.
— John