Skip to content

Commit 96c468a

Browse files
committed
feat(lit/helper/context): loading
1 parent 09ec068 commit 96c468a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/lit-helper/src/directives/context.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ class GecutContextDirective<T> extends GecutAsyncDirective {
1414

1515
protected _$signal?: ContextSignal<T>;
1616
protected _$render?: (data: T) => unknown;
17+
protected _$loading?: () => unknown;
1718

18-
render(signalContext: ContextSignal<T>, render: (data: T) => unknown = (data) => String(data)): unknown {
19-
this.log.methodArgs?.('render', {signalContext, render});
19+
render(
20+
signalContext: ContextSignal<T>,
21+
render: (data: T) => unknown = (data) => data,
22+
loading?: () => unknown,
23+
): unknown {
24+
signalContext;
25+
this.log.methodArgs?.('render', {signalContext});
2026

2127
this._$render = render;
28+
this._$loading = loading;
2229
if (this._$signal !== signalContext) {
2330
// When the observable changes, unsubscribe to the old one and subscribe to the new one
2431
this.unsubscribe?.();
@@ -35,6 +42,10 @@ class GecutContextDirective<T> extends GecutAsyncDirective {
3542
subscribe(signalContext: ContextSignal<T>): void {
3643
this.log.method?.('subscribe');
3744

45+
if (this._$loading) {
46+
this.setValue(this._$loading());
47+
}
48+
3849
this.unsubscribe = signalContext.subscribe(
3950
(v) => {
4051
this.setValue(this._$render!(v));
@@ -46,7 +57,7 @@ class GecutContextDirective<T> extends GecutAsyncDirective {
4657
// When the directive is disconnected from the DOM, unsubscribe to ensure
4758
// the directive instance can be garbage collected
4859
override disconnected(): void {
49-
this.unsubscribe!();
60+
this.unsubscribe?.();
5061
}
5162
// If the subtree the directive is in was disconnected and subsequently
5263
// re-connected, re-subscribe to make the directive operable again

0 commit comments

Comments
 (0)