I
I
Ilya Sviridov2019-08-13 15:31:40
JavaScript
Ilya Sviridov, 2019-08-13 15:31:40

How to improve the tree processing module?

updateChildren(model, modes) {
        model.get('children').forEach(child => {
            if (child.has('children')) {
                modes.forEach(mode => this[mode.actionName](child, mode));
                this.applyTreeModes(child, modes);
            }
        });
    },

    setCollapsed(model, options) {
        const { value, expandedConfiguration, save, reqres } = options;
        const currentValue = (typeof value === 'boolean') ? value : NavigationItemFactory.getCollapsed(expandedConfiguration, model.get('id'));
        model.set('collapsed', currentValue);
        if (save) {
            reqres.request('configuration:collapsed:save', model);
        }
    },

    setCurrentEdited(model, options) {
        const { value } = options;
        model.set('currentEdited', value);
    },

    setChecked(model, options) {
        const { value } = options;
        model.set('checked', value);
    },

    setFilterChecked(model, options) {
        const { value, filter } = options;
        if (filter.length === 0 || filter.indexOf(model.get('id')) !== -1) {
            model.set('checked', value);
        }
    },

    formSelectedConfiguration(model, options) {
        const { selectedConfiguration } = options;
        if (model.get('checked')) {
            selectedConfiguration.push(model.get('id'));
        }
    }

Wood structure processing module.
It is taken out separately, reuse of the code is required.
async __editMode() {
        this.model.set('editMode', !this.model.get('editMode'));
        const editMode = this.model.get('editMode');

        if (editMode) {
            await this.reload({ editMode: editMode });
            TreeService.updateChildren(this.model, [
                { actionName: 'setFilterChecked', value: true, filter: this.configuration.selectedConfiguration || [] },
                { actionName: 'setCurrentEdited', value: true },
                { actionName: 'setCollapsed', value: false, save: true, reqres: this.reqres }
            ]);
        } else {
            const selectedConfiguration = [];

            TreeService.updateChildren(this.model, [
                { actionName: 'setCurrentEdited', value: false },
                { actionName: 'formSelectedConfiguration', selectedConfiguration }
            ]);
            this.__applyConfigurationChanges(selectedConfiguration, false);
        }
    }

An example of using the module.
I would like to get an assessment in terms of:
- further support of the code (by another developer)
- scalability
- speed
What happens in the module?
It is necessary to go through children and perform actions with each child (operations are independent)
A tree node / array is passed. For each child, all processing methods from the array are applied for 1 DFS iteration.
The array contains:
- actionName for selecting the processing method child
- options for child

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexHell, 2019-08-23
@AlexHell

This is all not typed - no specific classes / functions, but 'setCurrentEdited' is passed by name, which you need to search for by text search, and its options value: false - bad from the point of view of architecture
.. but it depends on your tasks, you can leave it like that , if you don’t need to return to this too often, in principle you can understand, but then at least all these classes are in a separate directory, and there are no extra dependencies, otherwise
ps will grow and model, modes are a very bad name, in 1x this difference is only in one character, and there will be confusion in the body of the method (someone will miss the last letter), I’ll tell you purely from experience
, besides, modes doesn’t say at all what kind of mode
is better - actions
what to
- scalability
- speed
.. then you should measure it in your own conditions.. afraid of slow interpretation of dynamic function names and passing parameters to them? read the docs, maybe someone else will answer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question