From 156d2431dc722d98059c052573b7e84f32571414 Mon Sep 17 00:00:00 2001 From: Alex Kanunnikov Date: Sun, 24 Dec 2023 00:05:10 +0300 Subject: [PATCH] [chore] remove ie11 or fallback in addition, remove unused code --- .../test/modifiers/on-test.ts | 17 ---- .../@glimmer/runtime/lib/dom/operations.ts | 2 +- packages/@glimmer/runtime/lib/modifiers/on.ts | 91 ++----------------- packages/@glimmer/runtime/lib/vm/append.ts | 2 +- packages/@glimmer/runtime/lib/vm/arguments.ts | 19 ++-- packages/@glimmer/util/index.ts | 1 - packages/@glimmer/util/lib/intern.ts | 49 ---------- 7 files changed, 16 insertions(+), 165 deletions(-) delete mode 100644 packages/@glimmer/util/lib/intern.ts diff --git a/packages/@glimmer-workspace/integration-tests/test/modifiers/on-test.ts b/packages/@glimmer-workspace/integration-tests/test/modifiers/on-test.ts index 74ae5bbd9c..ad43550b90 100644 --- a/packages/@glimmer-workspace/integration-tests/test/modifiers/on-test.ts +++ b/packages/@glimmer-workspace/integration-tests/test/modifiers/on-test.ts @@ -25,13 +25,6 @@ const hasDom = self.navigator === navigator && typeof navigator.userAgent === 'string'; -// do this to get around type issues for these values -let global = window as any; -const isChrome = hasDom - ? typeof global.chrome === 'object' && !(typeof global.opera === 'object') - : false; -const isFirefox = hasDom ? /Firefox|FxiOS/u.test(navigator.userAgent) : false; - interface Counters { adds: number; removes: number; @@ -39,7 +32,6 @@ interface Counters { interface OnManager { counters: Counters; - SUPPORTS_EVENT_OPTIONS: boolean; } function getOnManager() { @@ -75,15 +67,6 @@ if (hasDom) { ); } - @test - 'SUPPORTS_EVENT_OPTIONS is correct (private API usage)'(assert: Assert) { - let { SUPPORTS_EVENT_OPTIONS } = getOnManager(); - - if (isChrome || isFirefox) { - assert.strictEqual(SUPPORTS_EVENT_OPTIONS, true, 'is true in chrome and firefox'); - } - } - @test 'it adds an event listener'(assert: Assert) { let count = 0; diff --git a/packages/@glimmer/runtime/lib/dom/operations.ts b/packages/@glimmer/runtime/lib/dom/operations.ts index f135c4889d..fea657ac98 100644 --- a/packages/@glimmer/runtime/lib/dom/operations.ts +++ b/packages/@glimmer/runtime/lib/dom/operations.ts @@ -40,7 +40,7 @@ export class DOMOperations { createElement(tag: string, context?: SimpleElement): SimpleElement { let isElementInSVGNamespace: boolean, isHTMLIntegrationPoint: boolean; - if (context) { + if (context !== undefined) { isElementInSVGNamespace = context.namespaceURI === NS_SVG || tag === 'svg'; isHTMLIntegrationPoint = !!(SVG_INTEGRATION_POINTS as Dict)[context.tagName]; } else { diff --git a/packages/@glimmer/runtime/lib/modifiers/on.ts b/packages/@glimmer/runtime/lib/modifiers/on.ts index 9f28482662..fd6f0b3427 100644 --- a/packages/@glimmer/runtime/lib/modifiers/on.ts +++ b/packages/@glimmer/runtime/lib/modifiers/on.ts @@ -16,41 +16,6 @@ import { reifyNamed } from '../vm/arguments'; const untouchableContext = buildUntouchableThis('`on` modifier'); -/* - Internet Explorer 11 does not support `once` and also does not support - passing `eventOptions`. In some situations it then throws a weird script - error, like: - - ``` - Could not complete the operation due to error 80020101 - ``` - - This flag determines, whether `{ once: true }` and thus also event options in - general are supported. -*/ -const SUPPORTS_EVENT_OPTIONS = (() => { - try { - const div = document.createElement('div'); - let counter = 0; - div.addEventListener('click', () => counter++, { once: true }); - - let event; - if (typeof Event === 'function') { - event = new Event('click'); - } else { - event = document.createEvent('Event'); - event.initEvent('click', true, true); - } - - div.dispatchEvent(event); - div.dispatchEvent(event); - - return counter === 1; - } catch (error) { - return false; - } -})(); - export class OnModifierState { public tag = createUpdatableTag(); public element: Element; @@ -88,11 +53,10 @@ export class OnModifierState { this.shouldUpdate = true; } - let options: AddEventListenerOptions; // we want to handle both `true` and `false` because both have a meaning: // https://bugs.chromium.org/p/chromium/issues/detail?id=770208 if (once !== undefined || passive !== undefined || capture !== undefined) { - options = this.options = { once, passive, capture } as AddEventListenerOptions; + this.options = { once, passive, capture } as AddEventListenerOptions; } else { this.options = undefined; } @@ -139,13 +103,11 @@ export class OnModifierState { ); } - let needsCustomCallback = - (SUPPORTS_EVENT_OPTIONS === false && once) /* needs manual once implementation */ || - (import.meta.env.DEV && passive); /* needs passive enforcement */ + let needsCustomCallback = import.meta.env.DEV && passive; /* needs passive enforcement */ if (this.shouldUpdate) { if (needsCustomCallback) { - let callback = (this.callback = function (this: Element, event) { + this.callback = function (this: Element, event) { if (import.meta.env.DEV && passive) { event.preventDefault = () => { throw new Error( @@ -155,12 +117,8 @@ export class OnModifierState { ); }; } - - if (!SUPPORTS_EVENT_OPTIONS && once) { - removeEventListener(this, eventName, callback, options); - } return userProvidedCallback.call(untouchableContext, event); - }); + }; } else if (import.meta.env.DEV) { // prevent the callback from being bound to the element this.callback = userProvidedCallback.bind(untouchableContext); @@ -182,24 +140,7 @@ function removeEventListener( ): void { removes++; - if (SUPPORTS_EVENT_OPTIONS) { - // when options are supported, use them across the board - element.removeEventListener(eventName, callback, options); - } else if (options !== undefined && options.capture) { - // used only in the following case: - // - // `{ once: true | false, passive: true | false, capture: true } - // - // `once` is handled via a custom callback that removes after first - // invocation so we only care about capture here as a boolean - element.removeEventListener(eventName, callback, true); - } else { - // used only in the following cases: - // - // * where there is no options - // * `{ once: true | false, passive: true | false, capture: false } - element.removeEventListener(eventName, callback); - } + element.removeEventListener(eventName, callback, options); } function addEventListener( @@ -209,25 +150,7 @@ function addEventListener( options?: AddEventListenerOptions ): void { adds++; - - if (SUPPORTS_EVENT_OPTIONS) { - // when options are supported, use them across the board - element.addEventListener(eventName, callback, options); - } else if (options !== undefined && options.capture) { - // used only in the following case: - // - // `{ once: true | false, passive: true | false, capture: true } - // - // `once` is handled via a custom callback that removes after first - // invocation so we only care about capture here as a boolean - element.addEventListener(eventName, callback, true); - } else { - // used only in the following cases: - // - // * where there is no options - // * `{ once: true | false, passive: true | false, capture: false } - element.addEventListener(eventName, callback); - } + element.addEventListener(eventName, callback, options); } /** @@ -317,8 +240,6 @@ function addEventListener( @public */ class OnModifierManager implements InternalModifierManager { - public SUPPORTS_EVENT_OPTIONS: boolean = SUPPORTS_EVENT_OPTIONS; - getDebugName(): string { return 'on'; } diff --git a/packages/@glimmer/runtime/lib/vm/append.ts b/packages/@glimmer/runtime/lib/vm/append.ts index 352341d390..9b1d6295b2 100644 --- a/packages/@glimmer/runtime/lib/vm/append.ts +++ b/packages/@glimmer/runtime/lib/vm/append.ts @@ -569,7 +569,7 @@ export class VM implements PublicVM, InternalVM { LOCAL_LOGGER.log(`EXECUTING FROM ${this[INNER_VM].fetchRegister($pc)}`); } - if (initialize) initialize(this); + if (initialize !== undefined) initialize(this); let result: RichIteratorResult; diff --git a/packages/@glimmer/runtime/lib/vm/arguments.ts b/packages/@glimmer/runtime/lib/vm/arguments.ts index 268c9329ce..4fb2b8fd5a 100644 --- a/packages/@glimmer/runtime/lib/vm/arguments.ts +++ b/packages/@glimmer/runtime/lib/vm/arguments.ts @@ -69,22 +69,19 @@ export class VMArgumentsImpl implements VMArguments { bbase pbase nbase sp */ - let named = this.named; let namedCount = names.length; let namedBase = stack[REGISTERS][$sp] - namedCount + 1; - named.setup(stack, namedBase, namedCount, names, atNames); + this.named.setup(stack, namedBase, namedCount, names, atNames); - let positional = this.positional; let positionalBase = namedBase - positionalCount; - positional.setup(stack, positionalBase, positionalCount); + this.positional.setup(stack, positionalBase, positionalCount); - let blocks = this.blocks; let blocksCount = blockNames.length; let blocksBase = positionalBase - blocksCount * 3; - blocks.setup(stack, blocksBase, blocksCount, blockNames); + this.blocks.setup(stack, blocksBase, blocksCount, blockNames); } get base(): number { @@ -193,7 +190,7 @@ export class PositionalArgumentsImpl implements PositionalArguments { private get references(): readonly Reference[] { let references = this._references; - if (!references) { + if (references === null) { let { stack, base, length } = this; references = this._references = stack.slice(base, base + length); } @@ -254,7 +251,7 @@ export class NamedArgumentsImpl implements NamedArguments { get names(): readonly string[] { let names = this._names; - if (!names) { + if (names === null) { names = this._names = this._atNames!.map(this.toSyntheticName); } @@ -264,7 +261,7 @@ export class NamedArgumentsImpl implements NamedArguments { get atNames(): readonly string[] { let atNames = this._atNames; - if (!atNames) { + if (atNames === null) { atNames = this._atNames = this._names!.map(this.toAtName); } @@ -336,7 +333,7 @@ export class NamedArgumentsImpl implements NamedArguments { private get references(): readonly Reference[] { let references = this._references; - if (!references) { + if (references === null) { let { base, length, stack } = this; references = this._references = stack.slice(base, base + length); } @@ -400,7 +397,7 @@ export class BlockArgumentsImpl implements BlockArguments { get values(): readonly BlockValue[] { let values = this.internalValues; - if (!values) { + if (values === null) { let { base, length, stack } = this; values = this.internalValues = stack.slice(base, base + length * 3); } diff --git a/packages/@glimmer/util/index.ts b/packages/@glimmer/util/index.ts index df4c6eb773..5c38e67687 100644 --- a/packages/@glimmer/util/index.ts +++ b/packages/@glimmer/util/index.ts @@ -6,7 +6,6 @@ export { default as debugToString } from './lib/debug-to-string'; export * from './lib/dom'; export * from './lib/dom-utils'; export * from './lib/immediate'; -export { default as intern } from './lib/intern'; export { isSerializationFirstNode, SERIALIZATION_FIRST_NODE_STRING, diff --git a/packages/@glimmer/util/lib/intern.ts b/packages/@glimmer/util/lib/intern.ts deleted file mode 100644 index 422ba91e92..0000000000 --- a/packages/@glimmer/util/lib/intern.ts +++ /dev/null @@ -1,49 +0,0 @@ -/** - Strongly hint runtimes to intern the provided string. - - When do I need to use this function? - - For the most part, never. Pre-mature optimization is bad, and often the - runtime does exactly what you need it to, and more often the trade-off isn't - worth it. - - Why? - - Runtimes store strings in at least 2 different representations: - Ropes and Symbols (interned strings). The Rope provides a memory efficient - data-structure for strings created from concatenation or some other string - manipulation like splitting. - - Unfortunately checking equality of different ropes can be quite costly as - runtimes must resort to clever string comparison algorithms. These - algorithms typically cost in proportion to the length of the string. - Luckily, this is where the Symbols (interned strings) shine. As Symbols are - unique by their string content, equality checks can be done by pointer - comparison. - - How do I know if my string is a rope or symbol? - - Typically (warning general sweeping statement, but truthy in runtimes at - present) static strings created as part of the JS source are interned. - Strings often used for comparisons can be interned at runtime if some - criteria are met. One of these criteria can be the size of the entire rope. - For example, in chrome 38 a rope longer then 12 characters will not - intern, nor will segments of that rope. - - Some numbers: http://jsperf.com/eval-vs-keys/8 - - Known Trickā„¢ - - @private - @return {String} interned version of the provided string -*/ -export default function intern(str: string): string { - let obj: Record = {}; - obj[str] = 1; - for (let key in obj) { - if (key === str) { - return key; - } - } - return str; -}