-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
… optimization
- Loading branch information
Showing
14 changed files
with
234 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,62 @@ | ||
import { ProxyRaw, RawNode } from './environment' | ||
import { PropertyKey } from './types' | ||
import { ObservablePath, PropertyKey, IOperation } from './types' | ||
import { concat } from './concat' | ||
|
||
export class DataChange { | ||
path: ObservablePath | ||
key: PropertyKey | ||
type: string | ||
value: any | ||
oldValue: any | ||
constructor(operation: IOperation, node: DataNode) { | ||
this.key = operation.key | ||
this.type = operation.type | ||
this.value = operation.value | ||
this.oldValue = operation.oldValue | ||
this.path = concat(node.path, operation.key) | ||
} | ||
} | ||
export class DataNode { | ||
target: any | ||
|
||
key: PropertyKey | ||
|
||
constructor(target: any, key: PropertyKey) { | ||
this.target = target | ||
this.key = key | ||
} | ||
|
||
get path() { | ||
if (!this.parent) return this.key ? [this.key] : [] | ||
return concat(this.parent.path, this.key) | ||
} | ||
|
||
get targetRaw() { | ||
return ProxyRaw.get(this.target) || this.target | ||
} | ||
|
||
get parent() { | ||
if (!this.target) return | ||
return RawNode.get(this.targetRaw) | ||
} | ||
|
||
isEqual(node: DataNode) { | ||
return node.targetRaw === this.targetRaw && node.key === this.key | ||
} | ||
|
||
contains(node: DataNode) { | ||
if (node === this) return true | ||
let parent = node.parent | ||
while (!!parent) { | ||
if (this.isEqual(parent)) return true | ||
parent = parent.parent | ||
} | ||
return false | ||
} | ||
} | ||
|
||
export const buildDataTree = (target: any, key: PropertyKey, value: any) => { | ||
const raw = ProxyRaw.get(value) || value | ||
const currentNode = RawNode.get(raw) | ||
const currentNode = RawNode.get(ProxyRaw.get(value) || value) | ||
if (currentNode) return currentNode | ||
const parentRaw = ProxyRaw.get(target) || target | ||
const parentNode = RawNode.get(parentRaw) | ||
if (parentNode) { | ||
RawNode.set(value, { | ||
get path() { | ||
return concat(parentNode.path, key) | ||
}, | ||
parent: parentNode, | ||
observers: [], | ||
deepObservers: [], | ||
}) | ||
} else { | ||
RawNode.set(value, { | ||
path: [], | ||
observers: [], | ||
deepObservers: [], | ||
}) | ||
} | ||
RawNode.set(value, new DataNode(target, key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.