You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
import type { OperationCmd } from "./globalCmd/globalCmd";
|
|
|
|
let _lastTimestamp = 0;
|
|
export function generateTxnNo() {
|
|
const txnNo = Date.now();
|
|
// 确保前后两条指令的txn 不一样
|
|
if (txnNo !== _lastTimestamp) {
|
|
if (txnNo < _lastTimestamp) {
|
|
_lastTimestamp++;
|
|
} else {
|
|
_lastTimestamp = txnNo;
|
|
}
|
|
} else {
|
|
_lastTimestamp = txnNo + 1;
|
|
}
|
|
return _lastTimestamp;
|
|
}
|
|
|
|
type DebugCmdRecord = {
|
|
category: "debug";
|
|
command: OperationCmd;
|
|
params: Record<string, any>;
|
|
};
|
|
|
|
type TaskCmdRecord = {
|
|
category: "task";
|
|
command: OperationCmd;
|
|
params: Record<string, any>;
|
|
}
|
|
|
|
type TaskCmdInjectFluidsRecord = {
|
|
category: "task";
|
|
command: OperationCmd,
|
|
injectFluids: [];
|
|
}
|
|
|
|
type TxnRecord = DebugCmdRecord | TaskCmdRecord | TaskCmdInjectFluidsRecord
|
|
|
|
const txnCmdMap: Record<string, TxnRecord> = {};
|
|
|
|
export function addTxnRecord(val: TxnRecord) {
|
|
const txn = generateTxnNo().toString();
|
|
txnCmdMap[txn] = val;
|
|
return txn;
|
|
}
|
|
|
|
export function injectFluidsRecord(val: TaskCmdInjectFluidsRecord){
|
|
const txn = generateTxnNo().toString();
|
|
txnCmdMap[txn] = val;
|
|
return txn;
|
|
}
|
|
|
|
export function getTxnRecord(txn: string, category: TxnRecord["category"]) {
|
|
const record = txnCmdMap[txn];
|
|
// 只有属于指定category时,才返回,且返回后删除记录,节约内存
|
|
if (record && record.category === category) {
|
|
delete txnCmdMap[txn];
|
|
return record;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function peekTxnRecord(txn: string, category: TxnRecord["category"]) {
|
|
const record = txnCmdMap[txn];
|
|
// 只有属于指定category时,才返回,且返回后删除记录,节约内存
|
|
if (record && record.category === category) {
|
|
return record;
|
|
}
|
|
return undefined;
|
|
}
|