Last active
July 4, 2016 07:24
-
-
Save magarcia/0a299cc1d352ad7e42a46d72817bc035 to your computer and use it in GitHub Desktop.
Blog: Events in Angular2 (https://thingsofgeek.com/2016/07/03/events-in-angular2.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Subject} from 'rxjs/Subject'; | |
import {Observable} from 'rxjs/Observable'; | |
import 'rxjs/add/operator/filter'; | |
import 'rxjs/add/operator/map'; | |
interface BroadcastEvent { | |
key: any; | |
data?: any; | |
} | |
export class Broadcaster { | |
private _eventBus: Subject<BroadcastEvent>; | |
constructor() { | |
this._eventBus = new Subject<BroadcastEvent>(); | |
} | |
broadcast(key: any, data?: any) { | |
this._eventBus.next({key, data}); | |
} | |
on<T>(key: any): Observable<T> { | |
return this._eventBus.asObservable() | |
.filter(event => event.key === key) | |
.map(event => <T>event.data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// child.ts | |
@Component({ | |
selector: 'child' | |
}) | |
export class ChildComponent { | |
constructor(private broadcaster: Broadcaster) { | |
} | |
registerStringBroadcast() { | |
this.broadcaster.on<string>('MyEvent') | |
.subscribe(message => { | |
... | |
}); | |
} | |
emitStringBroadcast() { | |
this.broadcaster.broadcast('MyEvent', 'some message'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!feed.isLocalScreen) { | |
// Until this timeout is reached, the "you are muted" notification | |
// will not be displayed again | |
var mutedWarningTimeout = now(); | |
scope.$on('muted.byRequest', function() { | |
mutedWarningTimeout = secondsFromNow(3); | |
MuteNotifier.muted(); | |
}); | |
scope.$on('muted.byUser', function() { | |
// Reset the warning timeout | |
mutedWarningTimeout = now(); | |
}); | |
scope.$on('muted.Join', function() { | |
mutedWarningTimeout = now(); | |
MuteNotifier.joinedMuted(); | |
}); | |
scope.$watch('vm.feed.isVoiceDetected()', function(newVal) { | |
// Display warning only if muted (check for false, undefined means | |
// still connecting) and the timeout has been reached | |
if (newVal && feed.getAudioEnabled() === false && now() > mutedWarningTimeout) { | |
MuteNotifier.speaking(); | |
mutedWarningTimeout = secondsFromNow(60); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment