index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. /* */
  4. // this will be preserved during build
  5. // $flow-disable-line
  6. var VueFactory = require('./factory');
  7. var instanceOptions = {};
  8. /**
  9. * Create instance context.
  10. */
  11. function createInstanceContext (
  12. instanceId,
  13. runtimeContext,
  14. data
  15. ) {
  16. if ( data === void 0 ) data = {};
  17. var weex = runtimeContext.weex;
  18. var instance = instanceOptions[instanceId] = {
  19. instanceId: instanceId,
  20. config: weex.config,
  21. document: weex.document,
  22. data: data
  23. };
  24. // Each instance has an independent `Vue` module instance
  25. var Vue = instance.Vue = createVueModuleInstance(instanceId, weex);
  26. // DEPRECATED
  27. var timerAPIs = getInstanceTimer(instanceId, weex.requireModule);
  28. var instanceContext = Object.assign({ Vue: Vue }, timerAPIs);
  29. Object.freeze(instanceContext);
  30. return instanceContext
  31. }
  32. /**
  33. * Destroy an instance with id. It will make sure all memory of
  34. * this instance released and no more leaks.
  35. */
  36. function destroyInstance (instanceId) {
  37. var instance = instanceOptions[instanceId];
  38. if (instance && instance.app instanceof instance.Vue) {
  39. try {
  40. instance.app.$destroy();
  41. instance.document.destroy();
  42. } catch (e) {}
  43. delete instance.document;
  44. delete instance.app;
  45. }
  46. delete instanceOptions[instanceId];
  47. }
  48. /**
  49. * Refresh an instance with id and new top-level component data.
  50. * It will use `Vue.set` on all keys of the new data. So it's better
  51. * define all possible meaningful keys when instance created.
  52. */
  53. function refreshInstance (
  54. instanceId,
  55. data
  56. ) {
  57. var instance = instanceOptions[instanceId];
  58. if (!instance || !(instance.app instanceof instance.Vue)) {
  59. return new Error(("refreshInstance: instance " + instanceId + " not found!"))
  60. }
  61. if (instance.Vue && instance.Vue.set) {
  62. for (var key in data) {
  63. instance.Vue.set(instance.app, key, data[key]);
  64. }
  65. }
  66. // Finally `refreshFinish` signal needed.
  67. instance.document.taskCenter.send('dom', { action: 'refreshFinish' }, []);
  68. }
  69. /**
  70. * Create a fresh instance of Vue for each Weex instance.
  71. */
  72. function createVueModuleInstance (
  73. instanceId,
  74. weex
  75. ) {
  76. var exports = {};
  77. VueFactory(exports, weex.document);
  78. var Vue = exports.Vue;
  79. var instance = instanceOptions[instanceId];
  80. // patch reserved tag detection to account for dynamically registered
  81. // components
  82. var weexRegex = /^weex:/i;
  83. var isReservedTag = Vue.config.isReservedTag || (function () { return false; });
  84. var isRuntimeComponent = Vue.config.isRuntimeComponent || (function () { return false; });
  85. Vue.config.isReservedTag = function (name) {
  86. return (!isRuntimeComponent(name) && weex.supports(("@component/" + name))) ||
  87. isReservedTag(name) ||
  88. weexRegex.test(name)
  89. };
  90. Vue.config.parsePlatformTagName = function (name) { return name.replace(weexRegex, ''); };
  91. // expose weex-specific info
  92. Vue.prototype.$instanceId = instanceId;
  93. Vue.prototype.$document = instance.document;
  94. // expose weex native module getter on subVue prototype so that
  95. // vdom runtime modules can access native modules via vnode.context
  96. Vue.prototype.$requireWeexModule = weex.requireModule;
  97. // Hack `Vue` behavior to handle instance information and data
  98. // before root component created.
  99. Vue.mixin({
  100. beforeCreate: function beforeCreate () {
  101. var options = this.$options;
  102. // root component (vm)
  103. if (options.el) {
  104. // set external data of instance
  105. var dataOption = options.data;
  106. var internalData = (typeof dataOption === 'function' ? dataOption() : dataOption) || {};
  107. options.data = Object.assign(internalData, instance.data);
  108. // record instance by id
  109. instance.app = this;
  110. }
  111. },
  112. mounted: function mounted () {
  113. var options = this.$options;
  114. // root component (vm)
  115. if (options.el && weex.document && instance.app === this) {
  116. try {
  117. // Send "createFinish" signal to native.
  118. weex.document.taskCenter.send('dom', { action: 'createFinish' }, []);
  119. } catch (e) {}
  120. }
  121. }
  122. });
  123. /**
  124. * @deprecated Just instance variable `weex.config`
  125. * Get instance config.
  126. * @return {object}
  127. */
  128. Vue.prototype.$getConfig = function () {
  129. if (instance.app instanceof Vue) {
  130. return instance.config
  131. }
  132. };
  133. return Vue
  134. }
  135. /**
  136. * DEPRECATED
  137. * Generate HTML5 Timer APIs. An important point is that the callback
  138. * will be converted into callback id when sent to native. So the
  139. * framework can make sure no side effect of the callback happened after
  140. * an instance destroyed.
  141. */
  142. function getInstanceTimer (
  143. instanceId,
  144. moduleGetter
  145. ) {
  146. var instance = instanceOptions[instanceId];
  147. var timer = moduleGetter('timer');
  148. var timerAPIs = {
  149. setTimeout: function () {
  150. var args = [], len = arguments.length;
  151. while ( len-- ) args[ len ] = arguments[ len ];
  152. var handler = function () {
  153. args[0].apply(args, args.slice(2));
  154. };
  155. timer.setTimeout(handler, args[1]);
  156. return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
  157. },
  158. setInterval: function () {
  159. var args = [], len = arguments.length;
  160. while ( len-- ) args[ len ] = arguments[ len ];
  161. var handler = function () {
  162. args[0].apply(args, args.slice(2));
  163. };
  164. timer.setInterval(handler, args[1]);
  165. return instance.document.taskCenter.callbackManager.lastCallbackId.toString()
  166. },
  167. clearTimeout: function (n) {
  168. timer.clearTimeout(n);
  169. },
  170. clearInterval: function (n) {
  171. timer.clearInterval(n);
  172. }
  173. };
  174. return timerAPIs
  175. }
  176. exports.createInstanceContext = createInstanceContext;
  177. exports.destroyInstance = destroyInstance;
  178. exports.refreshInstance = refreshInstance;