index.ts 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2
  PageAction,
fxy060608's avatar
fxy060608 已提交
3 4
  ACTION_TYPE_PAGE_CREATE,
  ACTION_TYPE_PAGE_CREATED,
fxy060608's avatar
fxy060608 已提交
5
  ACTION_TYPE_CREATE,
fxy060608's avatar
fxy060608 已提交
6
  // ACTION_TYPE_INSERT,
fxy060608's avatar
fxy060608 已提交
7
  ACTION_TYPE_REMOVE,
fxy060608's avatar
fxy060608 已提交
8
  ACTION_TYPE_SET_ATTRIBUTE,
fxy060608's avatar
fxy060608 已提交
9
  ACTION_TYPE_REMOVE_ATTRIBUTE,
fxy060608's avatar
fxy060608 已提交
10
  ACTION_TYPE_ADD_EVENT,
fxy060608's avatar
fxy060608 已提交
11
  ACTION_TYPE_REMOVE_EVENT,
fxy060608's avatar
fxy060608 已提交
12
  ACTION_TYPE_SET_TEXT,
fxy060608's avatar
fxy060608 已提交
13
  PageCreateAction,
fxy060608's avatar
fxy060608 已提交
14
} from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
15 16 17
import { UniNodeJSONMinify } from 'packages/uni-shared/src/vdom/Node'
import { ACTION_TYPE_DICT, DictAction, Dictionary } from '../../../constants'
import { createGetDict, decodeNodeJson } from './decodeActions'
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24
import {
  $,
  createElement,
  onPageCreate,
  onPageCreated,
  onPageReady,
} from './page'
fxy060608's avatar
fxy060608 已提交
25
import { flushPostActionJobs } from './scheduler'
fxy060608's avatar
fxy060608 已提交
26

fxy060608's avatar
fxy060608 已提交
27
export function onVdSync(actions: (PageAction | DictAction)[]) {
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
  const firstAction = actions[0]
  // page create
  if (firstAction[0] === ACTION_TYPE_PAGE_CREATE) {
    onPageCreateSync(firstAction)
  } else {
    onPageReady(() => onPageUpdateSync(actions))
  }
}

function onPageCreateSync(action: PageCreateAction) {
  return onPageCreate(action[1])
}

function onPageUpdateSync(actions: (PageAction | DictAction)[]) {
fxy060608's avatar
fxy060608 已提交
42 43 44 45
  const dictAction = actions[0]
  const getDict = createGetDict(
    dictAction[0] === ACTION_TYPE_DICT ? (dictAction[1] as Dictionary) : []
  )
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52
  actions.forEach((action) => {
    switch (action[0]) {
      case ACTION_TYPE_PAGE_CREATE:
        return onPageCreate(action[1])
      case ACTION_TYPE_PAGE_CREATED:
        return onPageCreated()
      case ACTION_TYPE_CREATE:
fxy060608's avatar
fxy060608 已提交
53 54 55 56
        return createElement(
          action[1],
          getDict(action[2] as number),
          action[3],
fxy060608's avatar
fxy060608 已提交
57 58
          action[4],
          decodeNodeJson(getDict, action[5] as UniNodeJSONMinify)
fxy060608's avatar
fxy060608 已提交
59
        )
fxy060608's avatar
fxy060608 已提交
60 61
      // case ACTION_TYPE_INSERT:
      //   return $(action[1]).insert(action[2], action[3])
fxy060608's avatar
fxy060608 已提交
62 63 64
      case ACTION_TYPE_REMOVE:
        return $(action[1]).remove()
      case ACTION_TYPE_SET_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
65 66 67 68
        return $(action[1]).setAttr(
          getDict(action[2] as number),
          getDict(action[3] as number)
        )
fxy060608's avatar
fxy060608 已提交
69
      case ACTION_TYPE_REMOVE_ATTRIBUTE:
fxy060608's avatar
fxy060608 已提交
70
        return $(action[1]).removeAttr(getDict(action[2] as number))
fxy060608's avatar
fxy060608 已提交
71
      case ACTION_TYPE_ADD_EVENT:
fxy060608's avatar
fxy060608 已提交
72
        return $(action[1]).addEvent(getDict(action[2] as number), action[3])
fxy060608's avatar
fxy060608 已提交
73
      case ACTION_TYPE_REMOVE_EVENT:
fxy060608's avatar
fxy060608 已提交
74
        return $(action[1]).removeEvent(getDict(action[2] as number))
fxy060608's avatar
fxy060608 已提交
75
      case ACTION_TYPE_SET_TEXT:
fxy060608's avatar
fxy060608 已提交
76
        return $(action[1]).setText(getDict(action[2] as number))
fxy060608's avatar
fxy060608 已提交
77 78
    }
  })
fxy060608's avatar
fxy060608 已提交
79
  flushPostActionJobs()
fxy060608's avatar
fxy060608 已提交
80
}