本文整理汇总了TypeScript中@angular/platform-webworker/src/web_workers/shared/message_bus.MessageBus类的典型用法代码示例。如果您正苦于以下问题:TypeScript MessageBus类的具体用法?TypeScript MessageBus怎么用?TypeScript MessageBus使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MessageBus类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: beforeEach
beforeEach(() => {
const buses = createPairedMessageBuses();
uiBus = buses.ui;
workerBus = buses.worker;
workerBus.initChannel('ng-Router');
uiBus.initChannel('ng-Router');
broker = new SpyMessageBroker();
});
示例2: inject
inject([AsyncTestCompleter, NgZone], (async: AsyncTestCompleter, zone: MockNgZone) => {
bus = createConnectedMessageBus();
setup(false, zone);
let wasCalled = false;
bus.from(CHANNEL).subscribe({next: (message: any) => { wasCalled = true; }});
bus.to(CHANNEL).emit('hi');
flushMessages(() => {
expect(wasCalled).toBeTruthy();
async.done();
});
}), 10000);
示例3: createConnectedMessageBus
(async: AsyncTestCompleter, zone: MockNgZone) => {
bus = createConnectedMessageBus();
setup(true, zone);
var wasCalled = false;
bus.from(CHANNEL).subscribe({next: (message: any) => { wasCalled = true; }});
bus.to(CHANNEL).emit('hi');
flushMessages(() => {
expect(wasCalled).toBeFalsy();
zone.simulateZoneExit();
flushMessages(() => {
expect(wasCalled).toBeTruthy();
async.done();
});
});
}),
示例4: describe
describe('PostMessageBusSink', () => {
let bus: MessageBus;
const CHANNEL = 'Test Channel';
function setup(runInZone: boolean, zone: NgZone) {
bus.attachToZone(zone);
bus.initChannel(CHANNEL, runInZone);
}
/**
* Flushes pending messages and then runs the given function.
*/
// TODO(mlaval): timeout is fragile, test to be rewritten
function flushMessages(fn: () => void) { setTimeout(fn, 50); }
it('should buffer messages and wait for the zone to exit before sending',
withModule({providers: [{provide: NgZone, useClass: MockNgZone}]})
.inject(
[AsyncTestCompleter, NgZone],
(async: AsyncTestCompleter, zone: MockNgZone) => {
bus = createConnectedMessageBus();
setup(true, zone);
let wasCalled = false;
bus.from(CHANNEL).subscribe({next: (message: any) => { wasCalled = true; }});
bus.to(CHANNEL).emit('hi');
flushMessages(() => {
expect(wasCalled).toBeFalsy();
zone.simulateZoneExit();
flushMessages(() => {
expect(wasCalled).toBeTruthy();
async.done();
});
});
}),
500);
it('should send messages immediately when run outside the zone',
inject([AsyncTestCompleter, NgZone], (async: AsyncTestCompleter, zone: MockNgZone) => {
bus = createConnectedMessageBus();
setup(false, zone);
let wasCalled = false;
bus.from(CHANNEL).subscribe({next: (message: any) => { wasCalled = true; }});
bus.to(CHANNEL).emit('hi');
flushMessages(() => {
expect(wasCalled).toBeTruthy();
async.done();
});
}), 10000);
});
示例5: describe
describe('WebWorkerPlatformLocation', () => {
let uiBus: MessageBus = null !;
let workerBus: MessageBus = null !;
let broker: any = null;
const TEST_LOCATION = new LocationType(
'http://www.example.com', 'http', 'example.com', 'example.com', '80', '/', '', '',
'http://www.example.com');
function createWebWorkerPlatformLocation(loc: LocationType): WebWorkerPlatformLocation {
broker.spy('runOnService')
.and.callFake((args: UiArguments, returnType: Type<any>| SerializerTypes) => {
if (args.method === 'getLocation') {
return Promise.resolve(loc);
}
});
const factory = new MockMessageBrokerFactory(broker);
return new WebWorkerPlatformLocation(factory, workerBus, null !);
}
function testPushOrReplaceState(pushState: boolean) {
const platformLocation = createWebWorkerPlatformLocation(null !);
const TITLE = 'foo';
const URL = 'http://www.example.com/foo';
expectBrokerCall(broker, pushState ? 'pushState' : 'replaceState', [null, TITLE, URL]);
if (pushState) {
platformLocation.pushState(null, TITLE, URL);
} else {
platformLocation.replaceState(null, TITLE, URL);
}
}
beforeEach(() => {
const buses = createPairedMessageBuses();
uiBus = buses.ui;
workerBus = buses.worker;
workerBus.initChannel('ng-Router');
uiBus.initChannel('ng-Router');
broker = new SpyMessageBroker();
});
it('should throw if getBaseHrefFromDOM is called', () => {
const platformLocation = createWebWorkerPlatformLocation(null !);
expect(() => platformLocation.getBaseHrefFromDOM()).toThrowError();
});
it('should get location on init', () => {
const platformLocation = createWebWorkerPlatformLocation(null !);
expectBrokerCall(broker, 'getLocation');
platformLocation.init();
});
it('should throw if set pathname is called before init finishes', () => {
const platformLocation = createWebWorkerPlatformLocation(null !);
platformLocation.init();
expect(() => platformLocation.pathname = 'TEST').toThrowError();
});
it('should send pathname to render thread', done => {
const platformLocation = createWebWorkerPlatformLocation(TEST_LOCATION);
platformLocation.init().then((_) => {
const PATHNAME = '/test';
expectBrokerCall(broker, 'setPathname', [PATHNAME]);
platformLocation.pathname = PATHNAME;
done();
});
});
it('should send pushState to render thread', () => { testPushOrReplaceState(true); });
it('should send replaceState to render thread', () => { testPushOrReplaceState(false); });
});
示例6: it
it('should broadcast', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const CHANNEL = 'CHANNEL 1';
const MESSAGE = 'TESTING';
const NUM_LISTENERS = 2;
bus.initChannel(CHANNEL, false);
let callCount = 0;
const emitHandler = (message: any) => {
expect(message).toEqual(MESSAGE);
callCount++;
if (callCount == NUM_LISTENERS) {
async.done();
}
};
for (let i = 0; i < NUM_LISTENERS; i++) {
const emitter = bus.from(CHANNEL);
emitter.subscribe({next: emitHandler});
}
const toEmitter = bus.to(CHANNEL);
toEmitter.emit(MESSAGE);
}));
示例7: setup
function setup(runInZone: boolean, zone: NgZone) {
bus.attachToZone(zone);
bus.initChannel(CHANNEL, runInZone);
}