Skip to content

Commit

Permalink
Fixed lint error and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kal-Aster committed Nov 16, 2020
1 parent 5df881e commit 2dae85f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -43,6 +43,37 @@ You can lazy load any component providing a fallback component during the loadin
</app>
```

When the component is loaded, Lazy will dispatch a 'load' event from the component root element.

This can be useful e.g. for removing splashscreen on app start:

```riot
<app>
<user name={state.name} onload={ removeSplashscreen }/>
<script>
import lazy from '@riotjs/lazy'
import Loader from './my-loader.riot'
export default {
components: {
// use a fallback loader
user: lazy(Loader, () => import('./user.riot'))
},
removeSplashscreen() {
// the splashscreen, in this example, is create in pure html
// in the main page, to avoid blank page loading
const splashscreen = document.querySelector("#splashscreen");
if (!splashscreen) {
return;
}
splashcreen.parentElement.removeChild(splashscreen);
}
}
</script>
</app>
```

Lazy loading Riot.js components is recommended combined with [`@riotjs/route`](https://github.com/riot/route)

```riot
Expand Down
2 changes: 1 addition & 1 deletion index.next.js
Expand Up @@ -20,7 +20,7 @@ lazy.export = function lazyExport(Loader, Component) {
this.isMounted = true
const mount = () => {
this.mountLazyComponent(parentScope)
this.el.dispatchEvent(new Event("load"))
this.el.dispatchEvent(new Event('load'))
}

if (cachedComponent) {
Expand Down
18 changes: 18 additions & 0 deletions test/components/component-with-load-listener.riot
@@ -0,0 +1,18 @@
<component-with-load-listener>
<user name={props.name} onload={ loaded }/>

<script>
import User from './user'
import lazy from '../../index.next'
export default {
components: {
user: lazy(() => Promise.resolve(User))
},
_loaded: false,
loaded() {
this._loaded = true;
}
}
</script>
</component-with-load-listener>
1 change: 0 additions & 1 deletion test/components/user-wrapper-without-loader.riot
Expand Up @@ -2,7 +2,6 @@
<user name={props.name}/>

<script>
import Loader from './loader'
import User from './user'
import lazy from '../../index.next'
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
@@ -1,3 +1,4 @@
import ComponentWithLoadListener from './components/component-with-load-listener.riot'
import ComponentWithSlotWrapper from './components/component-with-slot-wrapper.riot'
import UserWrapper from './components/user-wrapper.riot'
import UserWrapperWithoutLoader from './components/user-wrapper-without-loader.riot'
Expand Down Expand Up @@ -60,4 +61,17 @@ describe('lazy', () => {

el.unmount()
})

it('Lazy loaded component can dispatch load event', async function() {
const div = document.createElement('div')
const el = component(ComponentWithLoadListener)(div, {
name: 'Kal'
})

await defer()

expect(el._loaded).to.be.equal(true)

el.unmount()
})
})

0 comments on commit 2dae85f

Please sign in to comment.