event_stream 功能

event_stream 功能可讓元件訂閱元件生命週期各階段中發生的特定事件。這包含元件狀態變更和能力轉送的相關資訊。

如需支援事件的完整清單及其說明,請參閱 fuchsia.component.EventType 參考說明文件。如要瞭解 event_stream 功能的原始 RFC,請參閱 RFC-121

提供 event_stream 功能

event_stream 功能一律源自元件管理員。這些元件會提供來自 aboveRoot 的元件。個別元件無法在其資訊清單 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 能力,接收其集合的已啟動事件。

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