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

Added defaultEqualityCheck to createFacet, and introduced createStaticFacet #87

Merged
merged 9 commits into from
Aug 31, 2022
13 changes: 2 additions & 11 deletions packages/@react-facet/core/src/createFacetContext.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { createStaticFacet } from './facet/createStaticFacet'
import { createContext } from 'react'
import { Facet } from './types'

export function createFacetContext<T>(initialValue: T) {
const facet: Facet<T> = {
get: () => initialValue,
observe: (listener) => {
if (process.env.NODE_ENV !== 'production') {
console.log('Missing Provider')
}
listener(initialValue)
return () => {}
},
}
const facet = createStaticFacet(initialValue)
const context = createContext(facet)
return context
}
10 changes: 8 additions & 2 deletions packages/@react-facet/core/src/facet/createFacet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export interface FacetOptions<V> {
startSubscription?: StartSubscription<V>
equalityCheck?: EqualityCheck<V>
}

export function createFacet<V>({ initialValue, startSubscription, equalityCheck }: FacetOptions<V>): WritableFacet<V> {
/**
* The low level function to create a Facet, not recommended to be used if you can use any of the react facet hooks to create facets instead (Ex: useFacetState, useFacetWrap)
*/
export function createFacet<V>({
initialValue,
startSubscription,
equalityCheck = defaultEqualityCheck,
Shenato marked this conversation as resolved.
Show resolved Hide resolved
}: FacetOptions<V>): WritableFacet<V> {
const listeners: Set<Listener<V>> = new Set()
let currentValue = initialValue
let cleanupSubscription: Cleanup | undefined
Expand Down
18 changes: 18 additions & 0 deletions packages/@react-facet/core/src/facet/createStaticFacet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Facet } from '..'
/**
* Creates a nonwritable barebones static facet to be used when you need an initial facet value outside the react context
* that's meant to be replaced later by a real facet. Ex: with `createContext()`
*/
export function createStaticFacet<T>(value: T): Facet<T> {
Shenato marked this conversation as resolved.
Show resolved Hide resolved
const facet: Facet<T> = {
get: () => value,
observe: (listener) => {
if (process.env.NODE_ENV !== 'production') {
console.log(`Accessing a static facet, perhaps you're missing a Context Provider?`)
}
listener(value)
return () => {}
},
}
return facet
}
1 change: 1 addition & 0 deletions packages/@react-facet/core/src/facet/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './createFacet'
export * from './createReadOnlyFacet'
export * from './createStaticFacet'