-
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 differs extensibility
- Loading branch information
Showing
14 changed files
with
203 additions
and
0 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
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 './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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# IterableDiffers Extensibility | ||
|
||
Supports extending `IterableDiffers` with custom differ implementations. | ||
|
||
## Type | ||
|
||
**InjectionToken** | ||
|
||
## Provenance | ||
|
||
+ https://github.com/angular/angular/issues/11309 | ||
|
||
## NgModule | ||
|
||
`@angular-contrib/core#IterableDiffersModule` | ||
|
||
## Usage | ||
|
||
Providing custom `IterableDifferFactory`: | ||
|
||
```typescript | ||
import { IterableDiffersModule } from '@angular-contrib/core'; | ||
|
||
const customIterableDifferFactories = [ FastDifferFactory ]; | ||
|
||
@NgModule({ | ||
imports: [ IterableDiffersModule.extend(customIterableDifferFactories) ], | ||
}) | ||
class MyModule { } | ||
``` | ||
|
||
## Note | ||
|
||
+ Can only be provided in root scope; |
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 './iterable-differs'; | ||
export * from './iterable-differs.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Inject, IterableDiffers, IterableDifferFactory, ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { ITERABLE_DIFFER_FACTORIES } from './iterable-differs'; | ||
|
||
@NgModule() | ||
export class IterableDiffersModule { | ||
static extend(factories: IterableDifferFactory[]): ModuleWithProviders { | ||
return { | ||
ngModule: IterableDiffersModule, | ||
providers: [ | ||
{ provide: ITERABLE_DIFFER_FACTORIES, useValue: factories }, | ||
], | ||
}; | ||
} | ||
|
||
constructor( | ||
iterableDiffers: IterableDiffers, | ||
@Inject(ITERABLE_DIFFER_FACTORIES) extraIterableDifferFactories: IterableDifferFactory[], | ||
) { | ||
iterableDiffers.factories.splice(0, 0, ...extraIterableDifferFactories); | ||
} | ||
} |
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,27 @@ | ||
import { Component, IterableDiffer, IterableDiffers, IterableDifferFactory } from '@angular/core'; | ||
import { async, inject, TestBed } from '@angular/core/testing'; | ||
import { IterableDiffersModule } from './iterable-differs.module'; | ||
|
||
describe('IterableDiffers Extensibility', () => { | ||
let spyIterableDifferFactory: IterableDifferFactory; | ||
|
||
beforeEach(() => { | ||
spyIterableDifferFactory = { | ||
supports(value: any): boolean { return true; }, | ||
create(): IterableDiffer<any> { return { value: 42 } as any; }, | ||
}; | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [IterableDiffersModule.extend([spyIterableDifferFactory])], | ||
}).compileComponents(); | ||
})); | ||
|
||
it('should use custom IterableDifferFactory', inject([IterableDiffers], (differs: IterableDiffers) => { | ||
const differ = differs.find([1, 2]).create(); | ||
|
||
expect(differ).toEqual({ value: 42 } as any); | ||
})); | ||
|
||
}); |
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,3 @@ | ||
import { InjectionToken, IterableDifferFactory } from '@angular/core'; | ||
|
||
export const ITERABLE_DIFFER_FACTORIES = new InjectionToken<IterableDifferFactory[]>('ITERABLE_DIFFER_FACTORIES'); |
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,34 @@ | ||
# KeyValueDiffers Extensibility | ||
|
||
Supports extending `KeyValueDiffers` with custom differ implementations. | ||
|
||
## Type | ||
|
||
**InjectionToken** | ||
|
||
## Provenance | ||
|
||
+ https://github.com/angular/angular/issues/11309 | ||
|
||
## NgModule | ||
|
||
`@angular-contrib/core#KeyValueDiffersModule` | ||
|
||
## Usage | ||
|
||
Providing custom `KeyValueDifferFactory`: | ||
|
||
```typescript | ||
import { KeyValueDiffersModule } from '@angular-contrib/core'; | ||
|
||
const customKeyValueDifferFactories = [ FastDifferFactory ]; | ||
|
||
@NgModule({ | ||
imports: [ KeyValueDiffersModule.extend(customKeyValueDifferFactories) ], | ||
}) | ||
class MyModule { } | ||
``` | ||
|
||
## Note | ||
|
||
+ Can only be provided in root scope; |
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 './key-value-differs'; | ||
export * from './key-value-differs.module'; |
21 changes: 21 additions & 0 deletions
21
packages/core/key-value-differs/key-value-differs.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,21 @@ | ||
import { Inject, KeyValueDiffers, KeyValueDifferFactory, ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { KEY_VALUE_DIFFER_FACTORIES } from './key-value-differs'; | ||
|
||
@NgModule() | ||
export class KeyValueDiffersModule { | ||
static extend(factories: KeyValueDifferFactory[]): ModuleWithProviders { | ||
return { | ||
ngModule: KeyValueDiffersModule, | ||
providers: [ | ||
{ provide: KEY_VALUE_DIFFER_FACTORIES, useValue: factories }, | ||
], | ||
}; | ||
} | ||
|
||
constructor( | ||
keyValueDiffers: KeyValueDiffers, | ||
@Inject(KEY_VALUE_DIFFER_FACTORIES) extraKeyValueDifferFactories: KeyValueDifferFactory[], | ||
) { | ||
keyValueDiffers.factories.splice(0, 0, ...extraKeyValueDifferFactories); | ||
} | ||
} |
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,27 @@ | ||
import { Component, KeyValueDiffer, KeyValueDiffers, KeyValueDifferFactory } from '@angular/core'; | ||
import { async, inject, TestBed } from '@angular/core/testing'; | ||
import { KeyValueDiffersModule } from './key-value-differs.module'; | ||
|
||
describe('KeyValueDiffers Extensibility', () => { | ||
let spyKeyValueDifferFactory: KeyValueDifferFactory; | ||
|
||
beforeEach(() => { | ||
spyKeyValueDifferFactory = { | ||
supports(value: any): boolean { return true; }, | ||
create(): KeyValueDiffer<any, any> { return { value: 42 } as any; }, | ||
}; | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [KeyValueDiffersModule.extend([spyKeyValueDifferFactory])], | ||
}).compileComponents(); | ||
})); | ||
|
||
it('should use custom KeyValueDifferFactory', inject([KeyValueDiffers], (differs: KeyValueDiffers) => { | ||
const differ = differs.find([1, 2]).create(); | ||
|
||
expect(differ).toEqual({ value: 42 } as any); | ||
})); | ||
|
||
}); |
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,3 @@ | ||
import { InjectionToken, KeyValueDifferFactory } from '@angular/core'; | ||
|
||
export const KEY_VALUE_DIFFER_FACTORIES = new InjectionToken<KeyValueDifferFactory[]>('KEY_VALUE_DIFFER_FACTORIES'); |
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,23 @@ | ||
{ | ||
"name": "@angular-contrib/core", | ||
"version": "0.0.1-alpha.2", | ||
"description": "", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/trotyl/angular-contrib" | ||
}, | ||
"keywords": [ | ||
"angular" | ||
], | ||
"author": "Trotyl Yu <trotyl@qq.com>", | ||
"license": "MIT", | ||
"ngPackage": { | ||
"lib": { | ||
"entryFile": "index.ts" | ||
}, | ||
"dest": "../../publish/core" | ||
}, | ||
"peerDependencies": { | ||
"@angular/core": "^5.0.0" | ||
} | ||
} |