adding zombkill cmd
This commit is contained in:
parent
4ec53b4d1f
commit
0fe340387a
23
demo.html
23
demo.html
|
@ -1,7 +1,7 @@
|
|||
<!doctype>
|
||||
<html>
|
||||
<head>
|
||||
<title>pluie.org bt demo - v0.4</title>
|
||||
<title>pluie.org bt demo - v0.6</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
body { background-color:#888; color:white; font-family:arial; font-size:1.3rem; }
|
||||
|
@ -17,18 +17,20 @@
|
|||
</head>
|
||||
<body>
|
||||
<h2>pluie.org bt demo : </h2>
|
||||
<span>author : a-sansara - version : 0.4</span>
|
||||
<span>author : a-sansara - version : 0.6</span>
|
||||
<div>
|
||||
<p> Manage commnunication between browser tabs.</p>
|
||||
<p> this js lib can perform several actions on browser tabs like :<br/>
|
||||
<ul><li>append/rewrite/synchro node on all (other) tabs or a specific tab (and possibly on specific frame context) eventually with callback.</li>
|
||||
<li>reload all tabs or a specific tab with specified url or tab 's current url</li>
|
||||
<li>perform your custom actions on all tabs or specific tab</li>
|
||||
<li>(*new) kill zombies tabs onload</li>
|
||||
</ul>
|
||||
</p>
|
||||
<h3>Static actions</h3>
|
||||
<button class="cmd-sta-append" title="$bt.append('#test', '<b>it\'s cool to append</b><br/>');">static dom append</button>
|
||||
<button class="cmd-sta-html" title="$bt.html('#test', '<b>it\'s cool to rewrite</b><br/>')">static dom html</button>
|
||||
<button class="cmd-zombkill" title="">cmd zombkill</button>
|
||||
|
||||
<h3>Dynamic actions</h3>
|
||||
enter txt or html : <br/>
|
||||
|
@ -46,7 +48,7 @@ enter txt or html : <br/>
|
|||
<div id="test"></div>
|
||||
</div>
|
||||
<script type="text/javascript" src="src/svan-min.js"></script>
|
||||
<script type="text/javascript" src="src/bt-min.js"></script>
|
||||
<script type="text/javascript" src="src/bt.js"></script>
|
||||
<script type="text/javascript">
|
||||
// define a new custom cmd
|
||||
$bt.CMD_CUSTOM = "customCmd";
|
||||
|
@ -99,6 +101,21 @@ $(document).ready(function() {
|
|||
$('.cmd-sta-html').on('click', function(){
|
||||
eval(this.title);
|
||||
});
|
||||
$('.cmd-zombkill').on('click', function(){
|
||||
$bt.list = $j.obj($l.get($bt.LS_TABS));
|
||||
for (var i=4; i > 0; i--) {
|
||||
var zombi = (new Date).getTime()+i;
|
||||
$bt.list.push(zombi);
|
||||
}
|
||||
$l.set($bt.LS_TABS, $j.str($bt.list));
|
||||
$bt._broadcast();
|
||||
$bt.log($bt.list);
|
||||
var l = $bt.list.length;
|
||||
alert('zombies tabs created, so, check for zombies');
|
||||
$bt.zombkill(function(){
|
||||
alert('killing '+(l-$bt.list.length)+' zombies');
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
2
src/bt-min.js
vendored
2
src/bt-min.js
vendored
|
@ -1 +1 @@
|
|||
/* by a-sansara - v 0.5 - https://github.com/pluie-org/bt */var $l=function(){var t=localStorage;return{clear:function(){return t.clear()},get:function(n){return t.getItem(n)},rem:function(n){return t.removeItem(n)},set:function(n,e){return t.setItem(n,e)}}}(),$j=function(){var t=JSON;return{str:function(n){return t.stringify(n)},obj:function(n){return t.parse(n)}}}(),$bt={VERSION:.5,TRACE:!0&&!$.isNone(console),LS_TABS:"bt.tabs",LS_CURTAB:"bt.ctab",LS_CMD:"bt.cmd",CMD_SYNC:"bt.sync",CMD_VAR_SET:"var.set",CMD_VAR_GET:"var.get",CMD_APPEND:"dom.append",CMD_HTML:"dom.html",CMD_RELOAD:"bt.reload",vars:[],init:function(t){this._init(t)},on:function(t){this.log(t)},log:function(t){this.TRACE&&console.log(t)},send:function(t){t.uid=this.id+Math.random(),t.from=this.id,$.isNone(t.to)&&(t.to="*"),t=$j.str(t),$bt.log("sending cmd : "+this.LS_CMD+" : "+t),$l.set(this.LS_CMD,t),$l.rem(this.LS_CMD)},append:function(t,n,e,o,c){this._dom(this.CMD_APPEND,e,t,n,o,c)},html:function(t,n,e,o,c){this._dom(this.CMD_HTML,e,t,n,o,c)},sync:function(t,n,e,o){var c=$.isNone(n)||null==n?document:parent.frames[n].document;this._dom(this.CMD_HTML,n,t,$(t,c).html(),e,o)},reload:function(t,n){$bt.send({name:$bt.CMD_RELOAD,url:t,to:n?n:"*"})},varset:function(t,n){$bt.vars[t]=n,$bt.send({name:$bt.CMD_VAR_SET,data:{k:t,v:n}})},varget:function(t){if($bt.list.length>1){var n=$bt.list[0]==$bt.id?$bt.list[1]:$bt.list[0];$bt.send({name:$bt.CMD_VAR_GET,data:{k:t},to:n})}return $bt.vars[t]},_refresh:function(){$bt.list=$j.obj($l.get($bt.LS_TABS))},_broadcast:function(){$bt.send({name:$bt.CMD_SYNC})},_remove:function(t){t||(t=$bt.id);var n=$bt.list.indexOf(t);n>-1&&$bt.list.splice(n,1)},_init:function(t){$(window).on("beforeunload",$bt._unload),$(window).on("storage",$bt._cmd),$(window).on("focus",$bt._focus),$bt.id=(new Date).getTime();var n=$l.get($bt.LS_TABS);$bt.list=null==n?[]:$j.obj(n),$bt.list.push($bt.id),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),$bt.log($bt.list),$.isFunc(t)&&t()},_dom:function(t,n,e,o,c,i){$bt.send({name:t,context:n,selector:e,data:o,fn:c,to:i?i:"*"})},_unload:function(){return $bt._refresh(),$bt._remove(),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),null},_focus:function(){$l.set($bt.LS_CURTAB,$bt.id)},_cmd:function(e){if($.isNone(e.originalEvent)||(e=e.originalEvent),e.key==$bt.LS_CMD){var cmd=$j.obj(e.newValue);if(cmd)if($bt.log("RECEIVING cmd "+cmd.name+" : "),"*"==cmd.to||cmd.to==$bt.id){$bt.log("do "+cmd.name),$bt.log(cmd);try{$.isNone(cmd.context)||($bt.log(cmd.context),cmd.context=window.parent.frames[cmd.context].document,$bt.log(cmd.context))}catch(e){$bt.log("bad context "+cmd.context+" : "+e.message)}switch(cmd.name){case $bt.CMD_SYNC:$bt._refresh(),$bt.log($bt.list);break;case $bt.CMD_APPEND:$(cmd.selector,cmd.context).append(cmd.data);break;case $bt.CMD_HTML:$(cmd.selector,cmd.context).html(cmd.data);break;case $bt.CMD_RELOAD:window.location=$.isNone(cmd.url)?window.location:cmd.url;break;case $bt.CMD_VAR_GET:$bt.varset(cmd.data.k,$bt.vars[cmd.data.k],cmd.from);break;case $bt.CMD_VAR_SET:$bt.vars[cmd.data.k]=cmd.data.v;break;default:$.isFunc($bt.on)&&$bt.on(cmd)}if($.isStr(cmd.fn)&&cmd.fn.length>0){$bt.log(cmd.fn);var f=eval("window."+cmd.fn);$.isFunc(f)&&f()}}else $bt.log("ignoring (not target)")}}};
|
||||
/* by a-sansara - v 0.6 - https://github.com/pluie-org/bt */var $l=function(){var a=localStorage;return{clear:function(){return a.clear()},get:function(n){return a.getItem(n)},rem:function(n){return a.removeItem(n)},set:function(n,o){return a.setItem(n,o)}}}(),$j=function(){var a=JSON;return{str:function(n){return a.stringify(n)},obj:function(s){return a.parse(s)}}}(),$bt={VERSION:.6,TRACE:!0&&!$.isNone(console),LS_TABS:"bt.list",LS_CURTAB:"bt.current",LS_CMD:"bt.event",CMD_SYNC:"bt.sync",CMD_VAR_SET:"bt.set",CMD_VAR_GET:"bt.get",CMD_APPEND:"bt.dom.append",CMD_HTML:"bt.dom.rewrite",CMD_RELOAD:"bt.reload",CMD_ZOMBKILL:"bt.zombkill",CMD_DONTKILL:"bt.dontkill",vars:[],zomblist:[],zkillonload:!0,zombTimeout:250,init:function(n){this._init(n)},on:function(n){this.log(n)},log:function(n){this.TRACE&&console.log(n)},send:function(n){n.uid=this.id+Math.random(),n.from=this.id,$.isNone(n.to)&&(n.to="*"),n=$j.str(n),$bt.log("sending cmd : "+this.LS_CMD+" : "+n),$l.set(this.LS_CMD,n),$l.rem(this.LS_CMD)},append:function(n,o,i,b,c){this._dom(this.CMD_APPEND,i,n,o,b,c)},html:function(n,o,i,b,c){this._dom(this.CMD_HTML,i,n,o,b,c)},sync:function(n,o,i,b){var c=$.isNone(o)||null==o?document:parent.frames[o].document;this._dom(this.CMD_HTML,o,n,$(n,c).html(),i,b)},reload:function(n,o){$bt.send({name:$bt.CMD_RELOAD,url:n,to:o?o:"*"})},zombkill:function(n,o){var i=(new Date).getTime();$bt.zomblist[""+i]=[],$bt.list.forEach(function(n){n!=$bt.id&&($bt.zomblist[""+i]["ping"+n]="")}),$bt.send({name:$bt.CMD_ZOMBKILL,askid:i,to:"*"});var b=setTimeout(function(){$bt._refresh();for(var o in $bt.zomblist[""+i])if("pong"!=$bt.zomblist[""+i][o]){var c=$bt.list.indexOf(parseInt(o.substring(4)));c>-1&&$bt.list.splice(c,1)}$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),$bt.log($bt.list),clearTimeout(b),$.isFunc(n)&&n()},o?o:$bt.zombTimeout)},_dontkill:function(n,o){$bt.send({name:$bt.CMD_DONTKILL,askid:n,to:o})},_refresh:function(){$bt.list=$j.obj($l.get($bt.LS_TABS))},_broadcast:function(){$bt.send({name:$bt.CMD_SYNC})},_remove:function(n){n||(n=$bt.id);var o=$bt.list.indexOf(n);o>-1&&$bt.list.splice(o,1)},_init:function(n){$(window).on("beforeunload",$bt._unload),$(window).on("storage",$bt._cmd),$(window).on("focus",$bt._focus),$bt.id=(new Date).getTime();var t=$l.get($bt.LS_TABS);$bt.list=null==t?[]:$j.obj(t),$bt.list.push($bt.id),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),$bt.log($bt.list),$bt.zkillonload?$bt.zombkill(n):$.isFunc(n)&&n()},_dom:function(n,o,s,i,b,c){$bt.send({name:n,context:o,selector:s,data:i,fn:b,to:c?c:"*"})},_unload:function(){return $bt._refresh(),$bt._remove(),$l.set($bt.LS_TABS,$j.str($bt.list)),$bt._broadcast(),null},_focus:function(){$l.set($bt.LS_CURTAB,$bt.id)},_cmd:function(e){if($.isNone(e.originalEvent)||(e=e.originalEvent),e.key==$bt.LS_CMD){var cmd=$j.obj(e.newValue);if(cmd)if($bt.log("RECEIVING cmd "+cmd.name+" : "),"*"==cmd.to||cmd.to==$bt.id){$bt.log("do "+cmd.name),$bt.log(cmd);try{$.isNone(cmd.context)||($bt.log(cmd.context),cmd.context=window.parent.frames[cmd.context].document,$bt.log(cmd.context))}catch(e){$bt.log("bad context "+cmd.context+" : "+e.message)}switch(cmd.name){case $bt.CMD_SYNC:$bt._refresh(),$bt.log($bt.list);break;case $bt.CMD_APPEND:$(cmd.selector,cmd.context).append(cmd.data);break;case $bt.CMD_HTML:$(cmd.selector,cmd.context).html(cmd.data);break;case $bt.CMD_RELOAD:window.location=$.isNone(cmd.url)?window.location:cmd.url;break;case $bt.CMD_ZOMBKILL:$bt._dontkill(cmd.askid,cmd.from);break;case $bt.CMD_DONTKILL:$bt.zomblist[""+cmd.askid]["ping"+cmd.from]="pong";break;default:$.isFunc($bt.on)&&$bt.on(cmd)}if($.isStr(cmd.fn)&&cmd.fn.length>0){$bt.log(cmd.fn);var f=eval("window."+cmd.fn);$.isFunc(f)&&f()}}else $bt.log("ignoring (not target)")}}};
|
||||
|
|
74
src/bt.js
74
src/bt.js
|
@ -3,7 +3,7 @@
|
|||
* @contributors :
|
||||
* @copyright : pluie.org
|
||||
* @date : 2015-12-10 22:22:34
|
||||
* @version : 0.5
|
||||
* @version : 0.6
|
||||
* @license : MIT
|
||||
* @require : html5 localStorage svan (small vanilla jquery-like lib)
|
||||
* @desc : manage communication between browser tabs
|
||||
|
@ -49,6 +49,9 @@
|
|||
* // reload specific browser tab to specific url
|
||||
* $bt.reload(window.location.path+"?reloaded=1", '1449974562012');
|
||||
*
|
||||
* // check and kill zombi tabs
|
||||
* $bt.zombkill();
|
||||
*
|
||||
* // get browser tab list
|
||||
* $bt.list;
|
||||
*
|
||||
|
@ -102,7 +105,7 @@ var $j = (function alias() {
|
|||
}());
|
||||
|
||||
var $bt = {
|
||||
VERSION : 0.5,
|
||||
VERSION : 0.6,
|
||||
TRACE : true && !$.isNone(console),
|
||||
/*! @constant LS_TABS localStorage key for browsertabs list */
|
||||
LS_TABS : 'bt.list',
|
||||
|
@ -122,8 +125,18 @@ var $bt = {
|
|||
CMD_HTML : 'bt.dom.rewrite',
|
||||
/*! @constant CMD_RELOAD internal command to perform a browser tab reload */
|
||||
CMD_RELOAD : 'bt.reload',
|
||||
/*! @constant CMD_ZOMBKILL internal command to perform a browser tab zombies kill */
|
||||
CMD_ZOMBKILL : 'bt.zombkill',
|
||||
/*! @constant CMD_DONTKILL internal command to perform a dontkill browser tab (CMD_ZOMBKILL reply) */
|
||||
CMD_DONTKILL : 'bt.dontkill',
|
||||
/*! @var vars */
|
||||
vars : [],
|
||||
/*! @var zomblist */
|
||||
zomblist : [],
|
||||
/*! @var zkillonload */
|
||||
zkillonload : true,
|
||||
/*! @var zombTimeout in ms */
|
||||
zombTimeout : 250,
|
||||
/*!
|
||||
* @desc initialize on dom ready
|
||||
* @public
|
||||
|
@ -213,18 +226,38 @@ var $bt = {
|
|||
reload : function(url, btid) {
|
||||
$bt.send({ name : $bt.CMD_RELOAD, url : url, to : !btid ? '*' : btid });
|
||||
},
|
||||
/*! */
|
||||
varset : function(k, v) {
|
||||
$bt.vars[k] = v;
|
||||
$bt.send({ name : $bt.CMD_VAR_SET, data : { k : k, v : v } });
|
||||
},
|
||||
/*! */
|
||||
varget : function(k) {
|
||||
if ($bt.list.length >1) {
|
||||
var to = $bt.list[0] == $bt.id ? $bt.list[1] : $bt.list[0];
|
||||
$bt.send({ name : $bt.CMD_VAR_GET, data : { k : k }, to : to });
|
||||
/*!
|
||||
* @desc kill all zombi tabs
|
||||
* @public
|
||||
* @method zombkill
|
||||
* @param int timeout timeout ins ms for killing zombies (no ping reply)
|
||||
*/
|
||||
zombkill : function(callback, timeout) {
|
||||
var askid = (new Date).getTime();
|
||||
$bt.zomblist[''+askid] = [];
|
||||
$bt.list.forEach(function(id) {
|
||||
if (id != $bt.id) {
|
||||
$bt.zomblist[''+askid]['ping'+id] = '';
|
||||
}
|
||||
return $bt.vars[k];
|
||||
});
|
||||
$bt.send({ name : $bt.CMD_ZOMBKILL, askid : askid, to : '*' });
|
||||
var tid = setTimeout(function() {
|
||||
$bt._refresh();
|
||||
for(var k in $bt.zomblist[''+askid]) {
|
||||
if ($bt.zomblist[''+askid][k] != 'pong') {
|
||||
var i = $bt.list.indexOf(parseInt(k.substring(4)));
|
||||
if (i > -1) $bt.list.splice(i, 1);
|
||||
}
|
||||
}
|
||||
$l.set($bt.LS_TABS, $j.str($bt.list));
|
||||
$bt._broadcast();
|
||||
$bt.log($bt.list);
|
||||
clearTimeout(tid);
|
||||
if ($.isFunc(callback)) callback();
|
||||
}, !timeout ? $bt.zombTimeout : timeout);
|
||||
},
|
||||
_dontkill : function(askid, id) {
|
||||
$bt.send({ name : $bt.CMD_DONTKILL, askid : askid, to : id });
|
||||
},
|
||||
/*! @private */
|
||||
_refresh : function() {
|
||||
|
@ -252,7 +285,10 @@ var $bt = {
|
|||
$l.set($bt.LS_TABS, $j.str($bt.list));
|
||||
$bt._broadcast();
|
||||
$bt.log($bt.list);
|
||||
if ($.isFunc(fn)) fn();
|
||||
if ($bt.zkillonload) $bt.zombkill(fn);
|
||||
else if ($.isFunc(fn)) {
|
||||
fn();
|
||||
}
|
||||
},
|
||||
/*! @private */
|
||||
_dom : function(n, c, s, d, fn, id) {
|
||||
|
@ -309,12 +345,14 @@ var $bt = {
|
|||
window.location = !$.isNone(cmd.url) ? cmd.url : window.location;
|
||||
break;
|
||||
|
||||
case $bt.CMD_VAR_GET :
|
||||
$bt.varset(cmd.data.k, $bt.vars[cmd.data.k], cmd.from);
|
||||
// emit response for zombkill
|
||||
case $bt.CMD_ZOMBKILL :
|
||||
$bt._dontkill(cmd.askid, cmd.from);
|
||||
break;
|
||||
|
||||
case $bt.CMD_VAR_SET :
|
||||
$bt.vars[cmd.data.k] = cmd.data.v;
|
||||
// receiv response for zombkill
|
||||
case $bt.CMD_DONTKILL :
|
||||
$bt.zomblist[''+cmd.askid]['ping'+cmd.from] = 'pong';
|
||||
break;
|
||||
|
||||
default :
|
||||
|
|
Loading…
Reference in New Issue
Block a user