Skip to content

Programmatic Usage (Advanced)

Harry Rabin edited this page Aug 23, 2022 · 4 revisions

While the default project does a lot of heavy lifting, PatchBay can be used programmatically with just a few things to keep in mind to avoid unexpected or undefined behavior.

Global PatchBay

The global PatchBay will always be set to the latest-created instance of PBApp. This is why launch.ts in the default project doesn't bother assigning the PBApp to anything. However, the constructor does return the instance, so you can assign it to be used programmatically if you want multiple PBApp instances running. Just remember to avoid using the global PatchBay, or you may get some unexpected behavior.

Some internals rely on the global PatchBay, but there's an easy way to override it in all circumstances:

Core

/* Use of PBRequest is an edge case itself, let alone in programmatic usage,
but it is possible to manually set the PBAPP instance of a PBRequest when
creating one (beginning in 0.1.6)*/
PBRequest.ify(req, {
    app: customPBAppInstance
})

PBUtils

PBUtils.TemplateResponse("my-template", {data: "data"}, {
    /* Pass in a template record like below to override
    the use of the global one */
   
    templates: customPBInstance.templates
});

Templates

Unless you have a views folder in the root directory you'd like to use, you should disable the automatic loading of Handlebars templates to avoid an error on launch. See advanced usage in templates for details.

Clone this wiki locally