Skip to content

Commit

Permalink
Merge pull request #46 from maxisam/feature/singleton_pattern
Browse files Browse the repository at this point in the history
Feature/singleton pattern
  • Loading branch information
maxisam authored May 2, 2017
2 parents 046cc0e + 472277a commit 5dc2f60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/clipboard.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Directive: clipboard', () => {
declarations: [TestClipboardComponent],
imports: [
BrowserModule,
ClipboardModule.forRoot(),
ClipboardModule,
FormsModule]
});
});
Expand Down
14 changes: 12 additions & 2 deletions src/clipboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { element } from 'protractor';
import { WindowSrv } from './window.service';
import { Inject, Injectable, Renderer } from '@angular/core';
import { Inject, InjectionToken, Injectable, Optional, Renderer, SkipSelf } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';


@Injectable()
export class ClipboardService {
private tempTextArea: HTMLTextAreaElement;
Expand Down Expand Up @@ -101,3 +101,13 @@ export class ClipboardService {
return ta;
}
}
// this pattern is mentioned in https://github.com/angular/angular/issues/13854 in #43
export function CLIPBOARD_SERVICE_PROVIDER_FACTORY(doc, windowSrv: WindowSrv, parentDispatcher: ClipboardService) {
return parentDispatcher || new ClipboardService(doc, windowSrv);
};

export const CLIPBOARD_SERVICE_PROVIDER = {
provide: ClipboardService,
deps: [DOCUMENT, WindowSrv, [new Optional(), new SkipSelf(), ClipboardService]],
useFactory: CLIPBOARD_SERVICE_PROVIDER_FACTORY
};
18 changes: 6 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { CommonModule } from '@angular/common';
import { WindowSrv } from './window.service';
import { ClipboardService } from './clipboard.service';
import { ModuleWithProviders, NgModule } from '@angular/core';
import { ClipboardDirective } from './clipboard.directive';
import { CLIPBOARD_SERVICE_PROVIDER } from './clipboard.service';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
export * from './clipboard.directive';
export * from './clipboard.service';

@NgModule({
declarations: [ClipboardDirective],
exports: [ClipboardDirective],
imports: [CommonModule]
imports: [CommonModule],
providers: [WindowSrv, CLIPBOARD_SERVICE_PROVIDER]
})
export class ClipboardModule {
public static forRoot(): ModuleWithProviders {
return {
ngModule: ClipboardModule,
providers: [ClipboardService, WindowSrv]
};
}
}
export class ClipboardModule { }

0 comments on commit 5dc2f60

Please sign in to comment.