代码拉取完成,页面将自动刷新
同步操作将从 方瑾/async 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "events.h"
#define PutEvent(map, key, value) map->put(map, (let)key, (let)value)
#define GetEvent(map, key) (Array)map->get(map, (let)key)
#define RemoveEvent(map, key) map->remove(map, (let)key)
#define ExisteEvent(map, key) map->exists(map, (let)key)
Emitter createEmitter() {
Emitter emitter = newEmitter();
if (emitter == NULL) {
return NULL;
}
emitter->hashMap = createHashMap(NULL, NULL);
emitter->on = on;
emitter->emit = emit;
emitter->once = once;
emitter->prependListener = prependListener;
emitter->prependOnceListener = prependOnceListener;
emitter->removeListener = removeListener;
emitter->getEventListener = getEventListener;
return emitter;
}
void freeEmitter(Emitter * emitter) {
(*emitter)->hashMap->clear((*emitter)->hashMap);
free((*emitter)->hashMap);
free(*emitter);
*emitter = NULL;
}
void on(Emitter emitter, string eventName, Listener listener, let args) {
HashMap hashMap = emitter->hashMap;
Event event = newEvent();
if (event == NULL) {
return NULL;
}
event->listener = listener;
event->type = MoreEvent;
event->eventName = newString(eventName);
event->args = args;
Array eventQueue = NULL;
if (ExisteEvent(hashMap, event->eventName)) {
eventQueue = GetEvent(hashMap, event->eventName);
}
else {
eventQueue = createArray(0);
PutEvent(hashMap, event->eventName, eventQueue);
}
eventQueue->push(eventQueue, event);
}
void once(Emitter emitter, string eventName, Listener listener, let args) {
HashMap hashMap = emitter->hashMap;
Event event = newEvent();
if (event == NULL) {
return;
}
event->listener = listener;
event->type = OnceEvent;
event->eventName = newString(eventName);
event->args = args;
Array eventQueue = NULL;
if (ExisteEvent(hashMap, event->eventName)) {
eventQueue = GetEvent(hashMap, event->eventName);
}
else {
eventQueue = createArray(0);
string x = newString(event->eventName);
PutEvent(hashMap, x, eventQueue);
}
eventQueue->push(eventQueue, event);
assert(eventQueue->length > 0);
}
void emit(Emitter emitter, string eventName) {
HashMap hashMap = emitter->hashMap;
if (ExisteEvent(hashMap, eventName)) {
eventName = newString(eventName);
Array eventQueue = GetEvent(hashMap, eventName);
ArrayIterator iterator = createArrayIterator(eventQueue);
while (hasNextArrayIterator(iterator)) {
iterator = nextArrayIterator(iterator);
ListNode node = iterator->node;
if (node == NULL) {
break;
}
Event event = (Event)node->item;
event->listener(event, event->args);
if (event->type == OnceEvent) {
// 事件队列长度减少1
eventQueue->length--;
// 将迭代器指向返回上一个
iterator->node = iterator->node->last;
// 修改数组节点
if (node->last == NULL) {
eventQueue->head = node->next;
if (eventQueue->head != NULL) {
eventQueue->head->last = NULL;
}
}
else if (node->next == NULL) {
eventQueue->tail = node->last;
if (eventQueue->tail != NULL) {
eventQueue->tail->next = NULL;
}
}
else {
node->last->next = node->next;
}
free(event->eventName);
// 释放event节点
free(event);
// 释放节点
free(node);
}
}
if (eventQueue->length == 0) {
// 如果事件队列为空,则移除事件监听
free(RemoveEvent(hashMap, eventName));
freeArray(&eventQueue);
}
freeArrayIterator(&iterator);
free(eventName);
}
}
void prependListener(Emitter emitter, string eventName, Listener listener, let args) {
HashMap hashMap = emitter->hashMap;
Event event = newEvent();
if (event == NULL) {
return NULL;
}
event->listener = listener;
event->type = MoreEvent;
event->eventName = newString(eventName);
event->args = args;
Array eventQueue = NULL;
if (ExisteEvent(hashMap, eventName)) {
eventQueue = GetEvent(hashMap, eventName);
}
else {
eventQueue = createArray(0);
PutEvent(hashMap, eventName, eventQueue);
}
eventQueue->unshift(eventQueue, event);
}
void prependOnceListener(Emitter emitter, string eventName, Listener listener, let args) {
HashMap hashMap = emitter->hashMap;
Event event = newEvent();
if (event == NULL) {
return;
}
event->listener = listener;
event->type = OnceEvent;
event->eventName = newString(eventName);
event->args = args;
Array eventQueue = NULL;
if (ExisteEvent(hashMap, eventName)) {
eventQueue = GetEvent(hashMap, eventName);
}
else {
eventQueue = createArray(0);
PutEvent(hashMap, eventName, eventQueue);
}
eventQueue->unshift(eventQueue, event);
}
void removeListener(Emitter emitter, string eventName) {
HashMap hashMap = emitter->hashMap;
if (ExisteEvent(hashMap, eventName)) {
Array array = GetEvent(hashMap, eventName);
freeArray(&array);
RemoveEvent(hashMap, eventName);
}
}
Array getEventListener(Emitter emitter, string eventName) {
HashMap hashMap = emitter->hashMap;
if (ExisteEvent(hashMap, eventName)) {
return GetEvent(hashMap, eventName);
}
else {
return NULL;
}
}
void dispalyEmitter(Emitter emitter) {
HashMap hashMap = emitter->hashMap;
HashMapIterator iterator = createHashMapIterator(hashMap);
while (hasNextHashMapIterator(iterator)) {
iterator = nextHashMapIterator(iterator);
log("id: %s\n", (string)iterator->entry->key);
log("hashCode: %d\n", iterator->hashCode);
}
freeHashMapIterator(&iterator);
log("\n\n");
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。