Creating a single event handler service


#1

I am currently using ionic 4. Currently, I have separate functions for handling login events, internet status (connect/disconnect) events, and as I go on developing the number of events would also increase. So I was wondering, if it a good idea to create a separate provider for handling all the events?


#2

My initial inclination would be “no”, and in fact I don’t like Ionic’s events at all.

Exactly what you mean hinges on the meaning of “handling”, but in general I try to look at everything from the point of view of clients: objects that are using these service providers. Would a client always want to care about both login and network status? I wouldn’t think so. My app component might want to redirect to a login page on logout, but it wouldn’t do anything on network connect/disconnect. A notification watcher might toggle an icon in a status bar when the network goes up and down, but doesn’t give a fig about authentication. So I would keep those services separate, so everybody gets only what they want.


#3

Yeah, I read your comment on why you don’t like Ionic events. The typo error is indeed very hard to find. I am dynamically generating side menu bars by publishing the events when a user is logged in or out and I want to store any api request when app is offline and sync it when the app is online, I think i don’t need to publish any events for this. I am new to the concept of observable,I guess I will take a look at it and see. But you could you tell me about the why observable would be great choice over events


#4

It’s covered in the other post: the typo problem goes away and you also get free documentation and enforcement of typing. With events, you have no clue what any parameters are. Observables emit things of defined types.

You should also look into Ionic Storage, if you haven’t already. Using it to store your queue of requests will make it so that if the user does stuff while offline, quits the app, restarts it, and then comes back online, the queue will still be available.