Skip to content

Commit

Permalink
Support the entire getProject config in theatric's initialize() funct…
Browse files Browse the repository at this point in the history
…ion (theatre-js#384)

Support the entire getProject config in initialize
  • Loading branch information
AndrewPrifer authored Jan 22, 2023
1 parent aec79bd commit 8470b67
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/playground/src/shared/theatric/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {render} from 'react-dom'
import React, {useState} from 'react'
import state from './state.json'

initialize(state)
initialize({state})

function SomeComponent({id}: {id: string}) {
const {foo, $get, $set} = useControls(
Expand Down
14 changes: 7 additions & 7 deletions packages/theatric/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ const maybeTransaction =
? studio.transaction.bind(studio)
: () => {}

let _state: IProjectConfig['state'] | undefined = undefined
let _projectConfig: IProjectConfig['state'] | undefined = undefined

export function initialize(state: IProjectConfig['state']) {
if (_state !== undefined) {
export function initialize(config: IProjectConfig) {
if (_projectConfig !== undefined) {
console.warn(
'Theatric has already been initialized, either through another initialize call, or by calling useControls() before calling initialize().',
)
return
}
_state = state
_projectConfig = config
}

const allProps: Record<string, UnknownShorthandCompoundProps[]> = {}
Expand Down Expand Up @@ -81,8 +81,8 @@ export function useControls<Config extends ControlsAndButtons>(
$get: Getter<OmitMatching<Config, {type: 'button'}>>
} {
// initialize state to null, if it hasn't been initialized yet
if (_state === undefined) {
_state = null
if (_projectConfig === undefined) {
_projectConfig = null
}

/*
Expand Down Expand Up @@ -150,7 +150,7 @@ export function useControls<Config extends ControlsAndButtons>(
)

const sheet = useMemo(
() => getProject('Theatric', {state: _state}).sheet('Panels'),
() => getProject('Theatric', _projectConfig ?? undefined).sheet('Panels'),
[],
)
const panel = options.panel ?? 'Default panel'
Expand Down

0 comments on commit 8470b67

Please sign in to comment.