Skip to content

Commit 55c18bd

Browse files
committed
fix: #71
1 parent 6003e02 commit 55c18bd

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/behavior.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import type { IRelatedPathValue } from './data-tracer'
66

77
const deepClone = rfdc({ proto: true })
88

9+
interface BehaviorData {
10+
'_computedWatchInit': ComputedWatchInitStatus;
11+
[k: string]: any;
12+
}
913

1014
interface BehaviorExtend {
1115
// original
12-
data: Record<string, any>;
16+
data: BehaviorData;
1317
setData(d: Record<string, any>): void;
1418
_computedWatchInfo: Record<string, ComputedWatchInfo>;
1519
}
@@ -26,18 +30,23 @@ interface ComputedWatchInfo {
2630
_triggerFromComputedAttached: Record<string, boolean>;
2731
}
2832

33+
enum ComputedWatchInitStatus {
34+
CREATED,
35+
ATTACHED
36+
}
37+
2938
let computedWatchDefIdInc = 0
3039

3140
export const behavior = Behavior({
3241
lifetimes: {
3342
attached(this: BehaviorExtend) {
3443
this.setData({
35-
_computedWatchInit: 'attached',
44+
_computedWatchInit: ComputedWatchInitStatus.ATTACHED,
3645
})
3746
},
3847
created(this: BehaviorExtend) {
3948
this.setData({
40-
_computedWatchInit: 'created',
49+
_computedWatchInit: ComputedWatchInitStatus.CREATED,
4150
})
4251
},
4352
},
@@ -51,7 +60,7 @@ export const behavior = Behavior({
5160
fields: '_computedWatchInit',
5261
observer(this: BehaviorExtend) {
5362
const status = this.data._computedWatchInit
54-
if (status === 'created') {
63+
if (status === ComputedWatchInitStatus.CREATED) {
5564
// init data fields
5665
const computedWatchInfo = {
5766
computedUpdaters: [],
@@ -74,7 +83,7 @@ export const behavior = Behavior({
7483
computedWatchInfo.watchCurVal[watchPath] = curVal
7584
})
7685
}
77-
} else if (status === 'attached') {
86+
} else if (status === ComputedWatchInitStatus.ATTACHED) {
7887
// handling computed
7988
// 1. push to initFuncs
8089
// 2. push to computedUpdaters

src/data-tracer.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ const wrapData = (data: unknown, relatedPathValues: Array<IRelatedPathValue>, ba
2626
return keyWrapper
2727
},
2828
}
29-
let propDef
3029
try {
31-
propDef = new Proxy(data, handler)
30+
return new Proxy(data, handler)
3231
} catch (e) {
33-
propDef = new ProxyPolyfill(data, handler)
32+
return new ProxyPolyfill(data, handler)
3433
}
35-
return propDef
3634
}
3735

3836
export function create(data: unknown, relatedPathValues: Array<IRelatedPathValue>) {
@@ -41,7 +39,7 @@ export function create(data: unknown, relatedPathValues: Array<IRelatedPathValue
4139

4240
export function unwrap(wrapped: IWrappedData) {
4341
// #70
44-
if (typeof wrapped.__rawObject__ !== 'object' && typeof wrapped === 'object' && wrapped !== null) {
42+
if (wrapped !== null && typeof wrapped === 'object' && typeof wrapped.__rawObject__ !== 'object' ) {
4543
if (Array.isArray(wrapped)) {
4644
return wrapped.map((i) => unwrap(i))
4745
}

0 commit comments

Comments
 (0)