GithubHelp home page GithubHelp logo

framesg's Introduction

framesg

Talk to iframes sanely. Framesg manages request/response cycles on top of window.postMessage with ES2015 Promises.

Installation

Install via npm, for packaging with a bundler such as Webpack or Browserify:

npm install --save framesg

If your target environment does not supply Promise natively, provide it with any A+-compliant implementation such as Bluebird.

Usage

Register handlers with a target window, e.g., the parent of the current iframe:

import Framesg from 'framesg';
const parentFrame = new Framesg(window.parent, 'my-app', {

  sayHello: username => alert(`Hello ${username}!`),

  getUserInfo: userID => userInfo[userID], // response to caller

});

The first argument is the window/iframe to communicate with (typically window.parent within an iframe, and iframeEl.contentWindow within a parent where iframeEl is the iframe's DOM element). The second argument ('my-app' in the example above) is a user-supplied namespace. The third argument is an object mapping endpoint names to handler functions.

Send a message to another frame:

parentFrame.send('getWidgetInfo', widgetID)
  .then(widgetInfo => console.log(widgetInfo))
  .catch(err => console.error(`Error getting widget info: ${err}`));

send returns a promise, which is resolved with the response value from the other frame.

If a handler returns a promise rather than an immediate value, the response message is only sent to the other frame when the promise is resolved or rejected, which is useful for asynchronous actions:

const childFrame = new Framesg(iframeEl.contentWindow, 'my-app', {

  fetchWombatInfo: wombatID => new Promise((resolve, reject) =>
    makeLegacyWombatApiCall(
      function success(wombatInfo) { resolve(wombatInfo); },
      function error(errorMsg) { reject(errorMsg); }
    )
  ),

});

More handlers can be added after initialization:

parentFrame.addHandler('marco', () => 'polo');

License

MIT

framesg's People

Contributors

tdumitrescu avatar

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.