Skip to content

Instantly share code, notes, and snippets.

@magarcia
Last active July 4, 2016 07:24
Show Gist options
  • Save magarcia/0a299cc1d352ad7e42a46d72817bc035 to your computer and use it in GitHub Desktop.
Save magarcia/0a299cc1d352ad7e42a46d72817bc035 to your computer and use it in GitHub Desktop.
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);
}
}
// 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');
}
}
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