I am trying to implement a solution to display toast notifications using the library:
Pure JavaScript library for better notification messages
Is there a way to implement a gateway function, like toastify(message)
, that I can call from my Python scripts to send a message to Perspective and trigger the Toastify({...options}).showToast()
function?
Since the library handles element creation automatically, there's no need to create and manage a custom Perspective component. Is something like this possible?
Yup, you can do this. See Embr-Periscope for inspiration:
The Toasts Update
Periscope version 0.7.0 includes toasts powered by react-toastify .
The toasts also work in the designer .
Creating a Toast
These are client-side toasts, therefore all the interaction is done via system.perspective.runJavaScriptAsync .
periscope.toast is directly mapped to react-toastify's toast object, all features are supported.
Simplest Example
system.perspective.runJavaScriptAsync('''() => {
periscope.toast('Your first toast!')
}''')
Embedded View To…
On the Perspective client-side, add your own message handler to the ClientStore's
connection.
clientStore.connection.handlers.set('your-protocol', (payload) => { ... } )
export const PROTOCOL = {
RUN: 'periscope-js-run',
RESOLVE: 'periscope-js-resolve',
ERROR: 'periscope-js-error',
}
export function installRunJavaScript(clientStore: ClientStore) {
const thisArg = clientStore
clientStore.connection.handlers.set(PROTOCOL.RUN, (payload) => {
const { function: functionLiteral, args, id } = payload
function resolveSuccess(data: unknown) {
clientStore.connection.send(PROTOCOL.RESOLVE, {
id,
success: true,
data,
})
}
On the Perspective gateway-side, trigger this message by sending a message to a Page
.
page.send("your-protocol", payload)
)
.exceptionally { error ->
originalThreadContext.view.get()?.mdcSetup()
page.session.sendErrorToDesigner(error.message, error)
page.log.error("Exception occurred executing client-side JavaScript.", error)
originalThreadContext.view.get()?.mdcTeardown()
throw error
}
.whenCompleteAsync({ _, _ -> requestsInProgress.remove(id) }, queue::submit)
page.send(JavaScriptRunMsg.PROTOCOL, message.getPayload())
}
return future
}
inner class ScriptOverloads {
val runJavaScriptAsync =
PyArgOverloadBuilder()
.setName("runJavaScriptAsync")
.addOverload(
3 Likes
Thanks for the reply. This seems to be what I need
1 Like