Skip to content

Commit

Permalink
test true update
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb authored and hi-liang committed Oct 25, 2024
1 parent 20d2e52 commit dc5430a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
16 changes: 6 additions & 10 deletions src/components/object/ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@ import { Delete } from '../../systems/core/message-actions';
* Update *is* allowed, which will reset the timer to start from that moment. Note that this is a top-level property
* in MQTT messages, with the seconds value simply as a scalar rather than a nested object property.
*
* @property {number} seconds - Seconds until entity is removed
* @property {int} expireAt - Epoch until entity is removed
* @module ttl
*/
AFRAME.registerComponent('ttl', {
schema: {
seconds: { type: 'number' },
expireAt: { type: 'int' },
},
init() {
if (this.data.seconds > 0) {
console.log('ttl', 'init', this.data.expireAt, this.el.id);
if (this.data.expireAt > 0) {
this.expireAt = Date.now() + this.data.seconds * 1000;
this.tick = AFRAME.utils.throttleTick(this.tick, 1000, this);
}
},
update() {
if (this.data.seconds >= 0) {
// Allow -1 to bypass TTL update, retains previous timeout
this.expireAt = Date.now() + this.data.seconds * 1000;
}
},
tick() {
const now = Date.now();
if (this.expireAt && now > this.expireAt) {
if (this.data.expireAt > 0 && now > this.data.expireAt) {
console.log('ttl', 'delete', this.data.expireAt, this.el.id);
Delete.handle({ id: this.el.id });
}
},
Expand Down
9 changes: 5 additions & 4 deletions src/systems/core/message-actions/create-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ export default class CreateUpdate {
}

if (message.ttl !== undefined) {
// remove first to ensure updates will fire when seconds is unchanged as a keepalive
entityEl.removeAttribute('ttl');
// Allow falsy value of 0
entityEl.setAttribute('ttl', 'seconds', message.ttl);
console.log('ttl', 'message', message.ttl, id);
if (message.ttl >= 0) {
// Allow -1 to bypass TTL update, retains previous timeout
entityEl.setAttribute('ttl', { expireAt: Date.now() + message.ttl * 1000 });
}
}

// Private and program_id flags. Falsy values unset (undefined, null, 0, '')
Expand Down

0 comments on commit dc5430a

Please sign in to comment.