Created
October 23, 2014 09:18
-
-
Save alivedise/87cd6b95bc15173062a0 to your computer and use it in GitHub Desktop.
MockSettingsCore WIP
This file contains hidden or 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
'use strict'; | |
requireApp('system/shared/test/unit/mocks/mock_promise.js'); | |
(function(exports) { | |
exports.MockSettingsCore = { | |
name: 'SettingsCore', | |
mSettings: {}, | |
mOnflyOperations: [], | |
mTriggerSettingsChange: function(setting) { | |
var promise = new Promise(function(resolve) { | |
for (var key in setting) { | |
if (!this.mSettings[key]) { | |
this.mSettings[key] = { | |
name: key, | |
observers: [] | |
} | |
} | |
this.mSettings[key].value = setting[key]; | |
console.log(key, setting[key], ' written...'); | |
if (this.mSettings[key].observers) { | |
console.log('Trigger observers...', this.mSettings[key].observers); | |
this.mSettings[key].observers.forEach(function(observer) { | |
observer.observe(key, setting[key]); | |
}); | |
} | |
console.log('onfly:mTriggerSettingsChange', this.mOnflyOperations); | |
this.mOnflyOperations.forEach(function(operation, index) { | |
if (operation.type === 'get' && operation.data === key) { | |
console.log('Trigger getters...'); | |
operation.resolve(setting[key]); | |
this.mOnflyOperations.splice(index, 1); | |
} | |
}, this); | |
} | |
resolve(); | |
}.bind(this)); | |
return promise; | |
}, | |
mSetup: function() { | |
this.mOnflyOperations = []; | |
this.mSettings = {}; | |
this.mQueuingPromises = []; | |
System && System.register('addObserver', this); | |
System && System.register('removeObserver', this); | |
System && System.register('get', this); | |
System && System.register('set', this); | |
}, | |
mTeardown: function() { | |
this.mOnflyOperations = []; | |
this.mSettings = {}; | |
this.mQueuingPromises = []; | |
System && System.unregister('addObserver', this); | |
System && System.unregister('removeObserver', this); | |
System && System.unregister('get', this); | |
System && System.unregister('set', this); | |
}, | |
addObserver: function(key, observer) { | |
if (!this.mSettings[key]) { | |
this.mSettings[key] = { | |
name: key, | |
observers: [] | |
} | |
} | |
this.mSettings[key].observers.push(observer); | |
}, | |
removeObserver: function(key, observer) { | |
if (!this.mSettings[key]) { | |
return; | |
} | |
var index = this.mSettings[key].observers.indexOf(observer); | |
this.mSettings[key].observers.splice(index, 1); | |
}, | |
get: function(key) { | |
this.mQueuingPromises.forEach(function(p, index) { | |
if (p.type === 'set') { | |
p.resolve(); | |
} | |
this.mQueuingPromises.splice(index, 1); | |
}, this); | |
var fakePromise = new Promise(function(resolve) { | |
this.mOnflyOperations.push({ | |
type: 'get', | |
data: key, | |
resolve | |
}); | |
}.bind(this)); | |
return fakePromise; | |
}, | |
mAwait: function(type) { | |
var p = new Promise(function(resolve) { | |
this.mQueuingPromises.push({ | |
resolve: resolve, | |
type: type | |
}); | |
}.bind(this)); | |
return p; | |
}, | |
mCommit: function(desiredKey) { | |
console.log('onfly:mCommit', this.mOnflyOperations); | |
var promise = new Promise(function(resolve) { | |
var subpromises = []; | |
this.mOnflyOperations.forEach(function(operation, index) { | |
if (operation.type === 'set') { | |
var p = new Promise(function(iResolve) { | |
this.mTriggerSettingsChange(operation.data).then(function() { | |
console.log('set operation of ' + JSON.stringify(operation.data) + 'is done'); | |
operation.resolve(); | |
this.mOnflyOperations.splice(index, 1); | |
iResolve(); | |
}.bind(this)); | |
}.bind(this)); | |
} | |
}, this); | |
Promise.all(subpromises).then(function() { | |
resolve(); | |
}.bind(this)); | |
}.bind(this)); | |
return promise; | |
}, | |
set: function(setting) { | |
console.log('set', JSON.stringify(setting)); | |
this.mQueuingPromises.forEach(function(p, index) { | |
if (p.type === 'set') { | |
p.resolve(); | |
} | |
this.mQueuingPromises.splice(index, 1); | |
}, this); | |
var fakePromise = new Promise(function(resolve) { | |
this.mOnflyOperations.push({ | |
type: 'set', | |
data: setting, | |
resolve: resolve | |
}); | |
}.bind(this)); | |
return fakePromise; | |
} | |
}; | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment