Change detection optimizations

This commit is contained in:
Roman Hotsiy 2016-05-18 16:59:54 +03:00
parent 7380f89482
commit b9bc3f2eab
10 changed files with 40 additions and 23 deletions

View File

@ -11,7 +11,8 @@ import JsonPointer from '../../utils/JsonPointer';
templateUrl: './lib/components/JsonSchema/json-schema.html', templateUrl: './lib/components/JsonSchema/json-schema.html',
styleUrls: ['./lib/components/JsonSchema/json-schema.css'], styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
directives: [JsonSchema, DropDown], directives: [JsonSchema, DropDown],
inputs: ['isArray', 'final', 'nestOdd', 'childFor', 'isRequestSchema'] inputs: ['isArray', 'final', 'nestOdd', 'childFor', 'isRequestSchema'],
detect: true
}) })
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef]]) @Reflect.metadata('parameters', [[SchemaManager], [ElementRef]])
export class JsonSchema extends BaseComponent { export class JsonSchema extends BaseComponent {

View File

@ -14,7 +14,8 @@ import { RequestSamples } from '../RequestSamples/request-samples';
templateUrl: './lib/components/Method/method.html', templateUrl: './lib/components/Method/method.html',
styleUrls: ['./lib/components/Method/method.css'], styleUrls: ['./lib/components/Method/method.css'],
directives: [ ParamsList, ResponsesList, ResponsesSamples, SchemaSample, RequestSamples ], directives: [ ParamsList, ResponsesList, ResponsesSamples, SchemaSample, RequestSamples ],
inputs: ['tag'] inputs: ['tag'],
detect: true
}) })
export class Method extends BaseComponent { export class Method extends BaseComponent {
constructor(schemaMgr) { constructor(schemaMgr) {

View File

@ -10,7 +10,8 @@ import { EncodeURIComponentPipe } from '../../utils/pipes';
templateUrl: './lib/components/MethodsList/methods-list.html', templateUrl: './lib/components/MethodsList/methods-list.html',
styleUrls: ['./lib/components/MethodsList/methods-list.css'], styleUrls: ['./lib/components/MethodsList/methods-list.css'],
directives: [ forwardRef(() => Method) ], directives: [ forwardRef(() => Method) ],
pipes: [ EncodeURIComponentPipe ] pipes: [ EncodeURIComponentPipe ],
detect: true
}) })
export class MethodsList extends BaseComponent { export class MethodsList extends BaseComponent {

View File

@ -29,7 +29,9 @@ var _modeLocked = false;
], ],
templateUrl: './lib/components/Redoc/redoc.html', templateUrl: './lib/components/Redoc/redoc.html',
styleUrls: ['./lib/components/Redoc/redoc.css'], styleUrls: ['./lib/components/Redoc/redoc.css'],
directives: [ ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar ] directives: [ ApiInfo, ApiLogo, MethodsList, SideMenu, StickySidebar ],
detect: true,
onPushOnly: false
}) })
@Reflect.metadata('parameters', [ @Reflect.metadata('parameters', [
[SchemaManager], [OptionsService], [ElementRef], [RedocEventsService]]) [SchemaManager], [OptionsService], [ElementRef], [RedocEventsService]])

View File

@ -1,6 +1,6 @@
<header *ngIf="data.schemaPointer || data.samples.length"> Request samples </header> <header *ngIf="data.schemaPointer || data.samples.length"> Request samples </header>
<schema-sample *ngIf="!data.samples.length" [pointer]="data.schemaPointer"> </schema-sample> <schema-sample *ngIf="!data.samples.length" [pointer]="data.schemaPointer"> </schema-sample>
<tabs *ngIf="data.samples.length" (change)=changeLangNotify($event)> <tabs *ngIf="data.samples.length" [selected] = "selectedLang" (change)=changeLangNotify($event)>
<tab tabTitle="JSON"> <tab tabTitle="JSON">
<schema-sample [pointer]="data.schemaPointer"> </schema-sample> <schema-sample [pointer]="data.schemaPointer"> </schema-sample>
</tab> </tab>

View File

