Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active July 4, 2022 20:30
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/4f368f446cb1efc06fb0c764f0e8a4f6 to your computer and use it in GitHub Desktop.
Save dfkaye/4f368f446cb1efc06fb0c764f0e8a4f6 to your computer and use it in GitHub Desktop.
testing worker functions independently ~ prior to parsing and embedding
// 27 june 2022
// testing workers functions independently ~ prior to parsing and embedding
// yet one more step in the quest for making embedded workers testable
// from the console...
// ...continued from "communicating embedded web workers"
// at https://gist.github.com/dfkaye/c6d77fde86aea70ef6ae79bda2219582
// demonstrates how to *test* one or more worker script functions **as standalone functions** outside the worker context.
// This may be a bad or (worse) useless idea...
// Main concept, this function accepts a subject function with a single "self" parameter,
// plus an optional "self" options object to be passe to the subject function.
function Test(fn, self) {
self = Object(self);
typeof self.postMessage == "function" || (
Object.assign(self, {
postMessage: console.log.bind(console)
})
);
fn(self);
return self;
}
function onmessage(self) {
self.onmessage = function (request) {
var data = Object(request.data);
console.warn(data.sender, data.address, data.action, data.value);
var response = Object.assign({}, data);
self.postMessage(response);
}
}
var test = Test(onmessage);
// exercise onmessage directly
test.onmessage({ data: {
sender: "me",
address: "you",
action: "log",
value: "hello"
}});
// me you log hello
// Object { sender: "me", address: "you", action: "log", value: "hello" }
// override the default logging version of postMessage in the "self" options object...
var mocked = Test(onmessage, {
postMessage: function (response) {
console.log(
response.address, "posting", response.value,
"to", response.sender,
response
);
}
});
mocked.onmessage({ data: {
sender: "mocker",
address: "mocked",
action: "test",
value: "message"
}});
// mocked posting message to mocker
// Object { sender: "mocker", address: "mocked", action: "test", value: "message" }
@dfkaye
Copy link
Author

dfkaye commented Jul 4, 2022

Note on the Test() function

4 July 2022.

The Test() function uses the Object() constructor fix rather than default assignment on the self parameter if it's missing or undefined.

The default assignment fails when self is a primitive (because we don't store the result of Object.assign(), assuming self is already an object, whereas Object() creates a wrapped primitive.

Here's a comparison of the strategies...

function no(fn, self = {}) {
  typeof self.postMessage == "function" || (
    Object.assign(self, {
      postMessage: console.warn.bind(console)
    })
  );

  fn(self);
  
  return self;
}

var F = function (self) {
  self.postMessage({ self, type: typeof self });

  return self;
};


function yes(fn, self) {
  self = Object(self);

  typeof self.postMessage == "function" || (
    Object.assign(self, {
      postMessage: console.log.bind(console)
    })
  );

  fn(self);
  
  return self;
}

/* test it out */

var tests = [
  [F],
  [F, undefined],
  [F, null],
  [F, 0],
  [F, 42]
];

tests.forEach(function (pair) {
  try { no.apply({}, pair); }
  catch (e) { console.error("no:", e); }
});
// Object { self: { postMessage: warn() }, type: "object" }
// Object { self: { postMessage: warn() }, type: "object" }
// no: TypeError: self is null
// no: TypeError: self.postMessage is not a function
// no: TypeError: self.postMessage is not a function


tests.forEach(function (pair) {
  try { yes.apply({}, pair); }
  catch (e) { console.error("yes:", e); }
});
// Object { self: { postMessage: log() }, type: "object" }
// Object { self: { postMessage: log() }, type: "object" }
// Object { self: { postMessage: log() }, type: "object" }
// Object { self: Number, type: "object" }
// Object { self: Number, type: "object" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment