@@ -44,7 +44,7 @@ export default class Page<T> {
4444
4545 public beforeHide : ( ) => Promise < void > ;
4646 public afterHide : ( ) => Promise < void > ;
47- public beforeShow : ( options : Options < T > ) => Promise < void > ;
47+ protected _beforeShow : ( options : Options < T > ) => Promise < void > ;
4848 public afterShow : ( ) => Promise < void > ;
4949
5050 constructor ( props : PageProperties < T > ) {
@@ -54,36 +54,41 @@ export default class Page<T> {
5454 this . pathname = props . path ;
5555 this . beforeHide = props . beforeHide ?? empty ;
5656 this . afterHide = props . afterHide ?? empty ;
57- this . beforeShow = props . beforeShow ?? empty ;
57+ this . _beforeShow = props . beforeShow ?? empty ;
5858 this . afterShow = props . afterShow ?? empty ;
5959 }
60+
61+ public async beforeShow ( options : Options < T > ) : Promise < void > {
62+ await this . _beforeShow ?.( options ) ;
63+ }
6064}
6165
66+ type OptionsWithUrlParams < T , U extends UrlParamsSchema > = Options < T > & {
67+ urlParams ?: z . infer < U > ;
68+ } ;
69+
6270type UrlParamsSchema = z . ZodObject < Record < string , z . ZodTypeAny > > ;
63- type PagePropertiesWithUrlParams <
64- T ,
65- U extends UrlParamsSchema
66- > = PageProperties < T > & {
67- urlParams : {
68- schema : U ;
69- onUrlParamUpdate ?: ( params : z . infer < U > | null ) => void ;
70- } ;
71+ type PagePropertiesWithUrlParams < T , U extends UrlParamsSchema > = Omit <
72+ PageProperties < T > ,
73+ "beforeShow"
74+ > & {
75+ urlParamsSchema : U ;
76+ beforeShow ?: ( options : OptionsWithUrlParams < T , U > ) => Promise < void > ;
7177} ;
7278
7379export class PageWithUrlParams < T , U extends UrlParamsSchema > extends Page < T > {
7480 private urlSchema : U ;
75- private onUrlParamUpdate ?: ( params : z . infer < U > | null ) => void ;
81+ protected override _beforeShow : (
82+ options : OptionsWithUrlParams < T , U >
83+ ) => Promise < void > ;
7684
7785 constructor ( props : PagePropertiesWithUrlParams < T , U > ) {
7886 super ( props ) ;
79- this . urlSchema = props . urlParams . schema ;
80- this . onUrlParamUpdate = props . urlParams . onUrlParamUpdate ;
87+ this . urlSchema = props . urlParamsSchema ;
88+ this . _beforeShow = props . beforeShow ?? empty ;
8189 }
8290
83- public readUrlParams ( ) : void {
84- if ( this . onUrlParamUpdate === undefined ) {
85- return ;
86- }
91+ private readUrlParams ( ) : z . infer < U > | undefined {
8792 const urlParams = new URLSearchParams ( window . location . search ) ;
8893
8994 const parsed = parseUrlSearchParams ( {
@@ -92,12 +97,11 @@ export class PageWithUrlParams<T, U extends UrlParamsSchema> extends Page<T> {
9297 } ) ;
9398
9499 if ( ! parsed . success ) {
95- this . onUrlParamUpdate ?.( null ) ;
96- return ;
100+ return undefined ;
97101 }
98-
99- this . onUrlParamUpdate ?.( parsed . data ) ;
102+ return parsed . data ;
100103 }
104+
101105 public setUrlParams ( params : z . infer < U > ) : void {
102106 const urlParams = serializeUrlSearchParams ( {
103107 schema : this . urlSchema ,
@@ -106,4 +110,9 @@ export class PageWithUrlParams<T, U extends UrlParamsSchema> extends Page<T> {
106110 const newUrl = `${ window . location . pathname } ?${ urlParams . toString ( ) } ` ;
107111 window . history . replaceState ( { } , "" , newUrl ) ;
108112 }
113+
114+ public override async beforeShow ( options : Options < T > ) : Promise < void > {
115+ const urlParams = this . readUrlParams ( ) ;
116+ await this . _beforeShow ?.( { ...options , urlParams : urlParams } ) ;
117+ }
109118}
0 commit comments