combine-requests
Combines multiple incoming lists requests into a single list request when possible.
combineRequests( baseConnection )
Implements getListData to collect the requested sets for some time. Once the configured amount of time has passed, it tries to take the union of the requested sets. It then makes requests with those unified sets. Once the unified set requests have returned, the original requests are resolved by taking [can-connect/data/combine-requests.filterMembers subsets] of the unified response data.
Parameters
- baseConnection
{Object}
:can-connect
connection object that is having thecombine-requests
behavior added on to it. Should already contain a behavior that providesgetListData
(e.g data/url). If theconnect
helper is used to build the connection, the behaviors will automatically be ordered as required.
Returns
{Object}
:
a can-connect
connection containing the method implementations provided by combine-requests
.
Use
Create a connection with the combine-requests
plugin:
var combineRequests = require("can-connect/data/combine-requests/");
var dataUrl = require("can-connect/data/url/");
var todosConnection = connect([dataUrl, combineRequests], {
url: "/todos"
});
Since the configuration above doesn't include the time option, the following will only make a single request if all requests are made during the same "thread of execution" (i.e. before the browser takes a break from executing the current JavaScript):
todosConnection.getListData({})
todosConnection.getListData({filter: {userId: 5}});
todosConnection.getListData({filter: {userId: 5, type: "critical"}});
The above requests can all be joined since [can-set] intuitively knows that
({filter: {userId: 5}}
and {filter: {userId: 5, type: "critical"}}
are subsets of the complete set of todos, {}
.
For more advanced combining, a queryLogic must be configured. This allows combine-requests
to understand
what certain parameters of a set mean, and how they might be combined.