Skip to content

Commit

Permalink
feat(core): using NGXS NgxsAfterBootstrap lifecycle hook
Browse files Browse the repository at this point in the history
using NGXS NgxsAfterBootstrap lifecycle hook instead of ngxsOnInit
  • Loading branch information
xmlking committed Feb 18, 2019
1 parent d372b51 commit db09fb2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
5 changes: 3 additions & 2 deletions libs/chat-box/src/lib/state/chat-box.store.ts
Expand Up @@ -2,6 +2,7 @@ import {
Action,
Actions,
createSelector,
NgxsAfterBootstrap,
NgxsOnInit,
ofActionDispatched,
Selector,
Expand Down Expand Up @@ -70,7 +71,7 @@ export class ChatBoxStateModel {
loading: false,
},
})
export class ChatBoxState implements NgxsOnInit {
export class ChatBoxState implements NgxsAfterBootstrap {
constructor(
private nlp: NlpService,
private stt: SpeechToTextService,
Expand Down Expand Up @@ -149,7 +150,7 @@ export class ChatBoxState implements NgxsOnInit {
return state.voiceForm.model;
}

async ngxsOnInit({ getState, setState, patchState, dispatch }: StateContext<ChatBoxStateModel>) {
async ngxsAfterBootstrap({ patchState, dispatch }: StateContext<ChatBoxStateModel>) {
console.log('ChatBoxState initialized, setting defaults');
if (this.tts.canUseSpeechSynthesis) {
const voices = await this.tts.getVoiceList();
Expand Down
10 changes: 5 additions & 5 deletions libs/navigator/src/lib/state/menu.state.ts
@@ -1,4 +1,4 @@
import { Action, NgxsOnInit, Selector, State, StateContext, Store } from '@ngxs/store';
import { Action, NgxsAfterBootstrap, NgxsOnInit, Selector, State, StateContext, Store } from '@ngxs/store';
import { MenuItem } from '../models/menu-item.model';
import { MenuService } from '../services/menu.service';
import { Tree } from '@ngx-starter-kit/tree';
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface MenuStateModel {
iconMode: false,
},
})
export class MenuState implements NgxsOnInit {
export class MenuState implements NgxsAfterBootstrap {
constructor(private menuService: MenuService) {}

@Selector()
Expand All @@ -55,7 +55,7 @@ export class MenuState implements NgxsOnInit {
}
}

ngxsOnInit({ setState, getState }: StateContext<MenuStateModel>) {
ngxsAfterBootstrap({ setState, getState }: StateContext<MenuStateModel>) {
setState({
tree: this.menuService.tree,
currentlyOpened: [],
Expand Down Expand Up @@ -100,7 +100,7 @@ export class MenuState implements NgxsOnInit {
}

patchState({
currentlyOpened: currentlyOpened,
currentlyOpened,
});
}

Expand All @@ -123,7 +123,7 @@ export class MenuState implements NgxsOnInit {
}

patchState({
currentlyOpened: currentlyOpened,
currentlyOpened,
});
}
}
3 changes: 3 additions & 0 deletions libs/notifications/src/lib/notifications.handler.ts
Expand Up @@ -26,6 +26,9 @@ export class NotificationsHandler {
this.store.dispatch(new AddNotification(new AppNotification(message.notification.data)));
}
});
this.swPush.notificationClicks.subscribe(({ action, notification: { title, ...notification } }) => {
console.log(`action: ${action}, title: ${title}, notification`, notification);
});
}
}
}
8 changes: 4 additions & 4 deletions libs/notifications/src/lib/notifications.state.ts
@@ -1,4 +1,4 @@
import { Action, NgxsOnInit, Selector, State, StateContext } from '@ngxs/store';
import { Action, NgxsAfterBootstrap, Selector, State, StateContext } from '@ngxs/store';
import { produce } from '@ngxs-labs/immer-adapter';
import { tap } from 'rxjs/operators';
import { AppNotification } from './app-notification.model';
Expand All @@ -16,19 +16,19 @@ import {
name: 'notifications',
defaults: [],
})
export class NotificationsState implements NgxsOnInit {
export class NotificationsState implements NgxsAfterBootstrap {
constructor(private notificationsService: NotificationsService) {}

@Selector()
static unReadCount(state: AppNotification[]) {
return state.filter(note => !note.read).length;
}

ngxsOnInit(ctx: StateContext<AppNotification[]>) {
ngxsAfterBootstrap(ctx: StateContext<AppNotification[]>) {
console.log('State initialized, now getting Notifications.');
/**
* well, this way, it will be called before dashboard is routed due to preloadingStrategy.
* we will loose lazy loading benefits. so lets use ngOnInit on component to load initial data.
* we will loose lazy loading benefits. so lets use ngxsAfterBootstrap on component to load initial data.
*/
// ctx.dispatch(new FetchNotifications())
}
Expand Down

0 comments on commit db09fb2

Please sign in to comment.