Skip to content

Commit 5c7a017

Browse files
authored
on destroy save state (#102)
* on destroy save state * bump version
1 parent b3683bb commit 5c7a017

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

projects/table-builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mx-table-builder",
3-
"version": "0.4.11",
3+
"version": "0.4.12",
44
"peerDependencies": {
55
"@angular/common": "~11.0.0",
66
"@angular/core": "~11.0.0",

projects/table-builder/src/lib/classes/table-store.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Sort, SortDirection } from '@angular/material/sort' ;
99
import { ComponentStore } from '@ngrx/component-store' ;
1010
import update from 'immutability-helper';
1111
import { Dictionary } from '../interfaces/dictionary';
12-
import { map, tap } from 'rxjs/operators'
12+
import { last, map, tap } from 'rxjs/operators'
1313
import { moveItemInArray } from '@angular/cdk/drag-drop';
1414

1515
@Injectable({
@@ -31,10 +31,15 @@ export class TableStore extends ComponentStore<TableState> {
3131
);
3232
}
3333

34+
3435
on = <V>(srcObservable: Observable<V>, func: (obj: V) => void) => {
35-
this.effect((src: Observable<V>) => {
36-
return src.pipe(tap(func));
37-
})(srcObservable);
36+
this.effect(() => srcObservable.pipe(
37+
tap(func)
38+
))
39+
}
40+
41+
onLast(callback: (state: TableState) => void ) {
42+
this.on(this.state$.pipe(last()),callback);
3843
}
3944

4045
readonly filters$ = this.select(state => state.filters );

projects/table-builder/src/lib/components/table-container/table-container.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@angular/core';
1111
import { Observable, ReplaySubject } from 'rxjs';
1212
import { ArrayAdditional, FieldType, MetaData } from '../../interfaces/report-def';
13-
import { filter, first, map, shareReplay, switchMap, tap, withLatestFrom } from 'rxjs/operators';
13+
import { filter, first, map, shareReplay, switchMap, withLatestFrom } from 'rxjs/operators';
1414
import { TableBuilder } from '../../classes/table-builder';
1515
import { MatRowDef } from '@angular/material/table';
1616
import { CustomCellDirective } from '../../directives';
@@ -69,22 +69,18 @@ import { sortData } from '../../functions/sort-data-function';
6969
@Inject(TableBuilderConfigToken) private config: TableBuilderConfig,
7070
private store: Store<{globalStorageState: GlobalStorageState}>
7171
) {
72+
this.state.onLast( finalState => {
73+
if(this.tableId) {
74+
this.store.dispatch(setLocalProfile({key:this.tableId,value: finalState}));
75+
}
76+
});
7277
}
7378

7479
ngOnInit() {
7580
if(this.tableId) {
7681
this.state.updateState(
7782
this.store.pipe(
7883
select(selectors.selectLocalProfileState<PersistedTableState>(this.tableId)),
79-
tap( state => {
80-
// for backwards compatability we want to load the state from the old schema.
81-
if(!state) {
82-
const oldLocalState = localStorage.getItem(this.tableId);
83-
if(oldLocalState){
84-
this.store.dispatch(setLocalProfile({ key: this.tableId, value: JSON.parse( oldLocalState), persist: true} ));
85-
}
86-
}
87-
}),
8884
filter( state => !!state ),
8985
skipOneWhen(this.OnSaveState),
9086
)

src/app/app.module.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ import { LowerCasePipe } from '@angular/common';
2525
MatCheckboxModule,
2626
MatSelectModule,
2727
BrowserAnimationsModule,
28-
StoreModule.forRoot({}),
28+
StoreModule.forRoot({},{
29+
runtimeChecks: {
30+
strictStateImmutability: false,
31+
strictActionImmutability: false,
32+
strictStateSerializability: false,
33+
strictActionSerializability: false,
34+
strictActionWithinNgZone: false,
35+
strictActionTypeUniqueness: false,
36+
},
37+
}),
2938
EffectsModule.forRoot([]),
3039
TableBuilderModule.forRoot({
3140
defaultSettings: { dateFormat: 'short' },

0 commit comments

Comments
 (0)