MCPcopy Create free account
hub / github.com/Xu22Web/tech-study-js / watch

Function watch

tech-study.js:1209–1262  ·  view source on GitHub ↗
(source, callback, immediate = false)

Source from the content-addressed store, hash-verified

1207 * @param callback
1208 */
1209const watch = (source, callback, immediate = false) => {
1210 // 立刻执行
1211 immediate && callback(unref(source), unref(source));
1212 // array
1213 if (Array.isArray(source) && source.every((s) => isRef(s))) {
1214 for (const i in source) {
1215 // Proxy
1216 if (isProxy(source[i])) {
1217 watch(source[i], () => {
1218 const res = source.map((s) => unref(s));
1219 callback(res, res);
1220 });
1221 }
1222 }
1223 watch(() => source.map((s) => unref(s)), callback);
1224 return;
1225 }
1226 // function
1227 if (source instanceof Function) {
1228 watch(watchEffectRef(source), (n, o) => {
1229 callback(unref(n), unref(o));
1230 });
1231 return;
1232 }
1233 // Proxy
1234 if (isProxy(source)) {
1235 for (const key in source) {
1236 currentSub = () => {
1237 callback(source, source);
1238 };
1239 // sub source
1240 const subSource = source[key];
1241 currentSub = undefined;
1242 watch(subSource, () => {
1243 callback(source, source);
1244 });
1245 }
1246 return;
1247 }
1248 // Ref
1249 if (isRef(source)) {
1250 // Ref.value Proxy
1251 if (isProxy(source.value)) {
1252 watch(source.value, () => {
1253 callback(unref(source), unref(source));
1254 });
1255 }
1256 currentSub = callback;
1257 // 收集依赖
1258 trackRef(source);
1259 currentSub = undefined;
1260 return;
1261 }
1262};
1263/**
1264 * @description 监听数据变化影响
1265 * @param callback

Callers 9

initThemeColorFunction · 0.70
watchRefFunction · 0.70
subscribeFunction · 0.70
handleAttributeFunction · 0.70
handleChildrenFunction · 0.70
SelectFunction · 0.70
onMountedFunction · 0.70
TaskListFunction · 0.70
TaskBtnFunction · 0.70

Calls 5

unrefFunction · 0.70
isRefFunction · 0.70
isProxyFunction · 0.70
watchEffectRefFunction · 0.70
trackRefFunction · 0.70

Tested by

no test coverage detected