Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ client.processLifecycleQueues = async function processLifecycleQueues() {
router._changed = false
}

if (module.hot) {
client.klasses = {}
}

export default client
1 change: 0 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import rerender from './rerender'
import router from './router'
import settings from './settings'
import state from './state'
import windowEvent from './windowEvent'
import worker from './worker'

context.page = page
Expand Down
19 changes: 19 additions & 0 deletions client/lazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import LazyComponent from "../shared/lazyComponent"

let next = null
const queue = []

async function preload() {
let importer = queue.pop()
await importer()
requestIdleCallback(preload)
}

export default function lazy(_hash, importer) {
queue.push(importer)
cancelIdleCallback(next)
preload()
return class extends LazyComponent {
importer = importer
}
}
3 changes: 3 additions & 0 deletions client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import invoke from './invoke'
import context from './context'
import windowEvent from './windowEvent'
import client from './client'
import lazy from './lazy'

const $runtime = {
element,
fragment,
invoke,
lazy
}

if (module.hot) {
Expand Down Expand Up @@ -49,6 +51,7 @@ if (module.hot) {
}
}
}
client.klasses[declaration.klass.hash] = declaration.klass
declaration.klass.__hashes = declaration.hashes
}
windowEvent('environment')
Expand Down
6 changes: 6 additions & 0 deletions loaders/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (source, map) {
if (this.resourcePath.includes('ServerFunctions.njs')) {
require('fs').writeFileSync('debug.njs', source)
}
this.callback(null, source, map)
}
18 changes: 18 additions & 0 deletions server/lazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import LazyComponent from "../shared/lazyComponent"

const queue = {}

export default function lazy(hash, importer) {
queue[hash] = importer
return class extends LazyComponent {
importer = importer
}
}

export async function load(hash) {
const fileHash = module.hot ? hash.split('___')[0] : hash.slice(0, 8)
if (!queue[fileHash]) return
const importer = queue[fileHash]
delete queue[fileHash]
return importer()
}
4 changes: 3 additions & 1 deletion server/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default registry
import reqres from "./reqres"
import { generateContext } from "./context"
import Nullstack from '.'
import { load } from "./lazy"

export function register(klass, functionName) {
if (functionName) {
Expand All @@ -27,13 +28,14 @@ function bindStaticProps(klass) {
if (!klass[propName]) {
klass[propName] = klass[prop]
}
function _invoke(...args) {
async function _invoke(...args) {
if (underscored) {
return klass[propName].call(klass, ...args)
}
const params = args[0] || {}
const { request, response } = reqres
const subcontext = generateContext({ request, response, ...params })
await load(klass.hash)
return klass[propName].call(klass, subcontext)
}
if (module.hot) {
Expand Down
4 changes: 3 additions & 1 deletion server/runtime.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import element from '../shared/element'
import fragment from '../shared/fragment'
import { register } from './registry'
import lazy from './lazy'
import Nullstack from './index'

const $runtime = {
element,
fragment,
register
register,
lazy
}

if (module.hot) {
Expand Down
7 changes: 5 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import reqres from './reqres'
import generateRobots from './robots'
import template from './template'
import { generateServiceWorker } from './worker'
import { load } from './lazy'

const server = express()

Expand Down Expand Up @@ -124,6 +125,7 @@ server.start = function () {
const { hash, methodName } = request.params
const [invokerHash, boundHash] = hash.split('-')
const key = `${invokerHash}.${methodName}`
await load(boundHash || invokerHash)
const invokerKlass = registry[invokerHash]
let boundKlass = invokerKlass
if (boundHash) {
Expand Down Expand Up @@ -159,6 +161,7 @@ server.start = function () {
const [invokerHash, boundHash] = hash.split('-')
const key = `${invokerHash}.${methodName}`
let invokerKlass;
await load(boundHash || invokerHash)
async function reply() {
let boundKlass = invokerKlass
if (boundHash) {
Expand Down Expand Up @@ -186,10 +189,10 @@ server.start = function () {
}
async function delay() {
invokerKlass = registry[invokerHash]
if (invokerKlass.__hashes[methodName] !== version) {
if (invokerKlass && invokerKlass.__hashes[methodName] !== version) {
setTimeout(() => {
delay()
}, 200)
}, 0)
} else {
reply()
}
Expand Down
9 changes: 8 additions & 1 deletion shared/generateTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ async function generateBranch(siblings, node, depth, scope) {

if (isClass(node)) {
const key = generateKey(scope, node, depth)
const instance = scope.instances[key] || new node.type(scope)
let instance = scope.instances[key]
if (!instance) {
if (module.hot && node.type.hash) {
instance = new scope.klasses[node.type.hash](scope)
} else {
instance = new node.type(scope)
}
}
instance.persistent = !!node.attributes.persistent
instance.key = key
instance._attributes = node.attributes
Expand Down
25 changes: 25 additions & 0 deletions shared/lazyComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import element from './element'

class LazyComponent {

constructor(scope) {
this.server = scope.context.server
}

async initiate() {
this.component = (await this.importer()).default
}

render() {
if (!this.component) return false
const { key, ...attributes } = this._attributes
return element(this.component, attributes)
}

toJSON() {
return null
}

}

export default LazyComponent
Loading