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

Tweak PersistenceProvider types #3896

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions core/persist/PersistenceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ export abstract class PersistenceProvider<S> {

private disposer: IReactionDisposer;

private static readonly PROVIDERS: Record<PersistenceProviderType, typeof PersistenceProvider> =
{
pref: PrefProvider,
localStorage: LocalStorageProvider,
sessionStorage: SessionStorageProvider,
dashView: DashViewProvider,
viewManager: ViewManagerProvider,
custom: CustomProvider
};
private static readonly PROVIDERS: Record<
PersistenceProviderType,
new <S>(cfg: PersistenceProviderConfig<S>) => PersistenceProvider<S>
> = {
pref: PrefProvider,
localStorage: LocalStorageProvider,
sessionStorage: SessionStorageProvider,
dashView: DashViewProvider,
viewManager: ViewManagerProvider,
custom: CustomProvider
};

/**
* Register a custom `PersistenceProvider` subclass for use by the {@link create} factory called
Expand All @@ -87,7 +89,10 @@ export abstract class PersistenceProvider<S> {
* @param type - provider identifier to support as a `PersistOptions.type` value.
* @param provider - the custom provider class to instantiate.
*/
static registerType(type: PersistenceProviderType, provider: typeof PersistenceProvider) {
static registerType(
type: PersistenceProviderType,
provider: new <S>(cfg: PersistenceProviderConfig<S>) => PersistenceProvider<S>
) {
this.PROVIDERS[type] = provider;
}

Expand Down Expand Up @@ -122,10 +127,10 @@ export abstract class PersistenceProvider<S> {
if (rest.getData || rest.setData) type = 'custom';
}

const clazz = this.PROVIDERS[type] as any;
const clazz = this.PROVIDERS[type];
throwIf(!clazz, `Unknown Persistence Provider type: ${type}`);

ret = new clazz(cfg) as PersistenceProvider<S>;
ret = new clazz(cfg);
ret.bindToTarget(target);
return ret;
} catch (e) {
Expand Down