Advanced Usage
Sharing Workers
As of version 1.1, the SparseCollection provides the ability to customize how the worker is managed via the WrappedWorker configuration option. The WrappedWorker basically will allow only one message to be sent until a response is received, and will put any other outgoing messages into a queue. The interface is independent of the rest of Conduit's sparseData modules, which means you can extend it more easily and use your own by setting it as value for WrappedWorker when you invoke enableWorker in the Conduit config.
An included subclass of the WrappedWorker is WrappedWorker.SharedWorker. This will share the same actual worker "under the hood", so multiple sparse collections can share the same worker. The benefit of this is that you would be able to instantiate multiple SparseCollection instances without worrying as much about high memory usage (since each worker has its own JS VM instance).
You can use it like this:
Conduit.config.enableWorker({
paths: '/js/path/to/conduit/dist/',
WrappedWorker: Conduit.WrappedWorker.SharedWorker,
});
Caching Data
Related to sharing workers, version 1.1 and beyond include support for caching GET requests that are executed by the worker. f you combined the WrappedWorker.SharedWorker and the useCache option of the restGet or haul method calls, you can have multiple collections use the same raw data from restGet call, while each is still able to apply projections on top.
This makes a lot more sense in an example:
// Using the WrappedWorker.SharedWorker instance so that multiple
// SparseCollection data collections live on the same Web Worker ...
// Haul data for the first sparse collection
sparseCollectionA.haul({
url: '/api/foo',
useCache: true // will cause the response to be cached with the url as key
}).then(function () {
return sparseCollectionB.haul({
url: '/api/foo'
// useCache:true below will cause the haul method to use the existing
// cached data for the url on the worker. '/api/foo' will only be
// fetched from the server once
useCache: true
});
});