Skip to content

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

License

Notifications You must be signed in to change notification settings

markwylde/winmess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
});

About

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

Topics

Resources

License

Stars

Watchers

Forks