@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
294 lines (277 loc) • 9.8 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* 监听模块
*
*/
var Watch = /*#__PURE__*/function () {
function Watch() {
_classCallCheck(this, Watch);
// 监听列表
_defineProperty(this, "events", new Map());
// 启用列表 - 使用数组保持顺序
_defineProperty(this, "enableListMap", new Map());
}
_createClass(Watch, [{
key: "subscribe",
value:
/**
* 订阅事件
* @param event 事件名称
* @param callback 回调函数
* @returns 订阅对象,可用于取消订阅
*/
function subscribe(watchKey, event) {
var _this = this;
// 如果监听列表不存在,则创建
if (!this.events.has(watchKey)) {
this.events.set(watchKey, new Map());
}
// 获取监听列表
var callbacks = this.events.get(watchKey);
// 设置事件
callbacks.set(event.key, event.callback);
// 启用事件
this.enableEvent(watchKey, event.key);
return {
// 取消订阅
unsubscribe: function unsubscribe() {
// 取消订阅事件
_this.unsubscribe(watchKey, event);
}
};
}
/**
* 启用事件
* @param watchKey 监听的key
* @param event 事件名称
*/
}, {
key: "enableEvent",
value: function enableEvent(watchKey, eventKey) {
// 获取启用列表
var enableList = this.enableListMap.get(watchKey);
// 如果启用列表存在,则添加事件
if (enableList) {
if (enableList.includes(eventKey)) {
return;
}
enableList.push(eventKey);
} else {
// 如果启用列表不存在,则创建启用列表
this.enableListMap.set(watchKey, [eventKey]);
}
}
/**
* 禁用某个事件
* @param watchKey 监听的key
* @param eventKey 事件名称
*/
}, {
key: "disableEvent",
value: function disableEvent(watchKey, eventKey) {
// 获取启用列表
var enableList = this.enableListMap.get(watchKey);
if (enableList) {
// 找到当前事件的索引
var index = enableList.indexOf(eventKey);
if (index !== -1) {
// 删除当前事件以及之后的所有事件
enableList.splice(index);
}
}
}
/**
* 冻结某个监听下的所有事件
* @param watchKey 监听的key
*/
}, {
key: "disabledAllEventByWatchKey",
value: function disabledAllEventByWatchKey(watchKey) {
// 设置启用列表为空
this.enableListMap.set(watchKey, []);
}
/**
* 启用某个监听下的所有事件
* @param watchKey 监听的key
*/
}, {
key: "enableAllEventByWatchKey",
value: function enableAllEventByWatchKey(watchKey) {
var _this$events$get;
// 遍历当前监听下的所有事件, 并启用
this.enableListMap.set(watchKey, Array.from(((_this$events$get = this.events.get(watchKey)) === null || _this$events$get === void 0 ? void 0 : _this$events$get.keys()) || []));
}
/**
* 取消订阅事件
* @param event 事件名称
* @param callback 回调函数
* @returns 是否成功取消订阅
*/
}, {
key: "unsubscribe",
value: function unsubscribe(watchKey, event) {
// 获取监听列表
var callbacks = this.events.get(watchKey);
// 如果监听列表不存在,则返回false
if (!callbacks) {
return;
}
// 禁用事件
this.disableEvent(watchKey, event.key);
// 删除事件
callbacks.delete(event.key);
return;
}
/**
* 发布事件
* @param event 事件名称
* @param args 事件参数
*/
}, {
key: "publish",
value: function publish(watchKey) {
// 获取监听列表
var callbacks = this.events.get(watchKey);
// 如果监听列表不存在,则返回
if (!callbacks) {
return;
}
// 获取启用列表
var enableList = this.enableListMap.get(watchKey);
// 如果启用列表不存在,则返回
if (!enableList || enableList.length === 0) {
return;
}
// 只执行最后一个事件
var lastCallbackKey = enableList.at(-1);
// 如果最后一个事件存在,则执行
if (lastCallbackKey) {
try {
var _callbacks$get;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
(_callbacks$get = callbacks.get(lastCallbackKey)) === null || _callbacks$get === void 0 || _callbacks$get.apply(void 0, args);
} catch (error) {
console.error("\u6267\u884C\u4E8B\u4EF6\"".concat(watchKey, "\"\u7684\u56DE\u8C03\u65F6\u51FA\u9519:"), error);
}
}
}
/**
* 根据事件名称发布事件
* @param watchKey 监听的key
* @param eventKey 事件名称
* @param args 事件参数
*/
}, {
key: "publishByEventKey",
value: function publishByEventKey(watchKey, eventKey) {
var _callbacks$get2;
var callbacks = this.events.get(watchKey);
if (!callbacks) {
return;
}
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
(_callbacks$get2 = callbacks.get(eventKey)) === null || _callbacks$get2 === void 0 || _callbacks$get2.apply(void 0, args);
}
/**
* 获取最后一个启用的事件
* @param watchKey 监听的key
* @returns 最后一个启用的事件
*/
}, {
key: "getLastEnableEventKey",
value: function getLastEnableEventKey(watchKey) {
var enableList = this.enableListMap.get(watchKey);
console.log('enableList', enableList);
if (!enableList) {
return undefined;
}
return enableList.at(-1) || undefined;
}
/**
* 移除某个事件的所有订阅
* @param event 事件名称
* @returns 是否成功移除
*/
}, {
key: "clearEvent",
value: function clearEvent(watchKey) {
// 禁用所有事件
this.disabledAllEventByWatchKey(watchKey);
// 删除监听列表
this.events.delete(watchKey);
}
/**
* 移除所有事件订阅
*/
}, {
key: "clearAllEvents",
value: function clearAllEvents() {
// 清空监听列表
this.events.clear();
// 清空启用列表
this.enableListMap.clear();
}
/**
* 获取事件的订阅数量
* @param event 事件名称
* @returns 订阅数量
*/
}, {
key: "getSubscriberCount",
value: function getSubscriberCount(event) {
// 获取监听列表
var callbacks = this.events.get(event);
// 如果监听列表不存在,则返回0
return callbacks ? callbacks.size : 0;
}
/**
* 检查事件是否有订阅者
* @param event 事件名称
* @returns 是否有订阅者
*/
}, {
key: "hasSubscribers",
value: function hasSubscribers(event) {
// 获取监听列表
return this.getSubscriberCount(event) > 0;
}
/**
* 一次性订阅事件,事件触发后自动取消订阅
* @param event 事件名称
* @param callback 回调函数
* @returns 订阅对象,可用于取消订阅
*/
}, {
key: "once",
value: function once(watchKey, event) {
var _this2 = this;
// 包装回调函数
var wrappedCallback = function wrappedCallback() {
// 取消订阅
_this2.unsubscribe(watchKey, {
key: event.key,
callback: wrappedCallback
});
// 执行回调
event.callback.apply(event, arguments);
};
return this.subscribe(watchKey, {
key: event.key,
callback: wrappedCallback
});
}
}]);
return Watch;
}(); // 创建单例实例
var watch = new Watch();
export default watch;