How to disable BackgroundTransferRequest's TransferStatusChanged event handler after I unsubscribed it? - windows-phone-7

I'm using background transfer to download videos and I subscribed each request's TransferStatusChanged and TransferProgressChanged event to monitor it's status and download progress. When I cancelled one background transfer request using BackgroundTransferService.Remove() method, it fired TransferStatusChanged event as msdn mentioned. I don't want to execute event handlers, so I try to unsubscribe event before removed the request, like code below:
BackgroundTransferRequest transferToRemove = BackgroundTransferService.Find(requestId);
if (transferToRemove != null)
{
transferToRemove.TransferStatusChanged -= transfer_TransferStatusChanged;
transferToRemove.TransferProgressChanged -= transfer_TransferProgressChanged;
BackgroundTransferService.Remove(transferToRemove);
}
but TransferStatusChanged event handler still fired. Can anyone help me?

BackgroundTransferService.Remove(transferToRemove); only Accepts the request. It will take sometime to remove it. Meanwhile, you again call the Add() function and thus you got one more event i.e. transfer_TransferProgressChanged.
In event transfer_TransferProgressChanged, first check BackgroundTransferService contains your request or not.
if(BackgroundTransferService.Requests.Contains(m_currentRequest))
{
BackgroundTransferService.Remove(m_currentRequest);
UnsubscribeYourEvents();
DoOtherStuffRealtedToDownload();
}

Related

Google Publisher Tag, how to remove event listener from Service

There seem to be several questions on how to register events to a gpt service:
Google Publisher Tag registering to Events
registering to events with google publisher tag
How to do this is clearly defined in the documentation:
googletag.pubads().addEventListener('eventName', callbackFn);
I add my event to the service when the component (React) mounts inside the callback function of window.googletag.cmd.push as described in this tutorial by Google.
Now the problem is that every time I change page, more event listeners are added to the service. I can make sure only one event listener executes on the actually existing slots by using this method (from the documentation):
googletag.pubads().addEventListener('impressionViewable', function(event) {
if (event.slot == targetSlot) { // will only run on target slot
// Slot specific logic.
}
});
But more an more event listeners will remain active and keep on executing (without executing the code within the if-statement).
Now, I assumed google would have implemented something like this (to run on componentWillUnmount):
googletag.pubads().removeEventListener('eventName', callbackFn);
But it doesn't exist in the documentation and I can't seem to find any way to remove active event listeners from the service?
So I went with this:
let eventListnerCreated = false;
if(!eventListnerCreated) {
eventListnerCreated = googletag.pubads().addEventListener("slotRenderEnded", function(event) {
// blablabla
});
}
Not clean. But will work.
I know this doesn't solve the original issue of removing the event listener, but this will let's not create event listeners again and again.

Not receiving Apache Camel Event Notifications under the smallest load

I have extended EventNotiferSupport, and set the isEnable() to respond True for all events. I have a notify() that logs what events I receive and the corresponding Exchange ID for the event.
I have added my ExchangeMessageNotifier with this.context.getManagementStrategy().addEventNotifier(this.exchangeMessageNotifier);
I run my program under basically no load, sending 1 message at a time 1 second delay between messages into Camel to send out. Everything works the way I expect. I receive my events everything looks good.
I decrease the delay between messages to 0 milliseconds, and I find that 1 out of approximately 20 messages I fail to receive one of the Events, (Often the Completed event).
Add a second thread sending at the same rate and I don't get any events for any messages.
What am I missing? I've done searches and I don't find anything that I need to do differently. Is there something I am missing?
I am using Apache Camel 2.16.3, and moved to 2.18.1 still see the same behavior.
Well found my own answer. Part of the fun of inheriting code without any informaiton.
In your implementation of the EventNotifierSupport you need to override the doStart() method and configure the EventNotifierSupport for what events you wish to receive.
protected void doStart() throws Exception {
// filter out unwanted events
setIgnoreCamelContextEvents(true);
setIgnoreServiceEvents(true);
setIgnoreRouteEvents(true);
setIgnoreExchangeCreatedEvent(true);
setIgnoreExchangeCompletedEvent(false);
setIgnoreExchangeFailedEvents(true);
setIgnoreExchangeRedeliveryEvents(true);
setIgnoreExchangeSentEvents(false);
}
This is in addition to doing the following:
public boolean isEnabled(EventObject event) {
return true;
}
Which enables you to determine if you want a particular event, out of the selected groups you had set in the doStart().
Once these changes were in I was receiving consistent events.

Cancel "done" listener to jQuery AJAX Deferred?

If I have an AJAX request I initiated with a jQuery $.ajax() call, and at the time attached a done() listener to, is there a way to un-listen to it? I have a situation with a search-as-you-type field, where if a request gets made to the server that for whatever reason takes a long time to respond, but while waiting the user types a bit more and a second request fires and comes back quickly, when the first request eventually completes, the done() listener fires, rewriting the DOM with the the first request's data, not the second.
I've tried issuing an abort() call on the AJAX, or reject() on the Deferred right before the second request is made, but that shows the error state for the first call while the second call is in-flight.
I tried coming up with an ID system to hash out which request is the "current" one, so in the done() listener it first checks "am I still the most recent request?" before updating the DOM, but that seems clunky that the app needs to create an ID/Hash methodology for each API endpoint?
On every done callback you could log a timestamp, and execute the callback only if the timestamp is the latest one yet. So:
var latest_ts = 0;
$.get('...').done(function() {
var this_ts = (new Date).getTime();
if (this_ts < latest_ts) return;
latest_ts = this_ts;
//callback code here...
});
So if request A is fired but request B is fired after it but returns first, the timestamp is updated and A's callback will be ignored, as its timestamp is out of date.

Laravel testing without firing cache events

I am using the array cache driver while testing, however I want to disable the Cache Events.
I can do this with
$this->withoutEvents();
I'd rather just stop the one event, however
$this->expectsEvents(Illuminate\Cache\Events\KeyForgotten::class);
will throw an error if the event is not called.
One solution would be a function that on the outside allows an event to fire and hides it but doesn't throw an error if the event doesn't occur.
I think I need to mock the Events Dispatcher like so
$mock = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$mock->shouldReceive('fire')->andReturnUsing(function ($called) {
$this->firedEvents[] = $called;
});
$this->app->instance('events', $mock);
return $this;
The question would be how to carry on dispatching the non caught events?

Issue with order of event registeringe, sending, receiving and activities/places

I just finished my first own example with activities/places and MVP. all works fine but some events aren't send or received properly if i change back to a place (from another place). but on "moduleLoad" where this place is set as default place all works fine. i think it shouldn't make a difference if a place/activity is started on moduleLoad (via historyHandler = new PlaceHistoryHandler(historyMapper); historyHandler.register(placeController, eB, defaultPlace); historyHandler.handleCurrentHistory();) or via placeController.goTo(place); , should it?
via debugging i checked the order of event registration, event sending and event receiving (all is executed in start(...) of the activity). the problem is that all receiver don't receive the event if start() is executed via goTo(place) (registration and sending works fine). But if an event is sent after start() or within start()-executed on moduleLoad all works fine!
my activity start looks like that:
#Override
public final void start(final AcceptsOneWidget panel, final EventBus eventBus) {
// register events - to manipulate visibility of some display areas
eventBus.addHandler(SelectedEvent.TYPE, this);
//initiate presenters -(pseudo code)
[presenter that receives SelectedEvent]
[presenter that sends SelectedEvent]
//ading presenter's asWidgets to screen -> panel.setWidget..
...
}
#Override
public final void onSelected(final SelectedEvent event) {
//do something
}
(the use case for sending this event on start is, that i want say nothing is selected - the event's payload is null)
the problem is that neither the presenter's nor the activity's onSelected -method is called if the start() is called via goTo. But in all cases (checked with debugger) the registering on event occurs before sending it. what should i do?
Javascript is not multithread.
When you call a goTo() method, your activities will be started one after the others. If you fire an event inside a start(), the event will be dispatched before the remaining activities are started. So there is a good chance that the activity handling that event has not been started yet (the registration was not done).
You can solve your problem with the following code:
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
#Override
public void execute()
{
//Fire the event
}
});
Scheduler is a utility class provided by GWT. ScheduleDeferred will execute the command after the current browser event loop returns.
So, by pasting this code in a start() , you know the event will be fired as soon as every activity is started.

Resources