@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
99 lines (97 loc) • 3.43 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);
// src/modules/Guests/index.ts
var Guests_exports = {};
__export(Guests_exports, {
GuestListModule: () => GuestListModule
});
module.exports = __toCommonJS(Guests_exports);
var import_BaseModule = require("../BaseModule");
var import_types = require("./types");
var GuestListModule = class extends import_BaseModule.BaseModule {
constructor(name, version) {
super(name, version);
this.defaultName = "guestList";
this.defaultVersion = "1.0.0";
this.state = {
list: []
};
}
async initialize(core, options) {
this.core = core;
if (options == null ? void 0 : options.initialState) {
this.state = { ...this.state, ...options.initialState };
}
}
async addGuest(guest) {
this.state.list.push(guest);
await this.core.effects.emit(`${this.name}:onGuestAdd`, guest);
await this.core.effects.emit(`${this.name}:onGuestChange`, this.state.list);
}
async updateGuest(id, updates) {
const index = this.state.list.findIndex((g) => g.id === id);
if (index === -1)
return;
this.state.list[index] = {
...this.state.list[index],
...updates
};
await this.core.effects.emit(
import_types.GuestHooks.OnGuestUpdate,
this.state.list[index]
);
await this.core.effects.emit(`${this.name}:onGuestChange`, this.state.list);
}
async removeGuest(id) {
const index = this.state.list.findIndex((g) => g.id === id);
if (index === -1)
return;
const [removed] = this.state.list.splice(index, 1);
await this.core.effects.emit(`${this.name}:onGuestRemove`, removed);
await this.core.effects.emit(`${this.name}:onGuestChange`, this.state.list);
}
async getGuests() {
return this.state.list;
}
getGuestCount() {
return this.state.list.length;
}
async addItemToGuest(guestId, item) {
const guest = this.state.list.find((g) => g.id === guestId);
if (!guest)
return;
guest.items.push(item);
await this.core.effects.emit(`${this.name}:onGuestUpdate`, guest);
await this.core.effects.emit(`${this.name}:onGuestChange`, this.state.list);
}
async removeItemFromGuest(guestId, itemId) {
const guest = this.state.list.find((g) => g.id === guestId);
if (!guest)
return;
const index = guest.items.findIndex((i) => i.id === itemId);
if (index === -1)
return;
guest.items.splice(index, 1);
await this.core.effects.emit(`${this.name}:onGuestUpdate`, guest);
await this.core.effects.emit(`${this.name}:onGuestChange`, this.state.list);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
GuestListModule
});