@ashetm/ng-broadcast-channel
Version:
``@ashetm/ng-broadcast-channel`` is a library that simplify manipulation of ``BroadcastChannel`` API.
68 lines (61 loc) • 2.28 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, makeEnvironmentProviders } from '@angular/core';
import { Observable } from 'rxjs';
class BroadcastChannelRef {
get name() {
return this._broadcastChannel.name;
}
constructor(_ngZone, _name) {
this._ngZone = _ngZone;
this._name = _name;
this.data$ = new Observable((subscriber) => {
this._broadcastChannel.onmessage = (event) => {
this._ngZone.run(() => subscriber.next(event.data));
};
this._broadcastChannel.onmessageerror = (event) => {
this._ngZone.run(() => subscriber.error(event.data));
};
});
this._broadcastChannel = new BroadcastChannel(_name);
}
close() {
this._broadcastChannel.close();
}
send(data) {
this._broadcastChannel.postMessage(data);
}
}
class BroadcastChannelService {
constructor(_ngZone) {
this._ngZone = _ngZone;
this._broadcastChannels = {};
}
ngOnDestroy() {
for (const broadcastChannel in this._broadcastChannels) {
this._broadcastChannels[broadcastChannel].close();
}
}
create(name) {
return this._broadcastChannels[name]
|| (this._broadcastChannels[name] = new BroadcastChannelRef(this._ngZone, name));
}
get(name) {
return this._broadcastChannels[name];
}
static { this.ɵfac = function BroadcastChannelService_Factory(t) { return new (t || BroadcastChannelService)(i0.ɵɵinject(i0.NgZone)); }; }
static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: BroadcastChannelService, factory: BroadcastChannelService.ɵfac }); }
}
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BroadcastChannelService, [{
type: Injectable
}], function () { return [{ type: i0.NgZone }]; }, null); })();
const provideBroadcastChannel = () => {
return makeEnvironmentProviders([BroadcastChannelService]);
};
/*
* Public API Surface of broadcast-channel
*/
/**
* Generated bundle index. Do not edit.
*/
export { BroadcastChannelRef, BroadcastChannelService, provideBroadcastChannel };
//# sourceMappingURL=ashetm-ng-broadcast-channel.mjs.map