Coder Social home page Coder Social logo

Comments (9)

nsdub avatar nsdub commented on June 7, 2024 4

This is a useful feature and one that I'd love to see merged. We're currently using @jamesst20 's solution with patch-package

from set-interval-async.

jamesst20 avatar jamesst20 commented on June 7, 2024 2

@ealmansi I opened a PR #21

from set-interval-async.

james-em avatar james-em commented on June 7, 2024 1

Hi @ealmansi

Have you had the chance to look into this feature request? I see the PR #21 was declined, perhaps you had a better idea in mind?

The interface

setIntervalAsync (() => {}, 1000, ...args) // Always after 1000ms
setIntervalAsync (() => {}, [0, 1000], ...args) // First run after 0ms and then 1000ms

looked pretty logical to me.

Waiting for your answer :)

from set-interval-async.

ealmansi avatar ealmansi commented on June 7, 2024

Hi James,

Thanks for creating this issue; I appreciate it.

Like you pointed out, the snippet above slightly breaks the API. In particular, if the first execution takes longer than the interval to execute, the following property is no longer withheld:

Dynamic: If possible, the given function is called once every interval milliseconds. If any execution takes longer than the desired interval, the next execution is delayed until the previous one has finished, and called immediately after this condition is reached.

Also, if the first execution fails, the process would be interrupted entirely.

I think the best way to achieve this would be changing this line in the definition of setIntervalAsync:

  timer.timeouts[id] = setTimeout(
    timeoutHandler,
    0, // interval,
    timer,
    handler,
    interval,
    ...args
  )

I would be hesitant of adding in this feature if it implies a departure from the traditional setInterval API, since compatibility is one of the mail goals for the project.

from set-interval-async.

jamesst20 avatar jamesst20 commented on June 7, 2024

Thanks for getting back to me.

I understand the drawback from moving away from the original API. A possible workaround could be to export an additional method ?

Exemple usage:

import { setIntervalAsync, setIntervalNowAsync, SetIntervalAsyncTimer } from "set-interval-async/dynamic";

What do you think?

from set-interval-async.

jamesst20 avatar jamesst20 commented on June 7, 2024

@ealmansi

Sorry, I have been lazy and hadn't taken the time to check your implementation. I found a way my feature request could be implemented without any breaking API changes.

Example for dynamic.js

Current implementation:

function setIntervalAsync (handler, interval, ...args) {
  validateHandler(handler)
  validateInterval(interval)
  const timer = new SetIntervalAsyncTimer()
  const iterationId = 0
  timer.timeouts[iterationId] = setTimeout(
    timeoutHandler,
    interval,
    timer,
    iterationId,
    handler,
    interval,
    ...args
  )
  return timer
}

becomes

function setIntervalAsync (handler, interval, ...args) {
  validateHandler(handler)
  validateInterval(interval)
  const timer = new SetIntervalAsyncTimer()
  const iterationId = 0
  const intervals = [interval].flat();
  timer.timeouts[iterationId] = setTimeout(
    timeoutHandler,
    intervals[0],
    timer,
    iterationId,
    handler,
    intervals[1] || intervals[0],
    ...args
  )
  return timer
}

No broken calls:

setIntervalAsync (() => {}, 1000, ...args) // Always after 1000ms
setIntervalAsync (() => {}, [0, 1000], ...args) // First run after 0ms and then 1000ms

@ealmansi What do you think?

from set-interval-async.

arthur-caillaud avatar arthur-caillaud commented on June 7, 2024

I also find the API in PR #21 quite good design actually. No breaking change and pretty straightforward. Did you find another solution @james-em or @jamesst20 for this feature ? Another package maybe ?

Thank you for your help

from set-interval-async.

james-em avatar james-em commented on June 7, 2024

lazy and hadn't taken the time to check your implementation. I found a way my feature request could be implemented without any breaking API changes.

Example for dynamic.js

I still use the same implementation proposed in #21

I still don't understand why @ealmansi didn't merge it because it doesn't break anything and it was only one click away. We could have also exported extra method like setIntervalAsync, setIntervalNowAsync but looks like that proposition didn't sound good to him either.

from set-interval-async.

ealmansi avatar ealmansi commented on June 7, 2024

While I understand that there's some interest in this feature, extending vanilla setInterval other than making it work nicely with async functions is beyond the scope of this library.

from set-interval-async.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.