Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
Add visibility handling (only when property is set)
Saw this in the I/O2016 app and think it is useful.
  • Loading branch information
wburgers committed Jun 7, 2016
1 parent 36d984e commit 2bf53db
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ As the example above shows, it is possible to data-bind to some of the websocket
- The array of protocols to use.
- The status of the connection can only be read, not set. Use it with [status-dot] (https://github.com/wburgers/OZWSS-Polymer-status-dot) for UI visibility of the connection status.
- The auto propery tells the websocket to automatically connect on page load or url change. If auto is used, remember to data-bind the protocols before the url.

- The handle-visibility property tells the websocket to close the connection when the browser tab/page is out of focus and reopen when the tab is focussed.

The following events are thrown:
- "websocket-open": tells you the connection is opened.
- "websocket-error": tells you there was an error. `event.detail.data` contains more info about the error.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocket-component",
"version": "1.0.0",
"version": "1.1.0",
"homepage": "https://github.com/wburgers/websocket-component",
"description": "A websocket element wrapper",
"keywords": ["web-components"],
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
Check your console!
<websocket-component id="websocket" url="wss://echo.websocket.org" auto></websocket-component>
<websocket-component id="websocket" url="wss://echo.websocket.org" auto handle-visibility></websocket-component>
<script>
var ws_element = document.getElementById('websocket');

Expand Down
36 changes: 18 additions & 18 deletions test/basic-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
<body>

<test-fixture id="websocketComponentWithoutURL">
<template>
<websocket-component></websocket-component>
</template>
</test-fixture>

<test-fixture id="websocketComponentWithURL">
<template>
<websocket-component url="ws://echo.websocket.org"></websocket-component>
</template>
</test-fixture>

<test-fixture id="websocketComponentWithActiveConnection">
<template>
<websocket-component auto url="ws://echo.websocket.org"></websocket-component>
</template>
</test-fixture>
<template>
<websocket-component></websocket-component>
</template>
</test-fixture>

<test-fixture id="websocketComponentWithURL">
<template>
<websocket-component url="ws://echo.websocket.org"></websocket-component>
</template>
</test-fixture>

<test-fixture id="websocketComponentWithActiveConnection">
<template>
<websocket-component auto url="ws://echo.websocket.org"></websocket-component>
</template>
</test-fixture>

<script>
var websocketElementWithoutURL;
var websocketElementWithURL;
var websocketElementWithActiveConnection;

var ws;

setup(function() {
Expand All @@ -50,7 +50,7 @@
websocketElementWithURL = fixture("websocketComponentWithURL");
websocketElementWithActiveConnection = fixture("websocketComponentWithActiveConnection");
});

teardown(function() {
window.WebSocket.restore();
});
Expand Down
21 changes: 20 additions & 1 deletion websocket-component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Example:
<websocket-component auto url="wss://echo.websocket.org"></websocket-component>
<websocket-component auto handle-visibility url="wss://echo.websocket.org"></websocket-component>
@element websocket-component
@demo demo/index.html
Expand Down Expand Up @@ -55,6 +55,13 @@
value: false,
observer: '_autoChange'
},
/**
* If this boolean is set, the websocket will automatically close and reopen when the tab/page is out of focus and focussed again respectively
*/
handleVisibility: {
type: Boolean,
value: false,
},
_ws: {
type: Object,
observer: '_wsChange'
Expand All @@ -63,6 +70,11 @@
observers: [
'_computeStatus(_ws.readyState)'
],
ready: function () {
if (this.handleVisibility && typeof document.hidden !== 'undefined') {
document.addEventListener('visibilitychange', this._handleVisibilityChange.bind(this), false);
}
},
/**
* Open the connection manually. May throw errors when already connected or no url is specified.
*/
Expand Down Expand Up @@ -137,6 +149,13 @@
if(!this._ws) {
this._computeStatus(undefined);
}
},
_handleVisibilityChange() {
if (document.hidden) {
this.close();
} else {
this.open();
}
}
});
</script>
Expand Down

0 comments on commit 2bf53db

Please sign in to comment.