-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved global injecting code to a separate file
- Loading branch information
1 parent
d709f3b
commit 9c3968c
Showing
3 changed files
with
56 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2023 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import { Realm as RealmConstructor, flags, safeGlobalThis } from "./internal"; | ||
|
||
declare global { | ||
/** | ||
* @deprecated Use default import of Realm, this global will be removed in v13. | ||
* @example | ||
* import Realm from "realm"; | ||
*/ | ||
export type Realm = RealmConstructor; | ||
export const Realm: typeof RealmConstructor; | ||
} | ||
|
||
// Patch the global at runtime | ||
let warnedAboutGlobalRealmUse = false; | ||
Object.defineProperty(safeGlobalThis, "Realm", { | ||
get() { | ||
if (flags.THROW_ON_GLOBAL_REALM) { | ||
throw new Error( | ||
"Accessed global Realm, please update your code to ensure you import Realm via a default import:\nimport Realm from 'realm';", | ||
); | ||
} else if (!warnedAboutGlobalRealmUse) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
"Your app is relying on a Realm global, which will be removed in realm-js v13, please update your code to ensure you import Realm via a default import:\n\n", | ||
'import Realm from "realm"; // For ES Modules\n', | ||
'const Realm = require("realm"); // For CommonJS\n\n', | ||
"To determine where, put this in the top of your index file:\n", | ||
`import { flags } from "realm";\n`, | ||
`flags.THROW_ON_GLOBAL_REALM = true`, | ||
); | ||
warnedAboutGlobalRealmUse = true; | ||
} | ||
return RealmConstructor; | ||
}, | ||
configurable: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters