Aleksandr Aleksandrov 6 年之前
父節點
當前提交
1900ad3b6b

+ 1 - 1
package.json

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

+ 22 - 0
src/element.ts

@@ -0,0 +1,22 @@
+import {TransmitterInterface} from './transmitter/interface/transmitter.interface';
+
+declare module '@angular/core' {
+
+    class ElementRef<T = any> {
+        private _watchSizeCollection?: any;
+
+        nativeElement: T;
+
+        constructor(nativeElement: T);
+
+        watchSize?: (callback: (height: number, width: number) => void,
+                    options?: { attributes: boolean, childList: boolean, characterData: boolean },
+                    params?: { legacy: boolean, timeout?: number }) => () => void;
+
+        watchChanges?: (callback: MutationCallback, options: MutationObserverInit) => () => void;
+
+        triggerSizeCheck?: () => void;
+
+        transmitter?: TransmitterInterface;
+    }
+}

+ 3 - 0
src/transmitter/index.ts

@@ -0,0 +1,3 @@
+export * from './interface/transmitter.interface';
+export * from './interface/transmitterable.interface';
+export * from './transmitter';

+ 7 - 0
src/transmitter/interface/transmitter.interface.ts

@@ -0,0 +1,7 @@
+import {CommonStackManager} from '@ex-helpers/stack';
+import {TransmitterableInterface} from './transmitterable.interface';
+import {Observable} from 'rxjs';
+
+export interface TransmitterInterface extends CommonStackManager<TransmitterableInterface, string> {
+    waitFor<T = any>(name: string): Observable<TransmitterableInterface<T>>;
+}

+ 4 - 0
src/transmitter/interface/transmitterable.interface.ts

@@ -0,0 +1,4 @@
+export interface TransmitterableInterface<T = any> {
+    name: string;
+    data: T;
+}

+ 35 - 0
src/transmitter/transmitter.ts

@@ -0,0 +1,35 @@
+import {ElementRef} from '@angular/core';
+import {TransmitterInterface} from './interface/transmitter.interface';
+import {CommonStackManager} from '@ex-helpers/stack';
+import {TransmitterableInterface} from './interface/transmitterable.interface';
+import {Observable} from 'rxjs';
+
+export class Transmitter extends CommonStackManager<TransmitterableInterface, string> implements TransmitterInterface {
+    public static get(ref: ElementRef): TransmitterInterface {
+        if (!ref.transmitter) {
+            ref.transmitter = new Transmitter(ref);
+        }
+        return ref.transmitter;
+    }
+
+    constructor(public ref: ElementRef) {
+        super('name');
+    }
+
+    public waitFor<T>(name: string): Observable<TransmitterableInterface<T>> {
+        return new Observable(observer => {
+            const s = this.subscribe(() => {
+                const item = this.get(name);
+                if (item) {
+                    observer.next(item);
+                    observer.complete();
+                    if (s) {
+                        s.unsubscribe();
+                    } else {
+                        setTimeout(() => s.unsubscribe());
+                    }
+                }
+            });
+        });
+    }
+}