Bitwise Operators Part 6

Hey! Sorry, I was with a problem in my back (sciatic nerve), and I can’t stay in from of the computer for a long time.

I’m better now, and can continue with the Bitwise Operator post! At the last, we finished the Infraestructure layer and now, we will start with the logic.

The state manager

This is was we created a few posts ago:

const currentState = { stateName: '', value: -1 };

export class StateManager {
  set currentState(stateName) {}

  get currentState() {}

  createState(stateName) {}

  createBitwiseOrRule(ruleName, stateArray) {}

  getRules() {}

  clearAllData() {}

  checkRule(ruleName) {}
}

Creating a state

The code is pretty simple: it’ll receive a state name, and it’ll invoke the method ‘saveObject’ from the localStoreManager class.

To do this, we must import the localStoreManager class with this line:

import { LocalStoreManager } from '../dataAccess/localStoreManager.js';

And you should instantiate the class using the ‘new’ operator (please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new ).

const lsManager= new LocalStoreManager();

Now, let’s code the createState method. The ‘saveObject’ method receives a key and a value. The key will be the stateName parameter, and the value will be a JSON with the type and the value.

To create the value, we’ll use the left shift to automatically generates the values using the same logic as the example in the part 2 ( https://camilopedroso.com/2019/06/16/bitwise-operators-part-2/ ).

function nextValue() {
  let keys = lsManager.getKeys();
  const i = 1 << keys.length;
  return i;
}

And then, this is our createState method:

createState(stateName) {
    lsManager.saveObject(
      stateName,
      JSON.stringify({ type: 'state', value: nextValue() })
    );
  }

This way, it will be easier to retrieve the states and the rules when necessary.

Testing and debugging

In the ‘./viewModels/index.js’, delete all the content and put this code:

import { StateManager } from '../businessLogic/stateManager.js';

const sm = new StateManager();

sm.createState('MyFrstState');
sm.createState('MyScndState');
sm.createState('MyThrdState');

Running this code on VS Code, we can easily see it working:

Creating ‘MyFrstState’

The createState method will receive the argument ‘MyFrstState’…

To create the value, it will invoke the nextValue method, which works this way:

1. Retrieve all keys, in this case, as is the 1st round, there is no keys stored at local storage.

2. As the length of the array is zero, the result of the letf shift will be 1.

And in the next rounds, the length of the array will be 1, which will generate the value 2 and 2, which will generate the value 4.

If we check the local storage, we’ll find this:

Well, I won’t abuse of my back and tomorrow I’ll continue with the logic!

Stay tuned, and happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s