diff --git a/packages/validation/src/utils.ts b/packages/validation/src/utils.ts index 170dbeb..e7e0f06 100644 --- a/packages/validation/src/utils.ts +++ b/packages/validation/src/utils.ts @@ -4,17 +4,15 @@ import Configuration from "./configuration"; import type { AggregatedValidationResult, EntityValidator } from "./types"; export function attachValidator(target: TEntity, validator: EntityValidator) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - (target as any)[Configuration.validatorAttachedProperty] = validator; + (target as Record>)[Configuration.validatorAttachedProperty] = validator; } -export function getValidator(target: TEntity) { +export function getValidator(target: TEntity): EntityValidator | undefined { if (!target) { return undefined; } - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - return (target as any)[Configuration.validatorAttachedProperty] as EntityValidator | undefined; + return (target as Record | undefined>)[Configuration.validatorAttachedProperty]; } /** diff --git a/packages/views/src/hooks/useDisposable.ts b/packages/views/src/hooks/useDisposable.ts index ddbbf51..cc09106 100644 --- a/packages/views/src/hooks/useDisposable.ts +++ b/packages/views/src/hooks/useDisposable.ts @@ -1,3 +1,5 @@ +"use client"; + import { useEffect } from "react"; import type { IDisposable } from "@frui.ts/helpers"; diff --git a/packages/views/src/hooks/useViewModel.ts b/packages/views/src/hooks/useViewModel.ts index 7d2e2d7..988cc23 100644 --- a/packages/views/src/hooks/useViewModel.ts +++ b/packages/views/src/hooks/useViewModel.ts @@ -1,7 +1,10 @@ +"use client"; + import type { DependencyList } from "react"; import { useEffect, useRef } from "react"; import { ViewModelLifecycleManager } from "../helpers/viewModelLifecycleManager"; import type { IViewModel } from "../types"; +import { ManualPromise } from "@frui.ts/helpers"; export function useViewModel>( factory: () => TViewModel, @@ -9,12 +12,15 @@ export function useViewModel>( dependencies?: DependencyList ) { const vmManager = useRef(new ViewModelLifecycleManager(factory)); + const initializedPromise = useRef(new ManualPromise()); const currentContext = useRef(context); currentContext.current = context; useEffect(() => { - void vmManager.current.initialize(currentContext.current); + void vmManager.current + .initialize(currentContext.current) + .then(() => initializedPromise.current.status === "new" && initializedPromise.current.resolve(true)); return () => { void vmManager.current.close(currentContext.current); @@ -25,5 +31,5 @@ export function useViewModel>( void vmManager.current.navigate(currentContext.current); }, dependencies ?? [context]); - return vmManager.current.instance; + return { vm: vmManager.current.instance, initialized: initializedPromise.current.promise }; } diff --git a/packages/views/src/index.ts b/packages/views/src/index.ts index 7fd0be3..56ac752 100644 --- a/packages/views/src/index.ts +++ b/packages/views/src/index.ts @@ -5,11 +5,12 @@ export { default as assignDefaultProps } from "./helpers/assignDefaultProps"; export { default as createDataIdHandler } from "./helpers/dataIdHandler"; export { default as createMemoizedHandler } from "./helpers/memoizedDataHandler"; export { default as preventDefault } from "./helpers/preventDefault"; +export * from "./helpers/viewModelLifecycleManager"; +export * from "./hooks/useDisposable"; export * from "./hooks/useViewModel"; export * from "./router/router"; export * from "./router/types"; export * from "./types"; export * from "./view/helpers"; -export * from "./hooks/useDisposable"; export { default as View } from "./view/view"; export * from "./view/viewLocator"; diff --git a/packages/views/src/types.ts b/packages/views/src/types.ts index e11ea2e..f2daa84 100644 --- a/packages/views/src/types.ts +++ b/packages/views/src/types.ts @@ -14,4 +14,6 @@ export interface IViewModel { onNavigate?(context: TContext): Promise | unknown; onSearchChanged?(context: TContext): Promise | unknown; onDeactivate?(context: TContext): Promise | unknown; + + isVm?: true; }