Skip to content

Instantly share code, notes, and snippets.

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 petsel/e9725ec9e58ea4f963a8568ff689fcec to your computer and use it in GitHub Desktop.
Save petsel/e9725ec9e58ea4f963a8568ff689fcec to your computer and use it in GitHub Desktop.
// example-of-function-based-mixin-composition-with-class-and-shared-local-state.js
function withExposableSharedLocalState (state) {
state = ((typeof state === "object") && state) || (void 0);
var json_stringify = JSON.stringify;
var json_parse = JSON.parse;
var compositeType = this;
compositeType.valueOf = function () {
return ((typeof state !== "undefined") ? json_parse(json_stringify(state)) : state);
};
compositeType.toString = function () {
return json_stringify(state);
};
return compositeType;
}
const withEnumerableFirstLast = (function withEnumerableFirstLastFactory () {
function first () {
return this[0];
}
function last () {
return this[this.length - 1];
}
return function withEnumerableFirstLast () {
const enumerableType = this;
enumerableType.first = first;
enumerableType.last = last;
return enumerableType;
};
}());
function withEnumerableListProxy (sharedList) {
sharedList = withEnumerableFirstLast.call((Array.isArray(sharedList) && sharedList) || []);
const compositeType = this;
compositeType.first = function () {
return sharedList.first();
};
compositeType.last = function () {
return sharedList.last();
};
return compositeType;
}
class Queue {
constructor () {
const
queue = this,
list = [];
queue.enqueue = function (item) {
list.push(item);
return item;
};
queue.dequeue = function () {
return list.shift();
};
withEnumerableListProxy.call(queue, list);
withExposableSharedLocalState.call(queue, list);
return queue;
}
}
const queue = new Queue();
queue.enqueue("the");
queue.enqueue("quick");
queue.enqueue("brown");
console.log("queue : ", queue);
console.log("queue.enqueue('fox') : ", queue.enqueue('fox'));
console.log("queue.first() : ", queue.first());
console.log("queue.last() : ", queue.last());
console.log("queue.dequeue() : ", queue.dequeue());
console.log("queue.first() : ", queue.first());
console.log("queue.last() : ", queue.last());
console.log("queue.valueOf() : ", queue.valueOf());
console.log("queue.toString() : ", queue.toString());
console.log("('' + queue) : ", ('' + queue));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment