@sepveneto/mpd-core
Version:
## 安装 ```cmd pnpm i @sepveneto/mpd-core npm i @sepveneto/mpd-core yarn add @sepveneto/mpd-core ```
74 lines (71 loc) • 1.69 kB
JavaScript
import {
__decorateClass
} from "./chunk-SW7HDZD2.js";
// src/upgrade/fsm.ts
var StateV1 = class {
constructor(context) {
this.VERSION = "1.0";
this.context = context;
this.origin = this.context.data;
}
updateVersion() {
this.upgrade();
}
upgrade() {
this.origin.version = "1.0";
}
};
__decorateClass([
shouldUpdate
], StateV1.prototype, "upgrade", 1);
var VersionMachine = class {
constructor(data) {
this.data = JSON.parse(JSON.stringify(data));
this.current = new StateV1(this);
}
setVersion(state) {
this.current = state;
}
getVersion() {
return this.data.version;
}
upgrade() {
this.current.updateVersion();
return this.current.origin;
}
};
function shouldUpdate(target, propertyKey, descriptor) {
const fn = descriptor.value;
descriptor.value = function(...args) {
const current = extractVersion(this.origin.version);
const target2 = extractVersion(this.VERSION);
if (isSmaller(current, target2)) {
return fn.apply(this, args);
} else {
return () => ({});
}
};
}
function isSmaller(a, b) {
return a.reduce((flag, curr, index) => {
flag = curr < b[index];
return flag;
}, false);
}
function extractVersion(version) {
var _a;
if (!version) {
return [0];
}
if (typeof version === "number") {
throw new Error(`[/mpd-core] version ${version} must be string.`);
}
const [, major, patch] = (_a = version.match(/^([0-9]+)\.([0-9]+)$/)) != null ? _a : [];
if (!major || !patch) {
throw new Error(`[/mpd-core] version ${version} is invalid.`);
}
return [Number(major), Number(patch)];
}
export {
VersionMachine
};