question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`task.once` is invalid in worker-executor.js

See original GitHub issue

Describe the bug

task.once is invalid in client/src/templates/Challenges/utils/worker-executor.js.

To Reproduce

  1. 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);
    });
    
  2. run this test

  3. If task.once works well, then handler will be called just once.

  4. 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

https://github.com/freeCodeCamp/freeCodeCamp/blob/8f19b1926bc51c0149254fdd7a86ecb0708c6dd2/client/src/templates/Challenges/utils/worker-executor.js#L141

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

https://github.com/freeCodeCamp/freeCodeCamp/blob/8f19b1926bc51c0149254fdd7a86ecb0708c6dd2/client/src/templates/Challenges/utils/worker-executor.js#L141

should be changed to

self.removeListener(event, handler);

and change Line 96

https://github.com/freeCodeCamp/freeCodeCamp/blob/8f19b1926bc51c0149254fdd7a86ecb0708c6dd2/client/src/templates/Challenges/utils/worker-executor.js#L96 to

task.done = new Promise((resolve, reject) => {
  task
    .on('done', data => resolve(data))
    .on('error', err => reject(err.message));
});

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ojeytonwilliamscommented, Jul 19, 2022

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.

0reactions
ojeytonwilliamscommented, Jul 20, 2022

Awesome, I’m glad you were able to figure it out - that file is pretty confusing.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found