
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
The repository being in static storage is a bit of a hodgepodge, but in line with how our current storage partitioning is done. We should eventually move this, along with other across browsing context APIs to a proper location at a later stage. But for now, this makes progress on the meat of the BroadcastChannel API.
128 lines
4.3 KiB
JavaScript
128 lines
4.3 KiB
JavaScript
test(function() {
|
|
assert_throws_js(
|
|
TypeError,
|
|
() => BroadcastChannel(""),
|
|
"Calling BroadcastChannel constructor without 'new' must throw"
|
|
);
|
|
}, "BroadcastChannel constructor called as normal function");
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('eventType');
|
|
let c2 = new BroadcastChannel('eventType');
|
|
|
|
c2.onmessage = t.step_func(e => {
|
|
assert_true(e instanceof MessageEvent);
|
|
assert_equals(e.target, c2);
|
|
assert_equals(e.type, 'message');
|
|
assert_equals(e.origin, location.origin, 'origin');
|
|
assert_equals(e.data, 'hello world');
|
|
assert_equals(e.source, null, 'source');
|
|
t.done();
|
|
});
|
|
c1.postMessage('hello world');
|
|
}, 'postMessage results in correct event');
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('order');
|
|
let c2 = new BroadcastChannel('order');
|
|
let c3 = new BroadcastChannel('order');
|
|
|
|
let events = [];
|
|
let doneCount = 0;
|
|
let handler = t.step_func(e => {
|
|
events.push(e);
|
|
if (e.data == 'done') {
|
|
doneCount++;
|
|
if (doneCount == 2) {
|
|
assert_equals(events.length, 6);
|
|
assert_equals(events[0].target, c2, 'target for event 0');
|
|
assert_equals(events[0].data, 'from c1');
|
|
assert_equals(events[1].target, c3, 'target for event 1');
|
|
assert_equals(events[1].data, 'from c1');
|
|
assert_equals(events[2].target, c1, 'target for event 2');
|
|
assert_equals(events[2].data, 'from c3');
|
|
assert_equals(events[3].target, c2, 'target for event 3');
|
|
assert_equals(events[3].data, 'from c3');
|
|
assert_equals(events[4].target, c1, 'target for event 4');
|
|
assert_equals(events[4].data, 'done');
|
|
assert_equals(events[5].target, c3, 'target for event 5');
|
|
assert_equals(events[5].data, 'done');
|
|
t.done();
|
|
}
|
|
}
|
|
});
|
|
c1.onmessage = handler;
|
|
c2.onmessage = handler;
|
|
c3.onmessage = handler;
|
|
|
|
c1.postMessage('from c1');
|
|
c3.postMessage('from c3');
|
|
c2.postMessage('done');
|
|
}, 'messages are delivered in port creation order');
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('closed');
|
|
let c2 = new BroadcastChannel('closed');
|
|
let c3 = new BroadcastChannel('closed');
|
|
|
|
c2.onmessage = t.unreached_func();
|
|
c2.close();
|
|
c3.onmessage = t.step_func(() => t.done());
|
|
c1.postMessage('test');
|
|
}, 'messages aren\'t delivered to a closed port');
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('closed');
|
|
let c2 = new BroadcastChannel('closed');
|
|
let c3 = new BroadcastChannel('closed');
|
|
|
|
c2.onmessage = t.unreached_func();
|
|
c3.onmessage = t.step_func(() => t.done());
|
|
c1.postMessage('test');
|
|
c2.close();
|
|
}, 'messages aren\'t delivered to a port closed after calling postMessage.');
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('create-in-onmessage');
|
|
let c2 = new BroadcastChannel('create-in-onmessage');
|
|
|
|
c2.onmessage = t.step_func(e => {
|
|
assert_equals(e.data, 'first');
|
|
c2.close();
|
|
let c3 = new BroadcastChannel('create-in-onmessage');
|
|
c3.onmessage = t.step_func(event => {
|
|
assert_equals(event.data, 'done');
|
|
t.done();
|
|
});
|
|
c1.postMessage('done');
|
|
});
|
|
c1.postMessage('first');
|
|
c2.postMessage('second');
|
|
}, 'closing and creating channels during message delivery works correctly');
|
|
|
|
async_test(t => {
|
|
let c1 = new BroadcastChannel('close-in-onmessage');
|
|
let c2 = new BroadcastChannel('close-in-onmessage');
|
|
let c3 = new BroadcastChannel('close-in-onmessage');
|
|
let events = [];
|
|
c1.onmessage = e => events.push('c1: ' + e.data);
|
|
c2.onmessage = e => events.push('c2: ' + e.data);
|
|
c3.onmessage = e => events.push('c3: ' + e.data);
|
|
|
|
// c2 closes itself when it receives the first message
|
|
c2.addEventListener('message', e => {
|
|
c2.close();
|
|
});
|
|
|
|
c3.addEventListener('message', t.step_func(e => {
|
|
if (e.data == 'done') {
|
|
assert_array_equals(events, [
|
|
'c2: first',
|
|
'c3: first',
|
|
'c3: done']);
|
|
t.done();
|
|
}
|
|
}));
|
|
c1.postMessage('first');
|
|
c1.postMessage('done');
|
|
}, 'Closing a channel in onmessage prevents already queued tasks from firing onmessage events');
|