import assert from 'assert'; import type * as Lagrange from './type'; type PrivateUserInvoker = (message: Lagrange.PrivateMessage) => void; type GroupUserInvoker = (message: Lagrange.GroupMessage) => void; type MessageInvoker = PrivateUserInvoker | GroupUserInvoker; interface CustomDescriptor { value?: T; configurable?: boolean; enumerable?: boolean; writable?: boolean; get?(): any; set?(v: any): void; } interface MessageInvokerStorage { invoker: T; config: Partial } class LagrangeMapper { private _privateUserStorage: Map>; private _groupStorage: Map>; constructor() { this._privateUserStorage = new Map>(); this._groupStorage = new Map>(); } get privateUserStorage() { return this._privateUserStorage; } get groupStorage() { return this._groupStorage; } public matchPrivateUser(message: Lagrange.PrivateMessage) { const user_id = message.user_id; const userStorage = this._privateUserStorage.get(user_id); if (userStorage) { userStorage.invoker(message); } } public onPrivateUser(config: Partial) { assert(config.user_id, 'onPrivateUser 中 user_id 不能为空'); const _this = this; return function(target: any, propertyKey: string, descriptor: CustomDescriptor) { const user_id = config.user_id; if (_this.privateUserStorage.has(user_id)) { console.warn(`${propertyKey} -> 用户 ${user_id} 已经被注册过了,该操作将覆盖原本的!`); } const invoker = descriptor.value; _this.privateUserStorage.set(user_id, { invoker, config }); } } public onGroupUser(config: Partial) { assert(config.user_id, 'onGroupUser 中 user_id 不能为空'); assert(config.group_id, 'onGroupUser 中 group_id 不能为空'); const _this = this; return function(target: any, propertyKey: string, descriptor: CustomDescriptor) { } } public onGroup(config: Partial) { assert(config.group_id, 'onGroup 中 group_id 不能为空'); const _this = this; return function(target: any, propertyKey: string, descriptor: CustomDescriptor) { const group_id = config.group_id; if (_this.groupStorage.has(group_id)) { console.warn(`${propertyKey} -> 群 ${group_id} 已经被注册过了,该操作将覆盖原本的!`); } const invoker = descriptor.value; _this.groupStorage.set(group_id, { invoker, config }); } } } const lagMapper = new LagrangeMapper(); export default lagMapper;