2023-03-15 21:22:30 +00:00
|
|
|
import { noChange } from 'lit';
|
|
|
|
import { AsyncDirective, directive } from 'lit/async-directive.js';
|
|
|
|
import { rxjs } from '@pushrocks/smartrx';
|
|
|
|
|
2023-03-29 18:29:12 +00:00
|
|
|
class ResolveDirective extends AsyncDirective {
|
2023-03-15 21:22:30 +00:00
|
|
|
observable: rxjs.Observable<unknown> | undefined;
|
|
|
|
sub: rxjs.Subscription | null = null;
|
|
|
|
|
|
|
|
render(observable: rxjs.Observable<unknown>) {
|
|
|
|
if (this.observable !== observable) {
|
|
|
|
this.sub?.unsubscribe();
|
|
|
|
this.observable = observable;
|
|
|
|
|
|
|
|
if (this.isConnected) {
|
|
|
|
this.subscribe(observable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return noChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(observable: rxjs.Observable<unknown>) {
|
|
|
|
this.sub = observable.subscribe((v: unknown) => {
|
|
|
|
this.setValue(v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnected() {
|
|
|
|
this.sub?.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnected() {
|
|
|
|
this.subscribe(this.observable!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:29:12 +00:00
|
|
|
export const resolve = directive(ResolveDirective);
|