Skip to content

Instantly share code, notes, and snippets.

@pzuraq
Last active December 27, 2019 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzuraq/1f5043aecd771df5768f85fa9d3b67eb to your computer and use it in GitHub Desktop.
Save pzuraq/1f5043aecd771df5768f85fa9d3b67eb to your computer and use it in GitHub Desktop.
import { TrackedWeakMap } from 'tracked-built-ins';
function localCopy(target, key) {
let values = new TrackedWeakMap();
let lastValues = new WeakMap();
return {
get() {
let incoming = this.args[key];
let lastValue = lastValues.get(this);
if (lastValue !== incoming) {
values.set(this, incoming);
lastValues.set(this, incoming);
}
return values.get(this);
},
set(value) {
values.set(this, value);
}
}
}
class MyComponent extends Component {
@localCopy myValue;
}
class MyComponent extends Component {
@tracked _myValue;
get myValue() {
let incoming = this.args.myValue;
let lastValue = this._lastValue;
if (lastValue !== incoming) {
this._myValue = this._lastValue = incoming;
}
return this._myValue;
}
set myValue(value) {
this._myValue = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment