-
Notifications
You must be signed in to change notification settings - Fork 596
RJS-2648: Add ability for Realm instance to be used in RealmProvider #6714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
58ae94e
update 5 files
gagik 3347509
Merge branch 'main' of github.com:realm/realm-js into gagik/realm-ins…
gagik 776b6c5
renames in RealmProvider.tsx, RealmProvider.test.tsx and packages/rea…
gagik d2799d2
update RealmContext.ts, RealmProvider.tsx, RealmProvider.test.tsx and…
gagik 5e419b5
update RealmProvider.tsx and RealmProvider.test.tsx
gagik 58222e0
test: update RealmProvider.test.tsx
gagik 7756a56
docs: update CHANGELOG.md
gagik bf99a62
update CHANGELOG.md and RealmProvider.tsx
gagik 9788883
Merge branch 'main' of github.com:realm/realm-js into gagik/realm-ins…
gagik 88c9790
update RealmContext.ts and RealmProvider.tsx
gagik eabe2bd
rename flexible provider to generalized
gagik f454eae
changes based on review
gagik 9fde880
Update from review comments
gagik 73bff55
Update CHANGELOG.md
gagik 282f71a
PR changes
gagik bf4797a
Merge branch 'gagik/realm-instance-provider' of github.com:realm/real…
gagik 9f0366c
Reorganize and cleanup
gagik af1f579
update CHANGELOG.md and RealmProvider.tsx
gagik f024e9b
update RealmContext.ts, RealmProvider.tsx and packages/realm-react/sr…
gagik 91d28b3
Fix comment
gagik bb938f7
update packages/realm-react/src/index.tsx
gagik 39e486b
Merge branch 'main' of github.com:realm/realm-js into gagik/realm-ins…
gagik 0b7c9c8
Fix html
gagik dd0ab56
Merge branch 'main' of github.com:realm/realm-js into gagik/realm-ins…
gagik dda07c9
Add semicolon
gagik aff7d8a
Add errors with props
gagik e1b71e1
use less unrolling
gagik e5fc4c8
rename to configprops
gagik faeec83
update RealmProvider.tsx
gagik 8639d84
Update packages/realm-react/CHANGELOG.md
gagik 436ff07
More than 0
gagik c34b4a1
Merge branch 'gagik/realm-instance-provider' of github.com:realm/real…
gagik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,109 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 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 { createUseObject } from "./useObject"; | ||
import { createUseQuery } from "./useQuery"; | ||
import { createUseRealm } from "./useRealm"; | ||
import { GeneralizedRealmProviderFC } from "./RealmProvider"; | ||
|
||
export type RealmContext<RealmProvider = GeneralizedRealmProviderFC> = { | ||
/** | ||
* The Provider component that is required to wrap any component using | ||
* the Realm hooks. | ||
* @example | ||
* ``` | ||
* const AppRoot = () => { | ||
* const syncConfig = { | ||
* flexible: true, | ||
* user: currentUser | ||
* }; | ||
* | ||
* return ( | ||
* <RealmProvider schema={[Task, User]} path={"data.realm"} sync={syncConfig}> | ||
* <App/> | ||
* </RealmProvider> | ||
* ) | ||
* } | ||
* ``` | ||
* @param props - The {@link Realm.Configuration} or {@link Realm} of the provider | ||
* are set based on the options passed to `createRealmProvider`. When using a | ||
* {@link Realm.Configuration}, individual config keys can be overridden when | ||
* creating a `<RealmProvider>` by passing them as props. For example, to override | ||
* the `path` config value, use a prop named `path` e.g., `path="newPath.realm"` an | ||
* attribute of the same key. | ||
*/ | ||
RealmProvider: RealmProvider; | ||
/** | ||
* Returns the instance of the {@link Realm} opened by the `RealmProvider`. | ||
* @example | ||
* ``` | ||
* const realm = useRealm(); | ||
* ``` | ||
* @returns a realm instance | ||
*/ | ||
useRealm: ReturnType<typeof createUseRealm>; | ||
|
||
/** | ||
* Returns a {@link Realm.Collection} of {@link Realm.Object}s from a given type. | ||
* The hook will update on any changes to any object in the collection | ||
* and return an empty array if the collection is empty. | ||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* The result of this can be consumed directly by the `data` argument of any React Native | ||
* VirtualizedList or FlatList. If the component used for the list's `renderItem` prop is {@link React.Memo}ized, | ||
* then only the modified object will re-render. | ||
* @example | ||
* ```tsx | ||
* // Return all collection items | ||
* const collection = useQuery({ type: Object }); | ||
* | ||
* // Return all collection items sorted by name and filtered by category | ||
* const filteredAndSorted = useQuery({ | ||
* type: Object, | ||
* query: (collection) => collection.filtered('category == $0',category).sorted('name'), | ||
* }, [category]); | ||
* | ||
* // Return all collection items sorted by name and filtered by category, triggering re-renders only if "name" changes | ||
* const filteredAndSorted = useQuery({ | ||
* type: Object, | ||
* query: (collection) => collection.filtered('category == $0',category).sorted('name'), | ||
* keyPaths: ["name"] | ||
* }, [category]); | ||
* ``` | ||
* @param options | ||
* @param options.type - The object type, depicted by a string or a class extending Realm.Object | ||
* @param options.query - A function that takes a {@link Realm.Collection} and returns a {@link Realm.Collection} of the same type. This allows for filtering and sorting of the collection, before it is returned. | ||
* @param options.keyPaths - Indicates a lower bound on the changes relevant for the hook. This is a lower bound, since if multiple hooks add listeners (each with their own `keyPaths`) the union of these key-paths will determine the changes that are considered relevant for all listeners registered on the collection. In other words: A listener might fire and cause a re-render more than the key-paths specify, if other listeners with different key-paths are present. | ||
* @param deps - An array of dependencies that will be passed to {@link React.useMemo} | ||
* @returns a collection of realm objects or an empty array | ||
*/ | ||
useQuery: ReturnType<typeof createUseQuery>; | ||
/** | ||
* Returns a {@link Realm.Object} from a given type and value of primary key. | ||
* The hook will update on any changes to the properties on the returned object | ||
* and return null if it either doesn't exists or has been deleted. | ||
* @example | ||
* ``` | ||
* const object = useObject(ObjectClass, objectId); | ||
* ``` | ||
* @param type - The object type, depicted by a string or a class extending {@link Realm.Object} | ||
* @param primaryKey - The primary key of the desired object which will be retrieved using {@link Realm.objectForPrimaryKey} | ||
* @param keyPaths - Indicates a lower bound on the changes relevant for the hook. This is a lower bound, since if multiple hooks add listeners (each with their own `keyPaths`) the union of these key-paths will determine the changes that are considered relevant for all listeners registered on the object. In other words: A listener might fire and cause a re-render more than the key-paths specify, if other listeners with different key-paths are present. | ||
* @returns either the desired {@link Realm.Object} or `null` in the case of it being deleted or not existing. | ||
*/ | ||
useObject: ReturnType<typeof createUseObject>; | ||
}; |
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.