event_stream 功能

event_stream 功能允许组件订阅在组件生命周期的各个阶段发生的特定事件。这包括有关组件状态更改以及功能路由的信息。

如需查看受支持事件及其说明的完整列表,请参阅 fuchsia.component.EventType 参考文档。如需查看 event_stream 功能的原始 RFC,请参阅 RFC-121

提供 event_stream 功能

event_stream 功能始终来自组件管理器。系统会将这些 ID 提供给 OverRoot 中的组件。各个组件不能在其清单 capabilities 中声明事件,也不能使用 from: "framework"

每个 event_stream 功能都有一个可选的 scope,用于确定连接的客户端将收到的事件的子树。scope 可以是单个子树,也可以是引用多个子树。

路由 event_stream 功能

组件可以将它们从父级接收的 event_stream 功能提供给其他组件。event_stream 功能不能公开。

如需详细了解框架如何路由组件功能,请参阅功能路由

成为协作者

提供事件功能可让子组件使用该功能:

{
    offer: [
        {
            event_stream: "started",
            from: "parent",
            scope: ["#child_a"]
            to: [ "#child-a", "#child_b" ],
        },
        {
            event_stream: "stopped",
            from: "parent",
            to: "#child-c",
        }
    ]
}

活动只能通过父级提供。

使用 event_stream 功能

如需使用事件功能,组件必须使用 event_stream 功能。您可以通过在单个 use 中指定多个流来合并事件流。

如需请求该功能,请为其添加 use 声明:

{
    use: [
        { event_stream: ["started", "stopped"] },
    ]
}

event_streams 只能用于 from: "parent"

event_stream 路由示例

请参考以下组件拓扑示例:

event_stream 示例

请注意此示例的以下几个主要方面:

  • core/archivist:通过从 root 路由的事件功能接收整个拓扑的 started 事件。

    // root.cml
    {
        offer: [
            {
                event_stream: "started",
                from: "parent",
                to: "#core",
            },
        ]
    }
    
    // core.cml
    {
        offer: [
            {
                event_stream: "started",
                from: "parent",
                to: "#archivist",
            },
            {
                event_stream: "started",
                from: "parent",
                to: "#test_manager",
                scope: ["#test_manager"],
            }
        ]
    }
    
    // archivist.cml
    {
        use: [
            { event_stream: "started" },
        ]
    }
    
  • core/test_manager/archivist:通过从 test_manager 路由的事件功能接收所有测试组件的 started 事件。

    // test_manager.cml
    {
        offer: [
            {
                event_stream: "started",
                from: "parent",
                to: "#archivist",
            },
            {
                event_stream: "started",
                from: "parent",
                to: "#tests",
                // Downscopes the event to the tests collection
                scope: ["#tests"],
            }
        ]
    }
    
    - The root Archivist gets `started` events for all components in the
    topology from the root, whereas the embedded Archivist only gets events from
    its own test collection.
    NOTE: An event_stream must be routed through the entire topology from the root
    component_manager all the way down to the component that wants to use the event.
    event_streams cannot be used `from: "framework"`, and they are not
    automatically made available in every component's environment.
    
    // archivist.cml
    {
          use: [
            {
                event_stream: "started",
                from: "parent",
            },
        ]
    }
    
  • core/test_manager/tests:test-12345:通过从 test_manager 路由的 event_stream capability,接收其集合下内容的启动事件。

    // test-12345.cml
    {
        use: [
            {
                event_stream: "started",
                from: "parent",
            },
        ]
    }