ws-process
v0.3.46
Published
Process Management for Web Socket Client
Downloads
180
Readme
ws-process
Process management for Web Socket Client
Each process will be running parallelly.
Requests in a process will be running sequently.
import { newProcess } from "ws-process";
// Basic usage skeleton
const promise = newProcess()
.newRequest()
.newRequest()
.start();
promise
.then(result=>{})
.catch((err)=>{})
/*
.start() returns a Promise instance.
When the all requests belong to process are finished, it resolve with following:
*/
result = {
data: {
isLast: true,
processID,
jsonResponse,
cbData
},
dispatch,
getState,
processFinished: true,
processID
}
Sample
// To query a production order
dispatch(
getOrder(
{ orderID },
{
afterSucceed: () => {
history.push(`/prodconfirm/order/${orderID}`);
}
}
)
);
// getOrder - an action creator for the thunk which is the middleware of redux
/*
newProcess()
arguments:
Process ID: String without space
Options: {
appIsBusy = true(default)
}
Options are provided to "beforeEachProcess", "afterEachProcess" which are interceptors.
*/
const getOrder = (params, callback) => {
return (dispatch, getState) => {
const promise = newProcess("productionOrder")
.newRequest({
requestHandler: reqOrder,
params,
responseHandler: resOrder,
callback
})
.start();
};
};
/*
params: Provided to 'request' function
*/
const reqOrder = ({ params }) => {
return (dispatch, getState) => {
return {
version: "20190625",
action: "requestodata",
subAction: "getProductionOrder",
description: "Fetch production order data from ByDesign",
httpMethod: "GET",
headers: {}, // headers could be ignored, authentication will be added in ws-process package,
// Attributes in 'headers' will overwrite the attributes defined in ws-process package
url: dispatch(
convURL.custom("productionorder/ProductionOrderCollection", {
$expand: ["ProductionLot/ProductionLotMaterialOutput"].join(","),
$filter: [escape("ID eq '" + params.orderID + "'")]
})
)
/* convURL has following methods
.custom() - Returns url with process.env.REACT_APP_BYD_CUSTOM_ODATA_PATH,
.report() or .reportCC() - Returns url with process.env.REACT_APP_BYD_REPORT_ODATA_PATH,
.reportANA() - Returns url with process.env.REACT_APP_BYD_REPORT_ODATA_PATH_ANA,
.datasource() - Returns url with process.env.REACT_APP_BYD_DATASOURCE_ODATA_PATH;
*/
/* Default query parameters:
sap-language: currentUser.lang || "en"
$format: "json",
$inlinecount: "allpages"
*/
};
};
};
// resOrder - An action creator for thunk the middleware of redux
/*
arguments:
@isLast: SAP Odata would split response if the data size exceeds the limit(?), so isLast is used for determine if the response is last.
@jsonResponse: Response data as json format
return:
array - [boolean,data for callback]
If no data for callback function need to be transferred, then it could return only boolean value, not array.
If no callback function is provided, then 'return' could be ignored.
*/
function resOrder({ isLast, jsonResponse }) {
return (dispatch, getState) => {
const body = jsonResponse.body;
const results = body.d.results;
const count = parseInt(body.d.__count);
if (count > 0) {
dispatch(getProductionOrder(results));
return [true];
} else {
dispatch(addError(["None of order is fetched"]));
return [false];
}
};
}
callback functions: optional
afterSucceed(cbData): function - optional
If response handler returns true or [true, <callback data>], then invoke afterSucceed and always.
afterFailed(cbData): function - optional
If response handler returns false or [true, <callback data>], then invoke afterFailed and always.
always(cbData): function - optional
Whatever returned by response handler, if always callback function is provided, it will run anyway for the specific request.
callback functions will be invoked by the result of response handler.
Call REST API
url: The URL of REST API, if no domain is included, the API Hub domain will be used.
method: The HTTP Method
description: The text represented on Loading Dialog
params: The query string parameters
data: The body of request
newProcess("get master data")
.newRequest({
requestHandler: () => () => {
return {
url: "/master",
method: "GET",
description: "get vendor list",
params: {
type: "vendor"
}
};
},
responseHandler: ({ isFirst, isLast, jsonResponse }) => {
return dispatch => {
const body = jsonResponse.body;
return [false];
};
},
callback: {
afterSucceed: () => {},
afterFailed: () => {},
always: () => {}
}
})
.start();