Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Created May 2, 2023 07:48
Show Gist options
  • Save codeAdrian/c49fb32b40c1ebceffaad7196f4a634f to your computer and use it in GitHub Desktop.
Save codeAdrian/c49fb32b40c1ebceffaad7196f4a634f to your computer and use it in GitHub Desktop.
/* PROXY */
const handleSet = (obj, prop, value) => {
if (prop === "content" && value.length > 0 && value.length < 255) {
const { content, lastUpdated, history = [] } = obj;
const historyUpdated = [...history, { content, lastUpdated }];
if (content && lastUpdated) {
Reflect.set(obj, "history", historyUpdated);
}
Reflect.set(obj, prop, value);
Reflect.set(obj, "lastUpdated", Date.now());
return true;
}
console.error(
"Content length should not be empty and be less than 255 characters"
);
return false;
};
const createDocProxy = (doc) => {
return new Proxy(doc, {
set: handleSet
});
};
/* INITIALIZE */
const doc = createDocProxy({
content: "",
lastUpdated: Date.now()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment