Proposal to add `fromProperty` static function
See original GitHub issueI 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.
-
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 -
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:
- Created 5 years ago
- Comments:15
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
(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):
Express the dependency directly via flatMap and friends. When all of them end, emit the single value that results.
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:would express that as well.
Thoughts?
Hmm, another very interesting idea. I totally forgot about this possibility. More kudos to you! 👏