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

Factories for JS and Linux #202

Open
SaeedZhiany opened this issue Aug 31, 2024 · 3 comments
Open

Factories for JS and Linux #202

SaeedZhiany opened this issue Aug 31, 2024 · 3 comments

Comments

@SaeedZhiany
Copy link

Are there any implemented JS and Linux Factories that are ready to use? I can't find any. If there's no such implementation yet, how should I implement my own? is there any example to get started?

Thanks

@russhwolf
Copy link
Owner

The JS implemenetation does not include a factory, because the backing API doesn't support multiple named instances.

There currently is no Linux implementation shipping with the library, but there are some drafts that are open as PRs. You can take a look at those and see if any of them work for you. I'm interested in feedback on what's most useful as a backing API on Linux from an interop perspective, see #113

@SaeedZhiany
Copy link
Author

For JS implementation, we could simulate multiple named instances. my first idea is to use the name of each instance as a Prefix for the keys defined in the instance.

something like this code (I've not test it though):

class JsSettings(name: String?, delegate: Storage?) : Settings {
    private val keyPrefix = if (name != null) "${name}_" else ""
    private val underlyingSetting: Settings = if (delegate != null) StorageSettings(delegate) else StorageSettings()

    public override val keys: Set<String> get() = underlyingSetting.keys.filter { it.startsWith(keyPrefix) }.toSet()
    public override val size: Int get() = keys.size

    private fun getStorageKey(key: String): String = "${keyPrefix}_$key"

    override fun clear() = keys.forEach { key -> underlyingSetting.remove(key) }

    override fun getBoolean(key: String, defaultValue: Boolean): Boolean = underlyingSetting.getBoolean(getStorageKey(key), defaultValue)

    override fun getBooleanOrNull(key: String): Boolean? = underlyingSetting.getBooleanOrNull(getStorageKey(key))

    override fun getDouble(key: String, defaultValue: Double): Double = underlyingSetting.getDouble(getStorageKey(key), defaultValue)

    override fun getDoubleOrNull(key: String): Double? = underlyingSetting.getDoubleOrNull(getStorageKey(key))

    override fun getFloat(key: String, defaultValue: Float): Float = underlyingSetting.getFloat(getStorageKey(key), defaultValue)

    override fun getFloatOrNull(key: String): Float? = underlyingSetting.getFloatOrNull(getStorageKey(key))

    override fun getInt(key: String, defaultValue: Int): Int = underlyingSetting.getInt(getStorageKey(key), defaultValue)

    override fun getIntOrNull(key: String): Int? = underlyingSetting.getIntOrNull(getStorageKey(key))

    override fun getLong(key: String, defaultValue: Long): Long = underlyingSetting.getLong(getStorageKey(key), defaultValue)

    override fun getLongOrNull(key: String): Long? = underlyingSetting.getLongOrNull(getStorageKey(key))

    override fun getString(key: String, defaultValue: String): String = underlyingSetting.getString(getStorageKey(key), defaultValue)

    override fun getStringOrNull(key: String): String? = underlyingSetting.getStringOrNull(getStorageKey(key))

    override fun hasKey(key: String): Boolean = underlyingSetting.hasKey(getStorageKey(key))

    override fun putBoolean(key: String, value: Boolean) = underlyingSetting.putBoolean(getStorageKey(key), value)

    override fun putDouble(key: String, value: Double) = underlyingSetting.putDouble(getStorageKey(key), value)

    override fun putFloat(key: String, value: Float) = underlyingSetting.putFloat(getStorageKey(key), value)

    override fun putInt(key: String, value: Int) = underlyingSetting.putInt(getStorageKey(key), value)

    override fun putLong(key: String, value: Long) = underlyingSetting.putLong(getStorageKey(key), value)

    override fun putString(key: String, value: String) = underlyingSetting.putString(getStorageKey(key), value)

    override fun remove(key: String) = underlyingSetting.remove(getStorageKey(key))

    class Factory : Settings.Factory {
        override fun create(name: String?): Settings = JsSettings(name, null)
    }
}

@russhwolf
Copy link
Owner

I'm hesitant to include something like that because the interop story gets messy. Now if you talk to LocalStorage from JS code it looks different than from Kotlin. But the whole reason that Settings is an interface and not an expect class is so you can provide your own implementations. Give it a try in your codebase and see how it goes.

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

2 participants