Skip to content

Commit

Permalink
refactor: 💡 make Versioned state be an object
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 30, 2021
1 parent cc1ef6d commit 32a61bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import { SerializableState, VersionedState, MigrateFunctionsObject } from './typ

export function migrateToLatest<S extends SerializableState>(
migrations: MigrateFunctionsObject,
[state, oldVersion]: VersionedState
{ state, version: oldVersion }: VersionedState
): VersionedState<S> {
const versions = Object.keys(migrations || {})
.filter((v) => compare(v, oldVersion) > 0)
.sort(compare);

if (!versions.length) return [state, oldVersion] as VersionedState<S>;
if (!versions.length) return { state, version: oldVersion } as VersionedState<S>;

for (const version of versions) {
state = migrations[version]!(state);
}

return [state as S, versions[versions.length - 1]];
return {
state: state as S,
version: versions[versions.length - 1],
};
}
15 changes: 10 additions & 5 deletions src/plugins/kibana_utils/common/persistable_state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ export type Serializable = SerializableValue | SerializableValue[];
* For example:
*
* ```ts
* const obj: VersionedState<{ dashboardId: string }> = [{ dashboardId: '123' }, '7.14.0'];
* const obj: VersionedState<{ dashboardId: string }> = {
* version: '7.14.0',
* state: {
* dashboardId: '123',
* },
* };
* ```
*/
export type VersionedState<S extends SerializableState = SerializableState> = [
state: S,
version: string
];
export interface VersionedState<S extends SerializableState = SerializableState> {
version: string;
state: S;
}

/**
* Persistable state interface can be implemented by something that persists
Expand Down

0 comments on commit 32a61bf

Please sign in to comment.