@ -15,7 +15,9 @@ import { RedocEventsService } from '../../services/index';
styleUrls: ['./lib/components/RequestSamples/request-samples.css'], styleUrls: ['./lib/components/RequestSamples/request-samples.css'],
directives: [SchemaSample, Tabs, Tab], directives: [SchemaSample, Tabs, Tab],
inputs: ['schemaPointer'], inputs: ['schemaPointer'],
pipes: [PrismPipe] pipes: [PrismPipe],
detect: true,
onPushOnly: false
}) })
@Reflect.metadata('parameters', [[SchemaManager], [RedocEventsService], [new ViewChildren(Tabs), QueryList]]) @Reflect.metadata('parameters', [[SchemaManager], [RedocEventsService], [new ViewChildren(Tabs), QueryList]])
export class RequestSamples extends BaseComponent { export class RequestSamples extends BaseComponent {
@ -25,23 +27,14 @@ export class RequestSamples extends BaseComponent {
this.childTabs = childQuery.first; this.childTabs = childQuery.first;
}); });
this.events = events; this.events = events;
this.selectedLang = this.events.samplesLanguageChanged;
} }
init() {
this.subscribeForEvents();
}
changeLangNotify(lang) { changeLangNotify(lang) {
this.events.samplesLanguageChanged.next(lang); this.events.samplesLanguageChanged.next(lang);
} }
subscribeForEvents() {
this.events.samplesLanguageChanged.subscribe((sampleLang) => {
if (!this.childTabs) return;
this.childTabs.selectyByTitle(sampleLang);
});
}
prepareModel() { prepareModel() {
this.data = {}; this.data = {};
this.data.schemaPointer = JsonPointer.join(this.schemaPointer, 'schema'); this.data.schemaPointer = JsonPointer.join(this.schemaPointer, 'schema');

View File

@ -16,7 +16,8 @@ function isNumeric(n) {
selector: 'responses-list', selector: 'responses-list',
templateUrl: './lib/components/ResponsesList/responses-list.html', templateUrl: './lib/components/ResponsesList/responses-list.html',
styleUrls: ['./lib/components/ResponsesList/responses-list.css'], styleUrls: ['./lib/components/ResponsesList/responses-list.css'],
directives: [JsonSchema, Zippy, JsonSchemaLazy] directives: [JsonSchema, Zippy, JsonSchemaLazy],
detect: true
}) })
@Reflect.metadata('parameters', [[SchemaManager], [OptionsService]]) @Reflect.metadata('parameters', [[SchemaManager], [OptionsService]])
export class ResponsesList extends BaseComponent { export class ResponsesList extends BaseComponent {

View File

@ -11,7 +11,9 @@ import { ScrollService, Hash, MenuService, OptionsService } from '../../services
selector: 'side-menu', selector: 'side-menu',
templateUrl: './lib/components/SideMenu/side-menu.html', templateUrl: './lib/components/SideMenu/side-menu.html',
providers: [ScrollService, MenuService, Hash], providers: [ScrollService, MenuService, Hash],
styleUrls: ['./lib/components/SideMenu/side-menu.css'] styleUrls: ['./lib/components/SideMenu/side-menu.css'],
detect: true,
onPushOnly: false
}) })
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef], [BrowserDomAdapter], @Reflect.metadata('parameters', [[SchemaManager], [ElementRef], [BrowserDomAdapter],
[ScrollService], [MenuService], [Hash], [OptionsService], [ChangeDetectorRef]]) [ScrollService], [MenuService], [Hash], [OptionsService], [ChangeDetectorRef]])

View File

@ -67,6 +67,7 @@ export function RedocComponent(options) {
let inputs = safeConcat(options.inputs, commonInputs); let inputs = safeConcat(options.inputs, commonInputs);
let directives = safeConcat(options.directives, CORE_DIRECTIVES); let directives = safeConcat(options.directives, CORE_DIRECTIVES);
let pipes = safeConcat(options.pipes, [JsonPointerEscapePipe, MarkedPipe, JsonPipe, AsyncPipe]); let pipes = safeConcat(options.pipes, [JsonPointerEscapePipe, MarkedPipe, JsonPipe, AsyncPipe]);
if (options.onPushOnly === undefined) options.onPushOnly = true;
return function decorator(target) { return function decorator(target) {
@ -75,7 +76,9 @@ export function RedocComponent(options) {
inputs: inputs, inputs: inputs,
outputs: options.outputs, outputs: options.outputs,
providers: options.providers, providers: options.providers,
changeDetection: options.changeDetection || ChangeDetectionStrategy.Default, changeDetection: options.detect ?
(options.onPushOnly ? ChangeDetectionStrategy.OnPush : ChangeDetectionStrategy.Default) :
ChangeDetectionStrategy.Detached,
templateUrl: options.templateUrl, templateUrl: options.templateUrl,
template: options.template, template: options.template,
styles: options.styles, styles: options.styles,
@ -83,6 +86,9 @@ export function RedocComponent(options) {
pipes: pipes pipes: pipes
}); });
console.log(options.selector, options.detect ?
(options.onPushOnly ? 'OnPush' : 'Default') : 'Detached');
return componentDecorator(target) || target; return componentDecorator(target) || target;
}; };
} }

View File

@ -2,10 +2,12 @@
import { Component, EventEmitter } from '@angular/core'; import { Component, EventEmitter } from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common'; import { CORE_DIRECTIVES } from '@angular/common';
import { ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
@Component({ @Component({
selector: 'tabs', selector: 'tabs',
events: ['change'], events: ['change'],
inputs: ['selected'],
template: ` template: `
<ul> <ul>
<li *ngFor="let tab of tabs" [ngClass]="{active: tab.active}" (click)="selectTab(tab)" <li *ngFor="let tab of tabs" [ngClass]="{active: tab.active}" (click)="selectTab(tab)"
@ -14,12 +16,15 @@ import {CORE_DIRECTIVES} from '@angular/common';
<ng-content></ng-content> <ng-content></ng-content>
`, `,
directives: [CORE_DIRECTIVES], directives: [CORE_DIRECTIVES],
styleUrls: ['./lib/shared/components/Tabs/tabs.css'] styleUrls: ['./lib/shared/components/Tabs/tabs.css'],
changeDetection: ChangeDetectionStrategy.OnPush
}) })
@Reflect.metadata('parameters', [[ChangeDetectorRef]])
export class Tabs { export class Tabs {
constructor() { constructor(changeDetector) {
this.tabs = []; this.tabs = [];
this.change = new EventEmitter(); this.change = new EventEmitter();
this.changeDetector = changeDetector;
} }
selectTab(tab, notify = true) { selectTab(tab, notify = true) {
@ -47,6 +52,7 @@ export class Tabs {
prevActive.active = true; prevActive.active = true;
} }
notify && this.change.next(tabTitle); notify && this.change.next(tabTitle);
this.changeDetector.markForCheck();
} }
addTab(tab) { addTab(tab) {
@ -55,6 +61,10 @@ export class Tabs {
} }
this.tabs.push(tab); this.tabs.push(tab);
} }
ngOnInit() {
if (this.selected) this.selected.subscribe(title => this.selectyByTitle(title));
}
} }
@Component({ @Component({