Skip to content

Instantly share code, notes, and snippets.

@wesleyduff
Last active October 8, 2016 13:12
Show Gist options
  • Save wesleyduff/35e339530a45e5c1985f0770bea7c633 to your computer and use it in GitHub Desktop.
Save wesleyduff/35e339530a45e5c1985f0770bea7c633 to your computer and use it in GitHub Desktop.
//This is our base class
class BaseStateManager {
constructor() {
/*
store shape :
name :
state : -1, 0, 1 // this could change : -1 = not changed : 0 = changed but needs more work : 1 = finsihed
*/
this.stores = [];
/*
options states
*/
this.stateOptions = {
NOT_TOUCHED: -1,
TOUCHED: 0,
PRISTINE: 1
}
/*
set initial state
*/
this.overallState = this.stateOptions.NOT_TOUCHED
}
getOverallState() {
for (var store in this.stores) {
if (store.state !== 1) {
return this.stateOptions.TOUCHED;
}
}
}
setOverallState(state) {
for (var key in state) {
if (t.hasOwnProperty(state)) {
this.overallState = state;
}
}
}
/*
* Generic function to take in a collection and update that collections state.
*/
addStore(
name,
initialState,
collection
) {
var stores = this.stores,
createStore = options => {
if (options.collection === undefined) {
options.collection = [];
}
var _store = {
name: options.name,
state: options.state,
collection: options.collection
};
stores.push(_store);
return {
result: true,
store: _store
}
}
//loop over current stores and see if there is one already created
for (var i = 0; i < stores.length; i++) {
if (name === stores[i].name) {
return {
result: false,
message: "The store with the name of " + name + " already exists. Please update the current store or create a new unique store"
}
}
}
createStore({
name: name, state: initialState, collection: collection
});
}
updateStore(
indexOfStoreInArray,
updateStoreObject
) {
if (typeof indexOfStoreInArray !== 'number') {
return {
result: false,
message: "First parameter must be the correct index of your store within the array of stores"
}
}
if (typeof this.stores[indexOfStoreInArray] === 'object') {
this.stores[indexOfStoreInArray] = updateStoreObject;
return {
result: true,
message: "Store: " + updateStoreObject.name + " has been updated",
store: updateStoreObject
}
} else {
return {
result: false,
message: "The store with the index of " + indexOfStoreInArray + ", could not be found"
}
}
}
}
/*
Create your extension of StateManager
*
- Add any extra properties you may require.
* NOTE : New ES6 class structure for JavaScript.
* to convert this "Transpile the code to ES5 using a Transpiler"
*/
class MyStateManager extends BaseStateManager {
//load up any methods your class may need to add to the exisitng BaseStateManaer
myExtraMethod() {
return "Hello extra method!";
}
}
var mystateManager = new MyStateManager();
//set a new store and set its state to touched
//this store is namespaced under module
//this store is the customer contacts container to track its state.
mystateManager.addStore(
"module.customerContacts",
mystateManager.stateOptions.TOUCHED,
[
{
message: "This collecion can be anything you want to manage in the store, OR you can leave this blank"
}
]
);
//set a new store and set its state to touched
//tis store is namespaced under workflowsection
//this store is the general information container to track its state.
mystateManager.addStore(
"workflowSection.generalInformation",
mystateManager.stateOptions.TOUCHED,
[
{
message: "This collecion can be anything you want to manage in the store, OR you can leave this blank"
}
]
);
/*
* TODO : Delete the console logs
* ---
* Just some test examples ...
*
* --- this code is still under development.
*/
console.log('mystateManager---');
console.dir(mystateManager);
console.log('Should be able to call derrived class method "myExtraMethod"');
console.info(mystateManager.myExtraMethod());
console.log('mystateManager---overall state');
console.log(mystateManager.getOverallState());
console.info('Length of stores currently in the mystateManager ... should be 2');
console.log(mystateManager.stores.length === 2);
console.info('the first store in mystateManager should be .. module.customerContacts');
console.log(mystateManager.stores[0].name === 'module.customerContacts');
console.info('Overall state of mystateManager should be "0" "TOUCHED"');
console.log(mystateManager.getOverallState() === mystateManager.stateOptions.TOUCHED);
console.log('Should be able to update a store');
var indexValue = mystateManager.stores.findIndex(function (element, index, array) {
return element.name === 'module.customerContacts';
});
var storeToUpdate = {
name: mystateManager.stores[indexValue].name,
state: mystateManager.stateOptions.PRISTINE,
collection: []
};
var result = mystateManager.updateStore(indexValue, storeToUpdate);
console.dir(result);
console.info(mystateManager.stores[0].state === 1);
console.info('Should not be able to add stores with the same name"');
var result = mystateManager.addStore(
"workflowSection.generalInformation",
mystateManager.stateOptions.PRISTINE,
[
{
message: "This collecion can be anything you want to manage in the store, OR you can leave this blank"
}
]
);
console.dir(result);
console.warn(result.message);
@wesleyduff
Copy link
Author

Be able to store a collection (any type of JavaScript object and its state -1,0,1 : NOT_TOUCHED, TOUCHED, PRISTINE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment