Skip to content

Commit

Permalink
Merge pull request #152 from MurhafSousli/pr/151
Browse files Browse the repository at this point in the history
v4.3.0
  • Loading branch information
MurhafSousli authored Apr 21, 2018
2 parents c7dcb18 + 8e491d9 commit 4052d9b
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 82 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 4.3.0

- feat(core): add debounceTime option, closes [#141](https://github.com/MurhafSousli/ngx-progressbar/issues/141) in [#151](https://github.com/MurhafSousli/ngx-progressbar/pull/151)

## 4.2.0

- feat(core): Add `trickleFunc` input. This allows users to change the trickling amount based on progress state, closes [#146](https://github.com/MurhafSousli/ngx-progressbar/issues/146) in [#148](https://github.com/MurhafSousli/ngx-progressbar/pull/148).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class HomeComponent {
| **[direction]** | ltr+ | *Progress bar direction (`ltr+`, `ltr-`, `rtl+`, `rtl-`).* |
| **[trickleSpeed]** | 300 | *Progress trickling speed in ms.* |
| **[trickleFunc]** | Function | *A **function** that returns the trickling amount.* |
| **[debounceTime]** | 0 | *Debounce time before starting the progress bar in ms.* |
| **[speed]** | 200 | *Transition speed in ms.* |
| **[min]** | 8 | *Progress initial starting value.* |
| **[max]** | 100 | *Progress maximum value.* |
Expand Down Expand Up @@ -230,6 +231,7 @@ import { NgProgressModule } from '@ngx-progressbar/core';
| **direction** | ltr+ | *Progress bar direction (`ltr+`, `ltr-`, `rtl+`, `rtl-`).* |
| **trickleSpeed** | 300 | *Progress trickling speed in ms.* |
| **trickleFunc** | Function | *A **function** that returns the trickling amount.* |
| **debounceTime** | 0 | *Debounce time before starting the progress bar in ms.* |
| **speed** | 200 | *Transition speed in ms.* |
| **min** | 8 | *Progress initial starting value.* |
| **max** | 100 | *Progress maximum value.* |
Expand Down
3 changes: 2 additions & 1 deletion lib/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class HomeComponent {
| **[direction]** | ltr+ | *Progress bar direction (`ltr+`, `ltr-`, `rtl+`, `rtl-`).* |
| **[trickleSpeed]** | 300 | *Progress trickling speed in ms.* |
| **[trickleFunc]** | Function | *A **function** that returns the trickling amount.* |
| **[debounceTime]** | 0 | *Debounce time in ms.* |
| **[debounceTime]** | 0 | *Debounce time before starting the progress bar in ms.* |
| **[speed]** | 200 | *Transition speed in ms.* |
| **[min]** | 8 | *Progress initial starting value.* |
| **[max]** | 100 | *Progress maximum value.* |
Expand Down Expand Up @@ -231,6 +231,7 @@ import { NgProgressModule } from '@ngx-progressbar/core';
| **direction** | ltr+ | *Progress bar direction (`ltr+`, `ltr-`, `rtl+`, `rtl-`).* |
| **trickleSpeed** | 300 | *Progress trickling speed in ms.* |
| **trickleFunc** | Function | *A **function** that returns the trickling amount.* |
| **debounceTime** | 0 | *Debounce time before starting the progress bar in ms.* |
| **speed** | 200 | *Transition speed in ms.* |
| **min** | 8 | *Progress initial starting value.* |
| **max** | 100 | *Progress maximum value.* |
Expand Down
37 changes: 13 additions & 24 deletions lib/core/src/ng-progress-ref.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NgProgressState, NgProgressConfig } from './ng-progress.interface';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Subscription } from 'rxjs/Subscription';
import { timer } from 'rxjs/observable/timer';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
Expand All @@ -10,19 +9,16 @@ import { map } from 'rxjs/operators/map';
import { skip } from 'rxjs/operators/skip';
import { delay } from 'rxjs/operators/delay';
import { filter } from 'rxjs/operators/filter';
import { debounce } from 'rxjs/operators/debounce';
import { switchMap } from 'rxjs/operators/switchMap';
import { combineLatest} from 'rxjs/operators/combineLatest';
import { distinctUntilChanged } from 'rxjs/operators/distinctUntilChanged';
import { debounce } from 'rxjs/operators';

export class NgProgressRef {

private _state: NgProgressState = {active: false, value: 0};
private _config: NgProgressConfig;

/** Worker subscription used to unsubscribe from trickling$ stream */
private _workerSub$: Subscription;

/** Stream that increments and updates progress state */
private _trickling$ = new Subject();

Expand Down Expand Up @@ -55,41 +51,38 @@ export class NgProgressRef {
);
}

constructor(config: NgProgressConfig) {
constructor(customConfig: NgProgressConfig) {

/**
* Trickling stream starts the timer that increment the progress bar continuously
* This stream makes it possible to use latest config values while incrementing
*/
this._workerSub$ = this._trickling$.pipe(
debounce(start => timer(start ? this._config.debounceTime : 0)),
this._trickling$.pipe(
debounce((start: boolean) => timer(start ? this._config.debounceTime : 0)),
combineLatest(this.config$),
switchMap(([start, latestConfig]) => start ? this._trickling(latestConfig) : this._complete(latestConfig))
switchMap(([start, config]: [boolean, NgProgressConfig]) => start ? this._trickling(config) : this._complete(config))
).subscribe();

this.setConfig(config);
this.setConfig(customConfig);
}

start() {
this._trickling$.next(true);
}

complete() {
this._trickling$.next(false);
this._trickling$.next(false);
}

inc(amount?: number) {

let n = this._state.value;

const n = this._state.value;
if (!this.isStarted) {
this.start();
} else {
if (typeof amount !== 'number') {
amount = this._config.trickleFunc(n);
}
n = this._clamp(n + amount);
this.set(n);
this.set(n + amount);
}
}

Expand All @@ -107,10 +100,9 @@ export class NgProgressRef {
* Users should use NgProgressManager.destroy(id) instead
*/
destroy() {
this._workerSub$.unsubscribe();
this._trickling$.unsubscribe();
this.state$.unsubscribe();
this.config$.unsubscribe();
this._trickling$.complete();
this.state$.complete();
this.config$.complete();
}

private _setState(state: NgProgressState) {
Expand All @@ -133,10 +125,7 @@ export class NgProgressRef {

/** Completes then resets the progress */
private _complete(config: NgProgressConfig) {
if (!this.isStarted) {
return of({});
}
return of({}).pipe(
return !this.isStarted ? of({}) : of({}).pipe(
// Completes the progress
tap(() => this._setState({value: 100})),

Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/ng-progress.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class NgProgressComponent implements OnInit, OnChanges, OnDestroy {

ngOnChanges() {
if (this.progressRef instanceof NgProgressRef) {
// Update progress bar config when input changes
// Update progress bar config when inputs change
this.progressRef.setConfig({
max: (this.max > 0 && this.max <= 100) ? this.max : 100,
min: (this.min < 100 && this.min >= 0) ? this.min : 0,
Expand All @@ -122,10 +122,10 @@ export class NgProgressComponent implements OnInit, OnChanges, OnDestroy {
})));
/** Subscribes to started and completed events when user used them */
if (this.started.observers.length) {
this._started$ = this.progressRef.started.subscribe(() => this.started.next());
this._started$ = this.progressRef.started.subscribe(() => this.started.emit());
}
if (this.completed.observers.length) {
this._completed$ = this.progressRef.completed.subscribe(() => this.completed.next());
this._completed$ = this.progressRef.completed.subscribe(() => this.completed.emit());
}
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ngx-progressbar/demo",
"description": "A nanoscopic progress bar. Featuring realistic trickle animations to convince your users that something is happening!",
"version": "4.2.0",
"version": "4.3.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down Expand Up @@ -65,7 +65,7 @@
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/cli": "^1.7.0-beta.0",
"@angular/cli": "^1.7.4",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.5.53",
Expand Down
2 changes: 1 addition & 1 deletion src/app/home/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class HomeComponent {
testHttp() {
this.preventAbuse.next(true);

this.http.get('https://reqres.in/api/users?delay=5')
this.http.get('https://reqres.in/api/users?delay=1')
.subscribe(() => {
setTimeout(() => {
this.preventAbuse.next(false);
Expand Down
Loading

0 comments on commit 4052d9b

Please sign in to comment.