Skip to content

Commit

Permalink
Accommodate analysis errors in flipbook's recent PR (#24)
Browse files Browse the repository at this point in the history
# Problem

Trying to update flipbook's docs and part of that involves analyzing
code samples. But that's currently failing because Storyteller's Story
and Storybook types are too strict

flipbook-labs/flipbook#276

# Solution

Created new `LoadedStory` and`LoadedStorybook` types which have all the
implicitly defined properties that a user doesn't need to worry about.
`Story` and `Storybook` are now slimmed down
  • Loading branch information
vocksel authored Nov 16, 2024
1 parent 1e89a66 commit 248fc07
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/createRendererForStory.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local createReactRenderer = require("@root/renderers/createReactRenderer")
local createRoactRenderer = require("@root/renderers/createRoactRenderer")
local types = require("@root/types")

type Story<T> = types.Story<T>
type LoadedStory<T> = types.LoadedStory<T>
type StoryRenderer<T> = types.StoryRenderer<T>

--[[
Expand All @@ -18,7 +18,7 @@ type StoryRenderer<T> = types.StoryRenderer<T>
@tag Story
@within Storyteller
]]
local function createRendererForStory<T>(story: Story<T>): StoryRenderer<any>
local function createRendererForStory<T>(story: LoadedStory<T>): StoryRenderer<any>
local packages = if story.packages then story.packages else story.storybook.packages
if packages then
if packages.Roact then
Expand Down
2 changes: 1 addition & 1 deletion src/findStoryModulesForStorybook.luau
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local types = require("@root/types")
@within Storyteller
@since 0.1.0
]=]
local function findStoryModulesForStorybook(storybook: types.Storybook): { ModuleScript }
local function findStoryModulesForStorybook(storybook: types.LoadedStorybook): { ModuleScript }
local storyModules = {}
for _, root in storybook.storyRoots do
storyModules = Sift.List.join(
Expand Down
3 changes: 2 additions & 1 deletion src/findStoryModulesForStorybook.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ test("find all story modules for a storybook", function()
container = folder
end

local storybook: types.Storybook = {
local storybook: types.LoadedStorybook = {
name = "Storybook",
storyRoots = {
container,
},
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useStory.luau
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ local types = require("@root/types")
]=]
local function useStory(
module: ModuleScript,
storybook: types.Storybook,
storybook: types.LoadedStorybook,
loader: ModuleLoader.ModuleLoader
): (types.Story<unknown>?, string?)
): (types.LoadedStory<unknown>?, string?)
local state, setState = React.useState({} :: {
story: types.Story<unknown>?,
story: types.LoadedStory<unknown>?,
err: string?,
})

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useStorybooks.luau
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ local types = require("@root/types")
@tag Storybook
@within Storyteller
]=]
local function useStorybooks(parent: Instance, loader: ModuleLoader.ModuleLoader): { types.Storybook }
local function useStorybooks(parent: Instance, loader: ModuleLoader.ModuleLoader): { types.LoadedStorybook }
local storybooks, setStorybooks = React.useState({})

local loadStorybooks = React.useCallback(function()
Expand Down
2 changes: 2 additions & 0 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ local types = require("./types")

export type RenderLifecycle = types.RenderLifecycle
export type Story<T> = types.Story<T>
export type LoadedStory<T> = types.LoadedStory<T>
export type Storybook = types.Storybook
export type LoadedStorybook = types.LoadedStorybook
export type StoryControls = types.StoryControls
export type StoryProps = types.StoryProps
export type StoryRenderer<T> = types.StoryRenderer<T>
Expand Down
8 changes: 4 additions & 4 deletions src/loadStoryModule.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ local migrateLegacyPackages = require("@root/migrateLegacyPackages")
local types = require("@root/types")

type StoryPackages = types.StoryPackages
type Storybook = types.Storybook
type Story<T> = types.Story<T>
type LoadedStorybook = types.LoadedStorybook
type LoadedStory<T> = types.LoadedStory<T>

--[=[
Loads the source of a Story module.
Expand All @@ -31,8 +31,8 @@ type Story<T> = types.Story<T>
local function loadStoryModule<T>(
loader: ModuleLoader.ModuleLoader,
storyModule: ModuleScript,
storybook: Storybook
): Story<T>
storybook: LoadedStorybook
): LoadedStory<T>
local success, result = pcall(function()
return loader:require(storyModule)
end)
Expand Down
15 changes: 10 additions & 5 deletions src/loadStoryModule.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ local jest = JestGlobals.jest
local test = JestGlobals.test
local expect = JestGlobals.expect

local MOCK_PLAIN_STORYBOOK: types.Storybook = {
local MOCK_PLAIN_STORYBOOK: types.LoadedStorybook = {
name = "Plain Storybook",
storyRoots = {},
}

local MOCK_ROACT_STORYBOOK: types.Storybook = {
local MOCK_ROACT_STORYBOOK: types.LoadedStorybook = {
name = "Roact Storybook",
storyRoots = {},
packages = {
Roact = {
Expand All @@ -24,7 +26,8 @@ local MOCK_ROACT_STORYBOOK: types.Storybook = {
},
}

local MOCK_REACT_STORYBOOK: types.Storybook = {
local MOCK_REACT_STORYBOOK: types.LoadedStorybook = {
name = "React Storybook",
storyRoots = {},
packages = {
React = {
Expand Down Expand Up @@ -141,7 +144,8 @@ describe("legacy support", function()
test("packages attached to the story get grouped in `packages` object", function()
local loader = ModuleLoader.new()

local storybook: types.Storybook = {
local storybook: types.LoadedStorybook = {
name = "Storybook",
storyRoots = {},
}

Expand All @@ -167,7 +171,8 @@ describe("legacy support", function()
test("packages attached to the story take precedence over the storybook", function()
local loader = ModuleLoader.new()

local storybook: types.Storybook = {
local storybook: types.LoadedStorybook = {
name = "Storybook",
storyRoots = {},
packages = {
Roact = {},
Expand Down
2 changes: 1 addition & 1 deletion src/loadStorybookModule.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local types = require("@root/types")
@tag Module Loading
@within Storyteller
]=]
local function loadStorybookModule(loader: ModuleLoader.ModuleLoader, module: ModuleScript): types.Storybook
local function loadStorybookModule(loader: ModuleLoader.ModuleLoader, module: ModuleScript): types.LoadedStorybook
local wasRequired, result = pcall(function()
return loader:require(module)
end)
Expand Down
4 changes: 2 additions & 2 deletions src/render.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Sift = require("@pkg/Sift")
local createRendererForStory = require("@root/createRendererForStory")
local types = require("@root/types")

type Story<T> = types.Story<T>
type LoadedStory<T> = types.LoadedStory<T>
type StoryControls = types.StoryControls
type StoryProps = types.StoryProps
type StoryRenderer<T> = types.StoryRenderer<T>
Expand Down Expand Up @@ -75,7 +75,7 @@ end
@tag Rendering
@within Storyteller
]=]
local function render<T>(container: Instance, story: Story<T>): RenderLifecycle
local function render<T>(container: Instance, story: LoadedStory<T>): RenderLifecycle
local renderer = createRendererForStory(story)

local prevProps: StoryProps?
Expand Down
2 changes: 1 addition & 1 deletion src/render.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local test = JestGlobals.test
type StoryRenderer<T> = types.StoryRenderer<T>

local container: Instance
local story: types.Story<TextLabel>
local story: types.LoadedStory<TextLabel>
local mockCreateRendererForStory = jest.fn()
local render

Expand Down
4 changes: 2 additions & 2 deletions src/renderers/createFusionRenderer.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Sift = require("@pkg/Sift")

local types = require("@root/types")

type Story<T> = types.Story<T>
type LoadedStory<T> = types.LoadedStory<T>
type StoryProps = types.StoryProps
type StoryRenderer<T> = types.StoryRenderer<T>

Expand All @@ -15,7 +15,7 @@ local function createFusionRenderer(packages: Packages): StoryRenderer<Instance>

local handle: Instance?

local function mount(container: Instance, story: Story<Instance>, props: StoryProps)
local function mount(container: Instance, story: LoadedStory<Instance>, props: StoryProps)
if typeof(story.story) == "Instance" then
handle = story.story
elseif typeof(story.story) == "function" then
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/createManualRenderer.luau
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local types = require("@root/types")

type StoryProps = types.StoryProps
type Story<T> = types.Story<T>
type LoadedStory<T> = types.LoadedStory<T>
type StoryRenderer<T> = types.StoryRenderer<T>

type CleanupFn = () -> ()
Expand All @@ -12,7 +12,7 @@ local function createManualRenderer(): StoryRenderer<ManualStory>
local currentStory
local cleanup: (() -> ())?

local function mount(container: Instance, story: Story<ManualStory>, props: StoryProps)
local function mount(container: Instance, story: LoadedStory<ManualStory>, props: StoryProps)
currentContainer = container
currentStory = story

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/createReactRenderer.luau
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local function createReactRenderer(packages: Packages): StoryRenderer<unknown>
local root
local currentStory

local function reactRender(story: types.Story<unknown>, props)
local function reactRender(story: types.LoadedStory<unknown>, props)
local element
if isReactElement(story.story) then
element = story.story
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils/createStory.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local types = require("@root/types")

local function createStory<T>(element: T, packages: types.StoryPackages?): types.Story<T>
local function createStory<T>(element: T, packages: types.StoryPackages?): types.LoadedStory<T>
return {
name = "Sample",
story = element,
Expand Down
28 changes: 22 additions & 6 deletions src/types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type RenderLifecycle = {
}

export type StoryRenderer<T> = {
mount: (container: Instance, story: Story<T>, initialProps: StoryProps) -> (),
mount: (container: Instance, story: LoadedStory<T>, initialProps: StoryProps) -> (),
unmount: (() -> ())?,
update: ((props: StoryProps, prevProps: StoryProps?) -> ())?,
transformProps: ((props: StoryProps, prevProps: StoryProps?) -> StoryProps)?,
Expand All @@ -43,15 +43,20 @@ types.IStorybook = t.interface({
packages = t.optional(t.map(t.string, t.any)),
})

export type Story<T> = {
export type LoadedStorybook = {
name: string,
story: T | (props: StoryProps) -> T,
source: ModuleScript,
storybook: Storybook,
storyRoots: { Instance },

packages: StoryPackages?,
}

export type Story<T> = {
story: T | (props: StoryProps) -> T,

summary: string?,
controls: StoryControls?,
name: string?,
packages: StoryPackages?,
summary: string?,
}
types.IStory = t.interface({
story = t.any,
Expand All @@ -61,4 +66,15 @@ types.IStory = t.interface({
packages = t.optional(t.map(t.string, t.any)),
})

export type LoadedStory<T> = {
name: string,
story: T | (props: StoryProps) -> T,
source: ModuleScript,
storybook: Storybook,
packages: StoryPackages?,

summary: string?,
controls: StoryControls?,
}

return types
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flipbook-labs/storyteller"
version = "0.4.2"
version = "0.4.3"
license = "MIT"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
Expand Down

0 comments on commit 248fc07

Please sign in to comment.