-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
33 lines (25 loc) · 763 Bytes
/
index.ts
File metadata and controls
33 lines (25 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { reaction } from 'mobx'
import angular from 'angular'
const app = angular.module('mobx-angularjs', [])
const debounce = (fn, ms = 0) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};
const link: angular.IDirectiveLinkFn = ($scope) => {
const $$watchers = ($scope as any).$$watchers || [];
const debouncedDigest = debounce($scope.$digest.bind($scope), 0);
const dispose = reaction(
() => [...$$watchers].map(watcher => watcher.get($scope)),
() => !$scope.$root.$$phase && debouncedDigest()
)
$scope.$on('$destroy', dispose)
}
app.directive('mobxAutorun', () => ({
restrict: 'A',
scope: false,
link
}))
export default app.name