Why use angularfire $destroy()? - angularfire

What is the reason that $destroy() exists in Angularfire?
The documentation of angularfire sais:
https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-destroy
Stop listening for events and free memory used by this array (empties the local copy). Changes are no longer synchronized to or from Firebase.
sync = $firebase(ref).$asArray();
...
....
sync.$destroy()
Can I not just do:
sync = null
or
delete sync
Or should I really use $destroy() for some reason?

The $destroy() exists to empty the data and unbind event listeners. If you need to unbind the $firebaseArray() or $firebaseObject() you could use $destroy() but I think it would be nicer to use the unbind function that is resolved.
This was code snippet was taken from angularfire-seed
var unbind;
// create a 3-way binding with the user profile object in Firebase
var profile = $firebaseObject(fbutil.ref('users', user.uid));
profile.$bindTo($scope, 'profile').then(function(ub) { unbind = ub; });
// expose logout function to scope
$scope.logout = function() {
if( unbind ) { unbind(); }
profile.$destroy();
...
};

Related

Effector: how to reset all domain stores before each test?

I want to reset all domain stores before each test case. Is there some way to do it with Effector?
There is no such API in effector. You can create separate event and subscribe every store to it:
const resetForm = createEvent()
formDomain.onCreateStore(store => store.reset(resetForm))
But in general you shouldn't manually reset stores in tests.
Prefer Fork API usage instead
https://effector.dev/docs/api/effector/fork - docs
https://dev.to/effector/the-best-part-of-effector-4c27 - article
Example:
test('stuff', async () => {
// create new forked scope, which is completly independent
const scope = fork({
// apply modifications like initial store values in this scope
values: [[$myStore, "value"], [$myOtherStore, 0]], // changed value in $myStore specifically for this scope
handlers: [[myFx, mockHandler)]] // changed effect handler to mock one for this scope
});
// launching event or effect, which triggers the logic we want to test
// we doing it just in our forked scope
await allSettled(startEvent, {
scope,
params: // params of startEvent
})
// check states of stores in this scope after all calculations ended
expect(scope.getState($myStore)).toEqual(...)
})

after await next when I try to use serilog LogContext it doesn’t push any property to log messages

While setting everything for Serilog and SEQ I was stuck in an issue that you may have an answer for it.
I am trying to add some properties to all logs using LogContext.PushProperty. However, in my middleware(see image below) the LogContext can push property to logs before await next.Invoke(). While after await next when I try to use LogContext it doesn’t push any property to log messages.
The issue is that claims are always empty before the await next.Invoke() and they only have values after await next so I am forced to use LogContext after the await but it doesn’t work there as mentioned. Please advise if you have a clue?
Thanks,
LogContext needs to be used in conjunction with a using block, covering the whole scope in which you want the property to be available. You may need something like:
IDisposable popContext = null;
var user2 = context.User as IAMClaimsUser;
if (user2 != null && !string.IsNullOrEmpty(user2.FirstName))
{
popContext = LogContext.PushProperty("UserEmail", user2.Email);
}
using (popContext)
{
// `UserEmail` will be attached to events logged in this block
await next();
}

Using UserManager not working inside Timer

In my project I am trying to get a user based on it's email adress every second with the UserManager but when I do this I get the following error Cannot access a disposed object Object name: 'UserManager1, but this is when I do it inside of a Timer(). If I just do it once there is no problem, how can I fix this? This timer is inside a class that is being called by a SignalR Hub.
Code:
Timer = new System.Threading.Timer(async (e) =>
{
IEnumerable<Conversation> conversations = await _conversationsRepo.GetAllConversationsForUserEmailAsync(userMail);
List<TwilioConversation> twilioConversations = new List<TwilioConversation>();
foreach (Conversation conversation in conversations)
{
TwilioConversation twilioConversation = await _twilioService.GetConversation(conversation.TwilioConversationID);
twilioConversation.Messages = await _twilioService.GetMessagesForConversationAsync(conversation.TwilioConversationID);
twilioConversation.ParticipantNames = new List<string>();
List<TwilioParticipant> participants = await _twilioService.GetParticipantsForConversationAsync(conversation.TwilioConversationID);
foreach (TwilioParticipant participant in participants)
{
User user = await _userManager.FindByEmailAsync(participant.Email);
twilioConversation.ParticipantNames.Add(user.Name);
}
twilioConversations.Add(twilioConversation);
}
}, null, startTimeSpan, periodTimeSpan);
UserManager along with quite a few other types is a service that has a scoped lifetime. This means that they are only valid within the lifetime of a single request.
That also means that holding on to an instance for longer is not a safe thing to do. In this particular example, UserManager depends on the UserStore which has a dependency on a database connection – and those will definitely be closed when the request has been completed.
If you need to run something outside of the context of a request, for example in a background thread, or in your case in some timed execution, then you should create a service scope yourself and retrieve a fresh instance of the dependency you rely on.
To do that, inject a IServiceScopeFactory and then use that to create the scope within your timer code. This also applies to all other scoped dependencies, e.g. your repository which likely requires a database connection as well:
Timer = new System.Threading.Timer(async (e) =>
{
using (var scope = serviceScopeFactory.CreateScope())
{
var conversationsRepo = scope.ServiceProvider.GetService<ConversionsRepository>();
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();
// do stuff
}
}, null, startTimeSpan, periodTimeSpan);

