Publish-Subscriber wrapper of angular-resource
Wraps any Pub-Sub service to promise-like resource. Your Pub-Sub service should answer any message (except cases with timeout: 0)
@PubSubConfig({
publish: (message?: Action) => {
window.sendMessage(JSON.stringify(message)) // Send message to the non-http-server
},
subscriber: new Observable((subscriber: Subscriber<Action>) => {
window.handleMessage = (message: string) => { // Receive message from the non-http-server
subscriber.next(JSON.parse(message))
}
}),
timeout: 10000 // 10s by default. If no confirmation - timeout error
})
export class PubSubResource extends ReactiveResource {
requestWithoutConfirmation = Send({ timeout: 0 });
request = Send();
}
-
publish
(data?: Action) => void - Send message to Pub-Sub service -
subscriber
Observable - Handle events from Pub-Sub service -
timeout
number (default: 10000) - Max request duration in ms -
autoConnect
boolean (default: true) - Connect to Pub-Sub service when application starts -
mock
any | () => any - Returns the value instead of real request -
observable
boolean (default: false) - Return Observable or Promise
-
Send(config?: PubSubConfig)
returns (payload: any) => Promise | Observable - Create send method
See also: angular-resource documentation