From 573a63aaf543f37d64c2cc54dbeac8f3d8907b9c Mon Sep 17 00:00:00 2001 From: Ihor Dykhta Date: Mon, 9 Sep 2024 23:47:08 +0300 Subject: [PATCH] [Chore] Add composedReducerSchema to schema manager Signed-off-by: Ihor Dykhta --- src/schemas/src/schema-manager.ts | 44 ++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/schemas/src/schema-manager.ts b/src/schemas/src/schema-manager.ts index 4260a282fe..33635f3568 100644 --- a/src/schemas/src/schema-manager.ts +++ b/src/schemas/src/schema-manager.ts @@ -74,6 +74,19 @@ export const reducerSchema: { mapStyle: mapStyleSchema }; +export interface ComposedReducerSchema { + save(state: any): any; + load(config: SavedMap): SavedMap; +} + +export type KeplerGLSchemaProps = { + reducers?: typeof reducerSchema; + datasets?: typeof datasetSchema; + validVersions?: typeof VERSIONS; + version?: 'v1'; + composedReducerSchema?: ComposedReducerSchema; +}; + export class KeplerGLSchema { _validVersions: typeof VERSIONS; _version: 'v1'; @@ -81,22 +94,20 @@ export class KeplerGLSchema { _datasetSchema: typeof datasetSchema; _datasetLastSaved: SavedDatasetV1[] | null; _savedDataset: SavedDatasetV1[] | null; + _composedReducerSchema: ComposedReducerSchema | null; constructor({ reducers = reducerSchema, datasets = datasetSchema, validVersions = VERSIONS, - version = CURRENT_VERSION - }: { - reducers?: typeof reducerSchema; - datasets?: typeof datasetSchema; - validVersions?: typeof VERSIONS; - version?: 'v1'; - } = {}) { + version = CURRENT_VERSION, + composedReducerSchema +}: KeplerGLSchemaProps = {}) { this._validVersions = validVersions; this._version = version; this._reducerSchemas = reducers; this._datasetSchema = datasets; + this._composedReducerSchema = composedReducerSchema || null; this._datasetLastSaved = null; this._savedDataset = null; @@ -199,14 +210,18 @@ export class KeplerGLSchema { /** * Get App config to save - * @param {Object} state - app state - * @returns {{version: String, config: Object}} - config to save + * @param state - app state + * @returns - config to save */ getConfigToSave(state: any): SavedConfigV1 { + const toSave = + typeof this._composedReducerSchema?.save === 'function' + ? this._composedReducerSchema.save(state) + : state; const config = Object.keys(this._reducerSchemas).reduce( (accu, key) => ({ ...accu, - ...(state[key] ? this._reducerSchemas[key][this._version].save(state[key]) : {}) + ...(toSave[key] ? this._reducerSchemas[key][this._version].save(toSave[key]) : {}) }), {} ); @@ -244,11 +259,16 @@ export class KeplerGLSchema { return null; } - return Object.keys(config).reduce( + const toLoad = + typeof this._composedReducerSchema?.load === 'function' + ? this._composedReducerSchema.load(config) + : config; + + return Object.keys(toLoad).reduce( (accu, key) => ({ ...accu, ...(key in this._reducerSchemas - ? this._reducerSchemas[key][validVersion].load(config[key]) + ? this._reducerSchemas[key][validVersion].load(toLoad[key]) : {}) }), {}