feat(reactivity): collection iteration should inherit iterator instance methods

This commit is contained in:
daiwei 2025-01-03 16:25:10 +08:00
parent e8e842241a
commit 9f08043780
1 changed files with 19 additions and 15 deletions

View File

@ -55,22 +55,26 @@ function createIterableMethod(
) )
// return a wrapped iterator which returns observed versions of the // return a wrapped iterator which returns observed versions of the
// values emitted from the real iterator // values emitted from the real iterator
return { return extend(
// iterator protocol // inheriting all iterator properties
next() { Object.create(innerIterator),
const { value, done } = innerIterator.next() {
return done // iterator protocol
? { value, done } next() {
: { const { value, done } = innerIterator.next()
value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value), return done
done, ? { value, done }
} : {
value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
done,
}
},
// iterable protocol
[Symbol.iterator]() {
return this
},
}, },
// iterable protocol )
[Symbol.iterator]() {
return this
},
}
} }
} }