瀏覽代碼

fix the context API

Evan Almloff 1 年之前
父節點
當前提交
0be5dbe715
共有 2 個文件被更改,包括 8 次插入7 次删除
  1. 5 2
      packages/core/src/create.rs
  2. 3 5
      packages/core/src/scope_context.rs

+ 5 - 2
packages/core/src/create.rs

@@ -65,8 +65,11 @@ impl<'b> VirtualDom {
     /// Create a new template [`VNode`] and write it to the [`Mutations`] buffer.
     ///
     /// This method pushes the ScopeID to the internal scopestack and returns the number of nodes created.
-    pub(crate) fn create_scope(&mut self, _scope: ScopeId, template: &'b VNode<'b>) -> usize {
-        self.create(template)
+    pub(crate) fn create_scope(&mut self, scope: ScopeId, template: &'b VNode<'b>) -> usize {
+        self.runtime.scope_stack.borrow_mut().push(scope);
+        let nodes = self.create(template);
+        self.runtime.scope_stack.borrow_mut().pop();
+        nodes
     }
 
     /// Create this template and write its mutations

+ 3 - 5
packages/core/src/scope_context.rs

@@ -115,21 +115,19 @@ impl ScopeContext {
 
         let mut search_parent = self.parent_id;
         with_runtime(|runtime| {
-            let mut result = None;
             while let Some(parent_id) = search_parent {
                 let parent = runtime.get_context(parent_id).unwrap();
                 if let Some(shared) = parent
                     .shared_contexts
                     .borrow()
                     .iter()
-                    .find(|(k, _)| *k == TypeId::of::<T>())
+                    .find_map(|(_, any)| any.downcast_ref::<T>())
                 {
-                    result = shared.1.downcast_ref::<T>().cloned();
-                    break;
+                    return Some(shared.clone());
                 }
                 search_parent = parent.parent_id;
             }
-            result
+            None
         })
         .flatten()
     }