@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
181 lines (129 loc) • 3.9 kB
Markdown
PisellOS 是一个灵活的、可扩展的前端 SDK 框架,专为构建模块化应用设计。它支持插件系统,允许在不同运行环境中无缝集成功能。
- **模块化架构**:支持各种功能模块(如购物车、结账流程等)的插入和管理
- **插件系统**:通过插件提供环境抽象,解决跨环境兼容性问题
- **事件驱动**:基于事件总线的模块间通信机制
- **类型安全**:使用 TypeScript 构建,提供完整的类型定义
- **轻量级**:核心框架轻量,按需加载模块
```bash
npm install pisell-os
```
```typescript
import PisellOSCore, { plugins, modules } from "pisell-os";
// 创建 PisellOS 实例
const pisellOS = new PisellOSCore({
debug: true, // 开启调试日志
plugins: [{ plugin: plugins.window }, { plugin: plugins.request }],
modules: [
{
module: modules.cart,
options: {
plugins: ["window", "request"], // 依赖的插件
},
},
],
});
// 获取购物车模块并使用
const cartModule = pisellOS.getModuleExports("cart");
await cartModule.addItem({
id: "product-001",
name: "商品1",
price: 99.9,
quantity: 1,
});
// 使用插件
const windowPlugin = pisellOS.getPlugin("window");
windowPlugin.localStorage.setItem("key", "value");
```
PisellOS 的核心架构包括四个主要部分:
1. **核心框架**:负责模块和插件的注册、管理和生命周期控制
2. **插件系统**:为不同环境提供统一接口,如 window、request 等
3. **模块系统**:提供业务功能实现,可依赖插件和其他模块
4. **解决方案**:解决方案是 pisell_os 中对业务流程层的抽象,用于将多个独立的模块(Module)编排成一个完整的业务解决方案。
它本质上是一个特殊的“业务 orchestrator”,负责协调模块调用、控制数据流动和处理统一状态管理。
```typescript
// 创建实例
const pisellOS = new PisellOSCore(options);
// 注册插件
pisellOS.registerPlugin(plugin, options);
// 注册模块
pisellOS.registerModule(module, options);
// 获取插件
const plugin = pisellOS.getPlugin<PluginType>(name);
// 获取模块
const module = pisellOS.getModule<ModuleType>(name);
// 获取模块导出的 API
const api = pisellOS.getModuleExports<ApiType>(name);
// 事件机制
pisellOS.on(event, handler);
pisellOS.emit(event, data);
```
PisellOS 提供了几个内置插件以简化开发:
- **window**:提供 window 对象模拟,包括 localStorage、setTimeout 等
- **request**:提供网络请求功能,支持各种 HTTP 方法和拦截器
- **cart**:购物车模块,提供商品管理、数量更新等功能
```typescript
import { Plugin } from "pisell-os";
class MyPlugin implements Plugin {
name = "my-plugin";
version = "1.0.0";
initialize() {
console.log("My plugin initialized");
}
destroy() {
console.log("My plugin destroyed");
}
// 自定义方法
myMethod() {
return "Hello from my plugin";
}
}
export default new MyPlugin();
```
```typescript
import { Module, PisellCore } from "pisell-os";
class MyModule implements Module {
name = "my-module";
version = "1.0.0";
private core: PisellCore;
// 导出 API
exports = {
doSomething: () => this.doSomething(),
};
initialize(core: PisellCore) {
this.core = core;
console.log("My module initialized");
}
destroy() {
console.log("My module destroyed");
}
private doSomething() {
return "Hello from my module";
}
}
export default new MyModule();
```
```bash
npm install
npm run build
npm run example
npm run dev
```
MIT License