Skip to content

Commit

Permalink
Avoid circular imports (#6814)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen authored Aug 7, 2024
1 parent 780eb13 commit 9364039
Show file tree
Hide file tree
Showing 111 changed files with 2,419 additions and 1,495 deletions.
799 changes: 735 additions & 64 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,8 @@
"typedoc": "^0.25.7",
"typescript": "5.0.4",
"wireit": "^0.14.4"
},
"devDependencies": {
"madge": "^7.0.0"
}
}
}
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
1 change: 1 addition & 0 deletions packages/realm/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-duplicate-enum-values": "off",
"@typescript-eslint/consistent-type-imports": "error",
"no-console": "error",
"sort-imports": ["warn", { "ignoreDeclarationSort": true }],
"jsdoc/check-tag-names": "off",
Expand Down
11 changes: 11 additions & 0 deletions packages/realm/.madgerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"detectiveOptions": {
"ts": {
"skipTypeImports": true
}
},
"excludeRegExp": [
"namespace\\.ts",
"deprecated-global\\.ts"
]
}
13 changes: 12 additions & 1 deletion packages/realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"build:node": "wireit",
"bindgen:jsi": "wireit",
"check-types": "wireit",
"check-circular-imports": "wireit",
"install": "prebuild-install --runtime napi || echo 'Failed to download prebuild for Realm'",
"docs": "wireit",
"postinstall": "node ./scripts/submit-analytics.js"
Expand Down Expand Up @@ -296,6 +297,16 @@
"build:ts"
]
},
"check-circular-imports": {
"command": "madge --circular --extensions ts src",
"dependencies": [
"../fetch:build",
"bindgen:generate:typescript",
"bindgen:generate:node-wrapper",
"bindgen:generate:react-native-wrapper",
"bindgen:transpile"
]
},
"docs": {
"command": "typedoc",
"dependencies": [
Expand Down Expand Up @@ -357,4 +368,4 @@
6
]
}
}
}
18 changes: 7 additions & 11 deletions packages/realm/src/ClassHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
//
////////////////////////////////////////////////////////////////////////////

import {
CanonicalObjectSchema,
DefaultObject,
INTERNAL_HELPERS,
PropertyMap,
RealmObject,
RealmObjectConstructor,
binding,
} from "./internal";
import type { binding } from "../binding";
import type { CanonicalObjectSchema, DefaultObject, RealmObjectConstructor } from "./schema";
import type { PropertyMap } from "./PropertyMap";
import type { RealmObject } from "./Object";
import { OBJECT_HELPERS } from "./symbols";

type ObjectWrapper = (obj: binding.Obj) => (RealmObject & DefaultObject) | null;

Expand All @@ -40,7 +36,7 @@ export type ClassHelpers = {
/** @internal */
export function setClassHelpers(constructor: RealmObjectConstructor, value: ClassHelpers): void {
// Store the properties map on the object class
Object.defineProperty(constructor, INTERNAL_HELPERS, {
Object.defineProperty(constructor, OBJECT_HELPERS, {
enumerable: false,
writable: false,
configurable: false,
Expand All @@ -56,7 +52,7 @@ export function setClassHelpers(constructor: RealmObjectConstructor, value: Clas
* @internal
*/
export function getClassHelpers(arg: typeof RealmObject): ClassHelpers {
const helpers = arg[INTERNAL_HELPERS];
const helpers = arg[OBJECT_HELPERS];
if (helpers) {
return helpers as ClassHelpers;
} else {
Expand Down
29 changes: 10 additions & 19 deletions packages/realm/src/ClassMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,13 @@
//
////////////////////////////////////////////////////////////////////////////

import {
CanonicalObjectSchema,
Constructor,
INTERNAL,
KEY_ARRAY,
KEY_SET,
PropertyMap,
REALM,
Realm,
RealmObject,
RealmObjectConstructor,
assert,
binding,
getClassHelpers,
setClassHelpers,
} from "./internal";
import type { CanonicalObjectSchema, Constructor, RealmObjectConstructor } from "./schema";
import type { binding } from "../binding";
import { PropertyMap } from "./PropertyMap";
import { KEY_ARRAY, KEY_SET, RealmObject } from "./Object";
import { assert } from "./assert";
import { getClassHelpers, setClassHelpers } from "./ClassHelpers";
import { OBJECT_INTERNAL, OBJECT_REALM } from "./symbols";

/** @internal */
export class ClassMap {
Expand Down Expand Up @@ -81,15 +72,15 @@ export class ClassMap {
Object.defineProperty(constructor.prototype, propertyName, {
enumerable: true,
get(this: RealmObject) {
return get(this[INTERNAL]);
return get(this[OBJECT_INTERNAL]);
},
set(this: RealmObject, value: unknown) {
set(this[INTERNAL], value);
set(this[OBJECT_INTERNAL], value);
},
});
}

Object.defineProperty(constructor.prototype, REALM, {
Object.defineProperty(constructor.prototype, OBJECT_REALM, {
enumerable: false,
configurable: false,
writable: false,
Expand Down
27 changes: 17 additions & 10 deletions packages/realm/src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
//
////////////////////////////////////////////////////////////////////////////

import type {
Dictionary,
DictionaryAccessor,
List,
OrderedCollectionAccessor,
RealmSet,
Results,
TypeHelpers,
} from "./internal";
import { CallbackAdder, IllegalConstructorError, Listeners, TypeAssertionError, assert, binding } from "./internal";
import type { binding } from "../binding";
import { injectIndirect } from "./indirect";
import type { Dictionary } from "./Dictionary";
import type { List } from "./List";
import type { OrderedCollectionAccessor } from "./OrderedCollection";
import type { RealmSet } from "./Set";
import type { Results } from "./Results";
import type { TypeHelpers } from "./TypeHelpers";
import { type CallbackAdder, Listeners } from "./Listeners";
import { IllegalConstructorError, type TypeAssertionError } from "./errors";
import { assert } from "./assert";
import type { DictionaryAccessor } from "./collection-accessors/Dictionary";

/**
* Collection accessor identifier.
Expand Down Expand Up @@ -198,3 +200,8 @@ export abstract class Collection<
this.listeners.removeAll();
}
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- We define these once to avoid using "any" through the code */
export type AnyCollection = Collection<any, any, any, any, any>;

injectIndirect("Collection", Collection);
20 changes: 9 additions & 11 deletions packages/realm/src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
//
////////////////////////////////////////////////////////////////////////////

import type {
AnyRealmObject,
AppConfiguration,
ObjectSchema,
Realm,
RealmObjectConstructor,
SyncConfiguration,
User,
} from "./internal";

import { TypeAssertionError, assert, validateRealmSchema, validateSyncConfiguration } from "./internal";
import type { AnyRealmObject } from "./Object";
import type { AppConfiguration } from "./app-services/App";
import type { User } from "./app-services/User";
import type { ObjectSchema, RealmObjectConstructor } from "./schema";
import type { Realm } from "./Realm";
import { TypeAssertionError } from "./errors";
import { assert } from "./assert";
import { validateRealmSchema } from "./schema";
import { type SyncConfiguration, validateSyncConfiguration } from "./app-services/SyncConfiguration";

/**
* A function which can be called to migrate a Realm from one version of the schema to another.
Expand Down
8 changes: 6 additions & 2 deletions packages/realm/src/Counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
//
////////////////////////////////////////////////////////////////////////////

import { IllegalConstructorError, Realm, UpdateMode, assert, binding } from "./internal";
import { binding } from "../binding";
import { assert } from "./assert";
import { IllegalConstructorError } from "./errors";
import type { Realm } from "./Realm";
import type { UpdateMode } from "./Object";

const REALM = Symbol("Counter#realm");
const OBJ = Symbol("Counter#obj");
Expand Down Expand Up @@ -84,7 +88,7 @@ export class Counter {

/** @internal */
constructor(realm: Realm, obj: binding.Obj, columnKey: binding.ColKey) {
if (!(realm instanceof Realm) || !(obj instanceof binding.Obj)) {
if (!(obj instanceof binding.Obj)) {
throw new IllegalConstructorError("Counter");
}

Expand Down
Loading

0 comments on commit 9364039

Please sign in to comment.