Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): broadcast event when rehydrate is done #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/ngrx-store-idb/src/lib/ngrx-store-idb.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export interface RehydrateActionPayload {
export const rehydrateInitAction = createAction('NgrxStoreIdb/Init');
export const rehydrateAction = createAction('NgrxStoreIdb/Rehydrate', props<RehydrateActionPayload>());
export const rehydrateErrorAction = createAction('NgrxStoreIdb/RehydrateError');
export const rehydrateDoneAction = createAction('NgrxStoreIdb/RehydrateDone');
45 changes: 43 additions & 2 deletions projects/ngrx-store-idb/src/lib/ngrx-store-idb.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { Action } from '@ngrx/store';
import { get, UseStore } from 'idb-keyval';
import { combineLatest, from, of } from 'rxjs';
import { catchError, map, mergeMap, tap } from 'rxjs/operators';
import { rehydrateAction, rehydrateErrorAction, rehydrateInitAction } from './ngrx-store-idb.actions';
import {
rehydrateAction,
rehydrateDoneAction,
rehydrateErrorAction,
rehydrateInitAction
} from './ngrx-store-idb.actions';
import { IDB_STORE, NgrxStoreIdbOptions, OPTIONS, SAVED_STATE_KEY, SAVED_VERSION_KEY } from './ngrx-store-idb.options';
import { NgrxStoreIdbService } from './ngrx-store-idb.service';

@Injectable({
providedIn: 'root',
Expand Down Expand Up @@ -58,7 +64,9 @@ export class RehydrateEffects implements OnInitEffects {
console.debug('NgrxStoreIdb: Loaded state from IndexedDB:', value);
}
}),
map(value => rehydrateAction({ rehydratedState: value })),
map(value => {
return rehydrateAction({ rehydratedState: value });
}),
catchError(err => {
console.error('NgrxStoreIdb: Error reading state from IndexedDB', err);
return of(rehydrateErrorAction());
Expand All @@ -68,10 +76,43 @@ export class RehydrateEffects implements OnInitEffects {
),
);

/**
* This effect is triggered after root store initialisation is successfully completed.
* It is used to dispatch sync effect of rehydration done
*/
rehydrateDoneOnRehydrate$ = createEffect(() =>
this.actions$.pipe(
ofType(rehydrateAction),
map(() => rehydrateDoneAction())
),
);

/**
* This effect is triggered after root store initialisation is completed with failure.
* It is used to dispatch sync effect of rehydration done
*/
rehydrateDoneOnRehydrateError$ = createEffect(() =>
this.actions$.pipe(
ofType(rehydrateErrorAction),
map(() => rehydrateDoneAction())
)
);

/**
* This effect is triggered after root store initialisation is done to broadcast sync event.
*/
broadcastSyncEventOnRehydrateDone$ = createEffect(() =>
this.actions$.pipe(
ofType(rehydrateDoneAction),
tap(() => this.service.broadcastSyncEvent(rehydrateDoneAction, true))
), {dispatch: false}
);

constructor(
private actions$: Actions,
@Inject(OPTIONS) private options: NgrxStoreIdbOptions,
@Inject(IDB_STORE) private idbStore: UseStore,
private service: NgrxStoreIdbService
) { }

ngrxOnInitEffects(): Action {
Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx-store-idb/src/lib/ngrx-store-idb.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NgrxStoreIdbService } from './ngrx-store-idb.service';

/**
* Import this module in your AppModule using forRoot() method to
* enable synchornisation of redux store with IndexedDB.
* enable synchronisation of redux store with IndexedDB.
*/
@NgModule({
declarations: [],
Expand Down