O
O
okuznetsov12018-04-26 16:16:22
JavaScript
okuznetsov1, 2018-04-26 16:16:22

How to execute a line containing code (angular 4 + typescript + redux)?

There is a string variable template, into which the code of such a plan periodically comes, while this variable is in epic-e:

private createTranslateChangedEpic() {
    template = 'concat(
        of(new SummaryActivitySitesActions().setPage('4c3ef357', {page: 1})).delay(100),
        of(new SummaryActivitySitesActions().setPage('6d8540f0', {page: 1})).delay(100)
    )';
return <template>;

In general, you need to somehow transform the "template" string into code that will be executed and the value from epic-a will be returned.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
okuznetsov1, 2018-04-27
@okuznetsov1

Two solutions, someone will definitely come in handy.

import * as ts from 'typescript';
import {of} from 'rxjs/observable/of';
import {concat} from 'rxjs/observable/concat';

private createTranslateChangedEpic() {
                    const g = Groups.getSelectedGroupForSelectedPerspective(storeState);
                    const reportId1 = storeState.groups[0].id;
                    const reportId2 = storeState.groups[1].id;

                    const code = `({
                            Run: (concat: <T>, of: <T>, actions: SummaryActivitySitesActions, reportId1, reportId2) => {
                                console.log('run');
                                return concat(
                                    of(actions.setPage(reportId1, {page: 1})).delay(100),
                                    of(actions.setPage(reportId2, {page: 1})).delay(100)
                                );
                            }
                    })`;
                    const result = ts.transpile(code);
                    const runnalbe = eval(result);

                    return runnalbe.Run(concat, of, this.actions, reportId1, reportId2);
}

private createTranslateChangedEpic() {
                    const g = Groups.getSelectedGroupForSelectedPerspective(storeState);
                    const reportId1 = storeState.groups[0].id;
                    const reportId2 = storeState.groups[1].id;

                    const f = new Function(
                        'concat',
                        'of',
                        'actions',
                        'reportId1',
                        'reportId2',
                        `return function (concat, of, actions, reportId1, reportId2) {
                                    console.log('run');
                                    return concat(
                                        of(actions.setPage(reportId1, {page: 1})).delay(100),
                                        of(actions.setPage(reportId2, {page: 1})).delay(100)
                                    );
                        }`
                    )(concat, of, this.actions, reportId1, reportId2);

                    return f(concat, of, this.actions, reportId1, reportId2);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question