Event queue
Objective: Implement an event queue system that manages user actions (like liking posts) in sequence and handles cancelable events (e.g., like + unlike = no action).
Instructions:
- Create a function called
createEventQueue
that manages an event queue.
- The event queue should support adding and canceling actions (e.g., "like post" followed by "unlike post" results in no event).
- Ensure that events are processed in the order they were added, but allow new events to be triggered while previous events are still being processed.
Example:
const eventQueue = createEventQueue();
eventQueue.add('like', () => console.log('Liked post 1'));
eventQueue.add('unlike', () => console.log('Unliked post 1'));
eventQueue.add('like', () => console.log('Liked post 2'));
// Output:
// Liked post 2
// In this case, the "like" and "unlike" actions for post 1 cancel each other,
// so only the "Liked post 2" event is processed.
Requirements:
- Create a queue system that stores actions and processes them in sequence.
- Ensure that cancelable actions (like/unlike) cancel each other when triggered for the same post.
- Provide a way to trigger new events while the queue is still being processed.
Hints:
- Use an array to store the events in the queue.
- Keep track of actions (like/unlike) to determine if they cancel each other out before executing them.