Skip to content

Instantly share code, notes, and snippets.

@alexstrat
Last active November 27, 2018 08:07
Show Gist options
  • Save alexstrat/7f2ff16dca63bffc7767fc1a6141e346 to your computer and use it in GitHub Desktop.
Save alexstrat/7f2ff16dca63bffc7767fc1a6141e346 to your computer and use it in GitHub Desktop.
Mojom example 2

Overview

Local usage

import { Heartbeat } from './math.ts'
import { HeartbeatImpl } from './MathImpl'

const heartbeat = new Heartbeat(new HeartbeatImpl());

heartbeat.requestNotifications({
  onHeartbeat: () => {
    console.log('💓');
  }
})

Remote usage

In process A (server):

module bx.heartbeat;
interface HeartbeatClient {
OnHeartbeat()
}
interface Heartbeat {
RequestNotifications(HearbeatClient client);
}
export interface IHeartbeatClient {
onHeartbeat: () => void
}
export type IHeartbeatRequestNotificationsRequest = {
client: IHeartbeatClient
};
export interface IHeartbeat {
requestNotifications: (r: IHeartbeatRequestNotificationsRequest) => void
}
export class HeartbeatClient implements IHeartbeatClient {
constructor(impl: IHeartbeatClient) {
this.impl = impl;
}
onHeartbeat() {
return this.impl.onHeartbeat();
}
}
export class Heartbeat implements IHeartbeat {
constructor(impl: IHeartbeat) {
this.impl = impl;
}
requestNotifications(r: IHeartbeatRequestNotificationsRequest) {
return this.impl.requestNotifications(r);
}
}
import { IHeartbeat } from 'heartbeat.ts';
export class HeartbeatImpl implements IHeartbeat {
requestNotifications(r) {
const { client } = r;
setInterval(1000, () => client.onHeartbeat());
}
}
import { IHeartbeat } from 'heartbeat.ts';
export class HeartbeatClientRPCImpl implements IHeartbeatClient {
static bindImpl(pipe: Pipe, pipePool: PipePool, impl: IHeartbeatClient) {
const peer = new RPCPeer();
peer.addHander('bx.heartbeat.HeartbeatClient.OnHeartbeat', async () => {
return await impl.onHeartbeat();
});
this.peer.pipe(pipe);
}
constructor (pipe: Pipe, pipePool: PipePool) {
this.pipe = pipe;
this.pipePool = pipePool;
this.peer = new RPCPeer();
this.peer.pipe(pipte)
}
onHeartbeat() {
return this.peer.request('bx.heartbeat.HeartbeatClient.OnHeartbeat');
}
}
export class HeartbeatRPCImpl implements IHeartbeat {
static module = 'bx.heartbeat';
static interfaceName = 'Heartbeat';
static bindImpl(pipe: Pipe, pipePool: PipePool, impl: Heartbeat) {
const peer = new RPCPeer();
peer.addHander('bx.heartbeat.Heartbeat.RequestNotifications', async (r) => {
const pipe = pipePool.get(r.client.$$pipeRef);
const client = new HeartbeatClientRPCImpl(pipe, pipePool);
return await impl.requestNotifications(client);
})
}
constructor(pipe: Pipe, pipePool: PipePool) {
this.pipe = pipe;
this.pipePool = pipePool;
this.peer = new RPCPeer();
this.peer.pipe(pipte)
}
requestNotifications(r) {
const { client } = r;
// will create and open a new pipe (aka DuplexStream)
const pipe = this.pipePool.createPipe();
HeartbeatRPCImpl.bindImpl(pipe, this.pipePool, client);
return this.peer.request('bx.heartbeat.Heartbeat.RequestNotifications', { client : { $$pipeRef: pipe.id }})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment