-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MF-363 Application should update live in response to temporary config…
… changes (#17) https://issues.openmrs.org/browse/MF-363
- Loading branch information
Showing
7 changed files
with
178 additions
and
59 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,23 @@ | ||
import { Subject } from "rxjs"; | ||
import { useState, useCallback } from "react"; | ||
|
||
export const configCache = {}; | ||
export const configCacheNotifier = new Subject<null>(); | ||
|
||
export function invalidateConfigCache() { | ||
configCacheNotifier.next(); | ||
} | ||
|
||
export function clearConfigCache() { | ||
for (let member in configCache) { | ||
delete configCache[member]; | ||
} | ||
} | ||
|
||
export function useForceUpdate() { | ||
const [, setTick] = useState(0); | ||
const update = useCallback(() => { | ||
setTick((tick) => tick + 1); | ||
}, []); | ||
return update; | ||
} |
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 |
---|---|---|
@@ -1,39 +1,54 @@ | ||
import React from "react"; | ||
import { useContext, useEffect } from "react"; | ||
import { ModuleNameContext } from "@openmrs/esm-context"; | ||
import * as Config from "../module-config/module-config"; | ||
import { | ||
configCache, | ||
configCacheNotifier, | ||
useForceUpdate, | ||
} from "./config-cache"; | ||
|
||
let config = {}; | ||
let error; | ||
|
||
/** | ||
* Use this React Hook to obtain your module's configuration. | ||
*/ | ||
export function useConfig() { | ||
const moduleName = React.useContext(ModuleNameContext); | ||
const moduleName = useContext(ModuleNameContext); | ||
const forceUpdate = useForceUpdate(); | ||
|
||
if (!moduleName) { | ||
throw Error( | ||
"ModuleNameContext has not been provided. This should come from openmrs-react-root-decorator" | ||
); | ||
} | ||
|
||
function getConfigAndSetCache(moduleName: string) { | ||
return Config.getConfig(moduleName) | ||
.then((res) => { | ||
configCache[moduleName] = res; | ||
forceUpdate(); | ||
}) | ||
.catch((err) => { | ||
error = err; | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
const sub = configCacheNotifier.subscribe(() => { | ||
getConfigAndSetCache(moduleName); | ||
}); | ||
return () => sub.unsubscribe(); | ||
}, [moduleName]); | ||
|
||
if (error) { | ||
// Suspense will just keep calling useConfig if the thrown promise rejects. | ||
// So we check ahead of time and avoid creating a new promise. | ||
throw error; | ||
} | ||
if (!config[moduleName]) { | ||
if (!configCache[moduleName]) { | ||
// React will prevent the client component from rendering until the promise resolves | ||
throw Config.getConfig(moduleName) | ||
.then((res) => { | ||
config[moduleName] = res; | ||
}) | ||
.catch((err) => { | ||
error = err; | ||
}); | ||
throw getConfigAndSetCache(moduleName); | ||
} else { | ||
return config[moduleName]; | ||
return configCache[moduleName]; | ||
} | ||
} | ||
|
||
export function clearConfig() { | ||
config = {}; | ||
} |
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.