Skip to content

Commit

Permalink
Minor improvements in typings and "use client"
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Jul 26, 2023
1 parent 3b47f9e commit d2cde3f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
8 changes: 3 additions & 5 deletions packages/validation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import Configuration from "./configuration";
import type { AggregatedValidationResult, EntityValidator } from "./types";

export function attachValidator<TEntity>(target: TEntity, validator: EntityValidator<TEntity>) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(target as any)[Configuration.validatorAttachedProperty] = validator;
(target as Record<symbol, EntityValidator<TEntity>>)[Configuration.validatorAttachedProperty] = validator;
}

export function getValidator<TEntity>(target: TEntity) {
export function getValidator<TEntity>(target: TEntity): EntityValidator<TEntity> | undefined {
if (!target) {
return undefined;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return (target as any)[Configuration.validatorAttachedProperty] as EntityValidator<TEntity> | undefined;
return (target as Record<symbol, EntityValidator<TEntity> | undefined>)[Configuration.validatorAttachedProperty];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/views/src/hooks/useDisposable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { useEffect } from "react";
import type { IDisposable } from "@frui.ts/helpers";

Expand Down
10 changes: 8 additions & 2 deletions packages/views/src/hooks/useViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
"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<TContext, TViewModel extends IViewModel<TContext>>(
factory: () => TViewModel,
context: TContext,
dependencies?: DependencyList
) {
const vmManager = useRef(new ViewModelLifecycleManager(factory));
const initializedPromise = useRef(new ManualPromise<true>());

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);
Expand All @@ -25,5 +31,5 @@ export function useViewModel<TContext, TViewModel extends IViewModel<TContext>>(
void vmManager.current.navigate(currentContext.current);
}, dependencies ?? [context]);

return vmManager.current.instance;
return { vm: vmManager.current.instance, initialized: initializedPromise.current.promise };
}
3 changes: 2 additions & 1 deletion packages/views/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 2 additions & 0 deletions packages/views/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export interface IViewModel<TContext> {
onNavigate?(context: TContext): Promise<unknown> | unknown;
onSearchChanged?(context: TContext): Promise<unknown> | unknown;
onDeactivate?(context: TContext): Promise<unknown> | unknown;

isVm?: true;
}

0 comments on commit d2cde3f

Please sign in to comment.