I am currently implementing a new feature at work. The app wants to give sellers an admin where they can see various things. An example of one of the things they can see on their dashboard is their last 10 orders.
The order's API only returns an array of various ids (customer id, product id, seller id, etc) In order to populate the orders page, I have to make 3 different API call on each order to get the data to render on the order list page
Now that I have to create a dashboard that is different from the order list page, I do not want to make such a tedious API call again. I want to create a dashboard$ observable that has the last emitted value of orderList$ but I do not want anything subscribed to orderList$ to be cached.
So when orderList$ is called, it gets the lastest orderList data from the server (I do not feel something as sensitive as order list should be cached), when dashboard$ is called, get the last emitted value of orderList$ and if orderList$ has not emitted any values then dashboard$ can make a request to retrieve order list.
When working with a reactive state of mind, I like to define what should happen based on events. By this I mean that I'll have a Subject in which in can push values into to notify an event and from there we can react to these events.
In your case, here's an idea for what you want:
const navigationToOrdersPage$$ = new Subject<void>();
const refreshOrdersButtonClicked$$ = new Subject<void>();
const orders$: Order[] = merge(
navigationToOrdersPage$$,
refreshOrdersButtonClicked$$
).pipe(
switchMap(() => yourOrderService.getOrders()),
shareReplay({
bufferSize: 1,
// make sure that even if we don't have anyone subscribed to that observable
// we keep the result in the cache and if we ever go to the orders page or click
// on the refresh button it'll be updated anyway but with this it's safe to navigate
// to another page than orders and the dashboard and if you go back to the dashboard
// you'd still get an instant result
refCount: false,
})
);
So here it'd be safe to reuse this observable on both the admin and the dashboard page. Of course, you'll need to call next on the 2 subjects when appropriate so that the orders can be refreshed when they need to.
Related
I am working on Ethereum smart contract to build a marketplace between sellers and buyers. When someone (y) ask a product from (x), can I trigger an event to let ONLY (x) he has a new request for his product ??
I have done the full code of the smart contract and I have done the event listening as well using web3.py. But what I want is not triggering the event for everyone on the network, only the seller should have a notice, so every seller has his own requests list.
I have no idea if this is possible or not, if yes, could someone please help me what I should update in the event listing code I have
def handle_event(event):
if (event.args['_new_req'] == True ):
print(' New request for the product ')
else:
print (' The request has been failed ')
def log_loop(event_filter, poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event(event)
time.sleep(poll_interval)
def _new_req_listen():
block_filter = contract.events._new_req.createFilter(fromBlock = 'latest')
worker1 = Thread(target=log_loop, args=(block_filter, 2))
worker1.start()
Could I filter the same event to be triggered in different cases or for different persons in the network ??
Thanks in advance
I'd recommend you think of this from the high level (algorithm) point of view, without dropping down to the code immediately.
For your requirement of having an event raised for a particular person (seller), the way to do this would be either:
One event list. The event data structure contains a "destination" address property. You can set "destination" to a unique value (e.g. 0x000000) than means "everyone", or you can set "destination" to one person (e.g. the address of the seller).
Or, have an unique event list for every seller. Your event processing will need to have an outer loop to iterate over all the event lists. You can also have a 'global' event list which means every seller is sent the event.
I am subscribing a value from a reducer, where it will always true. when first time i subscribes it works. but next time it's not. from my research always the value is true if i change value to false then it works fine.
so subscribe expect the different value to observer or unsubscription. at present I do like this:
this.store.dispatch(new actionsMc.CheckConcurrencySuccess(false));
this.store.dispatch(new actionsMc.CheckConcurrency(true));
even though I required to pass true always i do above to subscribe my update. instead of this work around any other way is there is rxjs any one help me?
in subscription i tried with take once but it not working further. but looking for some other work around.
here is my subscription:
this.store.pipe(last(select(subscribe.newvalue))).subscribe(res => {
console.log('new res', res);
})
but not works. as a simple how to subscribe and unsubscribe on each value received?
If I understood you correctly, you are saying that even though the data in the store is updated to "true", you are not not notified about it in your subscription. That is because store automatically compares the new and the previous values, and fires .next() only if the values are not equal.
The simplest solution for you would be to wrap the boolean field in your store into an object (or array: [boolean] instead of boolean), so you would get notified every time it is updated.
Pimcore How to get event for first time product save using backend.
I have to apply some logic for first time product creation in pimcore.
How to find event name.
The name of the event is 'object.preAdd'.
If the Id of the saved object is 0, it's a newly created one
\Pimcore::getEventManager()->attach("object.preAdd", function (\Zend_EventManager_Event $e) {
// your code goes here
});
It would probably be best if you stick the code above into a custom made plugin to ensure it's executed on every call.
See https://www.pimcore.org/docs/latest/Extending_Pimcore/Event_API_and_Event_Manager.html for more information
I'm new in Flux/React Native.
I'm quite confused about dispatch vs emit using in Flux.
What is the main difference between them? And what happen when I use same Action Type in dispatch and emit.
For example:
Dispatcher.dispatch({
actionType: 'ACTION1'
});
SomeStore.emit('ACTION1');
In Flux, events are emitted by the store indicating a change in its state. This 'change' event is listened to by views. This will prompt a view to fetch new state from the store. Mind you, the event never contains payload / information about the new state. It is really just what it reads - an event.
Actions are slightly different. While they are indeed events, they are things that occur in our domain eg., Add item to cart. And they carry a payload that contains information about the action, eg.,
{
id: ‘add-item-to-cart’,
payload: {
cartId: 123,
itemId: 1234,
name: ‘Box of chocolates’,
quantity: 1
}
}
Actions are 'dispatched' from the views and the store(s) responds to the dispatch by possibly changing its state and emitting a 'change' event.
So basically:
A view dispatches an action with a payload (usually due to a user interaction) via the dispatcher
The store (which had previously registered itself with the dispatcher)
is notified of the action and uses the payload to change its state and emit an event.
The view (which had previously registered itself with the store) is notified of the change event which causes it to get the new state from the store and change itself.
So that's the difference. And about the question "use same Action Type in dispatch and emit", it doesn't really make sense, does it?
I suggest you read this blog post - http://blog.andrewray.me/flux-for-stupid-people/ (The title means no offence BTW :))
You already know this, but I'll say it again: A unidirectional data flow is central to the Flux pattern. That means data (not control) always flows in one direction.
I'm looping through several items and making an ajax request for each of them (using jQuery). I want them to execute independently, but populate into the DOM in the order they were called, not the order they are returned (for some reason some requests are taking longer than others). Any tips on the best practice for this type of thing?
Well the results can come back in any undefined order, they are asynchronous and subject to the vagaries of the internet and servers.
What you can do is deal with the problem in the same way TCP does over UDP. You use sequence identifiers.
Keep a sequence identifier going, and increment it every time you send out a request. As requests come back, check them off in order and only process them as they come in. Keep a list of what has returned with the data in order, and have a routine fire to check that list after each update to it. When the first expected is in, it should process the whole list down to the first gap.
Bare in mind that you could lose a request, so a suitable timeout before you ignore a given sequence identifier would be in order.
The answer to this ended up being a jQuery plugin called ajaxManager. This did exactly what I needed:
https://github.com/aFarkas/Ajaxmanager
You could send all the success result objects to a queue. Have an index that was sent with the original request, and continually check that queue for the next index.
But generally browsers only allow two simultaneous ajax requests, so it might be worth it to just send the next ajax request on success of the previous request.
Here's a start at the code:
var results = {}, lastProcessedIndex = 0;
var totalLength = $('a.myselector').each(function(el, index){
$.ajax({
url: $(this).attr('href'),
success: function(result){
results[index] = result; // add to results object
}
});
}).length;
var intervalId = setInterval(function(){
if(results[lastProcessedIndex]){
// use object
lastProcessedIndex++;
}
else if(totalLength == lastProcessedIndex){
clearInterval(intervalId);
}
}, 1000); // every 1 second
I'll be taking a stab in the dark with this one but it might help. Maybe you could create a global buffer array and then whenever the AJAX returns you can add the result to the buffer. You could then set up a timer that, when triggered, will check the contents of the buffer. If they are in order it will output it accordingly.