42 lines
654 B
JavaScript
42 lines
654 B
JavaScript
import {compose} from '@avocado/core';
|
|
|
|
import {StateProperty, Trait} from '../trait';
|
|
|
|
const decorate = compose(
|
|
);
|
|
|
|
export default class Perishable extends decorate(Trait) {
|
|
|
|
static defaultParams() {
|
|
return {
|
|
ttl: 300,
|
|
};
|
|
}
|
|
|
|
static describeParams() {
|
|
return {
|
|
ttl: {
|
|
type: 'number',
|
|
label: 'Time-to-live in seconds',
|
|
},
|
|
};
|
|
}
|
|
|
|
static type() {
|
|
return 'perishable';
|
|
}
|
|
|
|
constructor(entity, params, state) {
|
|
super(entity, params, state);
|
|
this.ttl = this.params.ttl;
|
|
}
|
|
|
|
tick(elapsed) {
|
|
this.ttl -= elapsed;
|
|
if (this.ttl <= 0) {
|
|
this.entity.destroy();
|
|
}
|
|
}
|
|
|
|
}
|