Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active March 20, 2023 05:21
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 dfkaye/2cce87cd5d4d8ce97c44536ec38b151e to your computer and use it in GitHub Desktop.
Save dfkaye/2cce87cd5d4d8ce97c44536ec38b151e to your computer and use it in GitHub Desktop.
factory pattern to create worker-like component objects or actors (eventually)
// 8-9 March 2023
// component.create
// factory pattern to create worker-like component objects or actors (eventually)
// no big plans for expansion, just an illustration; however,
// a couple methods trying to wriggle into the implementation include:
// 1. extend()...? maybe.
// 2. terminate() + onterminate()...? maybe.
// suppose we could turn this into a closure and track each created object
// with addresses then route all messages by address...
var component = {
// 19 March 2023
// TODO
// should be create(...data) where each datum or part is an object spec
// from which we create hybrid objects
create(data) {
// Clone an object from the "prototype" property.
var o = Object.assign({}, component._);
// Record properties to avoid overwriting them in the next step.
var keys = Object.getOwnPropertyNames(o).reduce(function(keys, key) {
keys[key] = key;
return keys;
}, {});
Object.keys(Object(data)).forEach(function(k, i) {
// Allow entries only if not their key is not already defined.
!(k in keys) && (
o[k] = data[k]
);
});
return Object.freeze(o);
},
// not sure this makes sense yet
extend() {
// what will we accept
// what will we allow to be overridden
},
_: {
postMessage(data) {
var error = Object(data) !== Object(data)
? `message data not compatible with structured clone, "${data}"`
: "";
if (error) {
console.error(error);
// TODO: add error dispatcher and error receiver support
return error;
}
'onmessage' in this
? this.onmessage({ data: structuredClone(data), timestamp: Date.now() })
: console.warn(`"onmessage function not defined"`);
}
}
}
var example = component.create({
"smh": "smh",
pwn: function() {
var msg = "you've been pwn'd";
console.warn(msg);
return msg;
},
onmessage(message) {
console.log(JSON.stringify(message, null, 2));
// console.log({ component });
// console.log({ "this": this });
}
});
/* test it out */
console.group("unstructured data");
example.postMessage("hi");
// message data not compatible with structured clone, "hi"
example.postMessage(example.smh);
// message data not compatible with structured clone, "smh"
example.postMessage(example.pwn());
// you've been pwn'd
// message data not compatible with structured clone, "you've been pwn'd"
console.groupEnd("unstructured data");
console.group("testing onmessage");
example.onmessage("testing");
// "testing"
console.groupEnd("testing onmessage");
console.group("default");
((empty) => {
empty = component.create();
console.group("missing onmessage");
empty.postMessage("something");
// message data not compatible with structured clone, "something"
console.groupEnd("missing onmessage");
console.group("frozen interface");
empty.onmessage = function(something) {
console.log("data:", something.data);
console.log("timestamp:", something.timestamp);
}
empty.postMessage(["something"]);
// onmessage function not defined
console.groupEnd("frozen interface");
console.group("patch by extending");
// patch onmessage on the empty default component
var patch = component.create(Object.assign({
onmessage(something) {
console.log("data:", something.data);
console.log("timestamp:", something.timestamp);
}
}, empty));
patch.postMessage(["something"]);
// data: Array [ "something" ]
// timestamp: 1678406504331
console.groupEnd("patched onmessage");
})();
console.groupEnd("default");
console.group("extend");
((base, next) => {
next = component.create(base);
console.group("postMessage");
next.postMessage({ string: "string" });
/*
{
"data": {
"string": "string"
},
"timestamp": 1679288659125
}
*/
console.groupEnd("postMessage");
console.group("trait");
next.onmessage = function() { throw new Error("overridden")
console.log("next.onmessage.overridden")
};
next.postMessage({})
console.groupEnd("trait");
})(example);
console.groupEnd("extend");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment