Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All examples use fluent pattern #85

Open
PonchoPowers opened this issue Oct 13, 2024 · 0 comments
Open

All examples use fluent pattern #85

PonchoPowers opened this issue Oct 13, 2024 · 0 comments

Comments

@PonchoPowers
Copy link

All the examples use a fluent pattern, but wouldn't it be good to have an example such as a real word example that is clear to follow and doesn't use a fluent pattern like so:

import { Mutex } from 'async-mutex';

class ConcurrentDictionary<K, V> {
    private dictionary: Map<K, V>;
    private mutex: Mutex;

    constructor() {
        this.dictionary = new Map<K, V>();
        this.mutex = new Mutex();
    }

    async set(key: K, value: V): Promise<void> {
        const release = await this.mutex.acquire();
        try {
            this.dictionary.set(key, value);
        } finally {
            release();
        }
    }

    async get(key: K): Promise<V | undefined> {
        const release = await this.mutex.acquire();
        try {
            return this.dictionary.get(key);
        } finally {
            release();
        }
    }

    async delete(key: K): Promise<boolean> {
        const release = await this.mutex.acquire();
        try {
            return this.dictionary.delete(key);
        } finally {
            release();
        }
    }

    async has(key: K): Promise<boolean> {
        const release = await this.mutex.acquire();
        try {
            return this.dictionary.has(key);
        } finally {
            release();
        }
    }

    async clear(): Promise<void> {
        const release = await this.mutex.acquire();
        try {
            this.dictionary.clear();
        } finally {
            release();
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant