Skip to content

Commit

Permalink
Add round trip message bus example
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbrisebois committed Dec 7, 2019
1 parent 88e40bd commit 80ebbf1
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 69 deletions.
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@

NAME = holo_hosting_comb

build/index.js: src/index.ts
npx tsc --esModuleInterop --lib es2015,dom --outDir ./build ./src/index.ts

dist/$(NAME).js: src/index.js
npx webpack --mode production --output-filename $(NAME).js ./src/index.js

docs/index.html: build/index.js
npx jsdoc --verbose -c ./docs/.jsdoc.json --destination ./docs build/index.js


.PHONY: src build dist docs watch-docs

build: dist/$(NAME).js
dist: dist/$(NAME).js
docs: docs/index.html

test:
npx mocha --recursive ./tests
test-debug:
LOG_LEVEL=silly npx mocha --recursive ./tests

test-unit:
LOG_LEVEL=silly npx mocha ./tests/unit/
test-integration:
LOG_LEVEL=silly npx mocha ./tests/integration/

watch-docs:
npx chokidar -d 3000 'src/**/*.ts' -c "make --no-print-directory docs" 2> /dev/null

clean-docs:
git clean -df ./docs
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ COMB is a library that facilitates the calls between the parent window (hApp UI)

Used by `Holo Chaperone` and `Holo Hosting Web SDK`.

## Architecture

We use `Postmate` to set up a message tunnel between the parent and child frames.

### API

```javascript
const child = await comb.connect( url );

await child.set("mode", mode );
await child.run("signIn");
```

```javascript
const parent = comb.listen({
"signIn": async function ( ...args ) {
if ( this.mode === DEVELOP )
...
else
...
return response;
},
});
```

## Features

- Parent/Child communication line
Expand Down
18 changes: 18 additions & 0 deletions html/chaperon/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@

<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/postmate.min.js"
integrity="sha256-QiOvVDY/8Oq7LftHYbaeDge2G+Jc2czsr19CtviluZw="
crossorigin="anonymous"></script>

<h1>Chaperon!</h1>

<script type="text/javascript">
(async function () {
Postmate.debug = true;
const parent = await new Postmate.Model({
"init": async function ( data ) {
const [msg_id, mode] = data;
console.log( mode );

parent.emit("response", [ msg_id, true ]);
}
});
})();
</script>
47 changes: 41 additions & 6 deletions html/happ/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@

<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/postmate.min.js"
integrity="sha256-QiOvVDY/8Oq7LftHYbaeDge2G+Jc2czsr19CtviluZw="
crossorigin="anonymous"></script>

<h1>hApp!</h1>

<div id="main-content">Hello World</div>

<script type="text/javascript">
window.addEventListener( "message", function ( event ) {
console.log( event );
console.log( event.origin );
console.log( event.source );
console.log( event.data );
});
(async function () {

Postmate.debug = true;
const chaperon = await new Postmate({
"container": document.body,
"url": "http://localhost:4532/index.html",
"classListArray": ["chaperon-frame"],
});

const responses = {};

chaperon.on('response', function ( data ) {
let [k,v] = data;
responses[ k ]( v );
});

let count = 0;
function request ( method, data ) {
let msg_id = count++;

chaperon.call( method, [ msg_id, data ] );

return new Promise((f,r) => {
responses[msg_id] = f;

// TODO: make a timout that calls r
});
}

try {
const initialized = await request("init", "development");

console.log( initialized );
} catch ( err ) {
console.error( err );
}
})();
</script>

Loading

0 comments on commit 80ebbf1

Please sign in to comment.