-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): introduce ChangeDetectionScheduler
- Loading branch information
Showing
10 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Auto Change Detection with NoopZone | ||
|
||
Automatically trigger change detection without Zone.js. | ||
|
||
## Type | ||
|
||
**Enhancement** | ||
|
||
## Provenance | ||
|
||
+ https://github.com/angular/angular/issues/20204 | ||
|
||
## NgModule | ||
|
||
`@angular-contrib/core#ChangeDetectionSchedulerModule` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import { ChangeDetectionSchedulerModule } from '@angular-contrib/core'; | ||
|
||
@NgModule({ | ||
imports: [ ChangeDetectionSchedulerModule ], | ||
}) | ||
class MyModule { } | ||
|
||
platformBrowserDynamic.bootstrapModule(MyModule, { ngZone: 'noop' }); | ||
``` | ||
|
||
## Note | ||
|
||
+ Depends on `markForCheck` integration like event binding, async pipe; |
19 changes: 19 additions & 0 deletions
19
packages/core/change-detection-scheduler/change-detection-scheduler.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ComponentFactoryResolver, Injector, NgModule, RendererFactory2 } from '@angular/core'; | ||
import { ChangeDetectionScheduler, ChangeDetectionScheduler_, ChangeDetectionSchedulerInitializer } from './change-detection-scheduler'; | ||
|
||
@NgModule({ | ||
declarations: [ ChangeDetectionSchedulerInitializer ], | ||
entryComponents: [ ChangeDetectionSchedulerInitializer ], | ||
providers: [ | ||
{ provide: ChangeDetectionScheduler, useClass: ChangeDetectionScheduler_ }, | ||
], | ||
}) | ||
export class ChangeDetectionSchedulerModule { | ||
constructor(cfResolver: ComponentFactoryResolver, injector: Injector, rendererFactory: RendererFactory2) { | ||
const componentFactory = cfResolver.resolveComponentFactory(ChangeDetectionSchedulerInitializer); | ||
const renderer = rendererFactory.createRenderer(null, null); | ||
const host = renderer.createElement('div'); | ||
const componentRef = componentFactory.create(injector, undefined, host); | ||
componentRef.destroy(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/core/change-detection-scheduler/change-detection-scheduler.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ChangeDetectorRef, Component, EventEmitter, NgZone } from '@angular/core'; | ||
import { async, inject, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ChangeDetectionScheduler } from './change-detection-scheduler'; | ||
import { ChangeDetectionSchedulerModule } from './change-detection-scheduler.module'; | ||
|
||
describe('ChangeDetectionScheduler', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
let component: TestComponent; | ||
|
||
let mockScheduler: ChangeDetectionScheduler; | ||
|
||
const dummyNoopNgZone: NgZone = { | ||
hasPendingMicrotasks: false, | ||
hasPendingMacrotasks: false, | ||
isStable: true, | ||
onUnstable: new EventEmitter(), | ||
onMicrotaskEmpty: new EventEmitter(), | ||
onStable: new EventEmitter(), | ||
onError: new EventEmitter(), | ||
run(fn: () => any): any { return fn(); }, | ||
runGuarded(fn: () => any): any { return fn(); }, | ||
runOutsideAngular(fn: () => any): any { return fn(); }, | ||
runTask(fn: () => any): any { return fn(); }, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockScheduler = { | ||
schedule: jasmine.createSpy('schedule'), | ||
}; | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestComponent], | ||
imports: [ChangeDetectionSchedulerModule], | ||
providers: [ | ||
{ provide: NgZone, useValue: dummyNoopNgZone }, | ||
{ provide: ChangeDetectionScheduler, useValue: mockScheduler }, | ||
], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('should perform change detection in startup', inject([ChangeDetectionScheduler], (scheduler: ChangeDetectionScheduler) => { | ||
expect(scheduler.schedule).toHaveBeenCalledTimes(1); | ||
})); | ||
|
||
it('should perform change detection after events', inject([ChangeDetectionScheduler], (scheduler: ChangeDetectionScheduler) => { | ||
component.changeDetectorRef.markForCheck(); | ||
expect(scheduler.schedule).toHaveBeenCalledTimes(2); | ||
})); | ||
}); | ||
|
||
@Component({ | ||
selector: 'test-cmp', | ||
template: ``, | ||
}) | ||
class TestComponent { | ||
constructor(public changeDetectorRef: ChangeDetectorRef) { } | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/core/change-detection-scheduler/change-detection-scheduler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ApplicationRef, ChangeDetectorRef, Component, Injectable, NgZone } from '@angular/core'; | ||
|
||
let tickScheduled = false; | ||
|
||
export abstract class ChangeDetectionScheduler { | ||
abstract schedule(): void; | ||
} | ||
|
||
@Injectable() | ||
export class ChangeDetectionScheduler_ implements ChangeDetectionScheduler { | ||
constructor(private appRef: ApplicationRef) { } | ||
|
||
schedule(): void { | ||
tickScheduled = true; | ||
Promise.resolve(null).then(() => { | ||
tickScheduled = false; | ||
this.appRef.tick(); | ||
}); | ||
} | ||
} | ||
|
||
@Component({ | ||
template: '', | ||
}) | ||
export class ChangeDetectionSchedulerInitializer { | ||
constructor(appRef: ApplicationRef, cdRef: ChangeDetectorRef, ngZone: NgZone, scheduler: ChangeDetectionScheduler) { | ||
const markForCheck = cdRef.markForCheck; | ||
|
||
const scheduleTickIfNeeded = () => { | ||
if (!(ngZone instanceof NgZone) && !tickScheduled) { | ||
scheduler.schedule(); | ||
} | ||
}; | ||
|
||
while (typeof cdRef === 'object' && !cdRef.hasOwnProperty('markForCheck') && cdRef.constructor.prototype) { | ||
cdRef = cdRef.constructor.prototype; | ||
} | ||
|
||
cdRef.markForCheck = function () { | ||
markForCheck.call(this); | ||
scheduleTickIfNeeded(); | ||
}; | ||
|
||
scheduleTickIfNeeded(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './change-detection-scheduler'; | ||
export * from './change-detection-scheduler.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './change-detection-scheduler/index'; | ||
export * from './iterable-differs/index'; | ||
export * from './key-value-differs/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters