import { tracked } from '@glimmer/tracking'; import Controller from '@ember/controller'; import { action } from '@ember/object'; import { VERSION } from '@ember/version'; import { later } from '@ember/runloop'; // Can't use this in Ember Twiddle yet because it // needs to build with Embroider // See https://github.com/ember-cli/ember-twiddle/issues/732 //import { deepTracked } from 'ember-deep-tracked'; class NumberContainer { @tracked x; y = new (class { @tracked a = 0; }); @tracked z = new (class { @tracked b = 0; }); // This works too onlyChild = { @tracked x: 0 }; // won't work natively (without .set()) @tracked foo = { bar: 0 }; baz = { quux: 0 }; constructor({x, y, z}) { this.x = x; this.y.a = y; this.z.b = z; } } export default class ApplicationController extends Controller { appName = 'Ember Twiddle'; @tracked emberVersion = VERSION; @tracked isIntervalTimerRunning = false; @tracked updateCount = 0; @tracked simpleStringMember = "test"; @tracked shallow = { untracked: 1 }; @tracked container = new NumberContainer({ x: 1, y: 0 }); /* @deepTracked deep = { foo: { bar: { a: [1, 2, 3], }, baz: 456, }, }; */ deep = { foo: { bar: { @tracked a: [1, 2, 3], }, @tracked baz: 456, }, }; updateIntervalMs = 500; onTimer() { if (!this.isIntervalTimerRunning) { return; } this.simpleStringMember = Math.random().toString(16).substr(2, 8); // Error: Assertion Failed...being tracked by a tracking context //this.shallow.untracked += 1; this.set('shallow.untracked', this.shallow.untracked + 1); this.container.x *= 2; this.container.y.a += 1; this.container.z.b = this.simpleStringMember; // These will fail if template references them //this.container.foo.bar = Math.random(); //this.container.baz.quux = Math.random(); this.set('container.foo.bar', Math.random()); this.set('container.baz.quux', Math.random()); this.container.onlyChild.x = Math.random(); // Doesn't work without @deepTracked this.deep.foo.bar.a.forEach((_x, i, array) => { array[i] = Math.floor(100*Math.random()); }) // even if we do this //this.notifyPropertyChange('deep.foo.bar.a'); this.deep.foo.baz = Math.floor(1000*Math.random()); this.updateCount += 1; later(this, this.onTimer, this.updateIntervalMs); } @action toggleTimer() { console.log('toggleTimer'); this.isIntervalTimerRunning = !this.isIntervalTimerRunning; if (this.isIntervalTimerRunning) { this.onTimer(); // yes, it might call it an extra time } } constructor(...args) { super(...args); this.isIntervalTimerRunning = true; this.onTimer(); } }