-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from kirillzyusko/feature/web-support
[RNBS-021] - implemented web-support
- Loading branch information
Showing
12 changed files
with
123 additions
and
35 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ docs | |
src | ||
ROADMAP.MD | ||
CONTRIBUTING.MD | ||
CHANGELOG.MD | ||
CODE_OF_CONDUCT.md | ||
node_modules | ||
|
||
# from docusaurus | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Features | ||
|
||
- [x] async import instead of require | ||
- [x] more platform support; | ||
- [ ] timeToInteraction feature | ||
- [ ] async import instead of require | ||
- [ ] add `preload` on rendered component (`Loadable.preload()` <=> `<Loadable />`) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,52 @@ | ||
import { mapLoadable } from './bundler'; | ||
import { RequireLoader, ImportLoader } from './interface'; | ||
|
||
const cache = {} as any; | ||
|
||
export const isCached = (componentName: string) => !!cache[componentName]; | ||
|
||
export const getComponent = (name: string) => { | ||
const DEPRECATED_API_MESSAGE = "You are using a deprecated API that will be removed in a future releases. Please consider using `loader` instead of `require`"; | ||
const ERROR_WHILE_LOADING = "An error occurred while lazy loading a component. Perhaps the path where you are trying to load the component does not exist? Stacktrace: "; | ||
|
||
// In react-native world call of `require` or `loader` will block thread (since it's sync operation) | ||
// As a result if screen is not loaded yet and you trigger a navigation - app will freeze for a time, | ||
// until screen is not loaded. That's why `setTimeout(..., 0)` code is used here. We simply call this | ||
// function in next event loop iteration. Such approach will not block transition animations. | ||
const nonBlockingLoader = (loader: RequireLoader | ImportLoader) => new Promise((resolve) => { | ||
setTimeout(async () => { | ||
try { | ||
const file = await loader(); | ||
resolve(file); | ||
} catch (e) { | ||
console.error(ERROR_WHILE_LOADING + e); | ||
// resolve it as a `null` - another error will be thrown | ||
// when it will evaluate `component[rest.extract]` | ||
resolve(null); | ||
} | ||
}, 0); | ||
}); | ||
|
||
export const getComponent = async (name: string) => { | ||
if (!isCached(name)) { | ||
const { require: load, ...rest } = mapLoadable[name]; | ||
const { require: load, loader, ...rest } = mapLoadable[name]; | ||
let component = null; | ||
|
||
if (loader) { | ||
component = await nonBlockingLoader(loader); | ||
} else if (load) { | ||
console.warn(DEPRECATED_API_MESSAGE); | ||
|
||
component = await nonBlockingLoader(load); | ||
} | ||
|
||
// @ts-ignore | ||
const component = load()[rest.extract]; | ||
cache[name] = { | ||
...rest, | ||
component, | ||
// @ts-ignore | ||
component: component[rest.extract], | ||
}; | ||
} | ||
|
||
return cache[name]; | ||
}; | ||
}; | ||
|
||
export const getComponentFromCache = (name: string) => cache[name]; |
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