-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from omochi/vnode-disc
VNodeDiscriminator
- Loading branch information
Showing
3 changed files
with
90 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
enum VNodeKind: Hashable { | ||
case html(String) | ||
case text | ||
case component(ObjectIdentifier) | ||
} | ||
|
||
extension Component { | ||
var vnodeKind: VNodeKind { | ||
switch self { | ||
case let html as HTMLElement: .html(html.tagName) | ||
case is TextElement: .text | ||
default: .component(ObjectIdentifier(type(of: self))) | ||
} | ||
} | ||
|
||
var vnodeDiscriminator: VNodeDiscriminator { | ||
VNodeDiscriminator(kind: vnodeKind, key: key) | ||
} | ||
} | ||
|
||
struct VNodeDiscriminator: Hashable { | ||
init( | ||
kind: VNodeKind, | ||
key: AnyHashable? | ||
) { | ||
self.kind = kind | ||
self.key = key | ||
} | ||
|
||
var kind: VNodeKind | ||
var key: AnyHashable? | ||
} | ||
|
||
struct VNodeMatchAdapter: Hashable { | ||
init(node: VNode) { | ||
self.node = node | ||
self.disc = node.component.vnodeDiscriminator | ||
} | ||
|
||
var node: VNode | ||
let disc: VNodeDiscriminator | ||
|
||
static func ==(a: Self, b: Self) -> Bool { a.disc == b.disc } | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(disc) | ||
} | ||
} | ||
|
||
extension VNode { | ||
static func match(newChildren: [VNode], oldChildren: [VNode]) -> CollectionDifference<VNode> { | ||
matchStdlib(newChildren: newChildren, oldChildren: oldChildren) | ||
} | ||
|
||
static func matchStdlib(newChildren: [VNode], oldChildren: [VNode]) -> CollectionDifference<VNode> { | ||
let newChildren = newChildren.map { VNodeMatchAdapter(node: $0) } | ||
let oldChildren = oldChildren.map { VNodeMatchAdapter(node: $0) } | ||
|
||
let diff: CollectionDifference<VNodeMatchAdapter> = newChildren | ||
.difference(from: oldChildren) | ||
.inferringMoves() | ||
|
||
return CollectionDifference<VNode>( | ||
diff.map { (patch) in | ||
switch patch { | ||
case .remove(offset: let o, element: let e, associatedWith: let a): | ||
.remove(offset: o, element: e.node, associatedWith: a) | ||
case .insert(offset: let o, element: let e, associatedWith: let a): | ||
.insert(offset: o, element: e.node, associatedWith: a) | ||
} | ||
} | ||
)! | ||
} | ||
} |