Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/processors/LayoutProcessorsStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,31 @@ describe('Layout processors Store', () => {
remove();
expect(uut.getProcessors()).toEqual([]);
});

it('should unregister only the subscribed processor', () => {
const firstProcessor = (value: any, _commandName: string) => value;
const secondProcessor = (value: any, _commandName: string) => value;
const thirdProcessor = (value: any, _commandName: string) => value;
uut.addProcessor(firstProcessor);
const { remove } = uut.addProcessor(secondProcessor);
uut.addProcessor(thirdProcessor);

remove();

expect(uut.getProcessors()).toEqual([firstProcessor, thirdProcessor]);
});

it('should ignore repeated subscription removal', () => {
const processor = (value: any, _commandName: string) => value;
const firstSubscription = uut.addProcessor(processor);
const secondSubscription = uut.addProcessor(processor);

firstSubscription.remove();
firstSubscription.remove();

expect(uut.getProcessors()).toEqual([processor]);

secondSubscription.remove();
expect(uut.getProcessors()).toEqual([]);
});
});
13 changes: 11 additions & 2 deletions src/processors/LayoutProcessorsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ export class LayoutProcessorsStore {

public addProcessor(processor: LayoutProcessor): ProcessorSubscription {
this.layoutProcessors.push(processor);
let removed = false;

return { remove: () => this.removeProcessor(processor) };
return {
remove: () => {
if (!removed) {
this.removeProcessor(processor);
removed = true;
}
},
};
}

public getProcessors(): LayoutProcessor[] {
return this.layoutProcessors;
}

private removeProcessor(processor: LayoutProcessor) {
this.layoutProcessors.splice(this.layoutProcessors.indexOf(processor));
const index = this.layoutProcessors.indexOf(processor);
if (index !== -1) this.layoutProcessors.splice(index, 1);
}
}
36 changes: 36 additions & 0 deletions src/processors/OptionProcessorsStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,47 @@ describe('Option processors Store', () => {
expect(uut.getProcessors('topBar')).toEqual([processor, secondProcessor]);
});

it.each(['toString', '__proto__'])(
'should register processors for the %s object path',
(optionPath) => {
const processor = (value: any, _commandName: string) => value;
uut.addProcessor(optionPath, processor);
expect(uut.getProcessors(optionPath)).toEqual([processor]);
}
);

it('should unregister processor', () => {
const processor = (value: any, _commandName: string) => value;
const { remove } = uut.addProcessor('topBar', processor);
expect(uut.getProcessors('topBar')).toEqual([processor]);
remove();
expect(uut.getProcessors('topBar')).toEqual([]);
});

it('should unregister only the subscribed processor', () => {
const firstProcessor = (value: any, _commandName: string) => value;
const secondProcessor = (value: any, _commandName: string) => value;
const thirdProcessor = (value: any, _commandName: string) => value;
uut.addProcessor('topBar', firstProcessor);
const { remove } = uut.addProcessor('topBar', secondProcessor);
uut.addProcessor('topBar', thirdProcessor);

remove();

expect(uut.getProcessors('topBar')).toEqual([firstProcessor, thirdProcessor]);
});

it('should ignore repeated subscription removal', () => {
const processor = (value: any, _commandName: string) => value;
const firstSubscription = uut.addProcessor('topBar', processor);
const secondSubscription = uut.addProcessor('topBar', processor);

firstSubscription.remove();
firstSubscription.remove();

expect(uut.getProcessors('topBar')).toEqual([processor]);

secondSubscription.remove();
expect(uut.getProcessors('topBar')).toEqual([]);
});
});
20 changes: 15 additions & 5 deletions src/processors/OptionProcessorsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { ProcessorSubscription } from '../interfaces/ProcessorSubscription';
import { OptionsProcessor } from '../interfaces/Processors';

export class OptionProcessorsStore {
private optionsProcessorsByObjectPath: Record<string, OptionsProcessor<any>[]> = {};
private optionsProcessorsByObjectPath: Record<string, OptionsProcessor<any>[]> = Object.create(
null
);

public addProcessor<T>(
optionPath: string,
Expand All @@ -12,17 +14,25 @@ export class OptionProcessorsStore {
this.optionsProcessorsByObjectPath[optionPath] = [];

this.optionsProcessorsByObjectPath[optionPath].push(processor);
let removed = false;

return { remove: () => this.removeProcessor(optionPath, processor) };
return {
remove: () => {
if (!removed) {
this.removeProcessor(optionPath, processor);
removed = true;
}
},
};
}

public getProcessors(optionPath: string) {
return this.optionsProcessorsByObjectPath[optionPath];
}

private removeProcessor(optionPath: string, processor: OptionsProcessor<any>) {
this.optionsProcessorsByObjectPath[optionPath].splice(
this.optionsProcessorsByObjectPath[optionPath].indexOf(processor)
);
const processors = this.optionsProcessorsByObjectPath[optionPath];
const index = processors.indexOf(processor);
if (index !== -1) processors.splice(index, 1);
}
}