( raw: ComponentWatchOptionItem, ctx: Data, publicThis: ComponentPublicInstance, key: string, )
| 847 | } |
| 848 | |
| 849 | export function createWatcher( |
| 850 | raw: ComponentWatchOptionItem, |
| 851 | ctx: Data, |
| 852 | publicThis: ComponentPublicInstance, |
| 853 | key: string, |
| 854 | ): void { |
| 855 | let getter = key.includes('.') |
| 856 | ? createPathGetter(publicThis, key) |
| 857 | : () => publicThis[key as keyof typeof publicThis] |
| 858 | |
| 859 | const options: WatchOptions = {} |
| 860 | if (__COMPAT__) { |
| 861 | const instance = |
| 862 | currentInstance && getCurrentScope() === currentInstance.scope |
| 863 | ? currentInstance |
| 864 | : null |
| 865 | |
| 866 | const newValue = getter() |
| 867 | if ( |
| 868 | isArray(newValue) && |
| 869 | isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance) |
| 870 | ) { |
| 871 | options.deep = true |
| 872 | } |
| 873 | |
| 874 | const baseGetter = getter |
| 875 | getter = () => { |
| 876 | const val = baseGetter() |
| 877 | if ( |
| 878 | isArray(val) && |
| 879 | checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance) |
| 880 | ) { |
| 881 | traverse(val) |
| 882 | } |
| 883 | return val |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (isString(raw)) { |
| 888 | const handler = ctx[raw] |
| 889 | if (isFunction(handler)) { |
| 890 | if (__COMPAT__) { |
| 891 | watch(getter, handler as WatchCallback, options) |
| 892 | } else { |
| 893 | watch(getter, handler as WatchCallback) |
| 894 | } |
| 895 | } else if (__DEV__) { |
| 896 | warn(`Invalid watch handler specified by key "${raw}"`, handler) |
| 897 | } |
| 898 | } else if (isFunction(raw)) { |
| 899 | if (__COMPAT__) { |
| 900 | watch(getter, raw.bind(publicThis), options) |
| 901 | } else { |
| 902 | watch(getter, raw.bind(publicThis)) |
| 903 | } |
| 904 | } else if (isObject(raw)) { |
| 905 | if (isArray(raw)) { |
| 906 | raw.forEach(r => createWatcher(r, ctx, publicThis, key)) |
no test coverage detected