mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
refactor: use primordials for extensions/broadcast_channel (#11231)
This commit is contained in:
parent
e90b97ada2
commit
d4de477ef9
1 changed files with 26 additions and 8 deletions
|
@ -1,10 +1,25 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
/// <reference path="../../core/internal.d.ts" />
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
const core = window.Deno.core;
|
const core = window.Deno.core;
|
||||||
const webidl = window.__bootstrap.webidl;
|
const webidl = window.__bootstrap.webidl;
|
||||||
const { setTarget } = window.__bootstrap.event;
|
const { setTarget } = window.__bootstrap.event;
|
||||||
|
const {
|
||||||
|
ArrayPrototypeIndexOf,
|
||||||
|
ArrayPrototypeSplice,
|
||||||
|
ArrayPrototypePush,
|
||||||
|
Symbol,
|
||||||
|
Uint8Array,
|
||||||
|
ObjectDefineProperty,
|
||||||
|
Map,
|
||||||
|
MapPrototypeSet,
|
||||||
|
MapPrototypeGet,
|
||||||
|
FunctionPrototypeCall,
|
||||||
|
} = window.__bootstrap.primordials;
|
||||||
|
|
||||||
const handlerSymbol = Symbol("eventHandlers");
|
const handlerSymbol = Symbol("eventHandlers");
|
||||||
function makeWrappedHandler(handler) {
|
function makeWrappedHandler(handler) {
|
||||||
|
@ -12,7 +27,7 @@
|
||||||
if (typeof wrappedHandler.handler !== "function") {
|
if (typeof wrappedHandler.handler !== "function") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return wrappedHandler.handler.call(this, ...args);
|
return FunctionPrototypeCall(wrappedHandler.handler, this, ...args);
|
||||||
}
|
}
|
||||||
wrappedHandler.handler = handler;
|
wrappedHandler.handler = handler;
|
||||||
return wrappedHandler;
|
return wrappedHandler;
|
||||||
|
@ -20,25 +35,28 @@
|
||||||
// TODO(lucacasonato) reuse when we can reuse code between web crates
|
// TODO(lucacasonato) reuse when we can reuse code between web crates
|
||||||
function defineEventHandler(emitter, name) {
|
function defineEventHandler(emitter, name) {
|
||||||
// HTML specification section 8.1.5.1
|
// HTML specification section 8.1.5.1
|
||||||
Object.defineProperty(emitter, `on${name}`, {
|
ObjectDefineProperty(emitter, `on${name}`, {
|
||||||
get() {
|
get() {
|
||||||
// TODO(bnoordhuis) The "BroadcastChannel should have an onmessage
|
// TODO(bnoordhuis) The "BroadcastChannel should have an onmessage
|
||||||
// event" WPT test expects that .onmessage !== undefined. Returning
|
// event" WPT test expects that .onmessage !== undefined. Returning
|
||||||
// null makes it pass but is perhaps not exactly in the spirit.
|
// null makes it pass but is perhaps not exactly in the spirit.
|
||||||
return this[handlerSymbol]?.get(name)?.handler ?? null;
|
if (!this[handlerSymbol]) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return MapPrototypeGet(this[handlerSymbol], name)?.handler ?? null;
|
||||||
},
|
},
|
||||||
set(value) {
|
set(value) {
|
||||||
if (!this[handlerSymbol]) {
|
if (!this[handlerSymbol]) {
|
||||||
this[handlerSymbol] = new Map();
|
this[handlerSymbol] = new Map();
|
||||||
}
|
}
|
||||||
let handlerWrapper = this[handlerSymbol]?.get(name);
|
let handlerWrapper = MapPrototypeGet(this[handlerSymbol], name);
|
||||||
if (handlerWrapper) {
|
if (handlerWrapper) {
|
||||||
handlerWrapper.handler = value;
|
handlerWrapper.handler = value;
|
||||||
} else {
|
} else {
|
||||||
handlerWrapper = makeWrappedHandler(value);
|
handlerWrapper = makeWrappedHandler(value);
|
||||||
this.addEventListener(name, handlerWrapper);
|
this.addEventListener(name, handlerWrapper);
|
||||||
}
|
}
|
||||||
this[handlerSymbol].set(name, handlerWrapper);
|
MapPrototypeSet(this[handlerSymbol], name, handlerWrapper);
|
||||||
},
|
},
|
||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
|
@ -115,7 +133,7 @@
|
||||||
|
|
||||||
this[webidl.brand] = webidl.brand;
|
this[webidl.brand] = webidl.brand;
|
||||||
|
|
||||||
channels.push(this);
|
ArrayPrototypePush(channels, this);
|
||||||
|
|
||||||
if (rid === null) {
|
if (rid === null) {
|
||||||
// Create the rid immediately, otherwise there is a time window (and a
|
// Create the rid immediately, otherwise there is a time window (and a
|
||||||
|
@ -152,10 +170,10 @@
|
||||||
webidl.assertBranded(this, BroadcastChannel);
|
webidl.assertBranded(this, BroadcastChannel);
|
||||||
this[_closed] = true;
|
this[_closed] = true;
|
||||||
|
|
||||||
const index = channels.indexOf(this);
|
const index = ArrayPrototypeIndexOf(channels, this);
|
||||||
if (index === -1) return;
|
if (index === -1) return;
|
||||||
|
|
||||||
channels.splice(index, 1);
|
ArrayPrototypeSplice(channels, index, 1);
|
||||||
if (channels.length === 0) core.opSync("op_broadcast_unsubscribe", rid);
|
if (channels.length === 0) core.opSync("op_broadcast_unsubscribe", rid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue