feature-libs/cart/quick-order/core/services/quick-order-state-persistance.service.ts
Properties |
|
Methods |
|
constructor(quickOrderService: QuickOrderFacade, siteContextParamsService: SiteContextParamsService, statePersistenceService: StatePersistenceService)
|
||||||||||||
|
Parameters :
|
| initSync |
initSync()
|
|
Initializes the synchronization between state and browser storage.
Returns :
void
|
| ngOnDestroy |
ngOnDestroy()
|
|
Returns :
void
|
| Protected onRead | ||||||
onRead(state: OrderEntry[] | undefined)
|
||||||
|
Parameters :
Returns :
void
|
| Protected key |
Type : string
|
Default value : 'quick-order'
|
|
Identifier used for storage key. |
| Protected subscription |
Default value : new Subscription()
|
import { Injectable, OnDestroy } from '@angular/core';
import { QuickOrderFacade } from '@spartacus/cart/quick-order/root';
import {
BASE_SITE_CONTEXT_ID,
OrderEntry,
SiteContextParamsService,
StatePersistenceService,
StorageSyncType,
} from '@spartacus/core';
import { Subscription } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class QuickOrderStatePersistenceService implements OnDestroy {
protected subscription = new Subscription();
constructor(
protected quickOrderService: QuickOrderFacade,
protected siteContextParamsService: SiteContextParamsService,
protected statePersistenceService: StatePersistenceService
) {}
/**
* Identifier used for storage key.
*/
protected key = 'quick-order';
/**
* Initializes the synchronization between state and browser storage.
*/
initSync(): void {
this.subscription.add(
this.statePersistenceService.syncWithStorage({
key: this.key,
state$: this.quickOrderService.getEntries(),
context$: this.siteContextParamsService.getValues([
BASE_SITE_CONTEXT_ID,
]),
storageType: StorageSyncType.SESSION_STORAGE,
onRead: (state) => this.onRead(state),
})
);
}
protected onRead(state: OrderEntry[] | undefined): void {
if (state) {
this.quickOrderService.loadEntries(state);
}
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}