Skip to content

Instantly share code, notes, and snippets.

@tkh44
Created January 13, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkh44/8c9b2a72272ff5b3a37549562474f537 to your computer and use it in GitHub Desktop.
Save tkh44/8c9b2a72272ff5b3a37549562474f537 to your computer and use it in GitHub Desktop.
var objectForeach = require("object-foreach");
// list of keys to make the tree structure map
const store = {
optimist: [],
entities: {
users: {}
}
};
// const time = new Set();
// const entry = new Map([[]]);
// 7 slots that have 7 slots each and so on.
// new emitter to organize it
// "ok everyone bob got a new car, lets all acknowlege that"
// everyone stamps their values at that time
// emitter organizes the point in time we are viewing
// walker https://github.com/matthewkastor/object-walk
const LINK = Symbol("LINK");
class Value {
constructor(trail, key, value) {
this.trail = trail;
this.key = key;
this.value = [value];
this.chain = this.create();
}
create() {
const value = this.value[
this.value.length - 1
];
let localTrail = [];
let chain = {};
if (
typeof value !== "object" ||
Array.isArray(value) ||
value == null
) {
return {};
}
Object.keys(value).forEach(key => {
localTrail.push(key);
chain[localTrail.join(".")] = new Value(
localTrail.slice(),
key,
value[key]
);
});
return chain;
}
// not sure of this name
// What if we were able to update a series of values at once.
update(key, value) {
this.value.push(value);
}
get(key) {
if (
key === this.trail.concat([key]).join(".")
) {
return this.value[this.value.length - 1];
}
if (this.chain[key] !== undefined) {
const instance = this.chain[key];
// console.log(instance)
return instance.get(key);
}
}
set(key, value) {
if (
key === this.trail.concat([key]).join(".")
) {
this.update(key, value);
return;
}
if (this.chain[key] !== undefined) {
const instance = this.chain[key];
instance.update(key, value);
return;
}
}
// lens(steps) {
// return this.value[this.value.length - steps];
// }
}
const test = new Value([], LINK, store);
console.log(test);
test.set("optimist.entities", [
"blue",
"red",
"green"
]);
console.log(test.get("optimist.entities"));
// Doesn't work yet
test.set(
"optimist.entities",
test.get(
"optimist.entities.session.analytics.typeaheadUsers.blockedUsers.settings"
)
);
console.log(test.get("optimist.entities"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment