Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option processors #6394

Merged
merged 8 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 57 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 { OptionProcessorsRegistry } from './processors/OptionProcessorsRegistry';
import { ComponentRegistry } from './components/ComponentRegistry';
import { Commands } from './commands/Commands';
import { LayoutTreeParser } from './commands/LayoutTreeParser';
Expand All @@ -26,6 +27,7 @@ export class NavigationRoot {
public readonly TouchablePreview = TouchablePreview;

private readonly store: Store;
private readonly optionProcessorsRegistry: OptionProcessorsRegistry;
private readonly nativeEventsReceiver: NativeEventsReceiver;
private readonly uniqueIdProvider: UniqueIdProvider;
private readonly componentRegistry: ComponentRegistry;
Expand All @@ -41,9 +43,13 @@ export class NavigationRoot {
constructor() {
this.componentWrapper = new ComponentWrapper();
this.store = new Store();
this.optionProcessorsRegistry = new OptionProcessorsRegistry();
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 +58,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.optionProcessorsRegistry,
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 +78,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 +91,38 @@ 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
);
}

/**
* Register an option processor which allows option interpolation by objectPath.
*/
public registerOptionProcessor(
objectPath: string,
processor: (value: any, commandName: string) => any
) {
this.optionProcessorsRegistry.registerProcessor(objectPath, processor);
}

public setLazyComponentRegistrator(lazyRegistratorFn: (lazyComponentRequest: string | number) => void) {
/**
* Unegister an option processor.
*/
public unregisterOptionProcessor(objectPath: string) {
yogevbd marked this conversation as resolved.
Show resolved Hide resolved
this.optionProcessorsRegistry.unregisterProcessor(objectPath);
}

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

Expand All @@ -92,7 +136,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