diff --git a/src/core.ts b/src/core.ts index acb6c3a..c1de918 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,5 +1,6 @@ import { createReactiveSystem, Dependency, Link, Subscriber, SubscriberFlags } from 'alien-signals'; import { ReactiveFlags } from "./contents" +import { isFunction } from './utils'; const { link, propagate, @@ -238,9 +239,6 @@ export function unSignal(signal: MaybeSignal | Computed): T { return (isSignal(signal) ? signal.value : signal) as T; } -export const isFunction = (val: unknown): val is Function => - typeof val === 'function' - export function toValue(source: MaybeSignalOrGetter): T { return isFunction(source) ? source() : unSignal(source) } diff --git a/src/index.ts b/src/index.ts index 3b1191d..148516e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,16 @@ export * from "./core"; export * from "./deepSignal"; export * from "./watch" +export { + isArray, + isDate, + isFunction, + isMap, + isObject, + isPlainObject, + isPromise, + isRegExp, + isSet, + isString, + isSymbol, +} from "./utils" diff --git a/src/utils.ts b/src/utils.ts index 5c45714..6e927f8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,3 +37,5 @@ export const isPlainObject = (val: unknown): val is object => export const hasChanged = (value: any, oldValue: any): boolean => !Object.is(value, oldValue) + +export function NOOP() {} diff --git a/src/watch.ts b/src/watch.ts index 19d2a67..f4a3fb0 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -1,5 +1,5 @@ import { Computed, Effect, isSignal, Signal } from './core'; -import { hasChanged, isArray, isMap, isObject, isPlainObject, isSet } from './utils'; +import { hasChanged, isArray, isMap, isObject, isPlainObject, isSet, NOOP } from './utils'; import { isDeepSignal, isShallow } from "./deepSignal" import { ReactiveFlags } from './contents'; @@ -69,9 +69,7 @@ export function watch( getter = () => source.value forceTrigger = isShallow(source) } else if (isDeepSignal(source)) { - getter = () => { - return signalGetter(source) - } + getter = () => signalGetter(source) forceTrigger = true } else if (isArray(source)) { isMultiSource = true @@ -85,6 +83,14 @@ export function watch( } }) } + else { + getter = NOOP + if (process.env.NODE_ENV !== 'production') { + console.warn( + 'Invalid watch source. Source must be a signal, a computed value !', + ) + } + } if (cb && deep) { const baseGetter = getter const depth = deep === true ? Infinity : deep