Comments (8)
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.
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.
maybe
greedy(lastEvent, //or tick(lastEvent, or last(..., or every(.....
async handler(data) {
sendAnalytics(data)
}
)
from effector.
there could be another options, e.g skipFirst: true
for skipping first immediately appeared update
from effector.
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.
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.
@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.
implemented as sample
for making updates non-greedy
from effector.
Related Issues (20)
- Allow passing `name` for `attach` without `mapParams/source`
- useUnit shape bug in old iOS Safari HOT 4
- useStore is deprecated, prefer useUnit instead HOT 2
- Storing nodes in an effector store? HOT 1
- Mutating store state directly HOT 3
- StoreWritable.on() reports `undefined` returned from reducer HOT 3
- `await $store.read()` as alternative to error-prone `$store.getState()` HOT 2
- Add guide for library authors
- Using an array of units with `split` HOT 1
- `allSettled` should not accept derived units HOT 1
- Unsubscribing a sample HOT 1
- What's the best way to `forward` return values from effects to other store? HOT 3
- Question about DI HOT 5
- Broken types for `createApi` HOT 1
- Проблема с извлечением типов HOT 1
- Deprecate `Domain` unit creator methods HOT 1
- [babel-plugin] Add an option to disable transforming `Domain` methods HOT 1
- store.map() and lastState HOT 2
- Name collision in `combine` with `domain` HOT 5
- `split` cases show error with inline prepend calls HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from effector.