dees-element/ts/dees-element.classes.dees-element.ts

68 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2022-03-16 13:46:51 +00:00
import * as plugins from './dees-element.plugins.js';
2021-03-27 16:52:06 +00:00
2021-11-27 16:07:33 +00:00
export class DeesElement extends plugins.lit.LitElement {
2021-03-27 16:52:06 +00:00
// INSTANCE
2021-11-27 16:07:33 +00:00
@plugins.lit.property({ type: Boolean })
2021-03-27 16:52:06 +00:00
public goBright: boolean = false;
// domtools
2022-04-21 21:15:14 +00:00
public domtoolsPromise: Promise<plugins.domtools.DomTools>;
2021-03-27 16:52:06 +00:00
2021-11-27 16:07:33 +00:00
@plugins.lit.property()
2021-03-27 16:52:06 +00:00
domtools?: plugins.domtools.DomTools;
2023-03-26 21:59:12 +00:00
public rxSubscriptions: plugins.smartrx.rxjs.Subscription[] = [];
2021-03-27 16:52:06 +00:00
private themeSubscription: plugins.smartrx.rxjs.Subscription;
2022-01-07 18:47:05 +00:00
private elementDomReadyDeferred = plugins.domtools.plugins.smartpromise.defer();
public elementDomReady = this.elementDomReadyDeferred.promise;
2022-04-21 21:15:14 +00:00
constructor(optionsArg: plugins.domtools.IDomToolsContructorOptions = {}) {
2021-03-27 16:52:06 +00:00
super();
2022-04-21 21:15:14 +00:00
this.domtoolsPromise = plugins.domtools.elementBasic.setup(this, optionsArg);
2021-03-27 16:52:06 +00:00
this.domtoolsPromise.then((domtoolsArg) => {
this.domtools = domtoolsArg;
});
}
2022-01-06 20:53:21 +00:00
public async connectedCallback() {
2021-03-27 16:52:06 +00:00
super.connectedCallback();
2022-01-06 21:08:08 +00:00
const domtools = await this.domtoolsPromise;
this.themeSubscription = domtools.themeManager.themeObservable.subscribe((goBrightArg) => {
this.goBright = goBrightArg;
2022-03-16 14:00:10 +00:00
});
2023-03-26 21:59:12 +00:00
this.rxSubscriptions.push(this.themeSubscription);
2024-02-05 11:35:32 +00:00
for (const startupFunction of this.startupFunctions) {
await startupFunction();
}
2021-03-27 16:52:06 +00:00
this.dispatchEvent(new CustomEvent('deesElementConnected'));
}
2022-01-07 18:47:05 +00:00
public firstUpdated(_changedProperties: Map<string | number | symbol, unknown>): void {
super.firstUpdated(_changedProperties);
this.elementDomReadyDeferred.resolve();
}
2024-02-05 11:35:32 +00:00
private startupFunctions: (() => void | Promise<any>)[] = [];
public registerStartupFunction(startupFunctionArg: () => void) {
this.startupFunctions.push(startupFunctionArg);
}
private garbageFunctions: (() => void | Promise<any>)[] = [];
2023-10-26 10:43:04 +00:00
public registerGarbageFunction(garbageFunctionArg: () => void) {
this.garbageFunctions.push(garbageFunctionArg);
}
2022-01-06 20:53:21 +00:00
public async disconnectedCallback() {
await this.domtoolsPromise;
2021-03-27 16:52:06 +00:00
super.disconnectedCallback();
2023-03-26 21:59:12 +00:00
for (const subscription of this.rxSubscriptions) {
subscription.unsubscribe();
}
2023-10-26 10:43:04 +00:00
for (const garbageFunction of this.garbageFunctions) {
2024-02-05 11:35:32 +00:00
await garbageFunction();
2023-10-26 10:43:04 +00:00
}
2021-03-27 16:52:06 +00:00
this.dispatchEvent(new CustomEvent('deesElementDisconnected'));
}
}