Skip to content

Commit 131b437

Browse files
committed
fix: URL changes so fast
fixes #252
1 parent cb106cc commit 131b437

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/services/hash.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
'use strict';
22
import { Injectable } from '@angular/core';
33
import { PlatformLocation } from '@angular/common';
4-
54
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
65

6+
import { debounce } from '../utils/';
7+
78
@Injectable()
89
export class Hash {
910
public value = new BehaviorSubject<string | null>(null);
1011
private noEmit:boolean = false;
12+
private debouncedUpdate: (hash:string, rewrite: boolean) => void;
13+
1114
constructor(private location: PlatformLocation) {
1215
this.bind();
16+
17+
this.debouncedUpdate = debounce(this._update.bind(this), 100);
1318
}
1419

1520
start() {
@@ -28,6 +33,10 @@ export class Hash {
2833
}
2934

3035
update(hash: string|null, rewriteHistory:boolean = false) {
36+
this.debouncedUpdate(hash, rewriteHistory);
37+
}
38+
39+
private _update(hash: string|null, rewriteHistory:boolean = false) {
3140
if (hash == undefined) return;
3241
if (rewriteHistory) {
3342
window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash);
@@ -39,4 +48,5 @@ export class Hash {
3948
this.noEmit = false;
4049
});
4150
}
51+
4252
}

lib/utils/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ export function throttle(fn, threshhold, scope) {
9999
};
100100
}
101101

102+
export function debounce(func, wait, immediate = false) {
103+
var timeout;
104+
return function() {
105+
var context = this, args = arguments;
106+
var later = function() {
107+
timeout = null;
108+
if (!immediate) func.apply(context, args);
109+
};
110+
var callNow = immediate && !timeout;
111+
clearTimeout(timeout);
112+
timeout = setTimeout(later, wait);
113+
if (callNow) func.apply(context, args);
114+
};
115+
}
116+
102117
export const isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0
103118
|| (function (p) { return p.toString() === '[object SafariRemoteNotification]'; })(!window['safari']
104119
|| safari.pushNotification);

0 commit comments

Comments
 (0)