Skip to content

Commit a6b0b6f

Browse files
committed
fix: issue #58
1 parent b63684a commit a6b0b6f

File tree

7 files changed

+77
-11
lines changed

7 files changed

+77
-11
lines changed

demo/computed/behavior.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/behavior.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface ComputedWatchInfo {
2222
computedUpdaters: Array<any>;
2323
computedRelatedPathValues: Record<string, any>;
2424
watchCurVal: Record<string, any>;
25+
_triggerFromComputedAttached: Record<string, boolean>;
2526
}
2627

2728
let computedWatchDefIdInc = 0;
@@ -55,6 +56,7 @@ export const behavior = Behavior({
5556
computedUpdaters: [],
5657
computedRelatedPathValues: {},
5758
watchCurVal: {},
59+
_triggerFromComputedAttached: {},
5860
};
5961
if (!this._computedWatchInfo) this._computedWatchInfo = {};
6062
this._computedWatchInfo[computedWatchDefId] = computedWatchInfo;
@@ -94,7 +96,7 @@ export const behavior = Behavior({
9496
this.setData({
9597
[targetField]: dataTracer.unwrap(val),
9698
});
97-
99+
computedWatchInfo._triggerFromComputedAttached[targetField] = true;
98100
computedWatchInfo.computedRelatedPathValues[targetField] =
99101
pathValues;
100102

@@ -153,6 +155,7 @@ export const behavior = Behavior({
153155
}
154156

155157
if (watchDef) {
158+
156159
Object.keys(watchDef).forEach((watchPath) => {
157160
const paths = dataPath.parseMultiDataPaths(watchPath);
158161
observersItems.push({
@@ -162,6 +165,19 @@ export const behavior = Behavior({
162165
const computedWatchInfo =
163166
this._computedWatchInfo[computedWatchDefId];
164167
if (!computedWatchInfo) return;
168+
// (issue #58) ignore watch func when trigger by computed attached
169+
if (Object.keys(computedWatchInfo._triggerFromComputedAttached).length) {
170+
const pathsMap: Record<string, boolean> = {}
171+
paths.forEach((path) => pathsMap[path.path[0]] = true);
172+
for (let computedVal in computedWatchInfo._triggerFromComputedAttached) {
173+
if (computedWatchInfo._triggerFromComputedAttached.hasOwnProperty(computedVal)) {
174+
if (pathsMap[computedVal]) {
175+
computedWatchInfo._triggerFromComputedAttached[computedVal] = false;
176+
return
177+
}
178+
}
179+
}
180+
}
165181
const oldVal = computedWatchInfo.watchCurVal[watchPath];
166182

167183
// get new watching field value
@@ -204,6 +220,7 @@ export const behavior = Behavior({
204220
if (typeof defFields.observers !== "object") {
205221
defFields.observers = {};
206222
}
223+
207224
if (Array.isArray(defFields.observers)) {
208225
defFields.observers.push(...observersItems);
209226
} else {

src/data-path.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ const WHITE_SPACE_CHAR_REGEXP = /^\s/;
33
const throwParsingError = function (path, index) {
44
throw new Error(
55
'Parsing data path "' +
6-
path +
7-
'" failed at char "' +
8-
path[index] +
9-
'" (index ' +
10-
index +
11-
")"
6+
path +
7+
'" failed at char "' +
8+
path[index] +
9+
'" (index ' +
10+
index +
11+
")"
1212
);
1313
};
1414

src/data-tracer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const wrapData = (data, relatedPathValues, basePath) => {
22
if (typeof data !== "object" || data === null) return data;
3-
43
const handler = {
54
get(_obj, key) {
65
if (key === "__rawObject__") return data;
@@ -22,6 +21,7 @@ const wrapData = (data, relatedPathValues, basePath) => {
2221
export function create(data, relatedPathValues) {
2322
return wrapData(data, relatedPathValues, []);
2423
}
24+
2525
export function unwrap(wrapped) {
2626
if (
2727
typeof wrapped !== "object" ||

swc_build/behavior.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,4 +776,53 @@ describe("computed behavior", () => {
776776
expect(c1TriggerCount).toBe(2);
777777
expect(c2TriggerCount).toBe(2);
778778
});
779+
780+
test("ignore watch func when trigger by computed attached", () => {
781+
let cTriggerCount = 0;
782+
let dTriggerCount = 0;
783+
const behA = BehaviorWithComputed({
784+
data: {
785+
a: 1,
786+
},
787+
computed: {
788+
d(data) {
789+
return 10
790+
},
791+
c(data) {
792+
return data.b * 2;
793+
},
794+
},
795+
watch: {
796+
c() {
797+
cTriggerCount += 1;
798+
},
799+
d() {
800+
dTriggerCount += 1;
801+
},
802+
},
803+
attached() {
804+
this.setData({
805+
a: 2,
806+
});
807+
},
808+
});
809+
const componentId = _.load({
810+
template: "<view>{{c}}</view>",
811+
behaviors: [behA, computedBehavior],
812+
data: {
813+
b: 10,
814+
},
815+
attached() {
816+
this.setData({
817+
b: 20,
818+
});
819+
},
820+
});
821+
const component = _.render(componentId);
822+
component.triggerLifeTime("attached");
823+
expect(_.match(component.dom!, "<wx-view>40</wx-view>")).toBe(true);
824+
expect(cTriggerCount).toBe(0);
825+
expect(dTriggerCount).toBe(0);
826+
827+
})
779828
});

0 commit comments

Comments
 (0)