Skip to content

Commit

Permalink
Allowed for better replacement of the Promise API, upped version
Browse files Browse the repository at this point in the history
  • Loading branch information
jakiestfu committed Aug 29, 2016
1 parent 6570432 commit 0087916
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": [ "es2015-rollup" ]
"presets": [ "es2015-rollup" ],
"plugins": ["transform-class-properties"]
}
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ You can download the compiled javascript directly [here](/build/postmate.min.js)
* Child exposes a `model`, an object literal whose values consist of serializable values, functions and promises, that the parent can retrieve.
* Child can emit events that the parent can listen to.
* *Zero* dependencies. Provide your own polyfill or abstraction for the `Promise` API if needed
* Lightweight, weighing in at ~ <span class="size">`4.6kb`</span>
* Lightweight, weighing in at ~ <span class="size">`4.7kb`</span>

## Installing
Postmate can be installed via NPM or Bower.
Expand All @@ -49,10 +49,10 @@ $ bower i postmate # Install via Bower
* **`Parent`**: The **top level** page that will embed an `iFrame`, creating a `Child`
* **`Child`**: The **bottom level** page loaded within the `iFrame`
* **`Model`**: The object that the `Child` exposes to the `Parent`
* **Handshake**: The process in which the parent frame identifies itself to the child, and vice versa. When a handshake is complete, the two contexts have bound their event listeners and identified one another.

## Usage
1. The `Parent` begins communication with the `Child`. A handshake is sent, the `Child` responds with
a handshake reply, finishing `Parent`/`Child` initialization. The two are bound and ready to communicate securely.
1. The `Parent` begins communication with the `Child`. A handshake is sent, the `Child` responds with a handshake reply, finishing `Parent`/`Child` initialization. The two are bound and ready to communicate securely.

2. The `Parent` fetches values from the `Child` by property name. The `Child` can emit messages to the parent.

Expand Down Expand Up @@ -104,7 +104,19 @@ new Postmate(options);

Name | Type | Description | Default
:--- | :--- | :--- | :---
`debug` | `Boolean` | _Set to `true` to enable logging of additional information_ | `undefined`
`debug` | `Boolean` | _Set to `true` to enable logging of additional information_ | `false`

***
> ## `Postmate.Promise`
```javascript
// parent.com or child.com
Postmate.Promise = RSVP.Promise;
new Postmate(options);
```

Name | Type | Description | Default
:--- | :--- | :--- | :---
`Promise` | `Object` | _Replace the Promise API that Postmate uses_ | `window.Promise`

***

Expand Down
4 changes: 2 additions & 2 deletions build/postmate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "postmate",
"version": "1.0.1",
"version": "1.0.2",
"description": "A powerful, simple, promise-based postMessage library",
"main": "src/postmate.js",
"devDependencies": {
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-preset-es2015-rollup": "^1.1.1",
"chai": "^3.5.0",
"connect": "^3.4.1",
Expand Down
11 changes: 7 additions & 4 deletions src/postmate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function sanitize(message, allowedOrigin) {
function resolveValue(model, property) {
const unwrappedContext = typeof model[property] === 'function'
? model[property]() : model[property];
return Promise.resolve(unwrappedContext);
return Postmate.Promise.resolve(unwrappedContext);
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ class ParentAPI {


get(property) {
return new Promise(resolve => {
return new Postmate.Promise(resolve => {
// Extract data from response and kill listeners
const uid = new Date().getTime();
const transact = e => {
Expand Down Expand Up @@ -171,6 +171,9 @@ class ChildAPI {
*/
class Postmate {

static debug = false;
static Promise = window.Promise;

/**
* Sets options related to the Parent
* @param {Object} userOptions The element to inject the frame into, and the url
Expand All @@ -195,7 +198,7 @@ class Postmate {
*/
sendHandshake(url) {
const childOrigin = resolveOrigin(url);
return new Promise((resolve, reject) => {
return new Postmate.Promise((resolve, reject) => {
const reply = e => {
if (!sanitize(e, childOrigin)) return false;
if (e.data.postmate === 'handshake-reply') {
Expand Down Expand Up @@ -251,7 +254,7 @@ Postmate.Model = class Model {
* @return {Promise} Resolves an object that exposes an API for the Child
*/
sendHandshakeReply() {
return new Promise((resolve, reject) => {
return new Postmate.Promise((resolve, reject) => {
const shake = e => {
if (e.data.postmate === 'handshake') {
log('Child: Received handshake from Parent');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/child.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
window.onerror = function () { console.log.apply(console, arguments); };
</script>
<script src="../node_modules/rsvp/dist/rsvp.min.js"></script>
<script>window.Promise = RSVP.Promise;</script>
<script type="text/javascript" src="../build/postmate.min.js"></script>
<script>Postmate.Promise = RSVP.Promise;</script>
<script type="text/javascript">
var parentAPI = null;
var height = function () { return document.height || document.body.offsetHeight; };
Expand Down
4 changes: 2 additions & 2 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/rsvp/dist/rsvp.min.js"></script>
<script>window.Promise = RSVP.Promise;</script>
<script>mocha.setup('bdd')</script>
<script src="../node_modules/rsvp/dist/rsvp.min.js"></script>
<script src="../build/postmate.min.js"></script>
<script>Postmate.Promise = RSVP.Promise;</script>
<script src="./test.js"></script>
<div id="frame"></div>
<script>
Expand Down

0 comments on commit 0087916

Please sign in to comment.