Skip to content

Commit

Permalink
feat(core): introduce differs extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
trotyl committed Mar 30, 2018
1 parent 9e86c44 commit 6d3a5a1
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Angular extensions powered by community.

Package Name | Features
------------------------- | -------------------------------------------
`@angular-contrib/core` | [`IterableDiffers.extend`][IterableDiffersExtensibility], [`KeyValueDiffers.extend`][KeyValueDiffersExtensibility]
`@angular-contrib/common` | [`<ng-host>`][NgHost], [`ngForIn`][NgForIn], [`ngSwitchCaseContinue`][NgSwitchContinue]

## Usage
Expand Down Expand Up @@ -54,6 +55,8 @@ class AppComponent { }
+ Any feature once implemented by Angular will be deprecated here;


[IterableDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/iterable-differs
[KeyValueDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/key-value-differs
[NgForIn]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/for-in
[NgHost]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/host
[NgSwitchContinue]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/switch-continue
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"build:core": "ng-packagr -p packages/core/package.json",
"build:common": "ng-packagr -p packages/common/package.json"
},
"private": true,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/index.ts
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';
34 changes: 34 additions & 0 deletions packages/core/iterable-differs/README.md
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;
2 changes: 2 additions & 0 deletions packages/core/iterable-differs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './iterable-differs';
export * from './iterable-differs.module';
21 changes: 21 additions & 0 deletions packages/core/iterable-differs/iterable-differs.module.ts
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);
}
}
27 changes: 27 additions & 0 deletions packages/core/iterable-differs/iterable-differs.spec.ts
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);
}));

});
3 changes: 3 additions & 0 deletions packages/core/iterable-differs/iterable-differs.ts
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');
34 changes: 34 additions & 0 deletions packages/core/key-value-differs/README.md
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;
2 changes: 2 additions & 0 deletions packages/core/key-value-differs/index.ts
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 packages/core/key-value-differs/key-value-differs.module.ts
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);
}
}
27 changes: 27 additions & 0 deletions packages/core/key-value-differs/key-value-differs.spec.ts
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);
}));

});
3 changes: 3 additions & 0 deletions packages/core/key-value-differs/key-value-differs.ts
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');
23 changes: 23 additions & 0 deletions packages/core/package.json
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"
}
}

0 comments on commit 6d3a5a1

Please sign in to comment.