Skip to content

Instantly share code, notes, and snippets.

@pelish8
Created September 5, 2017 19:59
Show Gist options
  • Save pelish8/6c1ce7d30ce53de693ae4cf0afb555e4 to your computer and use it in GitHub Desktop.
Save pelish8/6c1ce7d30ce53de693ae4cf0afb555e4 to your computer and use it in GitHub Desktop.
State is shared by multiple instances. On first set new deep copy of state will be created.
// copy-on-set
function Cos(state, parent) {
'use strict';
if (typeof this === 'undefined') {
return new Cos(state, parent);
}
state = state || {};
let copied = false;
this.get = name =>
state[name];
this.set = (name, value) => {
if (!copied) {
state = copy(state);
}
state[name] = value;
};
this.spawn = () => new Cos(state, this);
this.state = () => state;
this.parent = () => parent;
}
// deap copy
function copy(obj) {
if (obj === null || typeof obj !== 'object') {
// primitive types are passed by value
return obj;
}
if (Array.isArray(obj)) {
// copy array
return obj.map(item => copy(item));
}
if (obj instanceof Date) {
// copy date
return new Date(obj.getTime());
}
// copy object
return Object.keys(obj).reduce((obj, item) => obj[item] = copy(obj[item]) && obj,
Object.assign({}, obj));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment