forked from openshift-knative/showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working stub of event stream using u-cloudevents
- Loading branch information
Showing
24 changed files
with
2,108 additions
and
339 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
import './assets/style.css' | ||
import React from 'react' | ||
import { Info } from './index/types' | ||
import { factory as index } from './index/endpoint' | ||
import { factory as events, Stream } from './events/endpoint' | ||
import { CloudEvent } from './events/u-cloudevents' | ||
import Lead from './components/lead' | ||
import LeftColumn from './components/left-column' | ||
import RightColumn from './components/right-column' | ||
import Loader from './components/loader' | ||
|
||
interface AppState { | ||
info?: Info | ||
} | ||
|
||
class App extends React.Component<any, AppState> { | ||
stream?: Stream<CloudEvent> | ||
|
||
constructor(props: any) { | ||
super(props) | ||
this.state = { | ||
info: undefined | ||
} | ||
index().info().then((info: Info) => { | ||
this.setState({ info }) | ||
this.stream = events(info.config).stream() | ||
this.stream.onError((error: Error) => { | ||
console.error(error) | ||
this.stream?.reconnect() | ||
}) | ||
}) | ||
} | ||
|
||
render() { | ||
if (!this.state.info) { | ||
return <Loader /> | ||
} | ||
return ( | ||
<div> | ||
<Lead info={this.state.info} /> | ||
<section className="container"> | ||
<LeftColumn | ||
info={this.state.info} | ||
stream={this.stream!} | ||
/> | ||
<RightColumn info={this.state.info} /> | ||
</section> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default App |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
frontend/src/components/Lead.tsx → frontend/src/components/lead.tsx
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
frontend/src/components/RightColumn.tsx → frontend/src/components/right-column.tsx
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,12 @@ | ||
const nodeenv = process.env.NODE_ENV ?? 'development' | ||
|
||
const shouldStubBackend = process.env.REACT_APP_BACKEND === undefined && ( | ||
nodeenv === 'development' || nodeenv === 'test' | ||
) | ||
|
||
const baseAddress = process.env.BACKEND ?? 'http://localhost:8080' | ||
|
||
export { | ||
shouldStubBackend, | ||
baseAddress | ||
} |
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,23 @@ | ||
import { shouldStubBackend } from '../config/backend' | ||
import { Config } from '../index/types' | ||
import { CloudEvent } from './u-cloudevents' | ||
import Stub from './stub' | ||
import ServerSentEvents from './sse' | ||
|
||
export interface Stream<T> { | ||
close(): void | ||
reconnect(): void | ||
onData(listener: (data: T) => void): void | ||
onError(listener: (error: Error) => void): void | ||
} | ||
|
||
export interface Endpoint { | ||
stream(): Stream<CloudEvent> | ||
} | ||
|
||
export const factory = (config: Config): Endpoint => { | ||
if (shouldStubBackend) { | ||
return new Stub(config) | ||
} | ||
return new ServerSentEvents() | ||
} |
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,25 @@ | ||
import { CloudEvent } from './u-cloudevents' | ||
import { Endpoint, Stream } from './endpoint' | ||
|
||
export default class ServerSentEvents implements Endpoint { | ||
stream(): Stream<CloudEvent> { | ||
return new SseStream() | ||
} | ||
} | ||
|
||
class SseStream implements Stream<CloudEvent> { | ||
private listeners: ((ce: CloudEvent) => void)[] = [] | ||
|
||
close(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
reconnect(): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
onData(listener: (data: CloudEvent<any>) => void): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
onError(listener: (error: Error) => void): void { | ||
throw new Error('Method not implemented.') | ||
} | ||
} |
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,78 @@ | ||
import { CloudEvent } from './u-cloudevents' | ||
import { Config } from '../index/types' | ||
import { Endpoint, Stream } from './endpoint' | ||
|
||
export default class Stub implements Endpoint { | ||
private config: Config | ||
constructor(config: Config) { | ||
this.config = config | ||
} | ||
stream(): Stream<CloudEvent> { | ||
return new StubStream(this.config) | ||
} | ||
} | ||
|
||
class StubStream implements Stream<CloudEvent> { | ||
private counter : number | ||
private active = true | ||
private listeners: ((ce: CloudEvent<any>) => void)[] = [] | ||
private errs: ((error: Error) => void)[] = [] | ||
private config: Config | ||
|
||
constructor(config: Config) { | ||
this.config = config | ||
this.counter = 0 | ||
setInterval(() => { | ||
this.tick() | ||
}, 25) | ||
} | ||
|
||
close(): void { | ||
this.active = false | ||
} | ||
reconnect(): void { | ||
console.log('reconnecting...') | ||
this.active = true | ||
} | ||
onData(listener: (data: CloudEvent<any>) => void): void { | ||
this.listeners.push(listener) | ||
} | ||
onError(listener: (error: Error) => void): void { | ||
this.errs.push(listener) | ||
} | ||
|
||
private tick() { | ||
this.counter++ | ||
if (!this.active) { | ||
return | ||
} | ||
const chances = { | ||
event: this.counter % 100 === 0, | ||
error: this.counter % 1000 === 0, | ||
} | ||
if (chances.error) { | ||
return this.error() | ||
} | ||
if (chances.event) { | ||
return this.emit() | ||
} | ||
} | ||
|
||
private emit() { | ||
const ce = new CloudEvent({ | ||
source: '//devdata/js', | ||
type: 'com.redhat.openshift.example.Hello', | ||
datacontenttype: 'application/json', | ||
data: { | ||
greeting: this.config.greet, | ||
number: this.counter, | ||
who: 'Person', | ||
}, | ||
}) | ||
this.listeners.forEach((lst) => lst(ce)) | ||
} | ||
|
||
private error() { | ||
|
||
} | ||
} |
Oops, something went wrong.