File tree Expand file tree Collapse file tree
packages/labs/observers/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @lit-labs/observers ' : minor
3+ ---
4+
5+ Add unobserve method to ` ResizeController ` and ` IntersectionController ` .
Original file line number Diff line number Diff line change @@ -155,6 +155,15 @@ export class IntersectionController<T = unknown> implements ReactiveController {
155155 this . _unobservedUpdate = true ;
156156 }
157157
158+ /**
159+ * Unobserve the target element.
160+ * @param target Element to unobserve
161+ */
162+ unobserve ( target : Element ) {
163+ this . _targets . delete ( target ) ;
164+ this . _observer . unobserve ( target ) ;
165+ }
166+
158167 /**
159168 * Disconnects the observer. This is done automatically when the host
160169 * disconnects.
Original file line number Diff line number Diff line change @@ -147,6 +147,15 @@ export class ResizeController<T = unknown> implements ReactiveController {
147147 this . _host . requestUpdate ( ) ;
148148 }
149149
150+ /**
151+ * Unobserve the target element.
152+ * @param target Element to unobserve
153+ */
154+ unobserve ( target : Element ) {
155+ this . _targets . delete ( target ) ;
156+ this . _observer . unobserve ( target ) ;
157+ }
158+
150159 /**
151160 * Disconnects the observer. This is done automatically when the host
152161 * disconnects.
Original file line number Diff line number Diff line change @@ -531,4 +531,31 @@ const canTest = () => {
531531 // @ts -expect-error Type 'number' is not assignable to type 'string'
532532 D . value = 3 ;
533533 } ) ;
534+
535+ test ( 'observed target can be unobserved' , async ( ) => {
536+ const el = await getTestElement ( ( ) => ( { target : null } ) ) ;
537+ const d1 = document . createElement ( 'div' ) ;
538+
539+ // Reports initial changes when observe called.
540+ el . observer . observe ( d1 ) ;
541+ el . renderRoot . appendChild ( d1 ) ;
542+ await intersectionComplete ( ) ;
543+ assert . isTrue ( el . observerValue ) ;
544+ el . resetObserverValue ( ) ;
545+ await intersectionComplete ( ) ;
546+
547+ // Does not report change when unobserved
548+ el . observer . unobserve ( d1 ) ;
549+ intersectOut ( d1 ) ;
550+ await intersectionComplete ( ) ;
551+ assert . isUndefined ( el . observerValue ) ;
552+
553+ el . remove ( ) ;
554+ container . appendChild ( el ) ;
555+
556+ // Does not report changes when re-connected
557+ intersectIn ( d1 ) ;
558+ await intersectionComplete ( ) ;
559+ assert . isUndefined ( el . observerValue ) ;
560+ } ) ;
534561} ) ;
Original file line number Diff line number Diff line change @@ -500,4 +500,31 @@ if (DEV_MODE) {
500500 // @ts -expect-error Type 'number' is not assignable to type 'string'
501501 D . value = 3 ;
502502 } ) ;
503+
504+ test ( 'observed target can be unobserved' , async ( ) => {
505+ const el = await getTestElement ( ( ) => ( { target : null } ) ) ;
506+ const d1 = document . createElement ( 'div' ) ;
507+
508+ // Reports initial changes when observe called.
509+ el . observer . observe ( d1 ) ;
510+ el . renderRoot . appendChild ( d1 ) ;
511+ await resizeComplete ( ) ;
512+ assert . isTrue ( el . observerValue ) ;
513+ el . resetObserverValue ( ) ;
514+ await resizeComplete ( ) ;
515+
516+ // Does not report change when unobserved
517+ el . observer . unobserve ( d1 ) ;
518+ resizeElement ( d1 ) ;
519+ await resizeComplete ( ) ;
520+ assert . isUndefined ( el . observerValue ) ;
521+
522+ el . remove ( ) ;
523+ container . appendChild ( el ) ;
524+
525+ // Does not report changes when re-connected
526+ resizeElement ( d1 ) ;
527+ await resizeComplete ( ) ;
528+ assert . isUndefined ( el . observerValue ) ;
529+ } ) ;
503530} ) ;
You can’t perform that action at this time.
0 commit comments