例項方法
run()實驗性-
定義了在 Run 輸出門操作中定義的
run()方法應遵循的結構。
示例
在此示例中,一個名為 ReachMeasurementOperation 的類在 worklet 中定義,並使用 SharedStorageWorkletGlobalScope.register() 以 reach-measurement 的名稱進行註冊。SharedStorageRunOperation 定義了該類必須遵循的結構,本質上定義了 run() 方法所需的引數。除此要求外,類的功能可以靈活定義。
js
// 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);
注意:可以在同一個共享儲存 worklet 模組指令碼中定義和註冊具有不同名稱的多個操作。有關示例,請參閱 SharedStorageOperation。
在主瀏覽上下文中,使用 WindowSharedStorage.run() 方法呼叫 reach-measurement 操作。
js
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();
有關此示例的更多詳細資訊,請參閱 Unique reach measurement。有關更多示例,請參閱 Shared Storage API。
規範
此特性似乎未在任何規範中定義。瀏覽器相容性
載入中…