Skip to content

Commit

Permalink
Add factories and regions support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Mar 11, 2023
1 parent 068c189 commit e68e354
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 53 deletions.
73 changes: 55 additions & 18 deletions packages/effector/inspect.d.ts
Expand Up @@ -27,11 +27,11 @@ type Region =
| {
type: 'region'
meta: Record<string, unknown>
parent?: Region
region?: Region
}
| {
type: 'factory'
parent?: Region
region?: Region
meta: {
sid?: string
name?: string
Expand All @@ -44,22 +44,59 @@ type Region =
}
}

export type Declaration = {
type: 'unit'
kind: string
name?: string
id: string
sid?: string
loc?: {
file: string
line: number
column: number
}
meta: Record<string, unknown>
region?: Show<Region>
// for derived units - stores or events
derived?: boolean
}
export type Declaration =
| {
type: 'unit'
kind: string
name?: string
id: string
sid?: string
loc?: {
file: string
line: number
column: number
}
meta: Record<string, unknown>
region?: Show<Region>
// for derived units - stores or events
derived?: boolean
}
| {
type: 'factory'
meta: Record<string, unknown>
region?: Region
sid?: string
name?: string
method?: string
loc?: {
file: string
line: number
column: number
}
// these fields are not provided to factories
// however, to make it easier to work with it in Typescript
// and to avoid annoying `some prop does not exist` errors
// they are explictily set to undefined
kind?: undefined
id?: undefined
derived?: undefined
}
| {
type: 'region'
region?: Region
meta: Record<string, unknown>
// these fields are not provided to regions
// however, to make it easier to work with it in Typescript
// and to avoid annoying `some prop does not exist` errors
// they are explictily set to undefined
kind?: undefined
id?: undefined
derived?: undefined
sid?: undefined
name?: undefined
method?: undefined
loc?: undefined
}

export function inspectGraph(config: {
fn: (declaration: Declaration) => void
Expand Down
55 changes: 44 additions & 11 deletions src/effector/__tests__/inspect.test.ts
Expand Up @@ -191,6 +191,12 @@ describe('inspect API', () => {
})

function compactDeclaration(d: Declaration) {
if (d.type === 'region') return `region, parent: ${typeof d.region}`
if (d.type === 'factory')
return `factory, ${d.method}, ${d.sid}, ${
d.name
}, parent: ${typeof d.region}`

return `${d.derived ? 'derived ' : ''}${d.type} ${d.name} (${
d.kind
}) created (sid ${typeof d.sid}, parent region: ${typeof d.region}, id: ${typeof d.id}, loc: ${typeof d.loc})`
Expand Down Expand Up @@ -270,7 +276,7 @@ describe('inspectGraph API', () => {
const regionalUnitDeclared = jest.fn()
inspectGraph({
fn: d => {
declared()
declared(`${d.type} ${d.name} created`)
if (!d.region) {
nonRegionalUnitDeclared()
} else {
Expand All @@ -287,8 +293,6 @@ describe('inspectGraph API', () => {
target: targetEvent,
})

expect(declared).toHaveBeenCalledTimes(5) // store == 1 store + 2 events (updates + reinit)
expect(nonRegionalUnitDeclared).toHaveBeenCalledTimes(4)
expect(regionalUnitDeclared).toHaveBeenCalledTimes(1)
expect(regionalUnitDeclared).toHaveBeenCalledWith({
myLibType: 'customOperator',
Expand All @@ -297,6 +301,16 @@ describe('inspectGraph API', () => {
target: targetEvent,
},
})
expect(argumentHistory(declared)).toMatchInlineSnapshot(`
Array [
"unit updates created",
"unit reinit created",
"unit $source created",
"unit targetEvent created",
"unit internalEvent created",
"region undefined created",
]
`)
})
test('one-level withFactory', () => {
function customOperator(config: Record<string, unknown>) {
Expand All @@ -308,7 +322,7 @@ describe('inspectGraph API', () => {
const regionalUnitDeclared = jest.fn()
inspectGraph({
fn: d => {
declared()
declared(`${d.type} ${d.name} created`)
if (!d.region) {
nonRegionalUnitDeclared()
} else {
Expand All @@ -323,22 +337,30 @@ describe('inspectGraph API', () => {
withFactory({
sid: 'customOperator-call-1',
method: 'customOperator',
name: 'test-name',
fn: () =>
customOperator({
source: $source,
target: targetEvent,
}),
})

expect(declared).toHaveBeenCalledTimes(5) // store == 1 store + 2 events (updates + reinit)
expect(nonRegionalUnitDeclared).toHaveBeenCalledTimes(4)
expect(regionalUnitDeclared).toHaveBeenCalledTimes(1)
expect(regionalUnitDeclared).toHaveBeenCalledWith(
expect.objectContaining({
sid: 'customOperator-call-1',
method: 'customOperator',
}),
)
expect(argumentHistory(declared)).toMatchInlineSnapshot(`
Array [
"unit updates created",
"unit reinit created",
"unit $source created",
"unit targetEvent created",
"unit internalEvent created",
"factory test-name created",
]
`)
})
test('nested regions', () => {
function customOperator(config: Record<string, unknown>) {
Expand All @@ -354,7 +376,7 @@ describe('inspectGraph API', () => {
const regionalUnitDeclared = jest.fn()
inspectGraph({
fn: d => {
declared()
declared(`${d.type} ${d.name} created`)
if (!d.region) {
nonRegionalUnitDeclared()
} else {
Expand All @@ -369,15 +391,14 @@ describe('inspectGraph API', () => {
withFactory({
sid: 'customOperator-call-1',
method: 'customOperator',
name: 'test-name',
fn: () =>
customOperator({
source: $source,
target: targetEvent,
}),
})

expect(declared).toHaveBeenCalledTimes(5) // store == 1 store + 2 events (updates + reinit)
expect(nonRegionalUnitDeclared).toHaveBeenCalledTimes(4)
expect(regionalUnitDeclared).toHaveBeenCalledTimes(1)
expect(regionalUnitDeclared).toHaveBeenCalledWith({
type: 'region',
Expand All @@ -397,6 +418,18 @@ describe('inspectGraph API', () => {
},
},
})
expect(argumentHistory(declared)).toMatchInlineSnapshot(`
Array [
"unit updates created",
"unit reinit created",
"unit $source created",
"unit targetEvent created",
"unit internalEvent created",
"region undefined created",
"region undefined created",
"factory undefined created",
]
`)
})
})
})
Expand Down Expand Up @@ -638,7 +671,7 @@ describe('real use cases', () => {
const file = d.loc.file.split('/').at(-1) || ''
const units = unitsByFile[file] || []
const name = d.region
? `${d.region.parent?.meta.name!}/${d.name!}`
? `${d.region.region?.meta.name!}/${d.name!}`
: d.name!

units.push({
Expand Down
52 changes: 33 additions & 19 deletions src/effector/inspect.ts
Expand Up @@ -87,40 +87,48 @@ type Region =
| {
type: 'region'
meta: Record<string, unknown>
parent?: Region
region?: Region
}
| {
type: 'factory'
parent?: Region
meta: {
sid?: string
name?: string
method?: string
loc?: {
file: string
line: number
column: number
}
meta: Record<string, unknown>
region?: Region
sid?: string
name?: string
method?: string
loc?: {
file: string
line: number
column: number
}
}

type UnitDeclaration = {
type: 'unit'
meta: Record<string, unknown>
region?: Region
} & NodeCommonMeta

type Declaration = UnitDeclaration
type Declaration = UnitDeclaration | Region

const inspectGraphSubs = new Set<{
fn: (declaration: Declaration) => void
}>()

setGraphInspector((node: Node, regionStack: RegionStack) => {
const decl = readUnitDeclaration(node, regionStack)
setGraphInspector((node: Node | 'region', regionStack: RegionStack) => {
let decl: Declaration | undefined

inspectGraphSubs.forEach(sub => {
sub.fn(decl)
})
if (node === 'region') {
decl = readRegionStack(regionStack)
} else {
decl = readUnitDeclaration(node, regionStack)
}

if (decl) {
inspectGraphSubs.forEach(sub => {
sub.fn(decl!)
})
}
})

export function inspectGraph(config: {
Expand Down Expand Up @@ -194,16 +202,22 @@ function readRegionStack(regionStack?: RegionStack | null): Region | undefined {
const parentRegion = readRegionStack(parent) || undefined

if (meta.type === 'factory') {
const {sid, name, loc, method} = meta as any

return {
type: 'factory',
parent: parentRegion,
region: parentRegion,
meta,
sid,
name,
loc,
method,
}
}

return {
type: 'region',
parent: parentRegion,
region: parentRegion,
meta,
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/effector/region.ts
Expand Up @@ -4,7 +4,7 @@ import {getParent, getMeta, getGraph} from './getter'
import {createNode} from './createNode'

type DeclarationSourceReporter = (
node: Node,
node: Node | "region",
regionStack: RegionStack | null,
) => void

Expand Down Expand Up @@ -32,7 +32,7 @@ type RegionStack = {

export let regionStack: RegionStack | null = null

export const reportDeclaration = (node: Node) => {
export const reportDeclaration = (node: Node | "region") => {
if (reporter) {
reporter(node, regionStack)
}
Expand All @@ -47,16 +47,19 @@ export const readSidRoot = (sid?: string | null) => {
}

export function withRegion(unit: NodeUnit, cb: () => void) {
const meta = getGraph(unit).meta || {}

regionStack = {
parent: regionStack,
value: unit,
template: getMeta(unit, 'template') || readTemplate(),
sidRoot: getMeta(unit, 'sidRoot') || (regionStack && regionStack.sidRoot),
meta: getGraph(unit).meta || {},
template: meta.template || readTemplate(),
sidRoot: meta.sidRoot || (regionStack && regionStack.sidRoot),
meta: meta,
}
try {
return cb()
} finally {
reportDeclaration("region")
regionStack = getParent(regionStack)
}
}
Expand Down

0 comments on commit e68e354

Please sign in to comment.