Skip to content

Commit 34d9c75

Browse files
committed
fix: proxy array #70
1 parent 6a30298 commit 34d9c75

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

src/behavior.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,17 @@ export const behavior = Behavior({
8585
const val = updateMethod(
8686
dataTracer.create(this.data, relatedPathValuesOnDef)
8787
);
88-
8988
const pathValues = relatedPathValuesOnDef.map(({ path }) => ({
9089
path,
9190
value: dataPath.getDataOnPath(this.data, path),
9291
}));
93-
9492
// here we can do small setDatas
9593
// because observer handlers will force grouping small setDatas together
9694
this.setData({
9795
[targetField]: dataTracer.unwrap(val),
9896
});
99-
computedWatchInfo._triggerFromComputedAttached[targetField] = true;
97+
computedWatchInfo._triggerFromComputedAttached[targetField] =
98+
true;
10099
computedWatchInfo.computedRelatedPathValues[targetField] =
101100
pathValues;
102101

@@ -123,8 +122,12 @@ export const behavior = Behavior({
123122
this.setData({
124123
[targetField]: dataTracer.unwrap(val),
125124
});
125+
const pathValues = relatedPathValues.map(({ path }) => ({
126+
path,
127+
value: dataPath.getDataOnPath(this.data, path),
128+
}));
126129
computedWatchInfo.computedRelatedPathValues[targetField] =
127-
relatedPathValues;
130+
pathValues;
128131
return true;
129132
};
130133
computedWatchInfo.computedUpdaters.push(
@@ -155,7 +158,6 @@ export const behavior = Behavior({
155158
}
156159

157160
if (watchDef) {
158-
159161
Object.keys(watchDef).forEach((watchPath) => {
160162
const paths = dataPath.parseMultiDataPaths(watchPath);
161163
observersItems.push({
@@ -166,14 +168,25 @@ export const behavior = Behavior({
166168
this._computedWatchInfo[computedWatchDefId];
167169
if (!computedWatchInfo) return;
168170
// (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] && computedWatchInfo._triggerFromComputedAttached[computedVal]) {
175-
computedWatchInfo._triggerFromComputedAttached[computedVal] = false;
176-
return
171+
if (
172+
Object.keys(computedWatchInfo._triggerFromComputedAttached).length
173+
) {
174+
const pathsMap: Record<string, boolean> = {};
175+
paths.forEach((path) => (pathsMap[path.path[0]] = true));
176+
for (const computedVal in computedWatchInfo._triggerFromComputedAttached) {
177+
if (
178+
computedWatchInfo._triggerFromComputedAttached.hasOwnProperty(
179+
computedVal
180+
)
181+
) {
182+
if (
183+
pathsMap[computedVal] &&
184+
computedWatchInfo._triggerFromComputedAttached[computedVal]
185+
) {
186+
computedWatchInfo._triggerFromComputedAttached[
187+
computedVal
188+
] = false;
189+
return;
177190
}
178191
}
179192
}

src/data-tracer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const ProxyPolyfill = ProxyPolyfillBuilder();
44
const wrapData = (data, relatedPathValues, basePath) => {
55
if (typeof data !== "object" || data === null) return data;
66
const handler = {
7-
get(_obj, key) {
8-
if (key === "__rawObject__") return data;
7+
get(obj, key) {
8+
if (key === "__rawObject__") return obj;
99
let keyWrapper = null;
1010
const keyPath = basePath.concat(key);
11-
const value = data[key];
11+
const value = obj[key];
1212
relatedPathValues.push({
1313
path: keyPath,
1414
value,
@@ -17,14 +17,10 @@ const wrapData = (data, relatedPathValues, basePath) => {
1717
return keyWrapper;
1818
},
1919
};
20-
// for test
21-
// const Proxy = undefined;
22-
2320
let propDef;
2421
try {
2522
propDef = new Proxy(data, handler);
2623
} catch (e) {
27-
// console.log("[miniprogram-computed]: use Proxy Polyfill");
2824
propDef = new ProxyPolyfill(data, handler);
2925
}
3026
return propDef;
@@ -35,6 +31,10 @@ export function create(data, relatedPathValues) {
3531
}
3632

3733
export function unwrap(wrapped) {
34+
// @ts-ignore
35+
if (Array.isArray(wrapped) && typeof wrapped.__rawObject__ !== "object") {
36+
return wrapped.map((i) => unwrap(i));
37+
}
3838
if (
3939
typeof wrapped !== "object" ||
4040
wrapped === null ||

test/index.spec.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ describe("computed behavior", () => {
786786
},
787787
computed: {
788788
d(data) {
789-
return 10
789+
return 10;
790790
},
791791
c(data) {
792792
return data.b * 2;
@@ -823,6 +823,52 @@ describe("computed behavior", () => {
823823
expect(_.match(component.dom!, "<wx-view>40</wx-view>")).toBe(true);
824824
expect(cTriggerCount).toBe(1);
825825
expect(dTriggerCount).toBe(0);
826+
});
827+
828+
test("computed derivations", () => {
829+
let aTriggerCount = 0;
830+
let bTriggerCount = 0;
831+
const componentId = _.load({
832+
template: "<view></view>",
833+
behaviors: [computedBehavior],
834+
data: {
835+
root: [
836+
{
837+
values: [1, 2, 3],
838+
},
839+
],
840+
},
841+
computed: {
842+
computedWithRoot(data) {
843+
aTriggerCount++;
844+
const res = data.root.map((v) => v);
845+
return res;
846+
},
847+
computedWithDerivations(data) {
848+
bTriggerCount++;
849+
const res = data.computedWithRoot[0].values;
850+
return res;
851+
},
852+
},
853+
});
854+
855+
const component = _.render(componentId);
856+
component.triggerLifeTime("attached");
857+
expect(aTriggerCount).toBe(1);
858+
expect(bTriggerCount).toBe(1);
826859

827-
})
860+
component.data.root[0].values.push(1);
861+
component.setData({
862+
"root[0].values": component.data.root[0].values,
863+
});
864+
expect(aTriggerCount).toBe(1);
865+
expect(bTriggerCount).toBe(2);
866+
867+
component.data.root.push(2);
868+
component.setData({
869+
root: component.data.root,
870+
});
871+
expect(aTriggerCount).toBe(2);
872+
expect(bTriggerCount).toBe(3);
873+
});
828874
});

0 commit comments

Comments
 (0)