Skip to content

Commit 7692d08

Browse files
committed
feat: type guards isViewModel isViewModelClass; fix: useViewModel
1 parent 6aa0f3f commit 7692d08

File tree

8 files changed

+30
-3
lines changed

8 files changed

+30
-3
lines changed

.changeset/clear-forks-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-view-model": minor
3+
---
4+
5+
added type guards `isViewModel` and `isViewModelClass`

.changeset/clever-times-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-view-model": patch
3+
---
4+
5+
fix missing throwing exception in `useViewModel` hook

src/hooks/use-create-view-model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ViewModelsContext } from '../contexts/view-models-context.js';
1313
import { useIsomorphicLayoutEffect } from '../lib/hooks/use-isomorphic-layout-effect.js';
1414
import { useValue } from '../lib/hooks/use-value.js';
1515
import { generateVmId } from '../utils/generate-vm-id.js';
16+
import { isViewModelClass } from '../utils/typeguards.js';
1617
import { ViewModelSimple } from '../view-model/view-model-simple.js';
1718
import { ViewModelCreateConfig } from '../view-model/view-model.store.types.js';
1819
import {
@@ -83,7 +84,7 @@ export function useCreateViewModel(
8384
payload?: any,
8485
config?: any,
8586
) {
86-
if ('payloadChanged' in VM.prototype) {
87+
if (isViewModelClass(VM)) {
8788
// scenario for ViewModelBase
8889
return useCreateViewModelBase(VM, payload, config);
8990
}

src/hooks/use-view-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const useViewModel = <T extends AnyViewModel | AnyViewModelSimple>(
4141

4242
if (!activeViewModel) {
4343
if (process.env.NODE_ENV !== 'production') {
44-
console.error(
44+
throw new Error(
4545
'Active ViewModel not found.\n' +
4646
'This happened because "vmLookup" for hook "useViewModel" is not provided and hook trying to lookup active view model using ActiveViewModelContext which works only with using "withViewModel" HOC.\n' +
4747
'Please provide "vmLookup" (first argument for "useViewModel" hook) or use "withViewModel" HOC.\n' +

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export * from './hoc/index.js';
55
export * from './hooks/index.js';
66
export * from './config/index.js';
77
export * from './components/index.js';
8+
export * from './utils/index.js';
89

910
export const ViewModelsProvider = ViewModelsContext.Provider;

src/utils/generate-vm-id.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { createCounter } from 'yummies/complex';
22

3+
import { GenerateViewModelIdFn } from '../config/index.js';
4+
35
import { AnyObject } from './types.js';
46

57
declare const process: { env: { NODE_ENV?: string } };
68

79
const staticCounter = createCounter((counter) => counter.toString(16));
810

9-
export const generateVmId = (ctx: AnyObject) => {
11+
export const generateVmId: GenerateViewModelIdFn = (ctx: AnyObject) => {
1012
if (!ctx.generateId) {
1113
const staticId = staticCounter();
1214
const counter = createCounter((counter) =>

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './generate-vm-id.js';
2+
export * from './typeguards.js';

src/utils/typeguards.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable @typescript-eslint/ban-types */
2+
import { ViewModel } from '../view-model/view-model.js';
3+
4+
import { AnyObject, Class, EmptyObject } from './types.js';
5+
6+
export const isViewModel = <TPayload extends AnyObject = EmptyObject>(
7+
value: AnyObject,
8+
): value is ViewModel<TPayload> => 'payloadChanged' in value;
9+
10+
export const isViewModelClass = <TPayload extends AnyObject = EmptyObject>(
11+
value: Function,
12+
): value is Class<ViewModel<TPayload>> => 'payloadChanged' in value.constructor;

0 commit comments

Comments
 (0)