Browse Source

Watch Changes func

Aleksandr Aleksandrov 7 năm trước cách đây
mục cha
commit
d3738f25e5
2 tập tin đã thay đổi với 14 bổ sung3 xóa
  1. 1 1
      package.json
  2. 13 2
      src/watch.ts

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "@ex-helpers/element",
-  "version": "0.1.3",
+  "version": "0.1.4",
   "description": "Several element wrapper helpers",
   "scripts": {
     "build": "exb",

+ 13 - 2
src/watch.ts

@@ -13,6 +13,8 @@ declare module '@angular/core' {
                     options?: { attributes: boolean, childList: boolean, characterData: boolean },
                     params?: { legacy: boolean, timeout?: number }) => () => void;
 
+        watchChanges: (callback: MutationCallback, options: MutationObserverInit) => () => void;
+
         triggerSizeCheck: () => void;
     }
 }
@@ -32,7 +34,7 @@ const DEFAULT_OPTIONS = {
     characterData: true
 };
 
-(ElementRef as any).prototype.watchSize = function
+ElementRef.prototype.watchSize = function
 (callback: (height: number, width: number) => void,
  options?: { attributes?: boolean, childList?: boolean, characterData?: boolean },
  params: { timeout?: number, legacy: boolean } = {legacy: false}) {
@@ -87,8 +89,17 @@ const DEFAULT_OPTIONS = {
     }
 };
 
-(ElementRef as any).prototype.triggerSizeCheck = function () {
+ElementRef.prototype.triggerSizeCheck = function () {
     if (this._watchSizeCollection) {
         this._watchSizeCollection.call('triggerCheck');
     }
 };
+
+ElementRef.prototype.watchChanges = function (callback: MutationCallback, options: MutationObserverInit) {
+    if (MutationObserver) {
+        const observer = new MutationObserver(callback);
+        observer.observe(this.nativeElement, options);
+        return () => observer.disconnect();
+    }
+    return () => {};
+};