We have an angular project at work (to be developed, have not started yet). It's quite complex and has complicated data flows. Also, we have two kinds of users in our application. Manager and users. These users will see similar views but there are some different custom views for each. Manager will have access to more functionalities as you would imagine. In order to manage this quite big and complex application in a scalable way, we follow NX pattern. I.e. multiple apps within single repo. At the end, within single repo I have following applications.
Most of the development will be done in common
app and different views and customization will be done in both mngr-app
and user-app
respectively.
We, also, think of employing ngrx
in our application for state management. I've seen multiple examples and tutorials to how to do that. Everything is OK so far. This part is just for the background info.
Our problem starts after this. Our business team also wants us to develop both iOS and Android applications with webviews containing the web application (I forgot to mention it is a responsive web app). So, everything we have done will be shipped within a webview to mobile users. However, the business team, also, wants us to develop some custom native views for mobile applications.
Let's take a look at the following example: (this is from ngrx example)
When user clicks on Add Book to Collection
button, an action type of [Collection] Add Book
is dispatched to the store and an effect will take care of it as follows:
@Effect()
addBookToCollection$: Observable<Action> = this.actions$
.ofType(collection.ADD_BOOK)
.map((action: collection.AddBookAction) => action.payload)
.switchMap(book =>
this.db.insert('books', [ book ])
.map(() => new collection.AddBookSuccessAction(book))
.catch(() => of(new collection.AddBookFailAction(book)))
);
This is the normal flow of the web application.
Our business team wants us to build some sort of custom logic for mobile applications, so that when users navigates to this page in a mobile application (either iOS or Android), instead of adding a book to collection it'll open up a native page and user will take actions on that native page. What I mean by this is that they want web application to behave differently when present in a mobile application. I can achieve this with bunch of if(window.nativeFlag === true)
within web application. However, this is just a dirty hack we want to avoid. Since, we are using ngrx
and rxjs
we feel like this can be done with Observable
s of rxjs
and Action
s of ngrx
.
What we have tried so far is to expose store
and actions
object to DOM so that we can access it within mobile app.
constructor(private store: Store<fromRoot.State>, private actions$: Actions) {
window['store'] = this.store;
window['actions'] = this.actions$;
}
With this way, we can subscribe to [Collection] Add Book
event as follows
actions().ofType('[Collection] Add Book').subscribe(data => {
console.log(data)
})
and get notified when a book is added to the collection. However, web application still does what it does normally and we cannot override this behavior.
My question is how to subscribe to some ngrx
actions from mobile applications and cancel out web application behavior?
Edit
I've come up with a solution on my own, however any other solutions are appreciated and I'll give the bounty if you can come up with a better one.