Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active May 6, 2017 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/d70cfae2843066108153af8173355fcb to your computer and use it in GitHub Desktop.
Save Ibro/d70cfae2843066108153af8173355fcb to your computer and use it in GitHub Desktop.
RxJS Socket.IO Angular Chat - Angular Component - Coding Blast - www.codingblast.com
export class AppComponent implements OnInit {
message: string;
messages: string[] = [];
secretCode: string;
constructor(private chatService: ChatService) {
this.secretCode = 'DONT TELL';
}
ngOnInit() {
this.chatService
.getMessages()
.distinctUntilChanged()
.filter((message) => message.trim().length > 0)
.throttleTime(1000)
.skipWhile((message) => message !== this.secretCode)
.scan((acc: string, message: string, index: number) =>
`${message}(${index + 1})`
, 1)
.subscribe((message: string) => {
const currentTime = moment().format('hh:mm:ss a');
const messageWithTimestamp = `${currentTime}: ${message}`;
this.messages.push(messageWithTimestamp);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment