Skip to content

Commit

Permalink
Add Option processors (#6394)
Browse files Browse the repository at this point in the history
This PR adds the ability to intercept and modify options each time they applied from any command.
This feature comes in handy when there are guidelines need to be enforced when multiple developers are working together on different modules (or not).

- Add `Navigation.addOptionProcessor(optionPath: string, processor: (value: T, commandName: string) => T)` - When the specific option path is being processed, the processor function gets called and the processor has the option to manipulate this specific option value by returning a new one. It also receives the specific command name which triggered that option processing.
- Fix wallaby

`optionPath` examples: `topBar.title.color`, `bottomTabs.backgroundColor`.
  • Loading branch information
yogevbd authored Jul 14, 2020
1 parent ea4b427 commit 1d4d054
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 127 deletions.
58 changes: 51 additions & 7 deletions lib/src/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
import { Store } from './components/Store';
import { OptionProcessorsStore } from './processors/OptionProcessorsStore';
import { ComponentRegistry } from './components/ComponentRegistry';
import { Commands } from './commands/Commands';
import { LayoutTreeParser } from './commands/LayoutTreeParser';
Expand All @@ -21,11 +22,13 @@ import { ColorService } from './adapters/ColorService';
import { AssetService } from './adapters/AssetResolver';
import { AppRegistryService } from './adapters/AppRegistryService';
import { Deprecations } from './commands/Deprecations';
import { ProcessorSubscription } from './interfaces/ProcessorSubscription';

export class NavigationRoot {
public readonly TouchablePreview = TouchablePreview;

private readonly store: Store;
private readonly optionProcessorsStore: OptionProcessorsStore;
private readonly nativeEventsReceiver: NativeEventsReceiver;
private readonly uniqueIdProvider: UniqueIdProvider;
private readonly componentRegistry: ComponentRegistry;
Expand All @@ -41,9 +44,13 @@ export class NavigationRoot {
constructor() {
this.componentWrapper = new ComponentWrapper();
this.store = new Store();
this.optionProcessorsStore = new OptionProcessorsStore();
this.nativeEventsReceiver = new NativeEventsReceiver();
this.uniqueIdProvider = new UniqueIdProvider();
this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver, this.store);
this.componentEventsObserver = new ComponentEventsObserver(
this.nativeEventsReceiver,
this.store
);
const appRegistryService = new AppRegistryService();
this.componentRegistry = new ComponentRegistry(
this.store,
Expand All @@ -52,7 +59,14 @@ export class NavigationRoot {
appRegistryService
);
this.layoutTreeParser = new LayoutTreeParser(this.uniqueIdProvider);
const optionsProcessor = new OptionsProcessor(this.store, this.uniqueIdProvider, new ColorService(), new AssetService(), new Deprecations());
const optionsProcessor = new OptionsProcessor(
this.store,
this.uniqueIdProvider,
this.optionProcessorsStore,
new ColorService(),
new AssetService(),
new Deprecations()
);
this.layoutTreeCrawler = new LayoutTreeCrawler(this.store, optionsProcessor);
this.nativeCommandsSender = new NativeCommandsSender();
this.commandsObserver = new CommandsObserver(this.uniqueIdProvider);
Expand All @@ -65,7 +79,11 @@ export class NavigationRoot {
this.uniqueIdProvider,
optionsProcessor
);
this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.commandsObserver, this.componentEventsObserver);
this.eventsRegistry = new EventsRegistry(
this.nativeEventsReceiver,
this.commandsObserver,
this.componentEventsObserver
);

this.componentEventsObserver.registerOnceForAllComponentEvents();
}
Expand All @@ -74,11 +92,31 @@ export class NavigationRoot {
* Every navigation component in your app must be registered with a unique name.
* The component itself is a traditional React component extending React.Component.
*/
public registerComponent(componentName: string | number, componentProvider: ComponentProvider, concreteComponentProvider?: ComponentProvider): ComponentProvider {
return this.componentRegistry.registerComponent(componentName, componentProvider, concreteComponentProvider);
public registerComponent(
componentName: string | number,
componentProvider: ComponentProvider,
concreteComponentProvider?: ComponentProvider
): ComponentProvider {
return this.componentRegistry.registerComponent(
componentName,
componentProvider,
concreteComponentProvider
);
}

/**
* Adds an option processor which allows option interpolation by optionPath.
*/
public addOptionProcessor<T>(
optionPath: string,
processor: (value: T, commandName: string) => T
): ProcessorSubscription {
return this.optionProcessorsStore.addProcessor(optionPath, processor);
}

public setLazyComponentRegistrator(lazyRegistratorFn: (lazyComponentRequest: string | number) => void) {
public setLazyComponentRegistrator(
lazyRegistratorFn: (lazyComponentRequest: string | number) => void
) {
this.store.setLazyComponentRegistrator(lazyRegistratorFn);
}

Expand All @@ -92,7 +130,13 @@ export class NavigationRoot {
ReduxProvider: any,
reduxStore: any
): ComponentProvider {
return this.componentRegistry.registerComponent(componentName, getComponentClassFunc, undefined, ReduxProvider, reduxStore);
return this.componentRegistry.registerComponent(
componentName,
getComponentClassFunc,
undefined,
ReduxProvider,
reduxStore
);
}

/**
Expand Down
101 changes: 65 additions & 36 deletions lib/src/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ import { LayoutTreeCrawler } from './LayoutTreeCrawler';
import { OptionsProcessor } from './OptionsProcessor';
import { Store } from '../components/Store';

enum CommandNames {
SetRoot = 'setRoot',
SetDefaultOptions = 'setDefaultOptions',
MergeOptions = 'mergeOptions',
UpdateProps = 'updateProps',
ShowModal = 'showModal',
DismissModal = 'dismissModal',
DismissAllModals = 'dismissAllModals',
Push = 'push',
Pop = 'pop',
PopTo = 'popTo',
PopToRoot = 'popToRoot',
SetStackRoot = 'setStackRoot',
ShowOverlay = 'showOverlay',
DismissOverlay = 'dismissOverlay',
GetLaunchArgs = 'getLaunchArgs',
}

export class Commands {
constructor(
private readonly store: Store,
Expand All @@ -33,15 +51,18 @@ export class Commands {
return this.layoutTreeParser.parse(overlay);
});

const commandId = this.uniqueIdProvider.generate('setRoot');
this.commandsObserver.notify('setRoot', { commandId, layout: { root, modals, overlays } });
const commandId = this.uniqueIdProvider.generate(CommandNames.SetRoot);
this.commandsObserver.notify(CommandNames.SetRoot, {
commandId,
layout: { root, modals, overlays },
});

this.layoutTreeCrawler.crawl(root);
this.layoutTreeCrawler.crawl(root, CommandNames.SetRoot);
modals.forEach((modalLayout) => {
this.layoutTreeCrawler.crawl(modalLayout);
this.layoutTreeCrawler.crawl(modalLayout, CommandNames.SetRoot);
});
overlays.forEach((overlayLayout) => {
this.layoutTreeCrawler.crawl(overlayLayout);
this.layoutTreeCrawler.crawl(overlayLayout, CommandNames.SetRoot);
});

const result = this.nativeCommandsSender.setRoot(commandId, { root, modals, overlays });
Expand All @@ -50,81 +71,85 @@ export class Commands {

public setDefaultOptions(options: Options) {
const input = cloneDeep(options);
this.optionsProcessor.processDefaultOptions(input);
this.optionsProcessor.processDefaultOptions(input, CommandNames.SetDefaultOptions);

this.nativeCommandsSender.setDefaultOptions(input);
this.commandsObserver.notify('setDefaultOptions', { options });
this.commandsObserver.notify(CommandNames.SetDefaultOptions, { options });
}

public mergeOptions(componentId: string, options: Options) {
const input = cloneDeep(options);
this.optionsProcessor.processOptions(input);
this.optionsProcessor.processOptions(input, CommandNames.MergeOptions);

this.nativeCommandsSender.mergeOptions(componentId, input);
this.commandsObserver.notify('mergeOptions', { componentId, options });
this.commandsObserver.notify(CommandNames.MergeOptions, { componentId, options });
}

public updateProps(componentId: string, props: object) {
this.store.updateProps(componentId, props);
this.commandsObserver.notify('updateProps', { componentId, props });
this.commandsObserver.notify(CommandNames.UpdateProps, { componentId, props });
}

public showModal(layout: Layout) {
const layoutCloned = cloneDeep(layout);
const layoutNode = this.layoutTreeParser.parse(layoutCloned);

const commandId = this.uniqueIdProvider.generate('showModal');
this.commandsObserver.notify('showModal', { commandId, layout: layoutNode });
this.layoutTreeCrawler.crawl(layoutNode);
const commandId = this.uniqueIdProvider.generate(CommandNames.ShowModal);
this.commandsObserver.notify(CommandNames.ShowModal, { commandId, layout: layoutNode });
this.layoutTreeCrawler.crawl(layoutNode, CommandNames.ShowModal);

const result = this.nativeCommandsSender.showModal(commandId, layoutNode);
return result;
}

public dismissModal(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('dismissModal');
const commandId = this.uniqueIdProvider.generate(CommandNames.DismissModal);
const result = this.nativeCommandsSender.dismissModal(commandId, componentId, mergeOptions);
this.commandsObserver.notify('dismissModal', { commandId, componentId, mergeOptions});
this.commandsObserver.notify(CommandNames.DismissModal, {
commandId,
componentId,
mergeOptions,
});
return result;
}

public dismissAllModals(mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('dismissAllModals');
const commandId = this.uniqueIdProvider.generate(CommandNames.DismissAllModals);
const result = this.nativeCommandsSender.dismissAllModals(commandId, mergeOptions);
this.commandsObserver.notify('dismissAllModals', { commandId, mergeOptions });
this.commandsObserver.notify(CommandNames.DismissAllModals, { commandId, mergeOptions });
return result;
}

public push(componentId: string, simpleApi: Layout) {
const input = cloneDeep(simpleApi);
const layout = this.layoutTreeParser.parse(input);

const commandId = this.uniqueIdProvider.generate('push');
this.commandsObserver.notify('push', { commandId, componentId, layout });
this.layoutTreeCrawler.crawl(layout);
const commandId = this.uniqueIdProvider.generate(CommandNames.Push);
this.commandsObserver.notify(CommandNames.Push, { commandId, componentId, layout });
this.layoutTreeCrawler.crawl(layout, CommandNames.Push);

const result = this.nativeCommandsSender.push(commandId, componentId, layout);
return result;
}

public pop(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('pop');
const commandId = this.uniqueIdProvider.generate(CommandNames.Pop);
const result = this.nativeCommandsSender.pop(commandId, componentId, mergeOptions);
this.commandsObserver.notify('pop', { commandId, componentId, mergeOptions });
this.commandsObserver.notify(CommandNames.Pop, { commandId, componentId, mergeOptions });
return result;
}

public popTo(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('popTo');
const commandId = this.uniqueIdProvider.generate(CommandNames.PopTo);
const result = this.nativeCommandsSender.popTo(commandId, componentId, mergeOptions);
this.commandsObserver.notify('popTo', { commandId, componentId, mergeOptions });
this.commandsObserver.notify(CommandNames.PopTo, { commandId, componentId, mergeOptions });
return result;
}

public popToRoot(componentId: string, mergeOptions?: Options) {
const commandId = this.uniqueIdProvider.generate('popToRoot');
const commandId = this.uniqueIdProvider.generate(CommandNames.PopToRoot);
const result = this.nativeCommandsSender.popToRoot(commandId, componentId, mergeOptions);
this.commandsObserver.notify('popToRoot', { commandId, componentId, mergeOptions });
this.commandsObserver.notify(CommandNames.PopToRoot, { commandId, componentId, mergeOptions });
return result;
}

Expand All @@ -134,10 +159,14 @@ export class Commands {
return layout;
});

const commandId = this.uniqueIdProvider.generate('setStackRoot');
this.commandsObserver.notify('setStackRoot', { commandId, componentId, layout: input });
const commandId = this.uniqueIdProvider.generate(CommandNames.SetStackRoot);
this.commandsObserver.notify(CommandNames.SetStackRoot, {
commandId,
componentId,
layout: input,
});
input.forEach((layoutNode) => {
this.layoutTreeCrawler.crawl(layoutNode);
this.layoutTreeCrawler.crawl(layoutNode, CommandNames.SetStackRoot);
});

const result = this.nativeCommandsSender.setStackRoot(commandId, componentId, input);
Expand All @@ -148,25 +177,25 @@ export class Commands {
const input = cloneDeep(simpleApi);
const layout = this.layoutTreeParser.parse(input);

const commandId = this.uniqueIdProvider.generate('showOverlay');
this.commandsObserver.notify('showOverlay', { commandId, layout });
this.layoutTreeCrawler.crawl(layout);
const commandId = this.uniqueIdProvider.generate(CommandNames.ShowOverlay);
this.commandsObserver.notify(CommandNames.ShowOverlay, { commandId, layout });
this.layoutTreeCrawler.crawl(layout, CommandNames.ShowOverlay);

const result = this.nativeCommandsSender.showOverlay(commandId, layout);
return result;
}

public dismissOverlay(componentId: string) {
const commandId = this.uniqueIdProvider.generate('dismissOverlay');
const commandId = this.uniqueIdProvider.generate(CommandNames.DismissOverlay);
const result = this.nativeCommandsSender.dismissOverlay(commandId, componentId);
this.commandsObserver.notify('dismissOverlay', { commandId, componentId });
this.commandsObserver.notify(CommandNames.DismissOverlay, { commandId, componentId });
return result;
}

public getLaunchArgs() {
const commandId = this.uniqueIdProvider.generate('getLaunchArgs');
const commandId = this.uniqueIdProvider.generate(CommandNames.GetLaunchArgs);
const result = this.nativeCommandsSender.getLaunchArgs(commandId);
this.commandsObserver.notify('getLaunchArgs', { commandId });
this.commandsObserver.notify(CommandNames.GetLaunchArgs, { commandId });
return result;
}
}
Loading

0 comments on commit 1d4d054

Please sign in to comment.