Skip to content

Commit

Permalink
feat(reactivity): collection iteration should inherit iterator instan…
Browse files Browse the repository at this point in the history
…ce methods
  • Loading branch information
edison1105 committed Jan 3, 2025
1 parent e8e8422 commit 9f08043
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ function createIterableMethod(
)
// return a wrapped iterator which returns observed versions of the
// values emitted from the real iterator
return {
// iterator protocol
next() {
const { value, done } = innerIterator.next()
return done
? { value, done }
: {
value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
done,
}
},
// iterable protocol
[Symbol.iterator]() {
return this
return extend(
// inheriting all iterator properties
Object.create(innerIterator),
{
// iterator protocol
next() {
const { value, done } = innerIterator.next()
return done
? { value, done }
: {
value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
done,
}
},
// iterable protocol
[Symbol.iterator]() {
return this
},
},
}
)
}
}

Expand Down

0 comments on commit 9f08043

Please sign in to comment.