-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d785a59
commit 2f0a7a5
Showing
11 changed files
with
168 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// 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 { createContext } from "react"; | ||
import Realm from "realm"; | ||
|
||
import { RealmProvider, createRealmProvider } from "./RealmProvider"; | ||
import { UseObjectHook, createUseObject } from "./useObject"; | ||
import { UseQueryHook, createUseQuery } from "./useQuery"; | ||
import { UseRealmHook, createUseRealm } from "./useRealm"; | ||
|
||
export type RealmContext = { | ||
RealmProvider: RealmProvider; | ||
useRealm: UseRealmHook; | ||
useQuery: UseQueryHook; | ||
useObject: UseObjectHook; | ||
}; | ||
|
||
/** | ||
* Creates Realm React hooks and Provider component for a given Realm configuration | ||
* @example | ||
* ``` | ||
*class Task extends Realm.Object { | ||
* ... | ||
* | ||
* static schema = { | ||
* name: 'Task', | ||
* primaryKey: '_id', | ||
* properties: { | ||
* ... | ||
* }, | ||
* }; | ||
*} | ||
* | ||
*const {useRealm, useQuery, useObject, RealmProvider} = createRealmContext({schema: [Task]}); | ||
* ``` | ||
* @param realmConfig - {@link Realm.Configuration} used to open the Realm | ||
* @returns An object containing a `RealmProvider` component, and `useRealm`, `useQuery` and `useObject` hooks | ||
*/ | ||
export const createRealmContext: (realmConfig?: Realm.Configuration) => RealmContext = ( | ||
realmConfig: Realm.Configuration = {}, | ||
) => { | ||
const RealmContext = createContext<Realm | null>(null); | ||
const RealmProvider = createRealmProvider(realmConfig, RealmContext); | ||
|
||
const useRealm = createUseRealm(RealmContext); | ||
const useQuery = createUseQuery(useRealm); | ||
const useObject = createUseObject(useRealm); | ||
|
||
return { | ||
RealmProvider, | ||
useRealm, | ||
useQuery, | ||
useObject, | ||
}; | ||
}; |
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
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
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
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
Oops, something went wrong.