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',
styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
directives: [JsonSchema, DropDown],
inputs: ['isArray', 'final', 'nestOdd', 'childFor', 'isRequestSchema']
inputs: ['isArray', 'final', 'nestOdd', 'childFor', 'isRequestSchema'],
detect: true
})
@Reflect.metadata('parameters', [[SchemaManager], [ElementRef]])
export class JsonSchema extends BaseComponent {

View File

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

View File

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

View File

@ -29,7 +29,9 @@ var _modeLocked = false;
],
templateUrl: './lib/components/Redoc/redoc.html',
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', [
[SchemaManager], [OptionsService], [ElementRef], [RedocEventsService]])

View File

@ -1,6 +1,6 @@
<header *ngIf="data.schemaPointer || data.samples.length"> Request samples </header>
<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">
<schema-sample [pointer]="data.schemaPointer"> </schema-sample>
</tab>

View File

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

View File

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

View File

@ -11,7 +11,9 @@ import { ScrollService, Hash, MenuService, OptionsService } from '../../services
selector: 'side-menu',
templateUrl: './lib/components/SideMenu/side-menu.html',
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],
[ScrollService], [MenuService], [Hash], [OptionsService], [ChangeDetectorRef]])

View File

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

View File

@ -1,11 +1,13 @@
'use strict';
import {Component, EventEmitter} from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';
import { Component, EventEmitter } from '@angular/core';
import { CORE_DIRECTIVES } from '@angular/common';
import { ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'tabs',
events: ['change'],
inputs: ['selected'],
template: `
<ul>
<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>
`,
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 {
constructor() {
constructor(changeDetector) {
this.tabs = [];
this.change = new EventEmitter();
this.changeDetector = changeDetector;
}
selectTab(tab, notify = true) {
@ -47,6 +52,7 @@ export class Tabs {
prevActive.active = true;
}
notify && this.change.next(tabTitle);
this.changeDetector.markForCheck();
}
addTab(tab) {
@ -55,6 +61,10 @@ export class Tabs {
}
this.tabs.push(tab);
}
ngOnInit() {
if (this.selected) this.selected.subscribe(title => this.selectyByTitle(title));
}
}
@Component({