1 |
|
2 |
|
3 |
|
4 |
|
5 | const VueFactory = require('./factory')
|
6 |
|
7 | const instanceOptions: { [key: string]: WeexInstanceOption } = {}
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export function createInstanceContext (
|
13 | instanceId: string,
|
14 | runtimeContext: WeexRuntimeContext,
|
15 | data: Object = {}
|
16 | ): WeexInstanceContext {
|
17 | const weex: Weex = runtimeContext.weex
|
18 | const instance: WeexInstanceOption = instanceOptions[instanceId] = {
|
19 | instanceId,
|
20 | config: weex.config,
|
21 | document: weex.document,
|
22 | data
|
23 | }
|
24 |
|
25 |
|
26 | const Vue = instance.Vue = createVueModuleInstance(instanceId, weex)
|
27 |
|
28 |
|
29 | const timerAPIs = getInstanceTimer(instanceId, weex.requireModule)
|
30 |
|
31 | const instanceContext = Object.assign({ Vue }, timerAPIs)
|
32 | Object.freeze(instanceContext)
|
33 | return instanceContext
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export function destroyInstance (instanceId: string): void {
|
41 | const instance = instanceOptions[instanceId]
|
42 | if (instance && instance.app instanceof instance.Vue) {
|
43 | try {
|
44 | instance.app.$destroy()
|
45 | instance.document.destroy()
|
46 | } catch (e) {}
|
47 | delete instance.document
|
48 | delete instance.app
|
49 | }
|
50 | delete instanceOptions[instanceId]
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export function refreshInstance (
|
59 | instanceId: string,
|
60 | data: Object
|
61 | ): Error | void {
|
62 | const instance = instanceOptions[instanceId]
|
63 | if (!instance || !(instance.app instanceof instance.Vue)) {
|
64 | return new Error(`refreshInstance: instance ${instanceId} not found!`)
|
65 | }
|
66 | if (instance.Vue && instance.Vue.set) {
|
67 | for (const key in data) {
|
68 | instance.Vue.set(instance.app, key, data[key])
|
69 | }
|
70 | }
|
71 |
|
72 | instance.document.taskCenter.send('dom', { action: 'refreshFinish' }, [])
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function createVueModuleInstance (
|
79 | instanceId: string,
|
80 | weex: Weex
|
81 | ): GlobalAPI {
|
82 | const exports = {}
|
83 | VueFactory(exports, weex.document)
|
84 | const Vue = exports.Vue
|
85 |
|
86 | const instance = instanceOptions[instanceId]
|
87 |
|
88 |
|
89 |
|
90 | const weexRegex = /^weex:/i
|
91 | const isReservedTag = Vue.config.isReservedTag || (() => false)
|
92 | const isRuntimeComponent = Vue.config.isRuntimeComponent || (() => false)
|
93 | Vue.config.isReservedTag = name => {
|
94 | return (!isRuntimeComponent(name) && weex.supports(`@component/${name}`)) ||
|
95 | isReservedTag(name) ||
|
96 | weexRegex.test(name)
|
97 | }
|
98 | Vue.config.parsePlatformTagName = name => name.replace(weexRegex, '')
|
99 |
|
100 |
|
101 | Vue.prototype.$instanceId = instanceId
|
102 | Vue.prototype.$document = instance.document
|
103 |
|
104 |
|
105 |
|
106 | Vue.prototype.$requireWeexModule = weex.requireModule
|
107 |
|
108 |
|
109 |
|
110 | Vue.mixin({
|
111 | beforeCreate () {
|
112 | const options = this.$options
|
113 |
|
114 | if (options.el) {
|
115 |
|
116 | const dataOption = options.data
|
117 | const internalData = (typeof dataOption === 'function' ? dataOption() : dataOption) || {}
|
118 | options.data = Object.assign(internalData, instance.data)
|
119 |
|
120 | instance.app = this
|
121 | }
|
122 | },
|
123 | mounted () {
|
124 | const options = this.$options
|
125 |
|
126 | if (options.el && weex.document && instance.app === this) {
|
127 | try {
|
128 |
|
129 | weex.document.taskCenter.send('dom', { action: 'createFinish' }, [])
|
130 | } catch (e) {}
|
131 | }
|
132 | }
|
133 | })
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | Vue.prototype.$getConfig = function () {
|
141 | if (instance.app instanceof Vue) {
|
142 | return instance.config
|
143 | }
|
144 | }
|
145 |
|
146 | return Vue
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | function getInstanceTimer (
|
157 | instanceId: string,
|
158 | moduleGetter: Function
|
159 | ): Object {
|
160 | const instance = instanceOptions[instanceId]
|
161 | const timer = moduleGetter('timer')
|
162 | const timerAPIs = {
|
163 | setTimeout: (...args) => {
|
164 | const handler = function () {
|
165 | args[0](...args.slice(2))
|
166 | }
|
167 |
|
168 | timer.setTimeout(handler, args[1])
|
169 | return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
|
170 | },
|
171 | setInterval: (...args) => {
|
172 | const handler = function () {
|
173 | args[0](...args.slice(2))
|
174 | }
|
175 |
|
176 | timer.setInterval(handler, args[1])
|
177 | return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
|
178 | },
|
179 | clearTimeout: (n) => {
|
180 | timer.clearTimeout(n)
|
181 | },
|
182 | clearInterval: (n) => {
|
183 | timer.clearInterval(n)
|
184 | }
|
185 | }
|
186 | return timerAPIs
|
187 | }
|