Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task manager - clone without recursion #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sevriugin
Copy link

@sevriugin sevriugin commented Feb 5, 2023

With latest GVL updates the clone method of Cloneable abstract class that using recursion stops working due to call stack limit even with Error.stackTraceLimit = Infinity.

The solution could be to introduce an alternative approach to deep clone of the object using tasks.

This PR implements TaskManager class that clones the object without the recursion using Task objects and TaskQueue class.

The run method of the TaskManager creates a new root task for given object and call processor method. This methods is running processing loop: reads next task from TaskQueue.toDo process it using taskProcessor (refactoring copy of the Cloneable.clone) function and move task to the next state: Done or Blocked . If task data object is complex value the taskProcessor will create a number of subtasks and put them in the queue for processing.

After all tasks are processed and TaskQueue.toDo is empty the postProcessor method will work with TaskQueue.done to merge subtasks to parent task and update the parent task status status if needed. If the root task status is set to Done the postProcessor is finished and the root task data will be returned as result.

     run(data: unknown): unknown {
        this.taskQueue.addRootTask(data);
        this.processor();
        const result = this.postProcessor();
        this.taskQueue.clear();
        return result;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant