Class with array and dirty flag.

Uses Proxies to have the dirty flag monitor an array inside of the class.

by Bryan Braun

JavaScript

class CommerceAsset {
	constructor(map) {
  	this.lifeCycleState = "ORIGINAL";

    const assetMap = map || [];
    
    this.lifeCycleHandler = {
      set: (obj, prop, value) => {
        obj[prop] = value;
        this.lifeCycleState = "CHANGED";
        return true;
      },
    };
    this.maps = new Proxy(assetMap, this.lifeCycleHandler);
  }

	setNewItem(item) {
	  this.maps.push(item);
  }
  
  setLifeCycleState(state) {
  	this.lifeCycleState = state;
  }
  
  getLifeCycleState() {
  	return this.lifeCycleState
	}
}

const ca = new CommerceAsset();

console.log(ca.getLifeCycleState());

ca.setNewItem('hello');
ca.setNewItem('world');

console.log(ca.getLifeCycleState());

ca.setLifeCycleState('EXPIRED');

console.log(ca.getLifeCycleState());