fix(core): update

This commit is contained in:
2021-01-28 01:30:27 +00:00
parent bfa3330eb6
commit 08d7224016
8 changed files with 152 additions and 22 deletions

View File

@ -1 +1,2 @@
export * from './connection';
export * from './tag';

6
ts/interfaces/tag.ts Normal file
View File

@ -0,0 +1,6 @@
export interface ITag <T = any> {
id: string;
payload: T;
}
export type TTagStore = {[key: string]: ITag};

View File

@ -45,6 +45,40 @@ export class SmartsocketClient {
public socketRequests = new plugins.lik.ObjectMap<SocketRequest<any>>();
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
// tagStore
private tagStore: {[key: string]: interfaces.ITag} = {};
private tagStoreSubscription: plugins.smartrx.rxjs.Subscription;
/**
* adds a tag to a connection
*/
public async addTag(tagArg: interfaces.ITag) {
if (this.socketConnection) {
await this.socketConnection.addTag(tagArg);
} else {
this.tagStore[tagArg.id] = tagArg;
}
}
/**
* gets a tag by id
* @param tagIdArg
*/
public async getTagById(tagIdArg: interfaces.ITag['id']) {
return this.tagStore[tagIdArg];
};
/**
* removes a tag from a connection
*/
public async removeTagById(tagIdArg: interfaces.ITag['id']) {
if (this.socketConnection) {
this.socketConnection.removeTagById(tagIdArg);
} else {
delete this.tagStore[tagIdArg];
}
}
constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias;
this.serverUrl = optionsArg.url;
@ -129,6 +163,13 @@ export class SmartsocketClient {
// handle connection
this.socketConnection.socket.on('connect', async () => {
this.tagStoreSubscription?.unsubscribe();
this.tagStoreSubscription = this.socketConnection.tagStoreObservable.subscribe(tagStoreArg => {
this.tagStore = tagStoreArg
});
for (const keyArg of Object.keys(this.tagStore)) {
this.socketConnection.addTag(this.tagStore[keyArg]);
}
this.updateStatus('connected');
});

View File

@ -57,6 +57,10 @@ export class SocketConnection {
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
private tagStore: interfaces.TTagStore = {};
public tagStoreObservable = new plugins.smartrx.rxjs.Subject<interfaces.TTagStore>();
public remoteTagStoreObservable = new plugins.smartrx.rxjs.Subject<interfaces.TTagStore>();
constructor(optionsArg: ISocketConnectionConstructorOptions) {
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
@ -82,6 +86,42 @@ export class SocketConnection {
});
}
/**
* adds a tag to a connection
*/
public async addTag(tagArg: interfaces.ITag) {
const done = plugins.smartpromise.defer();
this.tagStore[tagArg.id] = tagArg;
this.tagStoreObservable.next(this.tagStore);
const remoteSubscription = this.remoteTagStoreObservable.subscribe((remoteTagStore) => {
const localTagString = plugins.smartjson.stringify(tagArg);
const remoteTagString = plugins.smartjson.stringify(remoteTagStore[tagArg.id])
if (localTagString === remoteTagString) {
remoteSubscription.unsubscribe();
done.resolve();
}
})
this.socket.emit('updateTagStore', this.tagStore);
await done.promise;
}
/**
* gets a tag by id
* @param tagIdArg
*/
public async getTagById(tagIdArg: interfaces.ITag['id']) {
return this.tagStore[tagIdArg];
};
/**
* removes a tag from a connection
*/
public async removeTagById(tagIdArg: interfaces.ITag['id']) {
delete this.tagStore[tagIdArg];
this.tagStoreObservable.next(this.tagStore);
this.socket.emit('updateTagStore', this.tagStore);
}
// authenticating --------------------------
/**
@ -91,7 +131,7 @@ export class SocketConnection {
const done = plugins.smartpromise.defer();
this.socket.on('dataAuth', async (dataArg: ISocketConnectionAuthenticationObject) => {
logger.log('info', 'received authentication data. now hashing and comparing...');
this.socket.removeListener('dataAuth', () => {});
this.socket.removeAllListeners('dataAuth');
if (await SocketRole.checkPasswordForRole(dataArg, this.smartsocketRef)) {
// TODO: authenticate password
this.alias = dataArg.alias;
@ -150,6 +190,20 @@ export class SocketConnection {
);
targetSocketRequest.handleResponse(dataArg);
});
this.socket.on('updateTagStore', async (tagStoreArg: interfaces.TTagStore) => {
const exitingStoreString = plugins.smartjson.stringify(this.tagStore);
const newStoreString = plugins.smartjson.stringify(tagStoreArg);
console.log(exitingStoreString);
console.log(newStoreString);
if (exitingStoreString !== newStoreString) {
this.tagStore = tagStoreArg;
this.socket.emit('updateTagStore', this.tagStore);
this.tagStoreObservable.next(this.tagStore);
}
this.remoteTagStoreObservable.next(tagStoreArg);
})
logger.log('info', `now listening to function requests for ${this.alias}`);
done.resolve(this);
} else {

View File

@ -8,6 +8,7 @@ import * as isohash from '@pushrocks/isohash';
import * as isounique from '@pushrocks/isounique';
import * as lik from '@pushrocks/lik';
import * as smartenv from '@pushrocks/smartenv';
import * as smartjson from '@pushrocks/smartjson';
import * as smartlog from '@pushrocks/smartlog';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise';
@ -19,6 +20,7 @@ export {
isounique,
lik,
smartenv,
smartjson,
smartlog,
smartdelay,
smartpromise,