jnilla / joomla-request-batcher
批量发送和处理请求
v0.0.3
2021-08-22 09:40 UTC
README
批量发送和处理请求。
批量发送多个请求以减少单独发送多个请求的开销。
安装(服务器端)
使用Composer安装
$ composer require jnilla/joomla-request-batcher
使用Composer自动加载器加载库
require('vendor/autoload.php');
安装(客户端)
此库依赖于jQuery。在jQuery之后、您的代码之前包含文件request-batcher.js
。示例
<script src='jquery.js'></script>
<script src='request-batcher.js'></script>
<script src='your-code.js'></script>
基本用法(客户端)
请求批处理器使用命名空间
Jnilla.Joomla.RequestBatcher
基本配置
// If true prints debug data to the console
Jnilla.Joomla.RequestBatcher.setDebug(true);
// Sends a batch every 5 seconds
Jnilla.Joomla.RequestBatcher.setBatchInterval(5);
// Set the server URL where you want to sent the data to. If not set the default value is the current page URL.
Jnilla.Joomla.RequestBatcher.setServerUrl('?index.php?option=com_example&task=ajax.processRequest');
将一个请求添加到实际批次中
Jnilla.Joomla.RequestBatcher.addRequest(
// Request data (Must be string type)
'some data',
// Response callback
function(responseData){console.log(responseData);}
);
addRequest
方法具有防止重复请求的机制。
如果实际批次中没有请求,批处理器不执行任何操作。
基本用法(服务器端)
声明
use Jnilla\Joomla\RequestBatcher as RequestBatcher;
process
方法处理批次,为实际批次中的每个请求执行回调。
// Process the batch
RequestBatcher::process(function($requestData){ // Callback for each request
// Some code here
});
示例
在客户端,我们得到一个脚本,每2秒显示一个产品(A)的值,每10秒显示另一个产品(B)的值。
Jnilla.Joomla.RequestBatcher.setBatchInterval(1); Send batches every 1 seconds
Jnilla.Joomla.RequestBatcher.setServerUrl('?index.php?option=com_example&task=ajax.processRequest'); // Server URL
// Add the 'Product A' request every 2 seconds.
setInterval(function(){
Jnilla.Joomla.RequestBatcher.addRequest(
// Request data. Only send string. That is why we used stringify
JSON.stringify({'task': 'getProductA'}),
// Response callback
function(responseData){
console.log('Product Name:'+responseData.name+', Value:'+responseData.value');
// Output: Product Name: Product A, Value: $100
}
);
}, 2000);
// Add the 'Product B' request every 10 seconds.
setInterval(function(){
Jnilla.Joomla.RequestBatcher.addRequest(
// Request data.
JSON.stringify({'task': 'getProductB'}),
// Response callback
function(responseData){
console.log('Product Name:'+responseData.name+', Value:'+responseData.value');
// Output: Product Name: Product B, Value: $350
}
);
}, 10000);
批次每1秒发送一次,但请求的添加间隔不同。您可以调整批次间隔和添加频率以满足您的需求。对于这个示例,有时什么也不发送,因为批次为空,有时批次中有1个请求,有时批次中有2个请求。
在服务器端,我们可以实现一个类似于以下的结构,类似于基于任务的控制器。
// Process the batch
RequestBatcher::process(function($requestData){ // Callback for each request
$requestData = json_decode($requestData); Parse the JSON string
switch ($requestData->task){
case 'getProductA':
// Some code here
$data = ['name'=> 'Product A' , 'value' => '$100'];
// The response must be string type. That is why we used json_encode()
$data = json_encode($data);
return $data;
case 'getProductB':
// Some code here
$data = ['name'=> 'Product B' , 'value' => '$350'];
$data = json_encode($data);
return $data;
}
});
批次处理完毕后,将自动发送服务器响应。
简单到极致。
许可
本项目采用MIT许可。