-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
useViewModelMemo
for factory alternative
It is difficult to differentiate between classes and functions in JS.
- Loading branch information
1 parent
3e1656d
commit e4910a3
Showing
10 changed files
with
67 additions
and
74 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 was deleted.
Oops, something went wrong.
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,31 @@ | ||
import type { INotifyPropertiesChanged } from '../events'; | ||
import { type DependencyList, useMemo } from 'react'; | ||
import { useViewModel } from './use-view-model'; | ||
|
||
/** Represents a view model factory callback. | ||
* @template TViewModel The type of view model to create. | ||
*/ | ||
export type ViewModelFactory<TViewModel extends INotifyPropertiesChanged> = () => TViewModel; | ||
|
||
/** Ensures a unique instance per component that is generated by the factory is created and watches the view model for changes. Returns the view model instance. | ||
* @template TViewModel The type of view model to create. | ||
* @param viewModelFactory The view model factory callback that initializes the instance. | ||
* @param deps Dependencies of the callback, whenever these change the callback is called again, similar to {@link useMemo}. | ||
* @param watchedProperties Optional, a render will be requested when only one of these properties has changed. | ||
*/ | ||
export function useViewModelMemo<TViewModel extends INotifyPropertiesChanged>(viewModelFactory: ViewModelFactory<TViewModel>, deps: DependencyList, watchedProperties?: readonly (keyof TViewModel)[]): TViewModel { | ||
const viewModel = useMemo(viewModelFactory, deps); | ||
useViewModel(viewModel, watchedProperties); | ||
|
||
return viewModel; | ||
} | ||
|
||
/** Ensures a unique instance per component that is generated by the factory is created and watches the view model for changes. Returns the view model instance. | ||
* @deprecated In future versions this hook will be removed, switch to {@link useViewModelMemo}. | ||
* @template TViewModel The type of view model to create. | ||
* @param viewModelFactory The view model factory callback that initializes the instance. | ||
* @param watchedProperties Optional, a render will be requested when only one of these properties has changed. | ||
*/ | ||
export function useViewModelFactory<TViewModel extends INotifyPropertiesChanged>(viewModelFactory: ViewModelFactory<TViewModel>, watchedProperties?: readonly (keyof TViewModel)[]): TViewModel { | ||
return useViewModelMemo(viewModelFactory, [], watchedProperties); | ||
} |
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
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