From d2cde3fa98c66e59a154152769e20c046b0431a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augustin=20=C5=A0ulc?= Date: Wed, 26 Jul 2023 14:51:42 +0200 Subject: [PATCH] Minor improvements in typings and "use client" --- packages/validation/src/utils.ts | 8 +++----- packages/views/src/hooks/useDisposable.ts | 2 ++ packages/views/src/hooks/useViewModel.ts | 10 ++++++++-- packages/views/src/index.ts | 3 ++- packages/views/src/types.ts | 2 ++ 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/validation/src/utils.ts b/packages/validation/src/utils.ts index 170dbeb5..e7e0f06f 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 ddbbf51f..cc091067 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 7d2e2d75..988cc236 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 7fd0be3c..56ac7525 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 e11ea2e6..f2daa84e 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; }