You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.1 KiB
97 lines
3.1 KiB
import { Subject } from "../observe/Observable";
|
|
import { Model } from "./AbstractModel";
|
|
import { Observer } from "../observe/Observer";
|
|
import { fetchErrorHandler } from "./FetchErrorHandler";
|
|
import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel";
|
|
import { JsonAPI } from "../singleton/JsonAPI";
|
|
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
|
|
import * as log from "loglevel";
|
|
|
|
export class UserModel implements Subject {
|
|
/**
|
|
* @type {Observer[]} List of subscribers. In real life, the list of
|
|
* subscribers can be stored more comprehensively (categorized by event
|
|
* type, etc.).
|
|
*/
|
|
private readonly observers: Observer[] = [];
|
|
private state: ActiveUserViewModel[] | undefined;
|
|
// @ts-ignore: Cannot find name 'hostAddress'.
|
|
|
|
constructor() { }
|
|
/**
|
|
* The subscription management methods.
|
|
*/
|
|
public attach(observer: Observer): void {
|
|
console.log('Subject: Attached an observer.');
|
|
this.observers.push(observer);
|
|
}
|
|
|
|
public detach(observer: Observer): void {
|
|
const observerIndex = this.observers.indexOf(observer);
|
|
this.observers.splice(observerIndex, 1);
|
|
console.log('Subject: Detached an observer.');
|
|
}
|
|
|
|
/**
|
|
* Trigger an update in each subscriber.
|
|
*/
|
|
public notify(): void {
|
|
console.log('Subject: Notifying observers...');
|
|
for (const observer of this.observers) {
|
|
observer.update(this.state);
|
|
}
|
|
}
|
|
|
|
public someBusinessMethod(activeuserList: ActiveUserViewModel[]): void {
|
|
this.state = activeuserList;
|
|
this.helperMethod();
|
|
console.log(`Subject: My state has just changed`);
|
|
console.log(activeuserList);
|
|
this.notify();
|
|
}
|
|
|
|
/**
|
|
* getActiveUsers
|
|
*/
|
|
public getActiveUsers(): void {
|
|
if(JsonAPI.authToken!= null){
|
|
this.getActiveUsersAjax(JsonAPI.authToken, JsonAPI.ACTIVE_USERS_GET)
|
|
.then(data => {
|
|
// // activeUsers = data;
|
|
// sessionStorage.setItem('activeUsers', JSON.stringify(data));
|
|
// console.log(sessionStorage.getItem('activeUsers'));
|
|
console.log(`Subject: received ajax active users`);
|
|
this.state = data;
|
|
this.notify();
|
|
})
|
|
}
|
|
else {
|
|
log.error('Auth token is null');
|
|
}
|
|
}
|
|
|
|
async getActiveUsersAjax(authToken2: string, URL: string): Promise<any> {
|
|
let headers = new Headers();
|
|
// headers.append('Authorization', basicAuthToken);
|
|
headers.append('X-AUTH-TOKEN', authToken2);
|
|
let response = await fetch(JsonAPI.ACTIVE_USERS_GET, {
|
|
method: 'GET',
|
|
headers: headers
|
|
});
|
|
console.log(response.clone());
|
|
if (fetchErrorHandler(response.clone())) {
|
|
return null;
|
|
}
|
|
let data = await response.json();
|
|
// return data;
|
|
return new Promise((resolve, reject) => {
|
|
if (data != null)
|
|
resolve(data)
|
|
else
|
|
reject('Response data null')
|
|
})
|
|
}
|
|
|
|
private helperMethod() { }
|
|
|
|
}
|