Skip to content

Commit

Permalink
Inject debug programs in globalThis context
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Jan 30, 2023
1 parent d988bfa commit 258130d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const content = new TextDecoder().decode(
That's it, you have successfully created a Webnative app! 🚀


## Basics
## POSIX Interface

WNFS exposes a familiar POSIX-style interface:
- `exists`: check if a file or directory exists
Expand Down Expand Up @@ -223,4 +223,21 @@ file.history.prior(1606236743)

## Migration

Some versions of Webnative require apps to migrate their codebase to address breaking changes. Please see our [migration guide](https://guide.fission.codes/developers/webnative/migration) for help migrating your apps to the latest Webnative version.
Some versions of Webnative require apps to migrate their codebase to address breaking changes. Please see our [migration guide](https://guide.fission.codes/developers/webnative/migration) for help migrating your apps to the latest Webnative version.


## Debugging

Debugging mode can be enable by setting `debug` to `true` in your configuration object that you pass to your `Program`. By default this will add your programs to the global context object (eg. `window`) under `globalThis.__webnative.programs` (can be disabled, see API docs).

```ts
const appInfo = { creator: "Nullsoft", name: "Winamp" }

await wn.program({
namespace: appInfo,
debug: true
})

// Automatically exposed Program in debug mode
const program = globalThis.__webnative[ wn.namespace(appInfo) ] // namespace: "Nullsoft/Winamp"
```
14 changes: 13 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ export type Configuration = {
namespace: string | AppInfo

/**
* Enable debug console statements.
* Enable debug mode.
*
* @default false
*/
debug?: boolean

/**
* Debugging settings.
*/
debugging?: {
/**
* Should I add programs to the global context while in debugging mode?
*
* @default true
*/
injectIntoGlobalContext?: boolean
}

/**
* File system settings.
*/
Expand Down
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,32 @@ export async function assemble(config: Configuration, components: Components): P
}
}

// Fin
return {
// Create `Program`
const program = {
...shorthands,
configuration: { ...config },
auth,
components,
capabilities,
session,
}

// Inject into global context if necessary
if (config.debug) {
const inject = config.debugging?.injectIntoGlobalContext === undefined
? true
: config.debugging?.injectIntoGlobalContext

if (inject) {
const container = globalThis as any
container.__webnative = container.__webnative || {}
container.__webnative.programs = container.__webnative.programs || {}
container.__webnative.programs[ namespace(config) ] = program
}
}

// Fin
return program
}


Expand Down

0 comments on commit 258130d

Please sign in to comment.