////////////////////////////////////////////////////////////
//// © Microsoft. All rights reserved. ////
////////////////////////////////////////////////////////////
///
///
(function (AppNS) {
// Error strings for logging purposes
var invalidKeyError = "Invalid argument: The specified key is not a non-empty string.";
var invalidValueError = "Invalid argument: The specified value is not valid.";
var storeValueError = "Error storing value '{0}' for key '{1}': '{2}'";
var getValueError = "Error getting value for key '{0}': '{1}'";
var removeError = "Error removing key '{0}': '{1}'";
// Local scope objects
var emptyFunction = function () {};
var localSettings = Windows.Storage.ApplicationData.current.localSettings.values;
// Etw with default noop handlers
var etw = {
stateManagerStoreValueStart: emptyFunction,
stateManagerStoreValueEnd: emptyFunction,
stateManagerGetValueStart: emptyFunction,
stateManagerGetValueEnd: emptyFunction
};
function validateKey(key) {
///
/// Check if the given key is a non-empty string
///
return (typeof key === "string" && key !== "");
};
///
/// Static methods for manipulating persisted states in key/value pairs
///
WinJS.Namespace.defineWithParent(AppNS, "StateManager", {
storeValue: function (key, value) {
///
/// Stores the given key/value pair
///
///
/// The name of the key the value is associated with
///
///
/// The value to store
///
///
/// true if successful; false otherwise.
///
if (!validateKey(key)) {
throw new Error(invalidKeyError);
}
if (value === undefined || value === null) {
throw new Error(invalidValueError);
}
etw.stateManagerStoreValueStart(key);
var succeeded = false;
try {
var valueToStore = "";
if (typeof value === "object") {
valueToStore = JSON.stringify(value);
} else {
valueToStore = value.toString();
}
localSettings.insert(key, valueToStore);
succeeded = true;
} catch (error) {
window.console.log("StateManager.storeValue", storeValueError.format([value, key, error]));
}
etw.stateManagerStoreValueEnd(key);
return succeeded;
},
getValueAsString: function (key) {
///
/// Retrieves the value for the given key
///
///
/// The name of the key to retrieve the value
///
///
/// The string value associated with the given key,
/// or null if the key is not found or error.
///
if (!validateKey(key)) {
throw new Error(invalidKeyError);
}
etw.stateManagerGetValueStart(key);
var value = null;
try {
value = localSettings.lookup(key);
} catch (error) {
window.console.log("StateManager.getValueAsString", getValueError.format([key, error]));
}
etw.stateManagerGetValueEnd(key);
return value;
},
getValueAsObject: function (key) {
///
/// Retrieves the value for the given key and returns it as a JSON object
///
///
/// The name of the key to retrieve the value
///
///
/// The JSON value associated with the given key,
/// or null if the key is not found or error.
///
if (!validateKey(key)) {
throw new Error(invalidKeyError);
}
etw.stateManagerGetValueStart(key);
var value = null;
try {
value = JSON.parse(localSettings.lookup(key));
} catch (error) {
window.console.log("StateManager.getValueAsObject", getValueError.format([key, error]));
}
etw.stateManagerGetValueEnd(key);
return value;
},
remove: function (key) {
///
/// Removes the given key from the store
///
///
/// The name of the key to be removed
///
///
/// true if the key is successfully removed or not present; false otherwise.
///
if (!validateKey(key)) {
throw new Error(invalidKeyError);
}
var succeeded = false;
try {
localSettings.remove(key);
succeeded = true;
} catch (error) {
window.console.log("StateManager.remove", removeError.format([key, error]));
}
return succeeded;
},
clear: function () {
///
/// Clear all settings
///
localSettings.clear();
},
init: function (etwProvider) {
///
/// Initializes state manager
///
///
/// etwProvider is a reference to the ETW provider for logging events.
///
if (etwProvider) {
etw.stateManagerStoreValueStart = etwProvider.stateManagerStoreValueStart || etw.stateManagerStoreValueStart;
etw.stateManagerStoreValueEnd = etwProvider.stateManagerStoreValueEnd || etw.stateManagerStoreValueEnd;
etw.stateManagerGetValueStart = etwProvider.stateManagerGetValueStart || etw.stateManagerGetValueStart;
etw.stateManagerGetValueEnd = etwProvider.stateManagerGetValueEnd || etw.stateManagerGetValueEnd;
}
}
});
})(Microsoft.Paint);