Hey all,
We're introducing updates to the Shared Storage API in M134, including Web Locks API integration and the sharedStorage.batchUpdate() for efficient data modification.
Batch update
sharedStorage.batchUpdate(methods, options)Â enables you to perform multiple operations on Shared Storage data such as set, append, delete, and clear using a single call.Â
The methods argument must be an ordered array of operations to be performed.Â
For example:
sharedStorage.batchUpdate([
    new SharedStorageSetMethod('keyOne', 'valueOne'),
    new SharedStorageAppendMethod('keyTwo', 'valueTwo'),
    new SharedStorageDeleteMethod('keyThree'),
    new SharedStorageClearMethod()
]);
Web Locks API integration
Web Locks in Shared Storage enables you to lock a defined resource before executing an operation, ensuring coordination across multiple worklet operations and preventing race conditions.Â
M134 introduces a withLock option that can be used with all modifier methods set, append, delete, and clear. Within a worklet you can use the Web Locks API (by calling navigator.locks.request()) to perform your Shared Storage operations.
For example:
// script.js
sharedStorage.set('keyOne', 'valueOne', { withLock: 'resource-lock' });
// worklet.js
class ModifyMultipleKeysOperation {
  async run(data) {
    await navigator.locks.request("resource-lock", async (lock) => {
      const value1 = await sharedStorage.get('keyOne');
      // Do something with `value1` here.
      await sharedStorage.delete('keyOne');
    });
  }
}
register('modify-multiple-keys', ModifyMultipleKeysOperation);
We’ve updated the Shared Storage Implementation documentation to reflect these changes and provide additional details on using batchUpdate() and the Web Locks API.
If you have any questions or comments, reply to this post or open an issue in the Privacy Sandbox Dev Support repository on GitHub.