@sepveneto/mpd-core
Version:
## 安装 ```cmd pnpm i @sepveneto/mpd-core npm i @sepveneto/mpd-core yarn add @sepveneto/mpd-core ```
102 lines (100 loc) • 3.04 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result) __defProp(target, key, result);
return result;
};
// src/upgrade/fsm.ts
var fsm_exports = {};
__export(fsm_exports, {
VersionMachine: () => VersionMachine
});
module.exports = __toCommonJS(fsm_exports);
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(`[@sepveneto/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(`[@sepveneto/mpd-core] version ${version} is invalid.`);
}
return [Number(major), Number(patch)];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
VersionMachine
});