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

0 like 0 dislike
7 views
A string variable template, which periodically returns to code such a plan, while this variable is 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 ;


In General, you need some way to transform the string "template" in the code which is executed and will return the value of the epic-a.
by | 7 views

1 Answer

0 like 0 dislike
Two solutions, someone will definitely come in handy.

Option 1

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: , of: , 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); }


Option 2

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); }
by

Related questions

110,608 questions
257,186 answers
0 comments
33,714 users