This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1026 from statechannels/tc-ag-xstate-messaging
Xstate Messaging Service (rebased)
- Loading branch information
Showing
13 changed files
with
400 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {Store} from './store/memory-store'; | ||
import {MessagingServiceInterface} from './messaging'; | ||
|
||
import {applicationWorkflow} from './workflows/application'; | ||
import ReactDOM from 'react-dom'; | ||
import React from 'react'; | ||
import WalletUi from './ui/wallet'; | ||
import {interpret, Interpreter} from 'xstate'; | ||
import {Guid} from 'guid-typescript'; | ||
import {convertToOpenEvent} from './utils/workflow-utils'; | ||
export interface Workflow { | ||
id: string; | ||
machine: Interpreter<any, any, any>; | ||
domain: string; // TODO: Is this useful? | ||
} | ||
export class ChannelWallet { | ||
private workflows: Workflow[]; | ||
|
||
constructor(private store: Store, private messagingService: MessagingServiceInterface) { | ||
this.workflows = []; | ||
this.messagingService.requestFeed.subscribe(r => { | ||
if (r.method === 'CreateChannel' || r.method === 'JoinChannel') { | ||
const workflow = this.startApplicationWorkflow(); | ||
this.workflows.push(workflow); | ||
workflow.machine.send(convertToOpenEvent(r)); | ||
} | ||
}); | ||
} | ||
|
||
private startApplicationWorkflow(): Workflow { | ||
const workflowId = Guid.create().toString(); | ||
const machine = interpret<any, any, any>( | ||
applicationWorkflow(this.store, this.messagingService), | ||
{ | ||
devTools: true | ||
} | ||
) | ||
.onDone(() => (this.workflows = this.workflows.filter(w => w.id !== workflowId))) | ||
.start(); | ||
// TODO: Figure out how to resolve rendering priorities | ||
this.renderUI(machine); | ||
|
||
return {id: workflowId, machine, domain: 'TODO'}; | ||
} | ||
|
||
private renderUI(machine) { | ||
if (document.getElementById('root')) { | ||
ReactDOM.render( | ||
React.createElement(WalletUi, {workflow: machine}), | ||
document.getElementById('root') | ||
); | ||
} | ||
} | ||
|
||
public async pushMessage(message) { | ||
// Update the store first | ||
await this.store.pushMessage(message); | ||
// Update any workflows waiting on an observable | ||
await this.messagingService.receiveMessage(message); | ||
} | ||
|
||
public onSendMessage(callback: (message) => void) { | ||
this.messagingService.outboxFeed.subscribe(m => callback(m)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
import {handleMessage} from './messaging'; | ||
|
||
import {ethers} from 'ethers'; | ||
|
||
// TODO import {ChainWatcher} from './chain'; | ||
import {WorkflowManager} from './workflow-manager'; | ||
import {MemoryStore, Store} from './store/memory-store'; | ||
|
||
const ourWallet = ethers.Wallet.createRandom(); | ||
import {MemoryStore} from './store/memory-store'; | ||
|
||
const store: Store = new MemoryStore([ourWallet.privateKey]); | ||
import {ChannelWallet} from './channel-wallet'; | ||
import {MessagingService} from './messaging'; | ||
|
||
const workflowManager = new WorkflowManager(store); | ||
const {privateKey} = ethers.Wallet.createRandom(); | ||
const store = new MemoryStore([privateKey]); | ||
const messagingService = new MessagingService(store); | ||
const channelWallet = new ChannelWallet(store, messagingService); | ||
|
||
// Communicate via postMessage | ||
window.addEventListener('message', async event => { | ||
await handleMessage(event, workflowManager, store, ourWallet); | ||
channelWallet.pushMessage(event.data); | ||
}); | ||
channelWallet.onSendMessage(m => window.parent.postMessage(m, '*')); |
Oops, something went wrong.