Can't get SignalR client events published with Aurelia Event Aggregator

I have a single page app based on Aurelia and I'm trying to get it to work with an existing SignalR backend. I've downloaded the SignalR javascript client and integrated it with the Aurelia app manually (i.e. I'm not using a proxy file). I'm able to connect to the SignalR hub and see the arrvive messages in the console.... so far so good. Now, I'm trying to use the Aurelia Event Aggregator so that when a new hub message arrives an event is fired and any components of the app subscribed to that particular event will do some work. The issue is that the SignalR event callback doesn't seem to be able to access the Event Aggregator object. Here's the code to illustrate the issue:
//Import statements omitted for brevity
#inject (EventAggregator)
export class MyService{
constructor(eventAggregator) {
this.ea = eventAggregator;
this.connection = $.hubConnection("http://localhost:8080/signalr", { useDefaultPath: false });
this.hub = this.connection.createHubProxy("myHub");
//Register a callback function to fire when a new hub message arrives
this.hub.on("sendMessage", this.processHubMessage);
//No issues so far - all this constructor code works fine
}
processHubMessage(message) {
// This doesn't work - this.ea is undefined in the scope of this function
this.ea.publish('deviceStatusUpdate', message);
}
}
The event aggregator object referenced within the callback function is not defined - I assume because it's not being called within the scope of the class. Is there a way to resolve this? How do I give the callback function access to the class properties (this.ea in my example).
Try using
this.hub.on("sendMessage", (message) => this.processHubMessage(message));
It's failing on you due to how this isn't what you're expecting it to be. By using a fat arrow function, this is what you expect it to be. This is a really frustrating part of JavaScript, but fat arrows provide a simple workaround for it.
I think you are missing the 'start' for your Proxy, also you may need to alias your view model to pass to the HubProxy.
This works for me:
constructor(eventAggregator){
this.eventAggregator = eventAggregator;
var signalrAddress = 'https://pathToYouServer';
var hubName = 'yourHubsName';
var connection = $.hubConnection(signalrAddress);
var eventHubProxy = connection.createHubProxy(hubName);
var vm = this;
eventHubProxy.on('yourBroadcastMessage', function(data) {
vm.eventAggregator.publish(data);
});
connection.start();
}

Durandal KO binding when data is fetched in activate

A parameter governs what data is to be displayed. The parameter is retrieved from activationData in the activate method of the view model and used in a call to a Web Api method. Data is returned, and added to the view model like this
define(['durandal/app', 'knockout', 'moment'],
function (app, config, ko, moment) {
var vm = {
app: app
};
vm.activate = function (activationData) {
vm.ChecklistInstanceId = activationData.ChecklistInstanceId;
$.ajax({
url: "api/ChecklistInstance/" + vm.ChecklistInstanceId,
headers: { Authorization: "Session " + app.SessionToken() }
}).done(function (data) {
$.extend(vm, ko.mapping.fromJS(data));
});
};
return vm;
});
Inspecting the viewmodel immediately after it is extended reveals that it is decorated with observables exactly as expected. For example, vm.Caption() exists and returns the string I expect, and vm.Section() is an appropriately populated observable array, and so on down a fairly elaborate object graph.
The problem is the binding phase has already occurred, and at that time the view model lacks all the observables to which I'm trying to bind.
Two possible strategies suggest themselves:
obtain the parameter earlier
re-bind
I don't know how to do either of those things. Can anyone tell me how to re-organise my code to allow binding to parametrically fetched data?
A third possibility occurred to me:
define(['durandal/app', 'knockout', 'moment'],
function (app, config, ko, moment) {
var vm = {
app: app,
Caption: ko.observable(),
Section: ko.observableArray()
};
vm.activate = function (activationData) {
vm.ChecklistInstanceId = activationData.ChecklistInstanceId;
$.ajax({
url: "api/ChecklistInstance/" + vm.ChecklistInstanceId,
headers: { Authorization: "Session " + app.SessionToken() }
}).done(function (data) {
var foo = ko.mapping.fromJS(data);
vm.Caption(foo.Caption());
vm.Section(foo.Section());
});
};
return vm;
});
This works because all the observables exist in the binding phase. This might seem surprising given that I describe only the root of a potentially deep object graph, but the fact that the observable array is empty causes the binding phase to exit without a hitch.
Later in the activate handler, values are added to the observable array after ko.mapping has its way with the data, and binding succeeds.
I have a sense of dèja vu from this: it is eerily reminiscent of problems solved using forward declarations in TurboPascal back in the eighties. La plus ça change...
In order to work on a fully-constructed view, you will need to move your logic to either the attached handler or the compositionComplete handler. As you said, at the activate stage, the DOM isn't yet fully constructed. You can read about these lifecycle callbacks here.
Typically, what we do is pass the activationData in through the activate handler, store the activationData locally (if your viewModel is instance-based, then on a property in the constructor), and then reference that activationData in the attached or the compositionComplete handler.
You can fetch the data in the activate handler, and then store the data locally. But that's all you should do there. Reserve view-related logic for later in the cycle. In this case, you may need to return a promise from activate, and then resolve upon receiving your data. You can read about it here.
UPDATE
Take a look at this post, and the conversation there.