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.getSubset 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({userId: 5});
todosConnection.getListData({userId: 5, type: "critical"});
The above requests can all be joined since can-set intuitively knows that
{userId: 5, type: "critical"}
and {userId: 5}
are subsets of the complete set of todos, {}
.
For more advanced combining, a set algebra must be configured. This allows can-set to understand what certain parameters of a set mean, and how they might be combined.
The following connection supports combining ranges:
var set = require("can-set");
var combineRequests = require("can-connect/data/combine-requests/");
var dataUrl = require("can-connect/data/url/");
var todosConnection = connect([dataUrl, combineRequests], {
url: "/todos",
algebra: new set.Algebra(set.props.rangeInclusive("start","end"))
});
Now the following will also be unified to make single request:
todosConnection.getListData({start: 0, end: 49})
todosConnection.getListData({start: 0, end: 5});
todosConnection.getListData({start: 50, end: 99});