Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native: ReferenceError: Property 'MessageChannel' doesn't exist #374

Open
damiandizeo opened this issue Feb 27, 2024 · 5 comments
Open

Comments

@damiandizeo
Copy link

damiandizeo commented Feb 27, 2024

In React Native, i get a crash here: https://github.com/nbd-wtf/nostr-tools/blob/master/helpers.ts#L5 when do relay.subscribe, i get event btw.

Looks like class MessageChannel is not part of React Native.

@alexgleason
Copy link
Collaborator

Lol I saw that. I think that function should be replaced with return new Promise(resolve => setTimeout(resolve)) for basically the same effect.

But you are still going to have the problems in #80. Try downgrading to 1.x and see if you can get it to work at all.

@damiandizeo
Copy link
Author

Well recreated the class that mimics the behavior of MessageChannel in React Native, leave it here if helps someone.

class MessageChannel {
  constructor() {
    this.port1 = new MessagePort();
    this.port2 = new MessagePort();
    this.port1.setOtherPort(this.port2);
    this.port2.setOtherPort(this.port1);
  }
}

class MessagePort {
  constructor() {
    this.otherPort = null;
    this.listeners = new Map();
  }

  setOtherPort(otherPort) {
    this.otherPort = otherPort;
  }

  addEventListener(event, listener) {
    if (!this.listeners.has(event)) {
      this.listeners.set(event, []);
    }
    this.listeners.get(event).push(listener);
  }

  removeEventListener(event, listener) {
    const eventListeners = this.listeners.get(event);
    if (eventListeners) {
      const index = eventListeners.indexOf(listener);
      if (index !== -1) {
        eventListeners.splice(index, 1);
      }
    }
  }

  postMessage(data) {
    this.otherPort.dispatchEvent('message', { data });
  }

  start() {
    // No-op in React Native
  }

  dispatchEvent(event, data) {
    const eventListeners = this.listeners.get(event);
    if (eventListeners) {
      eventListeners.forEach(listener => listener(data));
    }
  }
}

global.MessageChannel = MessageChannel;

global.MessageChannel is now compatible with yieldThread function in helper.ts

The crash gone and i get all events now, will be testing... Thanks @alexgleason.

@abhay-raizada
Copy link
Contributor

Used @damiandizeo 's workaround and it works!, can't evaluate the stability yet, will update if it breaks elsewhere! But thank you for posting! ❤️

@tempo-beme
Copy link

Well recreated the class that mimics the behavior of MessageChannel in React Native, leave it here if helps someone.

class MessageChannel {
  constructor() {
    this.port1 = new MessagePort();
    this.port2 = new MessagePort();
    this.port1.setOtherPort(this.port2);
    this.port2.setOtherPort(this.port1);
  }
}

class MessagePort {
  constructor() {
    this.otherPort = null;
    this.listeners = new Map();
  }

  setOtherPort(otherPort) {
    this.otherPort = otherPort;
  }

  addEventListener(event, listener) {
    if (!this.listeners.has(event)) {
      this.listeners.set(event, []);
    }
    this.listeners.get(event).push(listener);
  }

  removeEventListener(event, listener) {
    const eventListeners = this.listeners.get(event);
    if (eventListeners) {
      const index = eventListeners.indexOf(listener);
      if (index !== -1) {
        eventListeners.splice(index, 1);
      }
    }
  }

  postMessage(data) {
    this.otherPort.dispatchEvent('message', { data });
  }

  start() {
    // No-op in React Native
  }

  dispatchEvent(event, data) {
    const eventListeners = this.listeners.get(event);
    if (eventListeners) {
      eventListeners.forEach(listener => listener(data));
    }
  }
}

global.MessageChannel = MessageChannel;

global.MessageChannel is now compatible with yieldThread function in helper.ts

The crash gone and i get all events now, will be testing... Thanks @alexgleason.

This is awesome, thanks for posting this solution

@mrkvon
Copy link

mrkvon commented May 9, 2024

The solution of @damiandizeo worked for me, but isn't TypeScript typed, so I had to put a bunch of @ts-ignore in.

Alternatively, I was also able to solve the issue by installing MessageChannel polyfill message-port-polyfill:

npm i message-port-polyfill

and importing it before nostr-tools:

import 'message-port-polyfill'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants