1 | import _ from 'lodash';
|
2 | import Vue from 'vue';
|
3 |
|
4 |
|
5 | let vue;
|
6 | let lastDigestRequest = 0, digestInProgress = false;
|
7 | const bareDigest = function() {
|
8 | if (vue.digestRequest > lastDigestRequest) return;
|
9 | vue.digestRequest = lastDigestRequest + 1;
|
10 | };
|
11 |
|
12 | const angularProxy = {
|
13 | active: typeof window !== 'undefined' && window.angular
|
14 | };
|
15 |
|
16 | if (angularProxy.active) {
|
17 | initAngular();
|
18 | } else {
|
19 | _.forEach(['digest', 'watch', 'defineModule', 'debounceDigest'], method => {
|
20 | angularProxy[method] = _.noop;
|
21 | });
|
22 | }
|
23 |
|
24 | function initAngular() {
|
25 | const module = window.angular.module('firetruss', []);
|
26 | angularProxy.digest = bareDigest;
|
27 | angularProxy.watch = function() {throw new Error('Angular watch proxy not yet initialized');};
|
28 | angularProxy.defineModule = function(Truss) {
|
29 | module.constant('Truss', Truss);
|
30 | };
|
31 | angularProxy.debounceDigest = function(wait) {
|
32 | if (wait) {
|
33 | const debouncedDigest = _.debounce(bareDigest, wait);
|
34 | angularProxy.digest = function() {
|
35 | if (vue.digestRequest > lastDigestRequest) return;
|
36 | if (digestInProgress) bareDigest(); else debouncedDigest();
|
37 | };
|
38 | } else {
|
39 | angularProxy.digest = bareDigest;
|
40 | }
|
41 | };
|
42 |
|
43 | module.config(['$provide', function($provide) {
|
44 | $provide.decorator('$rootScope', ['$delegate', '$exceptionHandler',
|
45 | function($delegate, $exceptionHandler) {
|
46 | const rootScope = $delegate;
|
47 | angularProxy.watch = rootScope.$watch.bind(rootScope);
|
48 | const proto = Object.getPrototypeOf(rootScope);
|
49 | const angularDigest = proto.$digest;
|
50 | proto.$digest = bareDigest;
|
51 | proto.$digest.original = angularDigest;
|
52 | vue = new Vue({data: {digestRequest: 0}});
|
53 | vue.$watch(() => vue.digestRequest, () => {
|
54 | if (vue.digestRequest > lastDigestRequest) {
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Vue.nextTick(() => {
|
62 | if (vue.digestRequest <= lastDigestRequest) return;
|
63 | digestInProgress = true;
|
64 | rootScope.$digest.original.call(rootScope);
|
65 | lastDigestRequest = vue.digestRequest = vue.digestRequest + 1;
|
66 | });
|
67 | } else {
|
68 | digestInProgress = false;
|
69 | }
|
70 | });
|
71 | const watcher = _.last(vue._watchers || vue._scope.effects);
|
72 | watcher.id = Infinity;
|
73 | patchRenderWatcherGet(Object.getPrototypeOf(watcher));
|
74 | return rootScope;
|
75 | }
|
76 | ]);
|
77 | }]);
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function patchRenderWatcherGet(prototype) {
|
85 | const originalGet = prototype.get;
|
86 | prototype.get = function get() {
|
87 | try {
|
88 | return originalGet.call(this);
|
89 | } catch (e) {
|
90 | if (this.vm._watcher === this && Vue.config.errorHandler) {
|
91 | Vue.config.errorHandler(e, this.vm, 'uncaught render error');
|
92 | } else {
|
93 | throw e;
|
94 | }
|
95 | }
|
96 | };
|
97 | }
|
98 |
|
99 | export default angularProxy;
|