refactor(core): adjust directory structure:
This commit is contained in:
parent
e07d36f44e
commit
aad62b65f5
20 changed files with 381 additions and 93 deletions
27
src/core/init/index.js
Normal file
27
src/core/init/index.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import config from '../config'
|
||||
import { initLifecycle, callHook } from './lifecycle'
|
||||
import { initRender } from '../render'
|
||||
import { initRoute } from '../route'
|
||||
import { initEvent } from '../event'
|
||||
import { initFetch } from '../fetch'
|
||||
import { isFn } from '../util/core'
|
||||
|
||||
export function initMixin (Docsify) {
|
||||
Docsify.prototype._init = function () {
|
||||
const vm = this
|
||||
vm._config = config || {}
|
||||
|
||||
initLifecycle(vm) // Init hooks
|
||||
initPlugin(vm) // Install plugins
|
||||
callHook(vm, 'init')
|
||||
initRender(vm) // Render base DOM
|
||||
initEvent(vm) // Bind events
|
||||
initRoute(vm) // Add hashchange eventListener
|
||||
initFetch(vm) // Fetch data
|
||||
callHook(vm, 'ready')
|
||||
}
|
||||
}
|
||||
|
||||
function initPlugin (vm) {
|
||||
[].concat(vm.config.plugins).forEach(fn => isFn(fn) && fn(vm.bindHook))
|
||||
}
|
||||
41
src/core/init/lifecycle.js
Normal file
41
src/core/init/lifecycle.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { noop } from '../util/core'
|
||||
|
||||
export function initLifecycle (vm) {
|
||||
const hooks = ['init', 'beforeEach', 'afterEach', 'doneEach', 'ready']
|
||||
|
||||
vm._hooks = {}
|
||||
vm.bindHook = {}
|
||||
hooks.forEach(hook => {
|
||||
const arr = vm._hooks[hook] = []
|
||||
vm._bindHook[hook] = fn => arr.push(fn)
|
||||
})
|
||||
}
|
||||
|
||||
export function callHook (vm, hook, data, next = noop) {
|
||||
let newData = data
|
||||
const queue = vm._hooks[hook]
|
||||
|
||||
const step = function (index) {
|
||||
const hook = queue[index]
|
||||
if (index >= queue.length) {
|
||||
next(newData)
|
||||
} else {
|
||||
if (typeof hook === 'function') {
|
||||
if (hook.length === 2) {
|
||||
hook(data, result => {
|
||||
newData = result
|
||||
step(index + 1)
|
||||
})
|
||||
} else {
|
||||
const result = hook(data)
|
||||
newData = result !== undefined ? result : newData
|
||||
step(index + 1)
|
||||
}
|
||||
} else {
|
||||
step(index + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
step(0)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue