Skip to content
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
2 changes: 1 addition & 1 deletion docs/framework/preact/reference/functions/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: shallow
function shallow<T>(objA, objB): boolean;
```

Defined in: [index.ts:67](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L67)
Defined in: [index.ts:130](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L130)

## Type Parameters

Expand Down
4 changes: 2 additions & 2 deletions docs/framework/preact/reference/functions/useStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function useStore<TState, TSelected>(
options?): TSelected;
```

Defined in: [index.ts:41](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L41)
Defined in: [index.ts:104](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L104)

### Type Parameters

Expand Down Expand Up @@ -53,7 +53,7 @@ function useStore<TState, TSelected>(
options?): TSelected;
```

Defined in: [index.ts:46](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L46)
Defined in: [index.ts:109](https://github.com/TanStack/store/blob/main/packages/preact-store/src/index.ts#L109)

### Type Parameters

Expand Down
69 changes: 65 additions & 4 deletions packages/preact-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
import { useSyncExternalStore } from 'preact/compat'
import { useRef } from 'preact/hooks'
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks'
import type { Derived, Store } from '@tanstack/store'

export * from '@tanstack/store'

type InternalStore = {
_value: any
_getSnapshot: () => any
}

type StoreRef = {
_instance: InternalStore
}

/**
* This is taken from https://github.com/preactjs/preact/blob/main/compat/src/hooks.js#L8-L54
* which is taken from https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L84
* on a high level this cuts out the warnings, ... and attempts a smaller implementation.
* This way we don't have to import preact/compat with side effects
*/
function useSyncExternalStore(
subscribe: (onStoreChange: () => void) => () => void,
getSnapshot: () => any,
) {
const value = getSnapshot()

const [{ _instance }, forceUpdate] = useState<StoreRef>({
_instance: { _value: value, _getSnapshot: getSnapshot },
})

useLayoutEffect(() => {
_instance._value = value
_instance._getSnapshot = getSnapshot

if (didSnapshotChange(_instance)) {
forceUpdate({ _instance })
}
}, [subscribe, value, getSnapshot])

useEffect(() => {
if (didSnapshotChange(_instance)) {
forceUpdate({ _instance })
}

return subscribe(() => {
if (didSnapshotChange(_instance)) {
forceUpdate({ _instance })
}
})
}, [subscribe])

return value
}

function didSnapshotChange(inst: {
_getSnapshot: () => any
_value: any
}): boolean {
const latestGetSnapshot = inst._getSnapshot
const prevValue = inst._value
try {
const nextValue = latestGetSnapshot()
return !Object.is(prevValue, nextValue)
// eslint-disable-next-line no-unused-vars
} catch (_error) {
return true
}
}

/**
* @private
*/
Expand Down Expand Up @@ -120,5 +183,3 @@ function getOwnKeys(obj: object): Array<string | symbol> {
Object.getOwnPropertySymbols(obj),
)
}

// force
Loading