/
index.md
73 lines (51 loc) · 2.27 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
title: "SharedStorageWorkletGlobalScope: register() method"
short-title: register()
slug: Web/API/SharedStorageWorkletGlobalScope/register
page-type: web-api-instance-method
status:
- experimental
browser-compat: api.SharedStorageWorkletGlobalScope.register
---
{{APIRef("Shared Storage API")}}{{SeeCompatTable}}
The **`register()`** method of the
{{domxref("SharedStorageWorkletGlobalScope")}} interface registers an {{domxref("SharedStorageOperation", "operation", "", "nocode")}} defined inside the current worklet module.
## Syntax
```js-nolint
register(name, operationCtor)
```
### Parameters
- `name`
- : A string representing the name with which you want to register the operation. When the operation is invoked (say via {{domxref("WindowSharedStorage.run()")}} or {{domxref("WindowSharedStorage.selectURL()")}}), this name is used to identify the operation you want to run.
- `operationCtor`
- : A string representing the class name of the operation to be registered. This is the class constructor that is invoked when the operation is run.
### Return value
None (`undefined`).
### Exceptions
- {{jsxref("TypeError")}}
- : Thrown if:
- An operation has already been registered with the specified name.
- The `operationCtor` is not a valid constructor.
- The class does not contain a valid `run()` method.
- The worklet module has not been added with {{domxref("Worklet.addModule", "SharedStorageWorklet.addModule()")}}.
## Examples
```js
// ab-testing-worklet.js
class SelectURLOperation {
async run(urls, data) {
// Read the user's experiment group from shared storage
const experimentGroup = await this.sharedStorage.get("ab-testing-group");
// Return the group number
return experimentGroup;
}
}
register("ab-testing", SelectURLOperation);
```
See the [Shared Storage API](/en-US/docs/Web/API/Shared_Storage_API) landing page for a walkthrough of this example and for links to other examples.
> **Note:** It is possible to define and register multiple operations in the same shared storage worklet module script with different names; see {{domxref("SharedStorageOperation")}} for an example.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Shared Storage API](/en-US/docs/Web/API/Shared_Storage_API)