Skip to content

Commit

Permalink
Restored src/index.ts and fixed @realm/react bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Jul 25, 2024
1 parent cc651a3 commit 4ede85e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions packages/realm-react/src/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
////////////////////////////////////////////////////////////////////////////

import React, { createContext, useContext, useEffect, useReducer, useState } from "react";
import type Realm from "realm";
import type { Realm, AnyUser, DefaultFunctionsFactory } from "realm";

import { useApp } from "./AppProvider";

/**
* Create a context containing the Realm app. Should be accessed with the useApp hook.
*/
export const UserContext = createContext<Realm.User | null>(null);
export const UserContext = createContext<AnyUser | null>(null);

type UserProviderProps = {
/**
Expand All @@ -35,7 +35,7 @@ type UserProviderProps = {
children: React.ReactNode;
};

function userWasUpdated(userA: Realm.User | null, userB: Realm.User | null) {
function userWasUpdated(userA: AnyUser | null, userB: AnyUser | null) {
if (!userA && !userB) {
return false;
} else if (userA && userB) {
Expand All @@ -56,7 +56,7 @@ function userWasUpdated(userA: Realm.User | null, userB: Realm.User | null) {
*/
export const UserProvider: React.FC<UserProviderProps> = ({ fallback: Fallback, children }) => {
const app = useApp();
const [user, setUser] = useState<Realm.User | null>(() => app.currentUser);
const [user, setUser] = useState<AnyUser | null>(() => app.currentUser);
const [, forceUpdate] = useReducer((x) => x + 1, 0);

// Support for a possible change in configuration.
Expand Down Expand Up @@ -102,15 +102,15 @@ export const UserProvider: React.FC<UserProviderProps> = ({ fallback: Fallback,
*
*/
export const useUser = <
FunctionsFactoryType extends Realm.DefaultFunctionsFactory,
CustomDataType extends Record<string, unknown>,
UserProfileDataType extends Realm.DefaultUserProfileData,
FunctionsFactoryType extends Realm.DefaultFunctionsFactory = DefaultFunctionsFactory,
CustomDataType extends Record<string, unknown> = Record<string, unknown>,
UserProfileDataType extends Realm.DefaultUserProfileData = Realm.DefaultUserProfileData,
>(): Realm.User<FunctionsFactoryType, CustomDataType, UserProfileDataType> => {
const user = useContext(UserContext);

if (!user) {
throw new Error("No user found. Did you forget to wrap your component in a <UserProvider>?");
}

return user as Realm.User<FunctionsFactoryType, CustomDataType, UserProfileDataType>;
return user;
};
2 changes: 1 addition & 1 deletion packages/realm-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "Bundler",
"lib": [
"es2021",
],
Expand All @@ -10,7 +11,6 @@
"react"
],
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react",
"sourceMap": true,
"outDir": "./dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/deprecated-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
//
////////////////////////////////////////////////////////////////////////////

import * as ns from "./namespace";
import { Realm as RealmConstructor } from "./Realm";
import { safeGlobalThis } from "./safeGlobalThis";
import { flags } from "./flags";
import * as ns from "./namespace";

let warnedAboutGlobalRealmUse = false;
Object.defineProperty(safeGlobalThis, "Realm", {
Expand Down
22 changes: 22 additions & 0 deletions packages/realm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 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 } from "./Realm";
export = Realm;

import "./deprecated-global";
2 changes: 0 additions & 2 deletions packages/realm/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,3 @@ export * from "./ProgressRealmPromise";

export * from "./Unmanaged";
export * from "./schema";

import "./deprecated-global";

0 comments on commit 4ede85e

Please sign in to comment.