Coder Social home page Coder Social logo

Comments (8)

zmitry avatar zmitry commented on May 22, 2024 1

I guess for this particular case we could add some helper which uses subscribers field

function watchGreedy(store, cb) {
store.subscribers.entries.forEach(el=>{
  el.watch(cb);
})
}

from effector.

zerobias avatar zerobias commented on May 22, 2024

as both cases are pretty valid, we need to handle each one
as watch is going to become a thing on its own, we could add configuration during creation
for example

this code will be called as often as possible

watch(lastEvent, {
  greedy: true,
  async handler(data) {
    sendAnalytics(data)
  },
})

and this one will be called only with last relevant update

watch(lastEvent, {
  greedy: false,
  async handler(data) {
    render(data)
  },
})

what do you think?

from effector.

dundich avatar dundich commented on May 22, 2024

maybe

greedy(lastEvent,    //or tick(lastEvent,  or last(..., or every(.....
  async handler(data) {
    sendAnalytics(data)
  }
)

from effector.

zerobias avatar zerobias commented on May 22, 2024

there could be another options, e.g skipFirst: true for skipping first immediately appeared update

from effector.

dundich avatar dundich commented on May 22, 2024

Can do like in Rxjs?

https://www.learnrxjs.io/operators/filtering/skip.html

import {  skip,filter,tap,take,takeUntil,finalize, map ....  } from 'foo-magic/operators';

const clickButton = createEvent()

const clickStore = createStore({e:'none'}).on(clickButton, (e) => {e:'click'})

const cancel$ = createEvent();

const lastStore$  = clickStore    // as event emitter (state, events ....   )
.pipe(  // magic combine
  takeUntil(cancel$),  // listen until no cancel event
  skip(1),  // skip ...for skipping first
  debounceTime(1000), // button-events thrash filter
  tap((state) => console.log(state)), 
  map((state) => {...state,  say: "УРА!"   }),
  tap((c) => console.log(c.say)),
  finalize(() => console.log("THE END"))
).subscribe(); // for example

// bla-bla  
for(int i=0; i<10; i++) clickButton();

clickStore.watch((e)=> console.log('SPAM!'); )
lastStore$.watch((e)=> console.log(e.say)) // УРА!



from effector.

everdimension avatar everdimension commented on May 22, 2024

I don't see how clickButton and closeModal can be invoked synchronously.
I mean, they can, but it probably makes no sense.

If they are invoked at different points in time, then analytics will get all expected results.

So perhaps it doesn't make sense for isolated atomic actions to be invoked synchronously? And in the rare cases where it does, it might be okay to only get notified about latest state?

from effector.

artalar avatar artalar commented on May 22, 2024

@everdimension clickButton trigger closeModal synchronously by forward.
My example - is just an example, not from my product code, but it is a good illustration for a diamond problem for subscribers.
Library calculation is accumulative for subscribers, what is mean under the hood all store and events dependencies (graph) calculations "flatify" and subscribers receive only last updates (even if there were several).

from effector.

zerobias avatar zerobias commented on May 22, 2024

implemented as sample for making updates non-greedy

from effector.

Related Issues (20)

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.