From c049312d51b598e40795af4377c6bfcc282e6ad5 Mon Sep 17 00:00:00 2001 From: Saulius Skliutas <24278440+saskliutas@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:23:29 +0200 Subject: [PATCH] Polyfill `Symbol.dispose` and `Symbol.asyncDispose` (#828) * Make sure `Symbol.dispose` and `Symbol.asyncDispose` are defined before usage * Change * Update comment * Use example from typescript docs --- .changeset/silver-ears-change.md | 8 ++++++++ .../common/ContentDataProvider.ts | 1 + .../presentation-components/common/DisposePolyfill.ts | 10 ++++++++++ .../common/IPresentationDataProvider.ts | 1 + .../common/RulesetRegistrationHelper.ts | 1 + .../src/presentation-components/common/Utils.ts | 1 + .../properties/inputs/ItemsLoader.tsx | 2 ++ .../inputs/UseNavigationPropertyTargetsLoader.ts | 1 + .../properties/inputs/UseUniquePropertyValuesLoader.ts | 1 + .../propertygrid/DataProvider.ts | 1 + .../src/presentation-components/tree/DataProvider.ts | 1 + .../tree/FilteredDataProvider.ts | 1 + .../tree/controlled/TreeHooks.ts | 1 + .../tree/controlled/UsePresentationTreeState.ts | 1 + .../tree/controlled/UseUnifiedSelection.ts | 1 + .../viewport/ViewportSelectionHandler.ts | 1 + .../viewport/WithUnifiedSelection.tsx | 1 + .../internal/DisposePolyfill.ts | 10 ++++++++++ .../internal/TreeActions.ts | 1 + .../presentation-hierarchies-react/internal/Utils.ts | 1 + .../hierarchies/src/hierarchies/HierarchyProvider.ts | 1 + .../src/hierarchies/imodel/IModelHierarchyProvider.ts | 1 + .../hierarchies/src/hierarchies/internal/Common.ts | 1 + .../src/hierarchies/internal/DisposePolyfill.ts | 10 ++++++++++ .../src/unified-selection/CachingHiliteSetProvider.ts | 1 + .../src/unified-selection/DisposePolyfill.ts | 10 ++++++++++ .../EnableUnifiedSelectionSyncWithIModel.ts | 1 + .../unified-selection/src/unified-selection/Utils.ts | 1 + 28 files changed, 72 insertions(+) create mode 100644 .changeset/silver-ears-change.md create mode 100644 packages/components/src/presentation-components/common/DisposePolyfill.ts create mode 100644 packages/hierarchies-react/src/presentation-hierarchies-react/internal/DisposePolyfill.ts create mode 100644 packages/hierarchies/src/hierarchies/internal/DisposePolyfill.ts create mode 100644 packages/unified-selection/src/unified-selection/DisposePolyfill.ts diff --git a/.changeset/silver-ears-change.md b/.changeset/silver-ears-change.md new file mode 100644 index 000000000..e677db178 --- /dev/null +++ b/.changeset/silver-ears-change.md @@ -0,0 +1,8 @@ +--- +"@itwin/presentation-hierarchies-react": patch +"@itwin/unified-selection": patch +"@itwin/presentation-hierarchies": patch +"@itwin/presentation-components": patch +--- + +Polyfill `Symbol.dispose` and `Symbol.asyncDispose` to make sure that code using the upcoming JS recource management API works in all environments. diff --git a/packages/components/src/presentation-components/common/ContentDataProvider.ts b/packages/components/src/presentation-components/common/ContentDataProvider.ts index 98c85a36b..f08c3e32b 100644 --- a/packages/components/src/presentation-components/common/ContentDataProvider.ts +++ b/packages/components/src/presentation-components/common/ContentDataProvider.ts @@ -6,6 +6,7 @@ * @module Core */ +import "./DisposePolyfill.js"; import { PropertyDescription, PropertyRecord } from "@itwin/appui-abstract"; import { Logger } from "@itwin/core-bentley"; import { IModelApp, IModelConnection } from "@itwin/core-frontend"; diff --git a/packages/components/src/presentation-components/common/DisposePolyfill.ts b/packages/components/src/presentation-components/common/DisposePolyfill.ts new file mode 100644 index 000000000..188140a7f --- /dev/null +++ b/packages/components/src/presentation-components/common/DisposePolyfill.ts @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. + * See LICENSE.md in the project root for license terms and full copyright notice. + *--------------------------------------------------------------------------------------------*/ + +/** + * Polyfill for upcoming resource management feature: https://github.com/tc39/proposal-explicit-resource-management + */ +(Symbol as any).dispose ??= Symbol("Symbol.dispose"); +(Symbol as any).asyncDispose ??= Symbol("Symbol.asyncDispose"); diff --git a/packages/components/src/presentation-components/common/IPresentationDataProvider.ts b/packages/components/src/presentation-components/common/IPresentationDataProvider.ts index e73f0ab2a..2b786e0fc 100644 --- a/packages/components/src/presentation-components/common/IPresentationDataProvider.ts +++ b/packages/components/src/presentation-components/common/IPresentationDataProvider.ts @@ -6,6 +6,7 @@ * @module Core */ +import "./DisposePolyfill.js"; import { IModelConnection } from "@itwin/core-frontend"; /** diff --git a/packages/components/src/presentation-components/common/RulesetRegistrationHelper.ts b/packages/components/src/presentation-components/common/RulesetRegistrationHelper.ts index 0bdfb54f5..e320548e7 100644 --- a/packages/components/src/presentation-components/common/RulesetRegistrationHelper.ts +++ b/packages/components/src/presentation-components/common/RulesetRegistrationHelper.ts @@ -6,6 +6,7 @@ * @module Core */ +import "./DisposePolyfill.js"; import { RegisteredRuleset, Ruleset } from "@itwin/presentation-common"; import { Presentation } from "@itwin/presentation-frontend"; diff --git a/packages/components/src/presentation-components/common/Utils.ts b/packages/components/src/presentation-components/common/Utils.ts index 93450e6ee..9e2461c85 100644 --- a/packages/components/src/presentation-components/common/Utils.ts +++ b/packages/components/src/presentation-components/common/Utils.ts @@ -6,6 +6,7 @@ * @module Core */ +import "../common/DisposePolyfill.js"; import * as mm from "micro-memoize"; import { LegacyRef, MutableRefObject, RefCallback, useCallback, useEffect, useState } from "react"; import { Primitives, PrimitiveValue, PropertyDescription, PropertyRecord, PropertyValueFormat } from "@itwin/appui-abstract"; diff --git a/packages/components/src/presentation-components/properties/inputs/ItemsLoader.tsx b/packages/components/src/presentation-components/properties/inputs/ItemsLoader.tsx index bea213a1d..445cb7c6b 100644 --- a/packages/components/src/presentation-components/properties/inputs/ItemsLoader.tsx +++ b/packages/components/src/presentation-components/properties/inputs/ItemsLoader.tsx @@ -3,6 +3,8 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "../../common/DisposePolyfill.js"; + /** @internal */ export const VALUE_BATCH_SIZE = 100; diff --git a/packages/components/src/presentation-components/properties/inputs/UseNavigationPropertyTargetsLoader.ts b/packages/components/src/presentation-components/properties/inputs/UseNavigationPropertyTargetsLoader.ts index 591f22e6f..ba174dadb 100644 --- a/packages/components/src/presentation-components/properties/inputs/UseNavigationPropertyTargetsLoader.ts +++ b/packages/components/src/presentation-components/properties/inputs/UseNavigationPropertyTargetsLoader.ts @@ -6,6 +6,7 @@ * @module Internal */ +import "../../common/DisposePolyfill.js"; import { useEffect, useMemo, useState } from "react"; import { EMPTY, from, map, mergeMap, toArray } from "rxjs"; import { PropertyDescription } from "@itwin/appui-abstract"; diff --git a/packages/components/src/presentation-components/properties/inputs/UseUniquePropertyValuesLoader.ts b/packages/components/src/presentation-components/properties/inputs/UseUniquePropertyValuesLoader.ts index 2bdf569fa..0279b4baf 100644 --- a/packages/components/src/presentation-components/properties/inputs/UseUniquePropertyValuesLoader.ts +++ b/packages/components/src/presentation-components/properties/inputs/UseUniquePropertyValuesLoader.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "../../common/DisposePolyfill.js"; import { useEffect, useMemo, useState } from "react"; import { from, map, mergeMap, toArray } from "rxjs"; import { IModelConnection } from "@itwin/core-frontend"; diff --git a/packages/components/src/presentation-components/propertygrid/DataProvider.ts b/packages/components/src/presentation-components/propertygrid/DataProvider.ts index b8bf8da1f..46bedea11 100644 --- a/packages/components/src/presentation-components/propertygrid/DataProvider.ts +++ b/packages/components/src/presentation-components/propertygrid/DataProvider.ts @@ -6,6 +6,7 @@ * @module PropertyGrid */ +import "../common/DisposePolyfill.js"; import { inPlaceSort } from "fast-sort"; import { PropertyRecord, PropertyValueFormat as UiPropertyValueFormat } from "@itwin/appui-abstract"; import { IPropertyDataProvider, PropertyCategory, PropertyData, PropertyDataChangeEvent } from "@itwin/components-react"; diff --git a/packages/components/src/presentation-components/tree/DataProvider.ts b/packages/components/src/presentation-components/tree/DataProvider.ts index 394411b0d..ab404e905 100644 --- a/packages/components/src/presentation-components/tree/DataProvider.ts +++ b/packages/components/src/presentation-components/tree/DataProvider.ts @@ -7,6 +7,7 @@ * @module Tree */ +import "../common/DisposePolyfill.js"; import { DelayLoadedTreeNodeItem, PageOptions, PropertyFilterRuleGroupOperator, TreeNodeItem } from "@itwin/components-react"; import { Logger } from "@itwin/core-bentley"; import { IModelConnection } from "@itwin/core-frontend"; diff --git a/packages/components/src/presentation-components/tree/FilteredDataProvider.ts b/packages/components/src/presentation-components/tree/FilteredDataProvider.ts index a3ae6e5d5..a9b3b66d7 100644 --- a/packages/components/src/presentation-components/tree/FilteredDataProvider.ts +++ b/packages/components/src/presentation-components/tree/FilteredDataProvider.ts @@ -4,6 +4,7 @@ *--------------------------------------------------------------------------------------------*/ /* eslint-disable @typescript-eslint/no-deprecated */ +import "../common/DisposePolyfill.js"; import { ActiveMatchInfo, DelayLoadedTreeNodeItem, diff --git a/packages/components/src/presentation-components/tree/controlled/TreeHooks.ts b/packages/components/src/presentation-components/tree/controlled/TreeHooks.ts index 988d68c64..a53e472da 100644 --- a/packages/components/src/presentation-components/tree/controlled/TreeHooks.ts +++ b/packages/components/src/presentation-components/tree/controlled/TreeHooks.ts @@ -7,6 +7,7 @@ * @module Tree */ +import "../../common/DisposePolyfill.js"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Subscription } from "rxjs/internal/Subscription"; import { diff --git a/packages/components/src/presentation-components/tree/controlled/UsePresentationTreeState.ts b/packages/components/src/presentation-components/tree/controlled/UsePresentationTreeState.ts index 604c8f956..32e48379f 100644 --- a/packages/components/src/presentation-components/tree/controlled/UsePresentationTreeState.ts +++ b/packages/components/src/presentation-components/tree/controlled/UsePresentationTreeState.ts @@ -7,6 +7,7 @@ * @module Tree */ +import "../../common/DisposePolyfill.js"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { AbstractTreeNodeLoaderWithProvider, diff --git a/packages/components/src/presentation-components/tree/controlled/UseUnifiedSelection.ts b/packages/components/src/presentation-components/tree/controlled/UseUnifiedSelection.ts index 1c58eed24..5358c4a9e 100644 --- a/packages/components/src/presentation-components/tree/controlled/UseUnifiedSelection.ts +++ b/packages/components/src/presentation-components/tree/controlled/UseUnifiedSelection.ts @@ -7,6 +7,7 @@ * @module Tree */ +import "../../common/DisposePolyfill.js"; import { useCallback } from "react"; import { Subject, takeUntil, tap } from "rxjs"; import { diff --git a/packages/components/src/presentation-components/viewport/ViewportSelectionHandler.ts b/packages/components/src/presentation-components/viewport/ViewportSelectionHandler.ts index ee2d039b4..0a4aef5ac 100644 --- a/packages/components/src/presentation-components/viewport/ViewportSelectionHandler.ts +++ b/packages/components/src/presentation-components/viewport/ViewportSelectionHandler.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "../common/DisposePolyfill.js"; import { from, Subject, takeUntil } from "rxjs"; import { using } from "@itwin/core-bentley"; import { IModelConnection } from "@itwin/core-frontend"; diff --git a/packages/components/src/presentation-components/viewport/WithUnifiedSelection.tsx b/packages/components/src/presentation-components/viewport/WithUnifiedSelection.tsx index eda317ad0..11ee8f447 100644 --- a/packages/components/src/presentation-components/viewport/WithUnifiedSelection.tsx +++ b/packages/components/src/presentation-components/viewport/WithUnifiedSelection.tsx @@ -6,6 +6,7 @@ * @module Viewport */ +import "../common/DisposePolyfill.js"; import { createContext, memo, PropsWithChildren, useContext, useEffect, useState } from "react"; import { ViewportProps } from "@itwin/imodel-components-react"; import { getDisplayName } from "../common/Utils.js"; diff --git a/packages/hierarchies-react/src/presentation-hierarchies-react/internal/DisposePolyfill.ts b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/DisposePolyfill.ts new file mode 100644 index 000000000..188140a7f --- /dev/null +++ b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/DisposePolyfill.ts @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. + * See LICENSE.md in the project root for license terms and full copyright notice. + *--------------------------------------------------------------------------------------------*/ + +/** + * Polyfill for upcoming resource management feature: https://github.com/tc39/proposal-explicit-resource-management + */ +(Symbol as any).dispose ??= Symbol("Symbol.dispose"); +(Symbol as any).asyncDispose ??= Symbol("Symbol.asyncDispose"); diff --git a/packages/hierarchies-react/src/presentation-hierarchies-react/internal/TreeActions.ts b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/TreeActions.ts index 4a7548909..e8f27eb26 100644 --- a/packages/hierarchies-react/src/presentation-hierarchies-react/internal/TreeActions.ts +++ b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/TreeActions.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import { Draft, enableMapSet, produce } from "immer"; import { EMPTY, Observable, reduce, Subject, takeUntil } from "rxjs"; import { GenericInstanceFilter, HierarchyNode, HierarchyProvider } from "@itwin/presentation-hierarchies"; diff --git a/packages/hierarchies-react/src/presentation-hierarchies-react/internal/Utils.ts b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/Utils.ts index 32a2fa452..a87002c80 100644 --- a/packages/hierarchies-react/src/presentation-hierarchies-react/internal/Utils.ts +++ b/packages/hierarchies-react/src/presentation-hierarchies-react/internal/Utils.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import { HierarchyNode, HierarchyNodeKey } from "@itwin/presentation-hierarchies"; /** @internal */ diff --git a/packages/hierarchies/src/hierarchies/HierarchyProvider.ts b/packages/hierarchies/src/hierarchies/HierarchyProvider.ts index 46e932014..670f5003a 100644 --- a/packages/hierarchies/src/hierarchies/HierarchyProvider.ts +++ b/packages/hierarchies/src/hierarchies/HierarchyProvider.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./internal/DisposePolyfill.js"; import { filter, first, from, map, mergeMap, of } from "rxjs"; import { BeEvent } from "@itwin/core-bentley"; import { GenericInstanceFilter } from "@itwin/core-common"; diff --git a/packages/hierarchies/src/hierarchies/imodel/IModelHierarchyProvider.ts b/packages/hierarchies/src/hierarchies/imodel/IModelHierarchyProvider.ts index a579c11af..91d90c907 100644 --- a/packages/hierarchies/src/hierarchies/imodel/IModelHierarchyProvider.ts +++ b/packages/hierarchies/src/hierarchies/imodel/IModelHierarchyProvider.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "../internal/DisposePolyfill.js"; import { catchError, concat, diff --git a/packages/hierarchies/src/hierarchies/internal/Common.ts b/packages/hierarchies/src/hierarchies/internal/Common.ts index 847282dbe..6f7634d57 100644 --- a/packages/hierarchies/src/hierarchies/internal/Common.ts +++ b/packages/hierarchies/src/hierarchies/internal/Common.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import naturalCompare from "natural-compare-lite"; import { ConcatenatedValue } from "@itwin/presentation-shared"; import { HierarchyNodeKey } from "../HierarchyNodeKey.js"; diff --git a/packages/hierarchies/src/hierarchies/internal/DisposePolyfill.ts b/packages/hierarchies/src/hierarchies/internal/DisposePolyfill.ts new file mode 100644 index 000000000..188140a7f --- /dev/null +++ b/packages/hierarchies/src/hierarchies/internal/DisposePolyfill.ts @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. + * See LICENSE.md in the project root for license terms and full copyright notice. + *--------------------------------------------------------------------------------------------*/ + +/** + * Polyfill for upcoming resource management feature: https://github.com/tc39/proposal-explicit-resource-management + */ +(Symbol as any).dispose ??= Symbol("Symbol.dispose"); +(Symbol as any).asyncDispose ??= Symbol("Symbol.asyncDispose"); diff --git a/packages/unified-selection/src/unified-selection/CachingHiliteSetProvider.ts b/packages/unified-selection/src/unified-selection/CachingHiliteSetProvider.ts index 92b11b663..cda3a1758 100644 --- a/packages/unified-selection/src/unified-selection/CachingHiliteSetProvider.ts +++ b/packages/unified-selection/src/unified-selection/CachingHiliteSetProvider.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import { from, Observable, shareReplay } from "rxjs"; import { eachValueFrom } from "rxjs-for-await"; import { ECClassHierarchyInspector, ECSqlQueryExecutor } from "@itwin/presentation-shared"; diff --git a/packages/unified-selection/src/unified-selection/DisposePolyfill.ts b/packages/unified-selection/src/unified-selection/DisposePolyfill.ts new file mode 100644 index 000000000..188140a7f --- /dev/null +++ b/packages/unified-selection/src/unified-selection/DisposePolyfill.ts @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. + * See LICENSE.md in the project root for license terms and full copyright notice. + *--------------------------------------------------------------------------------------------*/ + +/** + * Polyfill for upcoming resource management feature: https://github.com/tc39/proposal-explicit-resource-management + */ +(Symbol as any).dispose ??= Symbol("Symbol.dispose"); +(Symbol as any).asyncDispose ??= Symbol("Symbol.asyncDispose"); diff --git a/packages/unified-selection/src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts b/packages/unified-selection/src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts index 74cc5f003..10767dfdf 100644 --- a/packages/unified-selection/src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts +++ b/packages/unified-selection/src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import { EMPTY, firstValueFrom, from, map, merge, Observable, Subject, takeUntil, toArray } from "rxjs"; import { ECClassHierarchyInspector, ECSqlQueryExecutor } from "@itwin/presentation-shared"; import { CachingHiliteSetProvider, createCachingHiliteSetProvider } from "./CachingHiliteSetProvider.js"; diff --git a/packages/unified-selection/src/unified-selection/Utils.ts b/packages/unified-selection/src/unified-selection/Utils.ts index 4061e0048..f3540e9a8 100644 --- a/packages/unified-selection/src/unified-selection/Utils.ts +++ b/packages/unified-selection/src/unified-selection/Utils.ts @@ -3,6 +3,7 @@ * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ +import "./DisposePolyfill.js"; import { bufferCount, concatAll, concatMap, delay, Observable, of } from "rxjs"; import { createMainThreadReleaseOnTimePassedHandler, ECSqlBinding, ECSqlQueryDef, ECSqlQueryExecutor, ECSqlQueryRow } from "@itwin/presentation-shared";