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

[Chore] Add composedReducerSchema to schema manager #2633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 32 additions & 12 deletions src/schemas/src/schema-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,40 @@ 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';
_reducerSchemas: typeof reducerSchema;
_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;
Expand Down Expand Up @@ -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]) : {})
}),
{}
);
Expand Down Expand Up @@ -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])
: {})
}),
{}
Expand Down
Loading