index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as Vue from '../../../packages/weex-vue-framework'
  2. import { compile } from '../../../packages/weex-template-compiler'
  3. import { Runtime, Instance } from 'weex-vdom-tester'
  4. import { config } from 'weex-js-runtime'
  5. // http://stackoverflow.com/a/35478115
  6. const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g
  7. export function strToRegExp (str) {
  8. return new RegExp(str.replace(matchOperatorsRe, '\\$&'))
  9. }
  10. export function compileAndStringify (template) {
  11. const { render, staticRenderFns } = compile(template)
  12. return {
  13. render: `function () { ${render} }`,
  14. staticRenderFns: parseStatic(staticRenderFns)
  15. }
  16. }
  17. function parseStatic (fns) {
  18. return '[' + fns.map(fn => `function () { ${fn} }`).join(',') + ']'
  19. }
  20. export function prepareRuntime () {
  21. let sendTasksHandler = function () {}
  22. config.sendTasks = config.Document.handler = function () {
  23. sendTasksHandler.apply(null, arguments)
  24. }
  25. Vue.init(config)
  26. const runtime = new Runtime(Vue)
  27. sendTasksHandler = function () {
  28. runtime.target.callNative.apply(runtime.target, arguments)
  29. }
  30. return runtime
  31. }
  32. export function resetRuntime () {
  33. delete config.Document.handler
  34. Vue.reset()
  35. }
  36. export function createInstance (runtime, code) {
  37. const instance = new Instance(runtime)
  38. if (code) {
  39. instance.$create(code)
  40. }
  41. return instance
  42. }
  43. export function syncPromise (arr) {
  44. let p = Promise.resolve()
  45. arr.forEach(item => {
  46. p = p.then(item)
  47. })
  48. return p
  49. }
  50. export function checkRefresh (instance, data, checker) {
  51. return () => new Promise(res => {
  52. instance.$refresh(data)
  53. setTimeout(() => {
  54. checker(instance.getRealRoot())
  55. res()
  56. })
  57. })
  58. }