2018-03-13 05:15:40 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
|
|
|
import { Timer, TimeStamp } from 'smarttime';
|
|
|
|
import { UniverseStore } from './smartuniverse.classes.universestore';
|
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
/**
|
|
|
|
* represents a message within a universe
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
export class UniverseMessage {
|
|
|
|
/**
|
|
|
|
* public and unique id
|
|
|
|
* numeric ascending
|
|
|
|
* adheres to time in milliseconds
|
|
|
|
* avoids duplications though
|
|
|
|
*/
|
|
|
|
public id: number;
|
2018-05-07 16:50:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the universe store the message is attached to
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
public universeStore: UniverseStore;
|
2018-05-19 22:41:59 +00:00
|
|
|
/**
|
|
|
|
* enables unprotected grouping of messages for efficiency purposes.
|
|
|
|
*/
|
|
|
|
public universeChannel: string;
|
2018-05-07 16:50:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* time of creation
|
|
|
|
*/
|
|
|
|
public timestamp: TimeStamp;
|
|
|
|
|
|
|
|
/**
|
2018-05-19 22:41:59 +00:00
|
|
|
* the actual message
|
2018-05-07 16:50:07 +00:00
|
|
|
*/
|
2018-05-19 22:41:59 +00:00
|
|
|
public message: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* any attached payloads. Can be of binary format.
|
|
|
|
*/
|
|
|
|
public attachedPayload: any;
|
2018-04-29 12:17:26 +00:00
|
|
|
public destructionTimer: Timer; // a timer to take care of message destruction
|
2018-03-13 05:15:40 +00:00
|
|
|
|
2018-04-29 12:17:26 +00:00
|
|
|
/**
|
|
|
|
* the constructor to create a universe message
|
2018-05-07 16:50:07 +00:00
|
|
|
* @param messageArg
|
|
|
|
* @param attachedPayloadArg
|
2018-04-29 12:17:26 +00:00
|
|
|
*/
|
2018-05-23 21:50:45 +00:00
|
|
|
constructor(messageArg: string, attachedPayloadArg: any) {
|
2018-03-13 05:15:40 +00:00
|
|
|
this.timestamp = new TimeStamp();
|
|
|
|
this.message = messageArg;
|
|
|
|
this.attachedPayload = attachedPayloadArg;
|
2018-05-19 22:41:59 +00:00
|
|
|
this.fallBackDestruction();
|
|
|
|
}
|
|
|
|
|
2018-05-23 21:50:45 +00:00
|
|
|
public setUniverseStore(universeStoreArg: UniverseStore) {
|
2018-05-19 22:41:59 +00:00
|
|
|
this.universeStore = universeStoreArg;
|
|
|
|
}
|
|
|
|
|
2018-05-23 21:50:45 +00:00
|
|
|
public setDestructionTimer(selfdestructAfterArg: number) {
|
2018-05-19 22:41:59 +00:00
|
|
|
if (selfdestructAfterArg) {
|
|
|
|
this.destructionTimer = new Timer(selfdestructAfterArg);
|
|
|
|
this.destructionTimer.start();
|
2018-03-13 05:15:40 +00:00
|
|
|
|
2018-05-19 22:41:59 +00:00
|
|
|
// set up self destruction by removing this from the parent messageStore
|
|
|
|
this.destructionTimer.completed.then(async () => {
|
|
|
|
this.universeStore.messageStore.remove(this);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.fallBackDestruction();
|
|
|
|
}
|
2018-05-23 21:50:45 +00:00
|
|
|
}
|
2018-05-19 22:41:59 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-23 21:50:45 +00:00
|
|
|
* prevents memory leaks if channels have no default
|
2018-05-19 22:41:59 +00:00
|
|
|
*/
|
2018-05-23 21:50:45 +00:00
|
|
|
private fallBackDestruction() {
|
2018-05-19 22:41:59 +00:00
|
|
|
plugins.smartdelay.delayFor(1000).then(() => {
|
|
|
|
if (!this.destructionTimer) {
|
|
|
|
this.setDestructionTimer(6000);
|
|
|
|
}
|
2018-03-20 07:16:54 +00:00
|
|
|
});
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
}
|