Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Style updates not properly cleaning previous subscriptions #11

Merged
merged 6 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 203 additions & 2 deletions packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createFacet } from '@react-facet/core'
import { createFacet, Facet } from '@react-facet/core'
import React, { ReactElement, useEffect, useState } from 'react'
import { Fiber } from 'react-reconciler'
import { createFiberRoot } from './createFiberRoot'
import { createReconciler } from './createReconciler'
import { InputType } from './types'
import { setupHostConfig } from './setupHostConfig'
import { InputType, ElementContainer, ElementProps, Props } from './types'

document.body.innerHTML = `<div id="root"></div>`

Expand Down Expand Up @@ -898,3 +900,202 @@ describe('update', () => {
})
})
})

describe('commitUpdate style prop', () => {
it('subscribes when updating from null', () => {
const hostConfig = setupHostConfig()
const instance: ElementContainer = {
element: document.createElement('div'),
styleUnsubscribers: new Map(),
}

const oldProps: ElementProps<HTMLDivElement> = {
style: {
color: undefined,
},
}

const newColorFacet: Facet<string> = {
get: () => 'blue',
observe: jest.fn(),
}

const newProps: ElementProps<HTMLDivElement> = {
style: {
color: newColorFacet,
},
}

hostConfig.commitUpdate?.(
instance,
true,
'fast-div',
oldProps as Props<HTMLDivElement>,
newProps as Props<HTMLDivElement>,
null as unknown as Fiber,
)

// Adds a new subscription to the new Facet
expect(newColorFacet.observe).toHaveBeenCalledTimes(1)
})

it('unsubscribes when updating to null', () => {
const colorUnsubscriber = jest.fn()

const hostConfig = setupHostConfig()
const instance: ElementContainer = {
element: document.createElement('div'),
styleUnsubscribers: new Map([['color', colorUnsubscriber]]),
}

const oldColorFacet: Facet<string> = {
get: () => 'blue',
observe: jest.fn(),
}

const oldProps: ElementProps<HTMLDivElement> = {
style: {
color: oldColorFacet,
},
}

const newProps: ElementProps<HTMLDivElement> = {
style: {
color: undefined,
},
}

hostConfig.commitUpdate?.(
instance,
true,
'fast-div',
oldProps as Props<HTMLDivElement>,
newProps as Props<HTMLDivElement>,
null as unknown as Fiber,
)

expect(colorUnsubscriber).toHaveBeenCalledTimes(1)
})

it('unsubscribes from previous facet when changing to a primitive value', () => {
const colorUnsubscriber = jest.fn()

const hostConfig = setupHostConfig()
const instance: ElementContainer = {
element: document.createElement('div'),
styleUnsubscribers: new Map([['color', colorUnsubscriber]]),
}

const oldProps: ElementProps<HTMLDivElement> = {
style: {
color: createFacet({ initialValue: 'blue' }),
},
}

const newProps: ElementProps<HTMLDivElement> = {
style: {
color: 'yellow',
},
}

hostConfig.commitUpdate?.(
instance,
true,
'fast-div',
oldProps as Props<HTMLDivElement>,
newProps as Props<HTMLDivElement>,
null as unknown as Fiber,
)

expect(colorUnsubscriber).toHaveBeenCalledTimes(1)
})

it('unsubscribes from previous facet when changing to a new facet', () => {
const colorUnsubscriber = jest.fn()

const hostConfig = setupHostConfig()
const instance: ElementContainer = {
element: document.createElement('div'),
styleUnsubscribers: new Map([['color', colorUnsubscriber]]),
}

const oldColorFacet: Facet<string> = {
get: () => 'blue',
observe: jest.fn(),
}

const oldProps: ElementProps<HTMLDivElement> = {
style: {
color: oldColorFacet,
},
}

const newColorFacet: Facet<string> = {
get: () => 'blue',
observe: jest.fn(),
}

const newProps: ElementProps<HTMLDivElement> = {
style: {
color: newColorFacet,
},
}

hostConfig.commitUpdate?.(
instance,
true,
'fast-div',
oldProps as Props<HTMLDivElement>,
newProps as Props<HTMLDivElement>,
null as unknown as Fiber,
)

// Unsubscribes from the old subscription, since it is a new Facet
expect(colorUnsubscriber).toHaveBeenCalledTimes(1)

// Adds a new subscription to the new Facet
expect(newColorFacet.observe).toHaveBeenCalledTimes(1)
})

it('keeps the same subscription when updating with the same facet', () => {
const colorUnsubscriber = jest.fn()

const hostConfig = setupHostConfig()
const instance: ElementContainer = {
element: document.createElement('div'),
styleUnsubscribers: new Map([['color', colorUnsubscriber]]),
}

const colorFacet: Facet<string> = {
get: () => 'blue',
observe: jest.fn(),
}

const oldProps: ElementProps<HTMLDivElement> = {
style: {
color: colorFacet,
},
}

const newProps: ElementProps<HTMLDivElement> = {
style: {
color: colorFacet,
},
}

hostConfig.commitUpdate?.(
instance,
true,
'fast-div',
oldProps as Props<HTMLDivElement>,
newProps as Props<HTMLDivElement>,
null as unknown as Fiber,
)

// I shouldn't unsubscribe, since it is the same Facet
expect(colorUnsubscriber).not.toHaveBeenCalled()

// So I must not also observe again, since I should stick with the previous subscription
expect(colorFacet.observe).toHaveBeenCalledTimes(0)
})
})
25 changes: 14 additions & 11 deletions packages/@react-facet/dom-fiber/src/setupHostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,20 @@ export const setupHostConfig = (): HostConfig<

if (newStyleProp != null) {
for (const key in newStyleProp) {
const value = newProps.style?.[key]

if (isFacet(value)) {
styleUnsubscribers.set(
key,
value.observe((value) => {
notNullStyle[key] = value
}),
)
} else {
notNullStyle[key] = value
const oldValue = oldStyleProp?.[key]
const newValue = newStyleProp[key]

if (oldValue !== newValue || oldStyleProp == null) {
if (isFacet(newValue)) {
styleUnsubscribers.set(
key,
newValue.observe((value) => {
notNullStyle[key] = value
}),
)
} else {
notNullStyle[key] = newValue
}
}
}
}
Expand Down