smartuniverse/ts/smartuniverse.classes.universemessage.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

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;
public universeStore: UniverseStore;
2018-04-29 12:17:26 +00:00
public timestamp: TimeStamp; // when has this message been created
public topic: string; // enables unprotected grouping of messages for efficiency purposes.
public message: string; // the actual message
public attachedPayload: any; // any attached payloads. Can be of binary format.
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
* @param parentUniverseStore
* @param messageArg
* @param attachedPayloadArg
* @param selfdestructAfterArg
*/
2018-03-20 07:16:54 +00:00
constructor(
parentUniverseStore: UniverseStore,
messageArg: string,
attachedPayloadArg: any,
selfdestructAfterArg: number
) {
2018-03-13 05:15:40 +00:00
this.universeStore = parentUniverseStore;
this.timestamp = new TimeStamp();
this.message = messageArg;
this.attachedPayload = attachedPayloadArg;
2018-03-20 07:16:54 +00:00
this.destructionTimer = new Timer(selfdestructAfterArg);
this.destructionTimer.start();
2018-03-13 05:15:40 +00:00
// set up self destruction by removing this from the parent messageStore
this.destructionTimer.completed.then(async () => {
this.universeStore.messageStore.remove(this);
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
}
}