Skip to content

Commit d5d7139

Browse files
committed
add update track event
1 parent a74aba7 commit d5d7139

File tree

3 files changed

+99
-22
lines changed

3 files changed

+99
-22
lines changed

README.md

+58-8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,55 @@ sysend.track('ready', () => {
105105

106106
with `list()` method and `open`/`close` events you can implement dynamic list of windows/tab. That will change when new window/tab is open or close.
107107

108+
```javascript
109+
let list = [];
110+
111+
sysend.track('open', data => {
112+
if (data.id !== sysend.id) {
113+
list.push(data);
114+
populate_list(list);
115+
}
116+
});
117+
118+
sysend.track('close', data => {
119+
list = list.filter(tab => data.id !== tab.id);
120+
populate_list(list);
121+
});
122+
123+
sysend.track('ready', () => {
124+
sysend.list().then(tabs => {
125+
list = tabs;
126+
populate_list(list);
127+
});
128+
});
129+
130+
function populate_list() {
131+
select.innerHTML = '';
132+
list.forEach(tab => {
133+
const option = document.createElement('option');
134+
option.value = tab.id;
135+
option.innerText = tab.id;
136+
select.appendChild(option);
137+
});
138+
}
139+
```
140+
141+
In version 1.16.0 this code was abstracted into:
142+
143+
```javascript
144+
sysend.track('update', (list) => {
145+
populate_list(list);
146+
});
147+
```
148+
149+
This can be simplified with point free style:
150+
151+
```javascript
152+
sysend.track('update', populate_list);
153+
```
154+
155+
### RPC mechanism
156+
108157
In version 1.15.0 new API was added called `rpc()` (build on top of tracking mechanism) that allow to use RPC (Remote Procedure Call) between open windows/tabs.
109158

110159
```javascript
@@ -135,12 +184,6 @@ sysend.proxy('https://terminal.jcubic.pl');
135184

136185
on Firefox you need to add **CORS** for the proxy.html that will be loaded into iframe (see [Cross-Domain LocalStorage](https://jcubic.wordpress.com/2014/06/20/cross-domain-localstorage/)).
137186

138-
### Security protection
139-
140-
Since version 1.10.0 as a security mesure Cross-Domain communication has been limited to only those domains that are allowed.
141-
To allow domain to listen to sysend communication you need to specify channel inside iframe. You need add your origins to the
142-
`sysend.channel()` function (origin is combination of protocol domain and optional port).
143-
144187
### Serialization
145188

146189
if you want to send custom data you can use serializer (new in 1.4.0) this API
@@ -166,6 +209,13 @@ sysend.serializer(function(data) {
166209
});
167210
````
168211
212+
### Security protection
213+
214+
Since version 1.10.0 as a security mesure Cross-Domain communication has been limited to only those domains that are allowed.
215+
To allow domain to listen to sysend communication you need to specify channel inside iframe. You need add your origins to the
216+
`sysend.channel()` function (origin is combination of protocol domain and optional port).
217+
218+
169219
## Demos
170220
171221
* [Simple demo using iframes](https://jcubic.pl/sysend-demo/).
@@ -189,8 +239,8 @@ sysend object:
189239
| `emit(name, [, object])` | same as `broadcast()` but also invoke the even on same page | name - string - The name of the event<br>object - optional any data | 1.5.0 |
190240
| `post(<window_id>, [, object])` | send any data to other window | window_id - string of the target window (use `'primary'` to send to primary window)<br>object - any data | 1.6.0 / `'primary'` target 1.14.0 |
191241
| `list()` | returns a Promise of objects `{id:<UUID>, primary}` for other windows, you can use those to send a message with `post()` | NA | 1.6.0 |
192-
| `track(event, callback)` | track inter window communication events | event - any of the strings: `"open"`, `"close"`, `"primary"`, <br>`"secondary"`, `"message"`<br>callback - different function depend on the event:<br>* `"message"` - `{data, origin}` - where data is anything the `post()` sends, and origin is `id` of the sender.<br>* `"open"` - `{count, primary, id}` when new window/tab is opened<br>* `"close"` - `{count, primary, id, self}` when window/tab is closed<br>* `"primary"` and `"secondary"` function has no arguments and is called when window/tab become secondary or primary.<br>* `"ready"` - event when tracking is ready. | 1.6.0 except `ready` - 1.10.0 |
193-
| `untrack(event [,callback])` | remove single event listener all listeners for a given event | event - any of the strings `'open'`, `'close'`, `'primary'`, `'secondary'`, or `'message'`. | 1.6.0 |
242+
| `track(event, callback)` | track inter window communication events | event - any of the strings: `"open"`, `"close"`, `"primary"`, <br>`"secondary"`, `"message"`, `"update"`<br>callback - different function depend on the event:<br>* `"message"` - `{data, origin}` - where data is anything the `post()` sends, and origin is `id` of the sender.<br>* `"open"` - `{count, primary, id}` when new window/tab is opened<br>* `"close"` - `{count, primary, id, self}` when window/tab is closed<br>* `"primary"` and `"secondary"` function has no arguments and is called when window/tab become secondary or primary.<br>* `"ready"` - event when tracking is ready. | 1.6.0 except `ready` - 1.10.0 and `update` - 1.16.0 |
243+
| `untrack(event [,callback])` | remove single event listener all listeners for a given event | event - any of the strings `'open'`, `'close'`, `'primary'`, `'secondary'`, `'message'`, or `'update'`. | 1.6.0 |
194244
| `isPrimary()` | function returns true if window is primary (first open or last that remain) | NA | 1.6.0 |
195245
| `channel()` | function restrict cross domain communication to only allowed domains. You need to call this function on proxy iframe to limit number of domains (origins) that can listen and send events. | any number of origins (e.g. 'http://localhost:8080' or 'https://jcubic.github.io') you can also use valid URL. | 1.10.0 |
196246
| `useLocalStorage([toggle])` | Function set or toggle localStorage mode. | argument is optional and can be `true` or `false`. | 1.14.0 |

demo.html

+9-13
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@
3333
message('just got empty notification');
3434
}
3535
});
36-
function list() {
37-
sysend.list().then(list => {
38-
target.innerHTML = '';
39-
list.forEach(function({ id: uuid }) {
40-
var option = document.createElement('option');
41-
option.innerHTML = uuid;
42-
option.value = uuid;
43-
target.appendChild(option);
44-
});
45-
});
46-
}
36+
sysend.track('update', list => {
37+
target.innerHTML = '';
38+
list.forEach(function({ id: uuid }) {
39+
var option = document.createElement('option');
40+
option.innerHTML = uuid;
41+
option.value = uuid;
42+
target.appendChild(option);
43+
});
44+
});
4745
var a = document.getElementById('a');
4846
var b = document.getElementById('b');
4947
var c = document.getElementById('c');
@@ -78,7 +76,6 @@
7876
track('send: Welcome');
7977
sysend.post(data.id, 'Welcome');
8078
}
81-
list();
8279
});
8380
sysend.track('message', function(data) {
8481
track('message: ' + data.data + ' from ' + data.origin);
@@ -100,7 +97,6 @@
10097
} else {
10198
track('close: ' + id(data.id));
10299
}
103-
list();
104100
});
105101
sysend.track('primary', function() {
106102
track('track: become primary');

sysend.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
secondary: [],
5454
message: [],
5555
visbility: [],
56-
ready: []
56+
ready: [],
57+
update: []
5758
};
5859
var events = Object.keys(handlers);
5960
// -------------------------------------------------------------------------
@@ -586,6 +587,9 @@
586587
setTimeout(init, 0);
587588
});
588589
}
590+
// -------------------------------------------------------------------------
591+
setup_update_tracking();
592+
// -------------------------------------------------------------------------
589593
function setup_ls() {
590594
// we need to clean up localStorage if broadcast called on unload
591595
// because setTimeout will never fire, even setTimeout 0
@@ -613,6 +617,33 @@
613617
}, false);
614618
}
615619
// -------------------------------------------------------------------------
620+
function setup_update_tracking() {
621+
let list = [];
622+
623+
function update() {
624+
trigger(handlers.update, list);
625+
}
626+
627+
sysend.track('open', data => {
628+
if (data.id !== sysend.id) {
629+
list.push(data);
630+
update();
631+
}
632+
}, true);
633+
634+
sysend.track('close', data => {
635+
list = list.filter(tab => data.id !== tab.id);
636+
update();
637+
}, true);
638+
639+
sysend.track('ready', () => {
640+
sysend.list().then(tabs => {
641+
list = tabs;
642+
update();
643+
});
644+
}, true);
645+
}
646+
// -------------------------------------------------------------------------
616647
function init() {
617648
if (typeof window.BroadcastChannel === 'function') {
618649
channel = new window.BroadcastChannel(uniq_prefix);

0 commit comments

Comments
 (0)