Skip to content

Possible improvements to IMO UI options #14

Description

@webJose

Motivation

Currently, options are set via the @collagejs/vite-im plug-in, but they only "work" as authoritative source if there are no settings stored in local storage. Once a copy exists in local storage, local storage becomes the authoritative source.

But there are 2 scenarios that need to be accounted for:

  1. As new options come or old options go, how can we clean up local storage/upgrade stored settings?
  2. It is conceivable to want the ability to just "reset" local storage. Should a mechanism be provided for this from "outside" (i. e., from @collagejs/vite-im or "manual" configurations)?

While I can think of a proposal now for the first scenario, I'm unsure if the second scenario would be at all desirable.

I did write in src/shared/options.ts 3 functions related to the UI options in local storage, and I exported them:

  • readCurrentImoUiOptions
  • writeImoUiOptions
  • deleteStoredImoUiOptions

These are currently not in use anywhere, and my guess is that I prepared these to export them for library consumption, but they aren't currently exposed outside the library.

Proposal

The package currently uses the svelte-persisted-state package to create a reactive mirror of the local storage data. This package provides the ability to set a callback that can transform/upgrade stored data before it is returned.

Especifically, the initImoUiOptions function in src/lib/state/imoUiOptions.ts:

/**
 * Initializes the Svelte reactive store that contains the options for the user interface piece of
 * the `@collagejs/imo` library.
 * @param initialValue Initial options to configure.
 */
export function initImoUiOptions(initialValue: RequiredImoUiOptions) {
    imoUiOptions = persistedState(skImoUiOptions, initialValue, {
        storage: 'local',
        syncTabs: true,
        beforeRead(value) {
            // Upgrade using initialValue as authoritative source for new properties.
            ...
            return upgradedOptions;
        },
    });
}

As seen in the sample code, we can just write arbitrary logic to upgrade stored objects at will. The value parameter of the callback is the stored value in local storage, and one can use the initialValue parameter from the function to run the upgrade.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions