refactor application.yaml to become an os definition #40

This commit is contained in:
undergroundwires
2020-09-08 21:47:18 +01:00
parent e4b6cdfb18
commit f7557bcc0f
62 changed files with 1926 additions and 573 deletions

View File

@@ -0,0 +1,43 @@
// Because we cannot do "T extends enum" 😞 https://github.com/microsoft/TypeScript/issues/30611
type EnumType = number | string;
type EnumVariable<T extends EnumType, TEnumValue extends EnumType> = { [key in T]: TEnumValue };
export interface IEnumParser<TEnum> {
parseEnum(value: string, propertyName: string): TEnum;
}
export function createEnumParser<T extends EnumType, TEnumValue extends EnumType>(
enumVariable: EnumVariable<T, TEnumValue>): IEnumParser<TEnumValue> {
return {
parseEnum: (value, propertyName) => parseEnumValue(value, propertyName, enumVariable),
};
}
function parseEnumValue<T extends EnumType, TEnumValue extends EnumType>(
value: string,
enumName: string,
enumVariable: EnumVariable<T, TEnumValue>): TEnumValue {
if (!value) {
throw new Error(`undefined ${enumName}`);
}
if (typeof value !== 'string') {
throw new Error(`unexpected type of ${enumName}: "${typeof value}"`);
}
const casedValue = getEnumNames(enumVariable)
.find((enumValue) => enumValue.toLowerCase() === value.toLowerCase());
if (!casedValue) {
throw new Error(`unknown ${enumName}: "${value}"`);
}
return enumVariable[casedValue as keyof typeof enumVariable];
}
export function getEnumNames<T extends EnumType, TEnumValue extends EnumType>(
enumVariable: EnumVariable<T, TEnumValue>): string[] {
return Object
.values(enumVariable)
.filter((enumMember) => typeof enumMember === 'string') as string[];
}
export function getEnumValues<T extends EnumType, TEnumValue extends EnumType>(
enumVariable: EnumVariable<T, TEnumValue>): TEnumValue[] {
return getEnumNames(enumVariable)
.map((level) => enumVariable[level]) as TEnumValue[];
}

View File

@@ -1,14 +1,18 @@
import { Category } from '@/domain/Category';
import { Application } from '@/domain/Application';
import { IApplication } from '@/domain/IApplication';
import { IProjectInformation } from '@/domain/IProjectInformation';
import { ApplicationYaml } from 'js-yaml-loader!./../application.yaml';
import { YamlApplication } from 'js-yaml-loader!./../application.yaml';
import { parseCategory } from './CategoryParser';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { parseProjectInformation } from './ProjectInformationParser';
import { ScriptCompiler } from './Compiler/ScriptCompiler';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { parseScriptingDefinition } from './ScriptingDefinitionParser';
import { createEnumParser } from '../Common/Enum';
export function parseApplication(content: ApplicationYaml, env: NodeJS.ProcessEnv = process.env): IApplication {
export function parseApplication(
content: YamlApplication,
env: NodeJS.ProcessEnv = process.env,
osParser = createEnumParser(OperatingSystem)): IApplication {
validate(content);
const compiler = new ScriptCompiler(content.functions);
const categories = new Array<Category>();
@@ -16,23 +20,18 @@ export function parseApplication(content: ApplicationYaml, env: NodeJS.ProcessEn
const category = parseCategory(action, compiler);
categories.push(category);
}
const info = readAppInformation(env);
const os = osParser.parseEnum(content.os, 'os');
const info = parseProjectInformation(env);
const scripting = parseScriptingDefinition(content.scripting, info);
const app = new Application(
os,
info,
categories);
categories,
scripting);
return app;
}
function readAppInformation(environment: NodeJS.ProcessEnv): IProjectInformation {
return new ProjectInformation(
environment.VUE_APP_NAME,
environment.VUE_APP_VERSION,
environment.VUE_APP_REPOSITORY_URL,
environment.VUE_APP_HOMEPAGE_URL,
);
}
function validate(content: ApplicationYaml): void {
function validate(content: YamlApplication): void {
if (!content) {
throw new Error('application is null or undefined');
}

View File

@@ -0,0 +1,73 @@
export interface IILCode {
compile(): string;
getUniqueParameterNames(): string[];
substituteParameter(parameterName: string, parameterValue: string): IILCode;
}
export function generateIlCode(rawText: string): IILCode {
const ilCode = generateIl(rawText);
return new ILCode(ilCode);
}
class ILCode implements IILCode {
private readonly ilCode: string;
constructor(ilCode: string) {
this.ilCode = ilCode;
}
public substituteParameter(parameterName: string, parameterValue: string): IILCode {
const newCode = substituteParameter(this.ilCode, parameterName, parameterValue);
return new ILCode(newCode);
}
public getUniqueParameterNames(): string[] {
return getUniqueParameterNames(this.ilCode);
}
public compile(): string {
ensureNoExpressionLeft(this.ilCode);
return this.ilCode;
}
}
// Trim each expression and put them inside "{{exp|}}" e.g. "{{ $hello }}" becomes "{{exp|$hello}}"
function generateIl(rawText: string): string {
return rawText.replace(/\{\{([\s]*[^;\s\{]+[\s]*)\}\}/g, (_, match) => {
return `\{\{exp|${match.trim()}\}\}`;
});
}
// finds all "{{exp|..}} left"
function ensureNoExpressionLeft(ilCode: string) {
const allSubstitutions = ilCode.matchAll(/\{\{exp\|(.*?)\}\}/g);
const allMatches = Array.from(allSubstitutions, (match) => match[1]);
const uniqueExpressions = getDistinctValues(allMatches);
if (uniqueExpressions.length > 0) {
throw new Error(`unknown expression: ${printList(uniqueExpressions)}`);
}
}
// Parses all distinct usages of {{exp|$parameterName}}
function getUniqueParameterNames(ilCode: string) {
const allSubstitutions = ilCode.matchAll(/\{\{exp\|\$([^;\s\{]+[\s]*)\}\}/g);
const allParameters = Array.from(allSubstitutions, (match) => match[1]);
const uniqueParameterNames = getDistinctValues(allParameters);
return uniqueParameterNames;
}
// substitutes {{exp|$parameterName}} to value of the parameter
function substituteParameter(ilCode: string, parameterName: string, parameterValue: string) {
const pattern = `{{exp|$${parameterName}}}`;
return ilCode.split(pattern).join(parameterValue); // as .replaceAll() is not yet supported by TS
}
function getDistinctValues(values: readonly string[]): string[] {
return values.filter((value, index, self) => {
return self.indexOf(value) === index;
});
}
function printList(list: readonly string[]): string {
return `"${list.join('","')}"`;
}

View File

@@ -1,3 +1,4 @@
import { generateIlCode, IILCode } from './ILCode';
import { IScriptCode } from '@/domain/IScriptCode';
import { ScriptCode } from '@/domain/ScriptCode';
import { YamlScript, YamlFunction, FunctionCall, ScriptFunctionCall, FunctionCallParameters } from 'js-yaml-loader!./application.yaml';
@@ -125,20 +126,22 @@ function compileCode(func: YamlFunction, parameters: FunctionCallParameters): IC
}
function compileExpressions(code: string, parameters: FunctionCallParameters): string {
let intermediateCode = compileToIL(code);
let intermediateCode = generateIlCode(code);
intermediateCode = substituteParameters(intermediateCode, parameters);
ensureNoExpressionLeft(intermediateCode);
return intermediateCode;
return intermediateCode.compile();
}
function substituteParameters(intermediateCode: string, parameters: FunctionCallParameters): string {
const parameterNames = getUniqueParameterNamesFromIL(intermediateCode);
function substituteParameters(intermediateCode: IILCode, parameters: FunctionCallParameters): IILCode {
const parameterNames = intermediateCode.getUniqueParameterNames();
if (parameterNames.length && !parameters) {
throw new Error(`no parameters defined, expected: ${printList(parameterNames)}`);
}
for (const parameterName of parameterNames) {
const parameterValue = parameters[parameterName];
intermediateCode = substituteParameter(intermediateCode, parameterName, parameterValue);
if (!parameterValue) {
throw Error(`parameter value is not provided for "${parameterName}" in function call`);
}
intermediateCode = intermediateCode.substituteParameter(parameterName, parameterValue);
}
return intermediateCode;
}
@@ -158,43 +161,3 @@ function getCallSequence(call: ScriptFunctionCall): FunctionCall[] {
}
return [ call as FunctionCall ];
}
function getDistinctValues(values: readonly string[]): string[] {
return values.filter((value, index, self) => {
return self.indexOf(value) === index;
});
}
// Trim each expression and put them inside "{{exp|}}" e.g. "{{ $hello }}" becomes "{{exp|$hello}}"
function compileToIL(code: string) {
return code.replace(/\{\{([\s]*[^;\s\{]+[\s]*)\}\}/g, (_, match) => {
return `\{\{exp|${match.trim()}\}\}`;
});
}
// Parses all distinct usages of {{exp|$parameterName}}
function getUniqueParameterNamesFromIL(ilCode: string) {
const allSubstitutions = ilCode.matchAll(/\{\{exp\|\$([^;\s\{]+[\s]*)\}\}/g);
const allParameters = Array.from(allSubstitutions, (match) => match[1]);
const uniqueParameterNames = getDistinctValues(allParameters);
return uniqueParameterNames;
}
// substitutes {{exp|$parameterName}} to value of the parameter
function substituteParameter(ilCode: string, parameterName: string, parameterValue: string) {
if (!parameterValue) {
throw Error(`parameter value is not provided for "${parameterName}" in function call`);
}
const pattern = `{{exp|$${parameterName}}}`;
return ilCode.split(pattern).join(parameterValue); // as .replaceAll() is not yet supported by TS
}
// finds all "{{exp|..}} left"
function ensureNoExpressionLeft(ilCode: string) {
const allSubstitutions = ilCode.matchAll(/\{\{exp\|(.*?)\}\}/g);
const allMatches = Array.from(allSubstitutions, (match) => match[1]);
const uniqueExpressions = getDistinctValues(allMatches);
if (uniqueExpressions.length > 0) {
throw new Error(`unknown expression: ${printList(uniqueExpressions)}`);
}
}

View File

@@ -0,0 +1,12 @@
import { IProjectInformation } from '@/domain/IProjectInformation';
import { ProjectInformation } from '@/domain/ProjectInformation';
export function parseProjectInformation(
environment: NodeJS.ProcessEnv): IProjectInformation {
return new ProjectInformation(
environment.VUE_APP_NAME,
environment.VUE_APP_VERSION,
environment.VUE_APP_REPOSITORY_URL,
environment.VUE_APP_HOMEPAGE_URL,
);
}

View File

@@ -1,37 +1,32 @@
import { Script } from '@/domain/Script';
import { YamlScript } from 'js-yaml-loader!./application.yaml';
import { parseDocUrls } from './DocumentationParser';
import { RecommendationLevelNames, RecommendationLevel } from '@/domain/RecommendationLevel';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { IScriptCompiler } from './Compiler/IScriptCompiler';
import { IScriptCode } from '@/domain/IScriptCode';
import { ScriptCode } from '@/domain/ScriptCode';
import { createEnumParser, IEnumParser } from '../Common/Enum';
export function parseScript(yamlScript: YamlScript, compiler: IScriptCompiler): Script {
export function parseScript(
yamlScript: YamlScript, compiler: IScriptCompiler,
levelParser = createEnumParser(RecommendationLevel)): Script {
validateScript(yamlScript);
if (!compiler) {
throw new Error('undefined compiler');
}
const script = new Script(
/* name */ yamlScript.name,
/* code */ parseCode(yamlScript, compiler),
/* docs */ parseDocUrls(yamlScript),
/* level */ getLevel(yamlScript.recommend));
/* name */ yamlScript.name,
/* code */ parseCode(yamlScript, compiler),
/* docs */ parseDocUrls(yamlScript),
/* level */ parseLevel(yamlScript.recommend, levelParser));
return script;
}
function getLevel(level: string): RecommendationLevel | undefined {
function parseLevel(level: string, parser: IEnumParser<RecommendationLevel>): RecommendationLevel | undefined {
if (!level) {
return undefined;
}
if (typeof level !== 'string') {
throw new Error(`level must be a string but it was ${typeof level}`);
}
const typedLevel = RecommendationLevelNames
.find((l) => l.toLowerCase() === level.toLowerCase());
if (!typedLevel) {
throw new Error(`unknown level: \"${level}\"`);
}
return RecommendationLevel[typedLevel as keyof typeof RecommendationLevel];
return parser.parseEnum(level, 'level');
}
function parseCode(yamlScript: YamlScript, compiler: IScriptCompiler): IScriptCode {

View File

@@ -0,0 +1,36 @@
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import { YamlScriptingDefinition } from 'js-yaml-loader!./application.yaml';
import { ScriptingDefinition } from '@/domain/ScriptingDefinition';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { IProjectInformation } from '@/domain/IProjectInformation';
import { createEnumParser } from '../Common/Enum';
import { generateIlCode } from './Compiler/ILCode';
export function parseScriptingDefinition(
definition: YamlScriptingDefinition,
info: IProjectInformation,
date = new Date(),
languageParser = createEnumParser(ScriptingLanguage)): IScriptingDefinition {
if (!info) {
throw new Error('undefined info');
}
if (!definition) {
throw new Error('undefined definition');
}
const language = languageParser.parseEnum(definition.language, 'language');
const startCode = applySubstitutions(definition.startCode, info, date);
const endCode = applySubstitutions(definition.endCode, info, date);
return new ScriptingDefinition(
language,
startCode,
endCode,
);
}
function applySubstitutions(code: string, info: IProjectInformation, date: Date): string {
let ilCode = generateIlCode(code);
ilCode = ilCode.substituteParameter('homepage', info.homepage);
ilCode = ilCode.substituteParameter('version', info.version);
ilCode = ilCode.substituteParameter('date', date.toUTCString());
return ilCode.compile();
}

View File

@@ -0,0 +1,20 @@
import { IApplicationContext } from './IApplicationContext';
import { IApplication } from '@/domain/IApplication';
import { IApplicationState } from './IApplicationState';
import { ApplicationState } from './ApplicationState';
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
import { parseApplication } from '../Parser/ApplicationParser';
export function createContext(): IApplicationContext {
const application = parseApplication(applicationFile);
const context = new ApplicationContext(application);
return context;
}
export class ApplicationContext implements IApplicationContext {
public readonly state: IApplicationState;
public constructor(public readonly app: IApplication) {
this.state = new ApplicationState(app);
}
}

View File

@@ -0,0 +1,9 @@
import { ApplicationContext } from './ApplicationContext';
import { IApplicationContext } from '@/application/State/IApplicationContext';
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
import { parseApplication } from '@/application/Parser/ApplicationParser';
export function buildContext(): IApplicationContext {
const application = parseApplication(applicationFile);
return new ApplicationContext(application);
}

View File

@@ -3,43 +3,18 @@ import { IUserFilter } from './Filter/IUserFilter';
import { ApplicationCode } from './Code/ApplicationCode';
import { UserSelection } from './Selection/UserSelection';
import { IUserSelection } from './Selection/IUserSelection';
import { AsyncLazy } from '@/infrastructure/Threading/AsyncLazy';
import { Signal } from '@/infrastructure/Events/Signal';
import { parseApplication } from '../Parser/ApplicationParser';
import { IApplicationState } from './IApplicationState';
import { Script } from '@/domain/Script';
import { IApplication } from '@/domain/IApplication';
import { IApplicationCode } from './Code/IApplicationCode';
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
/** Mutatable singleton application state that's the single source of truth throughout the application */
export class ApplicationState implements IApplicationState {
/** Get singleton application state */
public static GetAsync(): Promise<IApplicationState> {
return ApplicationState.instance.getValueAsync();
}
/** Application instance with all scripts. */
private static instance = new AsyncLazy<IApplicationState>(() => {
const application = parseApplication(applicationFile);
const selectedScripts = new Array<Script>();
const state = new ApplicationState(application, selectedScripts);
return Promise.resolve(state);
});
public readonly code: IApplicationCode;
public readonly stateChanged = new Signal<IApplicationState>();
public readonly selection: IUserSelection;
public readonly filter: IUserFilter;
private constructor(
/** Inner instance of the all scripts */
public readonly app: IApplication,
/** Initially selected scripts */
public readonly defaultScripts: Script[]) {
this.selection = new UserSelection(app, defaultScripts.map((script) => new SelectedScript(script, false)));
this.code = new ApplicationCode(this.selection, app.info.version);
public constructor(readonly app: IApplication) {
this.selection = new UserSelection(app, []);
this.code = new ApplicationCode(this.selection, app.scripting);
this.filter = new UserFilter(app);
}
}

View File

@@ -7,6 +7,7 @@ import { UserScriptGenerator } from './Generation/UserScriptGenerator';
import { Signal } from '@/infrastructure/Events/Signal';
import { IApplicationCode } from './IApplicationCode';
import { IUserScriptGenerator } from './Generation/IUserScriptGenerator';
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
export class ApplicationCode implements IApplicationCode {
public readonly changed = new Signal<ICodeChangedEvent>();
@@ -16,10 +17,10 @@ export class ApplicationCode implements IApplicationCode {
constructor(
userSelection: IUserSelection,
private readonly version: string,
private readonly scriptingDefinition: IScriptingDefinition,
private readonly generator: IUserScriptGenerator = new UserScriptGenerator()) {
if (!userSelection) { throw new Error('userSelection is null or undefined'); }
if (!version) { throw new Error('version is null or undefined'); }
if (!scriptingDefinition) { throw new Error('scriptingDefinition is null or undefined'); }
if (!generator) { throw new Error('generator is null or undefined'); }
this.setCode(userSelection.selectedScripts);
userSelection.changed.on((scripts) => {
@@ -29,7 +30,7 @@ export class ApplicationCode implements IApplicationCode {
private setCode(scripts: ReadonlyArray<SelectedScript>): void {
const oldScripts = Array.from(this.scriptPositions.keys());
const code = this.generator.buildCode(scripts, this.version);
const code = this.generator.buildCode(scripts, this.scriptingDefinition);
this.current = code.code;
this.scriptPositions = code.scriptPositions;
const event = new CodeChangedEvent(code.code, oldScripts, code.scriptPositions);

View File

@@ -1,7 +1,9 @@
import { ICodeBuilder } from './ICodeBuilder';
const NewLine = '\n';
const TotalFunctionSeparatorChars = 58;
export class CodeBuilder {
export class CodeBuilder implements ICodeBuilder {
private readonly lines = new Array<string>();
// Returns current line starting from 0 (no lines), or 1 (have single line)
@@ -54,7 +56,7 @@ export class CodeBuilder {
return this
.appendTrailingHyphensCommentLine()
.appendCommentLine(firstHyphens + sectionName + secondHyphens)
.appendTrailingHyphensCommentLine();
.appendTrailingHyphensCommentLine(TotalFunctionSeparatorChars);
}
public toString(): string {

View File

@@ -0,0 +1,9 @@
export interface ICodeBuilder {
currentLine: number;
appendLine(code?: string): ICodeBuilder;
appendTrailingHyphensCommentLine(totalRepeatHyphens: number): ICodeBuilder;
appendCommentLine(commentLine?: string): ICodeBuilder;
appendCommentLineWithHyphensAround(sectionName: string, totalRepeatHyphens: number): ICodeBuilder;
appendFunction(name: string, code: string): ICodeBuilder;
toString(): string;
}

View File

@@ -1,7 +1,9 @@
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { IUserScript } from './IUserScript';
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
export interface IUserScriptGenerator {
buildCode(
selectedScripts: ReadonlyArray<SelectedScript>,
version: string): IUserScript;
scriptingDefinition: IScriptingDefinition): IUserScript;
}

View File

@@ -4,55 +4,54 @@ import { CodeBuilder } from './CodeBuilder';
import { ICodePosition } from '@/application/State/Code/Position/ICodePosition';
import { CodePosition } from '../Position/CodePosition';
import { IUserScript } from './IUserScript';
export const adminRightsScript = {
name: 'Ensure admin privileges',
code: 'fltmc >nul 2>&1 || (\n' +
' echo Administrator privileges are required.\n' +
' PowerShell Start -Verb RunAs \'%0\' 2> nul || (\n' +
' echo Right-click on the script and select "Run as administrator".\n' +
' pause & exit 1\n' +
' )\n' +
' exit 0\n' +
')',
};
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import { ICodeBuilder } from './ICodeBuilder';
export class UserScriptGenerator implements IUserScriptGenerator {
public buildCode(selectedScripts: ReadonlyArray<SelectedScript>, version: string): IUserScript {
if (!selectedScripts) { throw new Error('scripts is undefined'); }
if (!version) { throw new Error('version is undefined'); }
constructor(private readonly codeBuilderFactory: () => ICodeBuilder = () => new CodeBuilder()) {
}
public buildCode(
selectedScripts: ReadonlyArray<SelectedScript>,
scriptingDefinition: IScriptingDefinition): IUserScript {
if (!selectedScripts) { throw new Error('undefined scripts'); }
if (!scriptingDefinition) { throw new Error('undefined definition'); }
let scriptPositions = new Map<SelectedScript, ICodePosition>();
if (!selectedScripts.length) {
return { code: '', scriptPositions };
}
const builder = initializeCode(version);
let builder = this.codeBuilderFactory();
builder = initializeCode(scriptingDefinition.startCode, builder);
for (const selection of selectedScripts) {
scriptPositions = appendSelection(selection, scriptPositions, builder);
}
const code = finalizeCode(builder);
const code = finalizeCode(builder, scriptingDefinition.endCode);
return { code, scriptPositions };
}
}
function initializeCode(version: string): CodeBuilder {
return new CodeBuilder()
.appendLine('@echo off')
.appendCommentLine(`https://privacy.sexy — v${version}${new Date().toUTCString()}`)
.appendFunction(adminRightsScript.name, adminRightsScript.code)
function initializeCode(startCode: string, builder: ICodeBuilder): ICodeBuilder {
if (!startCode) {
return builder;
}
return builder
.appendLine(startCode)
.appendLine();
}
function finalizeCode(builder: CodeBuilder): string {
function finalizeCode(builder: ICodeBuilder, endCode: string): string {
if (!endCode) {
return builder.toString();
}
return builder.appendLine()
.appendLine('pause')
.appendLine('exit /b 0')
.appendLine(endCode)
.toString();
}
function appendSelection(
selection: SelectedScript,
scriptPositions: Map<SelectedScript, ICodePosition>,
builder: CodeBuilder): Map<SelectedScript, ICodePosition> {
builder: ICodeBuilder): Map<SelectedScript, ICodePosition> {
const startPosition = builder.currentLine + 1;
appendCode(selection, builder);
const endPosition = builder.currentLine - 1;
@@ -61,7 +60,7 @@ function appendSelection(
return scriptPositions;
}
function appendCode(selection: SelectedScript, builder: CodeBuilder) {
function appendCode(selection: SelectedScript, builder: ICodeBuilder) {
const name = selection.revert ? `${selection.script.name} (revert)` : selection.script.name;
const scriptCode = selection.revert ? selection.script.code.revert : selection.script.code.execute;
builder.appendFunction(name, scriptCode);

View File

@@ -0,0 +1,7 @@
import { IApplication } from '@/domain/IApplication';
import { IApplicationState } from './IApplicationState';
export interface IApplicationContext {
readonly app: IApplication;
readonly state: IApplicationState;
}

View File

@@ -1,15 +1,10 @@
import { IApplication } from './../../domain/IApplication';
import { IUserFilter } from './Filter/IUserFilter';
import { IUserSelection } from './Selection/IUserSelection';
import { ISignal } from '@/infrastructure/Events/ISignal';
import { IApplicationCode } from './Code/IApplicationCode';
export { IUserSelection, IApplicationCode, IUserFilter };
export interface IApplicationState {
/** Event that fires when the application states changes with new application state as parameter */
readonly code: IApplicationCode;
readonly filter: IUserFilter;
readonly stateChanged: ISignal<IApplicationState>;
readonly selection: IUserSelection;
readonly app: IApplication;
}

View File

@@ -1,4 +1,22 @@
# Structure documented in "docs/application-file.md"
os: windows
scripting:
language: batchfile
startCode: |-
@echo off
:: {{ $homepage }} — v{{ $version }} — {{ $date }}
:: Ensure admin privileges
fltmc >nul 2>&1 || (
echo Administrator privileges are required.
PowerShell Start -Verb RunAs '%0' 2> nul || (
echo Right-click on the script and select "Run as administrator".
pause & exit 1
)
exit 0
)
endCode: |-
pause
exit /b 0
actions:
-
category: Privacy cleanup

View File

@@ -1,19 +1,21 @@
declare module 'js-yaml-loader!*' {
export interface ApplicationYaml {
actions: ReadonlyArray<YamlCategory>;
functions: ReadonlyArray<YamlFunction> | undefined;
export interface YamlApplication {
readonly os: string;
readonly scripting: YamlScriptingDefinition;
readonly actions: ReadonlyArray<YamlCategory>;
readonly functions?: ReadonlyArray<YamlFunction>;
}
export interface YamlCategory extends YamlDocumentable {
children: ReadonlyArray<CategoryOrScript>;
category: string;
readonly children: ReadonlyArray<CategoryOrScript>;
readonly category: string;
}
export type CategoryOrScript = YamlCategory | YamlScript;
export type DocumentationUrls = ReadonlyArray<string> | string;
export interface YamlDocumentable {
docs?: DocumentationUrls;
readonly docs?: DocumentationUrls;
}
export interface YamlFunction {
@@ -42,6 +44,13 @@ declare module 'js-yaml-loader!*' {
recommend: string | undefined;
}
const content: ApplicationYaml;
export interface YamlScriptingDefinition {
readonly language: string;
readonly fileExtension: string;
readonly startCode: string;
readonly endCode: string;
}
const content: YamlApplication;
export default content;
}

View File

@@ -1,9 +1,12 @@
import { getEnumNames, getEnumValues } from '@/application/Common/Enum';
import { IEntity } from '../infrastructure/Entity/IEntity';
import { ICategory } from './ICategory';
import { IScript } from './IScript';
import { IApplication } from './IApplication';
import { IProjectInformation } from './IProjectInformation';
import { RecommendationLevel, RecommendationLevelNames, RecommendationLevels } from './RecommendationLevel';
import { RecommendationLevel } from './RecommendationLevel';
import { OperatingSystem } from './OperatingSystem';
import { IScriptingDefinition } from './IScriptingDefinition';
export class Application implements IApplication {
public get totalScripts(): number { return this.queryable.allScripts.length; }
@@ -12,12 +15,18 @@ export class Application implements IApplication {
private readonly queryable: IQueryableApplication;
constructor(
public readonly os: OperatingSystem,
public readonly info: IProjectInformation,
public readonly actions: ReadonlyArray<ICategory>) {
public readonly actions: ReadonlyArray<ICategory>,
public readonly scripting: IScriptingDefinition) {
if (!info) {
throw new Error('info is undefined');
throw new Error('undefined info');
}
if (!scripting) {
throw new Error('undefined scripting definition');
}
this.queryable = makeQueryable(actions);
ensureValidOs(os);
ensureValid(this.queryable);
ensureNoDuplicates(this.queryable.allCategories);
ensureNoDuplicates(this.queryable.allScripts);
@@ -50,13 +59,25 @@ export class Application implements IApplication {
}
}
function ensureValidOs(os: OperatingSystem): void {
if (os === undefined) {
throw new Error('undefined os');
}
if (os === OperatingSystem.Unknown) {
throw new Error('unknown os');
}
if (!(os in OperatingSystem)) {
throw new Error(`os "${os}" is out of range`);
}
}
function ensureNoDuplicates<TKey>(entities: ReadonlyArray<IEntity<TKey>>) {
const totalOccurencesById = new Map<TKey, number>();
const totalOccurrencesById = new Map<TKey, number>();
for (const entity of entities) {
totalOccurencesById.set(entity.id, (totalOccurencesById.get(entity.id) || 0) + 1);
totalOccurrencesById.set(entity.id, (totalOccurrencesById.get(entity.id) || 0) + 1);
}
const duplicatedIds = new Array<TKey>();
totalOccurencesById.forEach((index, id) => {
totalOccurrencesById.forEach((index, id) => {
if (index > 1) {
duplicatedIds.push(id);
}
@@ -89,7 +110,7 @@ function ensureValidScripts(allScripts: readonly IScript[]) {
if (!allScripts || allScripts.length === 0) {
throw new Error('Application must consist of at least one script');
}
for (const level of RecommendationLevels) {
for (const level of getEnumValues(RecommendationLevel)) {
if (allScripts.every((script) => script.level !== level)) {
throw new Error(`none of the scripts are recommended as ${RecommendationLevel[level]}`);
}
@@ -143,7 +164,7 @@ function makeQueryable(
function groupByLevel(allScripts: readonly IScript[]): Map<RecommendationLevel, readonly IScript[]> {
const map = new Map<RecommendationLevel, readonly IScript[]>();
for (const levelName of RecommendationLevelNames) {
for (const levelName of getEnumNames(RecommendationLevel)) {
const level = RecommendationLevel[levelName];
const scripts = allScripts.filter((script) => script.level !== undefined && script.level <= level);
map.set(level, scripts);

View File

@@ -2,9 +2,14 @@ import { IScript } from '@/domain/IScript';
import { ICategory } from '@/domain/ICategory';
import { IProjectInformation } from './IProjectInformation';
import { RecommendationLevel } from './RecommendationLevel';
import { OperatingSystem } from './OperatingSystem';
import { IScriptingDefinition } from './IScriptingDefinition';
export interface IApplication {
readonly info: IProjectInformation;
readonly scripting: IScriptingDefinition;
readonly os: OperatingSystem;
readonly totalScripts: number;
readonly totalCategories: number;
readonly actions: ReadonlyArray<ICategory>;

View File

@@ -0,0 +1,8 @@
import { ScriptingLanguage } from './ScriptingLanguage';
export interface IScriptingDefinition {
readonly fileExtension: string;
readonly language: ScriptingLanguage;
readonly startCode: string;
readonly endCode: string;
}

View File

@@ -2,10 +2,3 @@ export enum RecommendationLevel {
Standard = 0,
Strict = 1,
}
export const RecommendationLevelNames = Object
.values(RecommendationLevel)
.filter((level) => typeof level === 'string') as string[];
export const RecommendationLevels = RecommendationLevelNames
.map((level) => RecommendationLevel[level]) as RecommendationLevel[];

View File

@@ -0,0 +1,32 @@
import { ScriptingLanguage } from './ScriptingLanguage';
import { IScriptingDefinition } from './IScriptingDefinition';
export class ScriptingDefinition implements IScriptingDefinition {
public readonly fileExtension: string;
constructor(
public readonly language: ScriptingLanguage,
public readonly startCode: string,
public readonly endCode: string,
) {
this.fileExtension = findExtension(language);
validateCode(startCode, 'start code');
validateCode(endCode, 'end code');
}
}
function findExtension(language: ScriptingLanguage): string {
switch (language) {
case ScriptingLanguage.bash:
return 'sh';
case ScriptingLanguage.batchfile:
return 'bat';
default:
throw new Error(`unsupported language: ${language}`);
}
}
function validateCode(code: string, name: string) {
if (!code) {
throw new Error(`undefined ${name}`);
}
}

View File

@@ -0,0 +1,4 @@
export enum ScriptingLanguage {
batchfile = 0,
bash = 1,
}

View File

@@ -32,8 +32,8 @@ export default class CardList extends StatefulVue {
public activeCategoryId?: number = null;
public async mounted() {
const state = await this.getCurrentStateAsync();
this.setCategories(state.app.actions);
const context = await this.getCurrentContextAsync();
this.setCategories(context.app.actions);
this.onOutsideOfActiveCardClicked((element) => {
if (hasDirective(element)) {
return;

View File

@@ -69,8 +69,8 @@ export default class CardListItem extends StatefulVue {
}
public async mounted() {
const state = await this.getCurrentStateAsync();
state.selection.changed.on(() => {
const context = await this.getCurrentContextAsync();
context.state.selection.changed.on(() => {
this.updateStateAsync(this.categoryId);
});
this.updateStateAsync(this.categoryId);
@@ -78,11 +78,12 @@ export default class CardListItem extends StatefulVue {
@Watch('categoryId')
public async updateStateAsync(value: |number) {
const state = await this.getCurrentStateAsync();
const category = !value ? undefined : state.app.findCategory(this.categoryId);
const context = await this.getCurrentContextAsync();
const category = !value ? undefined : context.app.findCategory(this.categoryId);
this.cardTitle = category ? category.name : undefined;
this.isAnyChildSelected = category ? state.selection.isAnySelected(category) : false;
this.areAllChildrenSelected = category ? state.selection.areAllSelected(category) : false;
const currentSelection = context.state.selection;
this.isAnyChildSelected = category ? currentSelection.isAnySelected(category) : false;
this.areAllChildrenSelected = category ? currentSelection.areAllSelected(category) : false;
}
}

View File

@@ -42,24 +42,19 @@
private filtered?: IFilterResult;
public async mounted() {
const state = await this.getCurrentStateAsync();
// React to state changes
state.selection.changed.on(this.handleSelectionChanged);
state.filter.filterRemoved.on(this.handleFilterRemoved);
state.filter.filtered.on(this.handleFiltered);
// Update initial state
await this.initializeNodesAsync(this.categoryId);
await this.initializeFilter(state.filter.currentFilter);
const context = await this.getCurrentContextAsync();
this.beginReactingToStateChanges(context.state);
this.setInitialState(context.state);
}
public async toggleNodeSelectionAsync(event: INodeSelectedEvent) {
const state = await this.getCurrentStateAsync();
const context = await this.getCurrentContextAsync();
switch (event.node.type) {
case NodeType.Category:
toggleCategoryNodeSelection(event, state);
toggleCategoryNodeSelection(event, context.state);
break;
case NodeType.Script:
toggleScriptNodeSelection(event, state);
toggleScriptNodeSelection(event, context.state);
break;
default:
throw new Error(`Unknown node type: ${event.node.id}`);
@@ -68,13 +63,13 @@
@Watch('categoryId')
public async initializeNodesAsync(categoryId?: number) {
const state = await this.getCurrentStateAsync();
const context = await this.getCurrentContextAsync();
if (categoryId) {
this.nodes = parseSingleCategory(categoryId, state.app);
this.nodes = parseSingleCategory(categoryId, context.app);
} else {
this.nodes = parseAllCategories(state.app);
this.nodes = parseAllCategories(context.app);
}
this.selectedNodeIds = state.selection.selectedScripts
this.selectedNodeIds = context.state.selection.selectedScripts
.map((selected) => getScriptNodeId(selected.script));
}
@@ -85,6 +80,17 @@
(category: ICategory) => node.id === getCategoryNodeId(category));
}
private beginReactingToStateChanges(state: IApplicationState) {
state.selection.changed.on(this.handleSelectionChanged);
state.filter.filterRemoved.on(this.handleFilterRemoved);
state.filter.filtered.on(this.handleFiltered);
}
private setInitialState(state: IApplicationState) {
this.initializeNodesAsync(this.categoryId);
this.initializeFilter(state.filter.currentFilter);
}
private initializeFilter(currentFilter: IFilterResult | undefined) {
if (!currentFilter) {
this.handleFilterRemoved();

View File

@@ -29,19 +29,20 @@
public async mounted() {
await this.onNodeChangedAsync(this.node);
const state = await this.getCurrentStateAsync();
this.updateState(state.selection.selectedScripts);
state.selection.changed.on((scripts) => this.updateState(scripts));
const context = await this.getCurrentContextAsync();
const currentSelection = context.state.selection;
this.updateState(currentSelection.selectedScripts);
currentSelection.changed.on((scripts) => this.updateState(scripts));
}
@Watch('node') public async onNodeChangedAsync(node: INode) {
const state = await this.getCurrentStateAsync();
this.handler = getReverter(node, state.app);
const context = await this.getCurrentContextAsync();
this.handler = getReverter(node, context.app);
}
public async onRevertToggledAsync() {
const state = await this.getCurrentStateAsync();
this.handler.selectWithRevertState(this.isReverted, state.selection);
const context = await this.getCurrentContextAsync();
this.handler.selectWithRevertState(this.isReverted, context.state.selection);
}
private updateState(scripts: ReadonlyArray<SelectedScript>) {

View File

@@ -49,6 +49,7 @@ import { IApplicationState } from '@/application/State/IApplicationState';
import { IScript } from '@/domain/IScript';
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { IApplicationContext } from '../../../application/State/IApplicationContext';
enum SelectionState {
Standard,
@@ -67,66 +68,79 @@ export default class TheSelector extends StatefulVue {
public currentSelection = SelectionState.None;
public async mounted() {
const state = await this.getCurrentStateAsync();
this.updateSelections(state);
state.selection.changed.on(() => {
this.updateSelections(state);
});
const context = await this.getCurrentContextAsync();
this.updateSelections(context);
this.beginReactingToChanges(context);
}
public async selectAsync(type: SelectionState): Promise<void> {
if (this.currentSelection === type) {
return;
}
const state = await this.getCurrentStateAsync();
selectType(state, type);
const context = await this.getCurrentContextAsync();
selectType(context, type);
}
private updateSelections(state: IApplicationState) {
this.currentSelection = getCurrentSelectionState(state);
private updateSelections(context: IApplicationContext) {
this.currentSelection = getCurrentSelectionState(context);
}
private beginReactingToChanges(context: IApplicationContext) {
context.state.selection.changed.on(() => {
this.updateSelections(context);
});
}
}
interface ITypeSelector {
isSelected: (state: IApplicationState) => boolean;
select: (state: IApplicationState) => void;
isSelected: (context: IApplicationContext) => boolean;
select: (context: IApplicationContext) => void;
}
const selectors = new Map<SelectionState, ITypeSelector>([
[SelectionState.None, {
select: (state) => state.selection.deselectAll(),
isSelected: (state) => state.selection.totalSelected === 0,
select: (context) =>
context.state.selection.deselectAll(),
isSelected: (context) =>
context.state.selection.totalSelected === 0,
}],
[SelectionState.Standard, {
select: (state) => state.selection.selectOnly(state.app.getScriptsByLevel(RecommendationLevel.Standard)),
isSelected: (state) => hasAllSelectedLevelOf(RecommendationLevel.Standard, state),
select: (context) =>
context.state.selection.selectOnly(
context.app.getScriptsByLevel(RecommendationLevel.Standard)),
isSelected: (context) =>
hasAllSelectedLevelOf(RecommendationLevel.Standard, context),
}],
[SelectionState.Strict, {
select: (state) => state.selection.selectOnly(state.app.getScriptsByLevel(RecommendationLevel.Strict)),
isSelected: (state) => hasAllSelectedLevelOf(RecommendationLevel.Strict, state),
select: (context) =>
context.state.selection.selectOnly(context.app.getScriptsByLevel(RecommendationLevel.Strict)),
isSelected: (context) =>
hasAllSelectedLevelOf(RecommendationLevel.Strict, context),
}],
[SelectionState.All, {
select: (state) => state.selection.selectAll(),
isSelected: (state) => state.selection.totalSelected === state.app.totalScripts,
select: (context) =>
context.state.selection.selectAll(),
isSelected: (context) =>
context.state.selection.totalSelected === context.app.totalScripts,
}],
]);
function selectType(state: IApplicationState, type: SelectionState) {
function selectType(context: IApplicationContext, type: SelectionState) {
const selector = selectors.get(type);
selector.select(state);
selector.select(context);
}
function getCurrentSelectionState(state: IApplicationState): SelectionState {
function getCurrentSelectionState(context: IApplicationContext): SelectionState {
for (const [type, selector] of Array.from(selectors.entries())) {
if (selector.isSelected(state)) {
if (selector.isSelected(context)) {
return type;
}
}
return SelectionState.Custom;
}
function hasAllSelectedLevelOf(level: RecommendationLevel, state: IApplicationState) {
const scripts = state.app.getScriptsByLevel(level);
const selectedScripts = state.selection.selectedScripts;
function hasAllSelectedLevelOf(level: RecommendationLevel, context: IApplicationContext) {
const scripts = context.app.getScriptsByLevel(level);
const selectedScripts = context.state.selection.selectedScripts;
return areAllSelected(scripts, selectedScripts);
}

View File

@@ -73,12 +73,13 @@
public searchHasMatches = false;
public async mounted() {
const state = await this.getCurrentStateAsync();
this.repositoryUrl = state.app.info.repositoryWebUrl;
state.filter.filterRemoved.on(() => {
const context = await this.getCurrentContextAsync();
this.repositoryUrl = context.app.info.repositoryWebUrl;
const filter = context.state.filter;
filter.filterRemoved.on(() => {
this.isSearching = false;
});
state.filter.filtered.on((result: IFilterResult) => {
filter.filtered.on((result: IFilterResult) => {
this.searchQuery = result.query;
this.isSearching = true;
this.searchHasMatches = result.hasAnyMatches();
@@ -86,8 +87,9 @@
}
public async clearSearchQueryAsync() {
const state = await this.getCurrentStateAsync();
state.filter.removeFilter();
const context = await this.getCurrentContextAsync();
const filter = context.state.filter;
filter.removeFilter();
}
public onGroupingChanged(group: Grouping) {

View File

@@ -1,11 +1,13 @@
import { ApplicationState } from '@/application/State/ApplicationState';
import { IApplicationState } from '@/application/State/IApplicationState';
import { Vue } from 'vue-property-decorator';
import { AsyncLazy } from '@/infrastructure/Threading/AsyncLazy';
import { IApplicationContext } from '@/application/State/IApplicationContext';
import { buildContext } from '@/application/State/ApplicationContextProvider';
export abstract class StatefulVue extends Vue {
public isLoading = true;
private static instance = new AsyncLazy<IApplicationContext>(
() => Promise.resolve(buildContext()));
protected getCurrentStateAsync(): Promise<IApplicationState> {
return ApplicationState.GetAsync();
protected getCurrentContextAsync(): Promise<IApplicationContext> {
return StatefulVue.instance.getValueAsync();
}
}

View File

@@ -10,6 +10,7 @@ import 'ace-builds/webpack-resolver';
import { CodeBuilder } from '@/application/State/Code/Generation/CodeBuilder';
import { ICodeChangedEvent } from '@/application/State/Code/Event/ICodeChangedEvent';
import { IScript } from '@/domain/IScript';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
const NothingChosenCode =
new CodeBuilder()
@@ -38,10 +39,11 @@ export default class TheCodeArea extends StatefulVue {
@Prop() private theme!: string;
public async mounted() {
this.editor = initializeEditor(this.theme, this.editorId);
const state = await this.getCurrentStateAsync();
this.editor.setValue(state.code.current || NothingChosenCode, 1);
state.code.changed.on((code) => this.updateCode(code));
const context = await this.getCurrentContextAsync();
this.editor = initializeEditor(this.theme, this.editorId, context.app.scripting.language);
const appCode = context.state.code;
this.editor.setValue(appCode.current || NothingChosenCode, 1);
appCode.changed.on((code) => this.updateCode(code));
}
private updateCode(event: ICodeChangedEvent) {
@@ -93,10 +95,10 @@ export default class TheCodeArea extends StatefulVue {
}
}
function initializeEditor(theme: string, editorId: string): ace.Ace.Editor {
const lang = 'batchfile';
function initializeEditor(theme: string, editorId: string, language: ScriptingLanguage): ace.Ace.Editor {
theme = theme || 'github';
const editor = ace.edit(editorId);
const lang = getLanguage(language);
editor.getSession().setMode(`ace/mode/${lang}`);
editor.setTheme(`ace/theme/${theme}`);
editor.setReadOnly(true);
@@ -105,6 +107,15 @@ function initializeEditor(theme: string, editorId: string): ace.Ace.Editor {
return editor;
}
function getLanguage(language: ScriptingLanguage) {
switch (language) {
case ScriptingLanguage.batchfile:
return 'batchfile';
default:
throw new Error('unkown language');
}
}
</script>
<style lang="scss">

View File

@@ -1,6 +1,5 @@
<template>
<div class="container" v-if="hasCode">
</IconButton>
<IconButton
:text="this.isDesktop ? 'Save' : 'Download'"
v-on:click="saveCodeAsync"
@@ -22,6 +21,9 @@ import { SaveFileDialog, FileType } from '@/infrastructure/SaveFileDialog';
import { Clipboard } from '@/infrastructure/Clipboard';
import IconButton from './IconButton.vue';
import { Environment } from '@/application/Environment/Environment';
import { IApplicationCode } from '../application/State/IApplicationState';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { IApplicationContext } from '@/application/State/IApplicationContext';
@Component({
components: {
@@ -33,22 +35,44 @@ export default class TheCodeButtons extends StatefulVue {
public isDesktop = false;
public async mounted() {
const state = await this.getCurrentStateAsync();
this.isDesktop = Environment.CurrentEnvironment.isDesktop;
this.hasCode = state.code.current && state.code.current.length > 0;
state.code.changed.on((code) => {
this.hasCode = code && code.code.length > 0;
const code = await this.getCurrentCodeAsync();
this.hasCode = code.current && code.current.length > 0;
code.changed.on((newCode) => {
this.hasCode = newCode && newCode.code.length > 0;
});
this.isDesktop = Environment.CurrentEnvironment.isDesktop;
}
public async copyCodeAsync() {
const state = await this.getCurrentStateAsync();
Clipboard.copyText(state.code.current);
const code = await this.getCurrentCodeAsync();
Clipboard.copyText(code.current);
}
public async saveCodeAsync() {
const state = await this.getCurrentStateAsync();
SaveFileDialog.saveFile(state.code.current, 'privacy-script.bat', FileType.BatchFile);
const context = await this.getCurrentContextAsync();
saveCode(context);
}
private async getCurrentCodeAsync(): Promise<IApplicationCode> {
const context = await this.getCurrentContextAsync();
const code = context.state.code;
return code;
}
}
function saveCode(context: IApplicationContext) {
const fileName = `privacy-script.${context.app.scripting.fileExtension}`;
const content = context.state.code.current;
const type = getType(context.app.scripting.language);
SaveFileDialog.saveFile(content, fileName, FileType.BatchFile);
}
function getType(language: ScriptingLanguage) {
switch (language) {
case ScriptingLanguage.batchfile:
return FileType.BatchFile;
default:
throw new Error('unknown file type');
}
}
</script>

View File

@@ -38,8 +38,8 @@ export default class DownloadUrlListItem extends StatefulVue {
}
private async getDownloadUrlAsync(os: OperatingSystem): Promise<string> {
const state = await this.getCurrentStateAsync();
return state.app.info.getDownloadUrl(os);
const context = await this.getCurrentContextAsync();
return context.app.info.getDownloadUrl(os);
}
}

View File

@@ -47,9 +47,9 @@ export default class TheFooter extends StatefulVue {
}
public async mounted() {
const state = await this.getCurrentStateAsync();
this.repositoryUrl = state.app.info.repositoryWebUrl;
this.feedbackUrl = state.app.info.feedbackUrl;
const context = await this.getCurrentContextAsync();
this.repositoryUrl = context.app.info.repositoryWebUrl;
this.feedbackUrl = context.app.info.feedbackUrl;
}
}
</script>

View File

@@ -74,7 +74,7 @@ export default class TheFooter extends StatefulVue {
}
public async mounted() {
const state = await this.getCurrentStateAsync();
const state = await this.getCurrentContextAsync();
const info = state.app.info;
this.version = info.version;
this.homepageUrl = info.homepage;

View File

@@ -15,8 +15,8 @@ export default class TheHeader extends StatefulVue {
public subtitle = '';
public async mounted() {
const state = await this.getCurrentStateAsync();
this.title = state.app.info.name;
const context = await this.getCurrentContextAsync();
this.title = context.app.info.name;
}
}
</script>

View File

@@ -24,19 +24,20 @@ export default class TheSearchBar extends StatefulVue {
public searchQuery = '';
public async mounted() {
const state = await this.getCurrentStateAsync();
const totalScripts = state.app.totalScripts;
const context = await this.getCurrentContextAsync();
const totalScripts = context.app.totalScripts;
this.searchPlaceHolder = `Search in ${totalScripts} scripts`;
this.beginReacting(state.filter);
this.beginReacting(context.state.filter);
}
@Watch('searchQuery')
public async updateFilterAsync(filter: |string) {
const state = await this.getCurrentStateAsync();
if (!filter) {
state.filter.removeFilter();
public async updateFilterAsync(newFilter: |string) {
const context = await this.getCurrentContextAsync();
const filter = context.state.filter;
if (!newFilter) {
filter.removeFilter();
} else {
state.filter.setFilter(filter);
filter.setFilter(newFilter);
}
}