confluentinc / confluentinc/vscode

Revise ResourceManager mutexing implementation with a decorator pattern

Open
#649 0 comments 0 reactions 1 assignee Claimed by @jlrobins View on GitHub
cleanup extension storage
Dominant language
TypeScript
Stars
34
Forks
17
Avg merge
1d 22h
Merged PRs (30d)
8

Description

Revise work done in https://github.com/confluentinc/vscode/pull/646 to use a decorator pattern instead. Decorator should take a parameter -- the key for which mutex to use.

And then write a new prove-race-proof test for at least one of the methods.

Sample method decorator:
```ts
function mutexWrapper(key: string) {
// target is the class, propertyKey is the method name, descriptor is the method descriptor
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;

descriptor.value = async function (...args: any[]) {
console.log("mutexWrapper() start", { methodName: propertyKey, key});
const result = await originalMethod(args);
console.log("mutexWrapper() end", { methodName: propertyKey, key});
return result;
};

return descriptor;
};
}

class ResourceManager {
constructor() {}

@mutexWrapper("FOO")
async readAndWrite() {
console.log("readAndWrite()");
}

@mutexWrapper("BAR")
async readAndDelete() {
console.log("readAndDelete()");
}
}

async function main() {
const resourceManager = new ResourceManager();
await resourceManager.readAndWrite();
await resourceManager.readAndDelete();
}

main()
```
Output:
```
mutexWrapper() start { methodName: 'readAndWrite', key: 'FOO' }
readAndWrite()
mutexWrapper() end { methodName: 'readAndWrite', key: 'FOO' }
mutexWrapper() start { methodName: 'readAndDelete', key: 'BAR' }
readAndDelete()
mutexWrapper() end { methodName: 'readAndDelete', key: 'BAR' }
```

> [!NOTE]
> This does require adding `"experimentalDecorators": true` into the tsconfig.json under `compilerOptions`, otherwise we'll see errors like:
> ```
> Unable to resolve signature of method decorator when called as an expression.
> The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.ts(1241)
> ```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.