GitHunt
MA

markwylde/winmess

This library provides a request/response style API for the browsers `postMessage` `onmessage` functions.

windowmess

This library provides a request/response style API for the browsers postMessage onmessage functions.

Installation

npm install --save winmess

Usage

The winmess function takes two arguments. The first being listener and the second sender.

These should be windows from the browser, but can really be anything that has a postMessage and addEventListener on it.

See the demo folder for a fully working example.

# index.js

import winmess from 'winmess';

const { send } = await winmess(self, document.getElementById('iframe').contentWindow);

const response = await send('convertToUpperCase', 'MiXeDcAsEmEsSaGe');

console.log('response', response);

# one.js

import winmess from 'winmess';

const { watch } = await wrap(self.parent, self);

watch(async (command, value) => {
  if (command === 'convertToUpperCase') {
    return value.toUpperCase();
  }
});

// You can have multiple watchers
watch(async (command, value) => {
  // skip to the next watcher
  return undefined

  // stop here and respond with null
  return null
});

Languages

JavaScript100.0%

Contributors

MIT License
Created October 29, 2022
Updated October 29, 2022
markwylde/winmess | GitHunt