`task.once` is invalid in worker-executor.js
See original GitHub issueDescribe the bug
task.once
is invalid in client/src/templates/Challenges/utils/worker-executor.js
.
To Reproduce
-
add a test in
client/src/templates/Challenges/utils/worker-executor.test.js
it('Task should emit handler once', () => { mockWorker(); const testWorker = createWorker('test'); const task = testWorker.execute('test'); const handler = jest.fn(); task.once('testOnce', handler); task.emit('testOnce', handler); task.emit('testOnce', handler); expect(handler).toBeCalledTimes(1); });
-
run this test
-
If
task.once
works well, thenhandler
will be called just once. -
However,
handler
is called twice time.
Expected behavior
handler
should only be called once.
Analyze
There is only one parameter handler
in the function self.removeListener
on Line 141
self.once = (event, listener) => {
self.on(event, function handler(...args) {
self.removeListener(handler);
listener.apply(self, args);
});
return self;
};
But functionremoveListener
needs two parameters (event
and listener
), since handler
is type of function instead of type of string, so the program never enters the if judgment.
self.removeListener = (event, listener) => {
if (typeof self._events[event] !== 'undefined') {
const index = self._events[event].indexOf(listener);
if (index !== -1) {
self._events[event].splice(index, 1);
}
}
return self;
};
Solution
Perhaps Line 141
should be changed to
self.removeListener(event, handler);
and change Line 96
task.done = new Promise((resolve, reject) => {
task
.on('done', data => resolve(data))
.on('error', err => reject(err.message));
});
Issue Analytics
- State:
- Created a year ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
WorkerAPI - Communicating out - Help/Discuss - Gradle Forums
I'm starting to play with the Worker API, and I have a question about the best way to send information from the workers...
Read more >Unable to submit batch ingestion task - Druid Forum
Hi ranjan,. In the interest of time, I would suggest explicitly specifying a smaller interval and running ingestion on a subset of the...
Read more >A failure occurred while executing com.android.build.gradle ...
For me there was an issue with one of my xmls files missing the tools namespace declaration. Hoping to help somebody who still...
Read more >Vert.x Core Manual
The trouble with a single thread is it can only run on a single core at any one time, so if you want...
Read more >COMPSs Manual
Once Paraver has started, lets visualize the tasks: 14. Chapter 2. Quickstart ... void initMatrix(Matrix *matrix,int mSize,int nSize,double val){.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s a tricky one to migrate - I tried myself and it was a bit of a mess. However, with your changes the number of arguments should match up, so that should help the migration.
Awesome, I’m glad you were able to figure it out - that file is pretty confusing.