feat: directly implement IntoIterator on keyed fields

This commit is contained in:
Greg Johnston 2024-09-14 21:22:16 -04:00
parent 550a3a4e6d
commit fc06980c60
2 changed files with 11 additions and 5 deletions

View file

@ -95,7 +95,10 @@ pub fn App() -> impl IntoView {
<input type="submit"/> <input type="submit"/>
</form> </form>
<ol> <ol>
<For each=move || store.todos().iter_keyed() key=|row| row.id().get() let:todo> // because `todos` is a keyed field, `store.todos()` returns a struct that
// directly implements IntoIterator, so we can use it in <For/> and
// it will manage reactivity for the store fields correctly
<For each=move || store.todos() key=|row| row.id().get() let:todo>
<TodoRow store todo/> <TodoRow store todo/>
</For> </For>

View file

@ -638,18 +638,21 @@ where
} }
} }
impl<Inner, Prev, K, T> KeyedSubfield<Inner, Prev, K, T> impl<Inner, Prev, K, T> IntoIterator for KeyedSubfield<Inner, Prev, K, T>
where where
Self: Clone, Self: Clone,
for<'a> &'a T: IntoIterator, for<'a> &'a T: IntoIterator,
Inner: StoreField<Value = Prev>, Inner: Clone + StoreField<Value = Prev> + 'static,
Prev: 'static, Prev: 'static,
K: Debug + Send + Sync + PartialEq + Eq + Hash + 'static, K: Debug + Send + Sync + PartialEq + Eq + Hash + 'static,
T: IndexMut<usize>, T: IndexMut<usize> + 'static,
T::Output: Sized, T::Output: Sized,
{ {
type Item = AtKeyed<Inner, Prev, K, T>;
type IntoIter = StoreFieldKeyedIter<Inner, Prev, K, T>;
#[track_caller] #[track_caller]
pub fn iter_keyed(self) -> StoreFieldKeyedIter<Inner, Prev, K, T> { fn into_iter(self) -> StoreFieldKeyedIter<Inner, Prev, K, T> {
// reactively track changes to this field // reactively track changes to this field
let trigger = self.get_trigger(self.path().into_iter().collect()); let trigger = self.get_trigger(self.path().into_iter().collect());
trigger.track(); trigger.track();