import { ArrayElementSelector, StateSubscriptionFunction, SubscribeStateFromElementOptions, SubscribeStateOptions } from './index.js'; import {deepCopy} from './litElementState.helpers.js'; export class LitElementStateSubscription { previousValue: SubscribedType | null = null; value: SubscribedType | null = null; valueDeepCopy: SubscribedType | null = null; path: readonly (string | ArrayElementSelector)[]; closed = false; private subscriptionFunction: StateSubscriptionFunction; private unsubscribeFunction: (subscription: LitElementStateSubscription) => void; subscriptionOptions: SubscribeStateOptions | SubscribeStateFromElementOptions; constructor( path: readonly (string | ArrayElementSelector)[], subscriptionFunction: StateSubscriptionFunction, unsubscriptionFunction: ( subscription: LitElementStateSubscription ) => void, subscriptionOptions?: SubscribeStateOptions ) { this.path = path; this.subscriptionFunction = subscriptionFunction; this.unsubscribeFunction = unsubscriptionFunction; this.subscriptionOptions = subscriptionOptions!; } next(value: SubscribedType, initial = false) { this.value = value; this.previousValue = this.valueDeepCopy; this.valueDeepCopy = deepCopy(value); if (!initial || this.subscriptionOptions.getInitialValue) { this.subscriptionFunction( { previous: this.previousValue, current: this.subscriptionOptions.getDeepCopy ? // We need to deepcopy again because we need to avoid that the user can edit the subscription's deep copy (it's needed for nested change comparisons) deepCopy(value) : this.value } ); } } unsubscribe() { this.unsubscribeFunction(this); this.closed = true; } }