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.

Proposal to add `fromProperty` static function

See original GitHub issue

I find myself wanting to throttle/debounce properties quite often. One example where it seems absolutely necessary – SSR. You need to wait until your state somehow stabillizes to make a snapshot of it for rendering.

  1. state$.throttle() / state$.debounce() don’t fit because they pass initial value through a time-based gateways. I mentioned that here https://github.com/kefirjs/kefir/issues/278

  2. state$.changes().throttle(), etc. don’t fit because they lose initial value completely.

I propose to add the following function to the standard toolkit:

function fromProperty(prop$) {
  return K.merge([K.later(0, prop$.take(1)).flatMap(), prop$.skip(1)])
}

Demo case:

let K = require("kefir")

let makeStore = (seed, action$) => {
  return action$
    .scan((z, fn) => fn(z), seed)
    .skipDuplicates()
}

let action$ = K.later(250, z => z + 1) // also try: K.never()

let state$ = makeStore(1, action$)

function fromProperty(prop$) {
  return K.merge([K.later(0, prop$.take(1)).flatMap(), prop$.skip(1)])
}

fromProperty(state$)
  .throttle(500, {leading: false}) // ---2| or ---1|
  .log("state$") // both async and initial values are throttled / debounced properly

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
mAAdhaTTahcommented, Jul 3, 2018

(I’m assuming a lot about your use case, so please correct me where I’m wrong.)

Thinking about this more, if you’re determining that you state has stabilized as a factor of time, it feels a bit Doing It Wrong™. Expressing it as “state has stabilized after receiving no updates after Xms” doesn’t capture the relationship between a “stable state” & whatever other tasks are responsible for stabilizing it. You’re expressing the relationship implicitly through time, rather than directly through the streams themselves.

I would approach the issue one of two ways (I’m assuming your approach is Redux-like, which I have a decent amount of experience in):

  1. Express the dependency directly via flatMap and friends. When all of them end, emit the single value that results.

  2. Express the dependency through the state itself, e.g. if you have 4 tasks that need to finish before emitting the state, then having state.tasks.competed = 0 and incrementing that as they complete, then doing something like:

    state$.filter(state => state.tasks.completed >= 4).take(1)
    

    would express that as well.

Thoughts?

0reactions
ivan-kleshnincommented, Jul 5, 2018

Hmm, another very interesting idea. I totally forgot about this possibility. More kudos to you! 👏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does PyCharm propose to change method to static?
I'd like to add that you maybe want to move the method to a top-level function instead of a static method inside a...
Read more >
The static parts of new class features, in a separate proposal
This proposal has been merged into the class fields proposal. Please see that repository for current status. The rest of this repository is...
Read more >
babel/plugin-proposal-class-properties
Below is a class with four class properties which will be transformed. class Bork { //Property initializer syntax instanceProperty = "bork"; boundFunction = ......
Read more >
functools — Higher-order functions and operations on callable ...
Accordingly, it doesn't make sense to cache functions with side-effects, functions that need to create distinct mutable objects on each call, or impure ......
Read more >
ECMAScript proposal: private static methods and accessors in ...
The following class has a private static method .#createInternal() : class Point { static create(x, y) { return Point.
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