Skip to content

refactor: remove AsyncPipe to reduce bundle size #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions lib/src/clipboard-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AsyncPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ChangeDetectionStrategy, Component, computed } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { merge, of, Subject, timer } from 'rxjs';
import { distinctUntilChanged, map, mapTo, shareReplay, startWith, switchMap } from 'rxjs/operators';
import { distinctUntilChanged, mapTo, shareReplay, switchMap } from 'rxjs/operators';

const BUTTON_TEXT_COPY = 'Copy';
const BUTTON_TEXT_COPIED = 'Copied';
Expand All @@ -11,32 +11,30 @@ const BUTTON_TEXT_COPIED = 'Copied';
template: `
<button
class="markdown-clipboard-button"
[class.copied]="copied$ | async"
[class.copied]="copied()"
(click)="onCopyToClipboardClick()"
>{{ copiedText$ | async }}</button>
>{{ copiedText() }}</button>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [AsyncPipe],
})
export class ClipboardButtonComponent {

private _buttonClick$ = new Subject<void>();

readonly copied$ = this._buttonClick$.pipe(
switchMap(() => merge(
of(true),
timer(3000).pipe(mapTo(false)),
)),
distinctUntilChanged(),
shareReplay(1),
readonly copied = toSignal(
this._buttonClick$.pipe(
switchMap(() => merge(
of(true),
timer(3000).pipe(mapTo(false)),
)),
distinctUntilChanged(),
shareReplay(1),
)
);

readonly copiedText$ = this.copied$.pipe(
startWith(false),
map(copied => copied
? BUTTON_TEXT_COPIED
: BUTTON_TEXT_COPY),
);
readonly copiedText = computed(() => {
const copied = this.copied();
return copied ? BUTTON_TEXT_COPIED : BUTTON_TEXT_COPY;
});

onCopyToClipboardClick(): void {
this._buttonClick$.next();
Expand Down
3 changes: 1 addition & 2 deletions lib/src/markdown.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CommonModule } from '@angular/common';
import { InjectionToken, ModuleWithProviders, NgModule, Provider, SecurityContext } from '@angular/core';
import { MarkedExtension } from 'marked';
import { ClipboardButtonComponent } from './clipboard-button.component';
Expand Down Expand Up @@ -66,7 +65,7 @@ const sharedDeclarations = [
];

@NgModule({
imports: [CommonModule, ...sharedDeclarations],
imports: sharedDeclarations,
exports: sharedDeclarations,
})
export class MarkdownModule {
Expand Down