Code Monkey home page Code Monkey logo

Comments (4)

greggman avatar greggman commented on July 17, 2024

We can remove the throw unknown message but, expecting some random page to not throw on an unknown message seems like it would make many pages not work in the debugger. Is there some other way to pass the message? Even worse, the inspector's use of messages might conflict with the page's use of messages in general.

Like any random page might have something like

postMesasge({a: {b: 123}});
onmessage = (e) => {
  console.log(e.data.a.b);
}

The page expects it always gets an object with a. If it doesn't it will throw

You could maybe wrap onmessage and window.addEventListener and filter out your messages before they get to the page?

from webgpu-samples.

greggman avatar greggman commented on July 17, 2024

I think this will wrap onmessage and addEventListener

const GlobalCtor = globalThis.constructor;

const GlobalPropertyDescriptors = Object.getOwnPropertyDescriptors(globalThis);

const isOurMessage = e => e.data.sig === 'webgpu_inspector';
const listenerToWrapperFn = new WeakMap();

function makeWrapper(listener) {
  const wrapper = function(event) {
    if (isOurMessage(event)) {
      // do something with our message
      console.log('got message from us', event.data);
    } else {
      return listener.call(this, event);
    }
  };
  listenerToWrapperFn.set(listener, wrapper);
  return wrapper;
}

Object.defineProperty(globalThis, 'onmessage', {
  get() {
    const listener = GlobalPropertyDescriptors.onmessage.get.call(this);
    return listenerToWrapperFn.get(listener) ?? listener;
  },
  set(listener) {
    GlobalPropertyDescriptors.onmessage.set.call(this, makeWrapper(listener));
  }
});

GlobalCtor.prototype.addEventListener = (function(origFn) {
  return function(name, listener, ...args) {
    listener = name === 'message' ? makeWrapper(listener) : listener;
    return origFn.call(this, name, listener, ...args);
  }

})(GlobalCtor.prototype.addEventListener);

GlobalCtor.prototype.removeEventListener = (function(origFn) {
  return function(name, listener, ...args) {
    listener = listenerToWrapperFn.get(listener) ?? listener;
    return origFn.call(this, name, listener, ...args);
  };
})(GlobalCtor.prototype.removeEventListener);

example

from webgpu-samples.

brendan-duncan avatar brendan-duncan commented on July 17, 2024

Yeah, maybe I need to go back to the drawing board on that one.

from webgpu-samples.

brendan-duncan avatar brendan-duncan commented on July 17, 2024

I'll close this issue. I think a CustomEvent is probably the better choice for this rather than message. Thanks for setting me straight.

from webgpu-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.