Skip to content

Commit

Permalink
feat: add introspector (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed May 21, 2023
1 parent 45a9339 commit e508151
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/examples/purity-todo/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {init, makeAsync} from '../../index.js'
import {makeIntrospector} from '../../introspector.js'
import {get} from './services/storage.js'
import type {QueryType} from './services/google-api.js'

Expand Down Expand Up @@ -59,3 +60,8 @@ window.visualViewport?.addEventListener('resize', () => {
document.body.style.maxHeight = height + 'px'
document.body.scrollIntoView()
})

export const theIntrospector = makeIntrospector(
rerender,
new URLSearchParams(location.search).has('introspect')
)
3 changes: 2 additions & 1 deletion src/examples/purity-todo/components/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {render} from '../../../index.js'
import {state} from '../app.js'
import {state, theIntrospector} from '../app.js'
import {closeSettings} from '../services/settings.js'
import {closeTaskDetails} from '../services/task-details.js'
import {appStyle} from './app-style.js'
Expand Down Expand Up @@ -38,5 +38,6 @@ export const root = (): string => render`
}
</div>
${modalStyle()}
${theIntrospector({state, localStorage: window.localStorage})}
</div>
`
2 changes: 1 addition & 1 deletion src/examples/purity-todo/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Purity Todo 2.10",
"name": "Purity Todo 2.11",
"short_name": "ToDo",
"description": "Todo list written with purity.js",
"icons": [
Expand Down
2 changes: 1 addition & 1 deletion src/examples/purity-todo/purity-todo.sw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const appScope = self.registration.scope

const cacheName = `${appScope}@2.10`
const cacheName = `${appScope}@2.11`
const contentToCache = [
'./',
'./index.html',
Expand Down
50 changes: 50 additions & 0 deletions src/introspector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {makeOnce} from './once.js'
import {render} from './purity.js'
import type {Rerender} from './purity.js'

const makeState = <T>(rerender: Rerender) => {
let state: T

const get = () => state

const set = (callback: (s: T) => T) => {
state = callback(state)
rerender()
}

return {set, get}
}

const once = makeOnce()

export const makeIntrospector = (rerender: Rerender, isVisible = false) => {
if (!isVisible) return () => ''
const theState = makeState<boolean>(rerender)
return (data: Record<string, unknown>) => {
once('init-introspector', () => {
theState.set(() => true)
})
const size = theState.get() ? 'calc(100% - 20px)' : '30px'
return render`
<pre id="the-introspector" ::click=${() => {
theState.set(s => !s)
}}>
${JSON.stringify(data, null, 2)}
</pre>
<style id="introspector-style">
#the-introspector {
position: fixed;
bottom: 10px;
right: 10px;
width: ${size};
height: ${size};
overflow: auto;
background: rgba(0, 0, 0, 0.75);
color: white;
z-index: 1000;
border: 1px solid red;
}
</style>
`
}
}

0 comments on commit e508151

Please sign in to comment.