Created
August 9, 2017 14:26
-
-
Save piotrpalek/6eb2e30136ca059c71e2631b47ab5ca2 to your computer and use it in GitHub Desktop.
latest change CP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
function latestProperty(dependentKeyA, dependentKeyB, latestWin = false) { | |
let aTimestamp, bTimestamp; | |
aTimestamp = bTimestamp = + new Date(); | |
let aCache, bCache; | |
return Ember.computed(dependentKeyA, dependentKeyB, function() { | |
let currentA = Ember.get(this, dependentKeyA); | |
let currentB = Ember.get(this, dependentKeyB); | |
let timestamp = + new Date(); | |
if (currentA !== aCache) { | |
aCache = currentA; | |
aTimestamp = timestamp; | |
} | |
if (currentB !== bCache) { | |
bCache = currentB; | |
bTimestamp = timestamp; | |
} | |
if (aTimestamp === bTimestamp) { | |
return latestWin ? dependentKeyA : dependentKeyB; | |
} else if (aTimestamp > bTimestamp) { | |
return dependentKeyA; | |
} else { | |
return dependentKeyB; | |
} | |
}); | |
} | |
function latestValue(dependentKeyA, dependentKeyB, latestWin = false) { | |
let aTimestamp, bTimestamp; | |
aTimestamp = bTimestamp = + new Date(); | |
let aCache, bCache; | |
return Ember.computed(dependentKeyA, dependentKeyB, function() { | |
let currentA = Ember.get(this, dependentKeyA); | |
let currentB = Ember.get(this, dependentKeyB); | |
let timestamp = + new Date(); | |
if (currentA !== aCache) { | |
aCache = currentA; | |
aTimestamp = timestamp; | |
} | |
if (currentB !== bCache) { | |
bCache = currentB; | |
bTimestamp = timestamp; | |
} | |
if (aTimestamp === bTimestamp) { | |
return latestWin ? currentA : currentB; | |
} else if (aTimestamp > bTimestamp) { | |
return currentA; | |
} else { | |
return currentB; | |
} | |
}); | |
} | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
valueA: 'aaaa', | |
valueB: 'bbbb', | |
latestChangeValue: latestValue('valueA', 'valueB'), | |
latestChangeProperty: latestProperty('valueA', 'valueB') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment