大网数据广告活动
Web 标准与前端开发

SharedStorageRunOperation

· 52CE 我爱测

The **`SharedStorageRunOperation`** interface of the Shared Storage API represents a Run output gate operation.

Shared Storage API

The SharedStorageRunOperation interface of the Shared Storage API represents a Run output gate operation.

Instance methods

  • SharedStorageRunOperation.run
    • : Defines the structure to which the run() method defined inside a Run output gate operation should conform.

Examples

In this example, a class called ReachMeasurementOperation is defined in a worklet and is registered using SharedStorageWorkletGlobalScope.register() with the name reach-measurement. SharedStorageRunOperation defines the structure to which this class must conform, essentially defining the parameters required for the run() method. Other than this requirement, the functionality of the class can be defined flexibly.

// reach-measurement-worklet.js
const SCALE_FACTOR = 65536;

function convertContentIdToBucket(contentId) {
  return BigInt(contentId);
}

class ReachMeasurementOperation {
  async run(data) {
    const { contentId } = data;

    // Read from Shared Storage
    const key = "has-reported-content";
    const hasReportedContent = (await this.sharedStorage.get(key)) === "true";

    // Do not report if a report has been sent already
    if (hasReportedContent) {
      return;
    }

    // Generate the aggregation key and the aggregatable value
    const bucket = convertContentIdToBucket(contentId);
    const value = 1 * SCALE_FACTOR;

    // Send an aggregatable report via the Private Aggregation API
    privateAggregation.sendHistogramReport({ bucket, value });

    // Set the report submission status flag
    await this.sharedStorage.set(key, true);
  }
}

// Register the operation
register("reach-measurement", ReachMeasurementOperation);

[!NOTE] It is possible to define and register multiple operations in the same shared storage worklet module script with different names. See SharedStorageOperation for an example.

In the main browsing context, the reach-measurement operation is invoked using the WindowSharedStorage.run() method:

async function measureUniqueReach() {
  // Load the Shared Storage worklet
  await window.sharedStorage.worklet.addModule("reach-measurement-worklet.js");

  // Run the reach measurement operation
  await window.sharedStorage.run("reach-measurement", {
    data: { contentId: "1234" },
  });
}

measureUniqueReach();

For more details about this example, see Unique reach measurement. See Shared Storage API for more examples.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。