Exposing change events to applications

The docs (Defining data types — remoteStorage.js 1.2.3 documentation) give examples of imperative access to the privateClient. Methods like “add” which can be called by an application.

What is the usual way for a module to notify applications when a change event arrives? Should privateClient be exposed in exports, so applications can write
privateClient.on(‘change’, evt => {})
Or should a subscribe method be exposed in exports, which calls privateClient.on() with the passed function?

Good question! I think there is currently no best practice, but we should definitely tackle this one for modules.

The bookmarks module does indeed just export a client:

… which is then used by e.g. Webmarks with an extra scope for the subfolder that it currently uses:

This feels dirty to be honest.

I’m wondering if modules shouldn’t automatically export an on() handler function for their category scope.

I’m currently having the module export a subscribe(callback) function. That prevents apps from calling privateClient.storeObject() directly. (Of course, apps can fork the module or not use it and write whatever they like, but at that point, they are clearly not following the rules.)

I use my own library which exposes sync events as a function that receives a single object that you can handle, see “Sync events” in Zero Data Wrap – Unified API for remoteStorage + Fission + other? for a comparison.