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

Doesn't render elements on different containers. #52

Open
FazleRabbbiferdaus172 opened this issue Sep 2, 2023 · 0 comments
Open

Doesn't render elements on different containers. #52

FazleRabbbiferdaus172 opened this issue Sep 2, 2023 · 0 comments

Comments

@FazleRabbbiferdaus172
Copy link

Didact.render(element1, container1);
Didact.render(element2, container2);

Expected Behaviour:
element1 should have been appended to the container1 and element2 should have been appended to the container2.

Current Behaviour:
Only element2 is being appended to container2.

Reason:
The second render call is executed before workLoop is executed for the first time as a result wipRoot and nextUnitOfWork change before the first render call gets the chance to render element1, container1.

Possible Fix:

  1. Instead of assigning wipRoot in the render function, we maintain a queue of wipRoot.
  2. In workLoop we check if there is no nextUnitOfwork and wipQueue is not empty then we pop the first element assign it to wipRoot and set the nextUnitOfWork.
let wipQueue = [];
function render(element, container) {
  let tWipRoot;
  tWipRoot = {
    dom: container,
    props: {
      children: [element],
    },
    alternate: currentRoot,
  };
  deletions = [];
 wipQueue.push(tWipRoot);
}

function workLoop(deadline) {
  let shouldYield = false;

  if (!nextUnitOfWork && wipQueue) {
    wipRoot = wipQueue.shift();
    nextUnitOfWork = wipRoot;
  }

  while (nextUnitOfWork && !shouldYield) {
    nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
    shouldYield = deadline.timeRemaining() < 1;
  }

  if (!nextUnitOfWork && wipRoot) {
    commitRoot();
  }

Although this solution works, we also have to maintain another list of currentRoot for hooks to work.
Also, I am new to JS and front-end in general. So there might be a better solution, Just wanted to share my thoughts.

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

No branches or pull requests

1 participant