fix(core): update

This commit is contained in:
Philipp Kunz 2023-09-04 14:56:57 +02:00
parent 9d072cde61
commit f2aa825f95
3 changed files with 33 additions and 14 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@design.estate/dees-element', name: '@design.estate/dees-element',
version: '2.0.25', version: '2.0.26',
description: 'a custom element class extending lit element class' description: 'a custom element class extending lit element class'
} }

View File

@ -1,36 +1,46 @@
import { noChange } from 'lit'; import { noChange } from 'lit';
import { AsyncDirective, directive } from 'lit/async-directive.js'; import { AsyncDirective, directive } from 'lit/async-directive.js';
import { rxjs } from '@push.rocks/smartrx';
class ResolveDirective extends AsyncDirective { class ResolveDirective extends AsyncDirective {
observable: rxjs.Observable<unknown> | undefined; promise: Promise<unknown> | undefined;
sub: rxjs.Subscription | null = null; hasPromiseSettled: boolean = false;
render(observable: rxjs.Observable<unknown>) { render(promise: Promise<unknown>) {
if (this.observable !== observable) { if (this.promise !== promise) {
this.sub?.unsubscribe(); this.promise = promise;
this.observable = observable;
if (this.isConnected) { if (this.isConnected) {
this.subscribe(observable); this.handlePromise(promise);
} }
} }
return noChange; return noChange;
} }
subscribe(observable: rxjs.Observable<unknown>) { handlePromise(promise: Promise<unknown>) {
this.sub = observable.subscribe((v: unknown) => { this.hasPromiseSettled = false;
this.setValue(v);
promise.then((value) => {
if (this.promise === promise && !this.hasPromiseSettled) {
this.setValue(value);
this.hasPromiseSettled = true;
}
}).catch((error) => {
if (this.promise === promise && !this.hasPromiseSettled) {
this.setValue(error);
this.hasPromiseSettled = true;
}
}); });
} }
disconnected() { disconnected() {
this.sub?.unsubscribe(); this.hasPromiseSettled = true; // prevent setting value if the promise settles after disconnection
} }
reconnected() { reconnected() {
this.subscribe(this.observable!); if (!this.hasPromiseSettled) {
this.handlePromise(this.promise!);
}
} }
} }

View File

@ -24,3 +24,12 @@ export * from './dees-element.classes.resolvedirective.js';
* a singleton instance of CssManager * a singleton instance of CssManager
*/ */
export const cssManager = new CssManager(); export const cssManager = new CssManager();
// better scoped exports
import { resolve } from './dees-element.classes.resolvedirective.js';
import { subscribe } from './dees-element.classes.subscribedirective.js';
export const directives = {
resolve,
subscribe,
}