Skip to content

Commit

Permalink
updated: dependencies and add the lazy.export method
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Mar 1, 2020
1 parent 00fc83f commit e01ece2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 96 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -18,6 +18,7 @@ npm i -S @riotjs/lazy

## Documentation

The following examples show how you can lazy load Riot.js components using modern javascript bundlers like [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/).

You can lazy load any component providing a fallback component during the loading process for example:

Expand Down
87 changes: 46 additions & 41 deletions index.next.js
Expand Up @@ -6,56 +6,61 @@ const cache = new WeakMap()
// expose the cache as static property
lazy.cache = cache

export default function lazy(Loader, Component) {
// static attribute in case we want to just export a lazy riot component
lazy.export = function lazyExport(Loader, Component) {
// it could be that the user don't want to use a loader for whatever reason
const hasLoader = Loader && Component
const LazyComponent = hasLoader ? Component : Loader
const load = () => typeof LazyComponent === 'function' ? LazyComponent() : Promise.resolve(LazyComponent)
const cachedComponent = cache.get(LazyComponent)

return {
name: 'lazy',
exports: pure(({ slots, attributes, props }) => ({
mount(el, parentScope) {
this.el = el
this.isMounted = true
const mount = () => this.mountLazyComponent(parentScope)
return pure(({ slots, attributes, props }) => ({
mount(el, parentScope) {
this.el = el
this.isMounted = true
const mount = () => this.mountLazyComponent(parentScope)

if (cachedComponent) {
mount()
} else {
if (hasLoader) this.createManagedComponent(Loader, parentScope)

if (cachedComponent) {
load().then(data => {
cache.set(LazyComponent, data.default || data)
mount()
} else {
if (hasLoader) this.createManagedComponent(Loader, parentScope)

load().then(data => {
cache.set(LazyComponent, data.default || data)
mount()
})
}
},
createManagedComponent(Child, parentScope) {
this.component = component(Child)(this.el, props, {
attributes, slots, parentScope
})
},
mountLazyComponent(parentScope) {
// if this component was unmounted just return here
if (!this.isMounted) return

// unmount the loader if it was previously created
// notice the true flat to keep the root node
if (this.component) this.component.unmount(true)

// replace the old component instance with the new lazy loaded component
this.createManagedComponent(cache.get(LazyComponent), parentScope)
},
update(parentScope) {
if (this.isMounted && this.component) this.component.update({}, parentScope)
},
unmount(...args) {
this.isMounted = false

if (this.component) this.component.unmount(...args)
}
}))
},
createManagedComponent(Child, parentScope) {
this.component = component(Child)(this.el, props, {
attributes, slots, parentScope
})
},
mountLazyComponent(parentScope) {
// if this component was unmounted just return here
if (!this.isMounted) return

// unmount the loader if it was previously created
// notice the true flat to keep the root node
if (this.component) this.component.unmount(true)

// replace the old component instance with the new lazy loaded component
this.createManagedComponent(cache.get(LazyComponent), parentScope)
},
update(parentScope) {
if (this.isMounted && this.component) this.component.update({}, parentScope)
},
unmount(...args) {
this.isMounted = false

if (this.component) this.component.unmount(...args)
}
}))
}

export default function lazy(Loader, Component) {
return {
name: 'lazy',
exports: lazy.export(Loader, Component)
}
}
101 changes: 50 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -27,12 +27,12 @@
"eslint": "^6.8.0",
"eslint-config-riot": "^3.0.0",
"esm": "^3.2.25",
"jsdom": "16.1.0",
"jsdom": "16.2.0",
"jsdom-global": "3.0.2",
"mocha": "^7.0.1",
"mocha": "^7.1.0",
"nyc": "^15.0.0",
"riot": "^4.8.9",
"rollup": "^1.31.0",
"riot": "^4.10.0",
"rollup": "^1.32.0",
"rollup-plugin-terser": "^5.2.0"
}
}

0 comments on commit e01ece2

Please sign in to comment.