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

Move Initialization Check to Its Own Function #2

Open
GibsonRuitiari opened this issue Jan 31, 2024 · 1 comment
Open

Move Initialization Check to Its Own Function #2

GibsonRuitiari opened this issue Jan 31, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@GibsonRuitiari
Copy link

this is in reference to https://github.com/Bizyback/youtubestream/blob/main/youtubestream/src/main/java/com/bizyback/libs/youtubestream/YoutubeStream.kt and val stream = ytStream ?: return YoutubeStreamResult.Error(message = "YoutubeStream is not initialized, please initialize to continue \nRun `YoutubeStream.init(context)` to initialize")

Maybe we could move the initialization check bit to its own function to avoid the repetition.

Also, fun init(context: Context) { ytStream = YTStream(context = context) } this can be a potential for races and deadlocks, if different threads try to invoke the init function at the same time, it can lead to unwanted results. In as much as the YoutubeStream object is singleton, the initialization function isn't, so chances of the ytStream being initialized twice are high.

I suggest we either do a single or double lock synchronization when creating the ytStream

Example:

private val lock = Any()

    fun init(context: Context) {
        synchronized(lock) {
            if (ytStream == null) {
                ytStream = YTStream(context = context)
            }
        }
    }

It is something we can probably, think about

@GibsonRuitiari GibsonRuitiari added the enhancement New feature or request label Jan 31, 2024
@MamboBryan
Copy link
Contributor

Okay I think we can add this enhancement to v0.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants