-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve lit-ts example with shared Component class (#1995)
- Loading branch information
Showing
5 changed files
with
113 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Machine, StateFrom } from "@zag-js/core" | ||
import { LitElement } from "lit" | ||
import { state } from "lit/decorators.js" | ||
|
||
interface ComponentInterface<Api, Service> { | ||
api: Api | ||
service: ReturnType<any> | ||
state: StateFrom<Service> | ||
} | ||
|
||
export abstract class Component<Context, Api, Service> extends LitElement implements ComponentInterface<Api, Service> { | ||
api: Api | ||
service: ReturnType<any> | ||
|
||
@state() | ||
state: StateFrom<Service> | ||
|
||
abstract initService(context: Context): Machine<any, any, any> | ||
abstract initApi(): Api | ||
|
||
constructor(context: Context) { | ||
super() | ||
this.service = this.initService(context) | ||
this.service._created() | ||
|
||
this.state = this.service.getState() | ||
this.api = this.initApi() | ||
|
||
this.service.subscribe((state: StateFrom<Service>) => { | ||
this.state = state | ||
this.api = this.initApi() | ||
}) | ||
} | ||
|
||
override connectedCallback(): void { | ||
super.connectedCallback() | ||
this.service.start() | ||
} | ||
|
||
override disconnectedCallback(): void { | ||
super.disconnectedCallback() | ||
this.service.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters