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

[bug]: useQueue #119

Closed
wmoooid opened this issue Jun 2, 2024 · 6 comments
Closed

[bug]: useQueue #119

wmoooid opened this issue Jun 2, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@wmoooid
Copy link
Contributor

wmoooid commented Jun 2, 2024

Писал тесты для useQueue и нашел баг:
Функция remove возвращает undefined вместо удаленного значения.

Текущая реализация:

  const remove = () => {
    let removed;
    setQueue(([first, ...rest]) => {
      removed = first;
      return rest;
    });
    return removed as Value;
  };

Возможный фикс:

  const remove = () => {
    const [first, ...rest] = queue;
    setQueue(rest);
    return first;
  };

При такой реализации бага нет, но смущает, что берем очередь напрямую из стейта, а не из сетстейта. Что думаете?

@wmoooid
Copy link
Contributor Author

wmoooid commented Jun 2, 2024

Как вариант можно ещё что-то такое сделать:

  const remove = () => {
    setQueue(([, ...rest]) => rest);
    return queue[0];
  };

@debabin
Copy link
Member

debabin commented Jun 2, 2024

@wmoooid Забавный баг, с учетом того, что такая реализация у многих, баг понятен return выполняется первеe чем setState

Такая же бага uidotdev/usehooks#276

@debabin
Copy link
Member

debabin commented Jun 2, 2024

Тут вероятнее всего такая же бага https://github.com/streamich/react-use/blob/master/src/useQueue.ts#L17

@debabin
Copy link
Member

debabin commented Jun 2, 2024

Все твои варианты с огрехом, если вызывать по 2 remove подряд, то получаем баг
image

@debabin
Copy link
Member

debabin commented Jun 2, 2024

Залил фикс, прошу закрыть issue после проверки

@wmoooid
Copy link
Contributor Author

wmoooid commented Jun 2, 2024

Проверил, работает как надо

@wmoooid wmoooid closed this as completed Jun 2, 2024
@debabin debabin added the bug Something isn't working label Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants