Skip to content
View andreaperinu's full-sized avatar
Block or Report

Block or report andreaperinu

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
andreaperinu/README.md
export const useStore = (shouldListen = true): [StoreState, StoreDispatch] => {
	const setState = useState(state)[1];

	const dispatch: StoreDispatch = async ({ type, payload }) => {
		const newState = await updatedState(type, payload)
		state = { ...state, ...newState }

		for (const listener of listeners) listener(state)
	};

	const listenerFilter = useCallback((listener: React.Dispatch<React.SetStateAction<StoreState>>): boolean => (
		listener !== setState
	), [setState]);

	useEffect(() => {
		pipe(shouldListen, B.fold(constVoid, () => listeners.push(setState)));

		return () => {
			listeners = pipe(
				shouldListen,
				O.fromPredicate(I.of),
				O.map(() => pipe(listeners, A.filter(listenerFilter))),
				O.getOrElse(() => listeners),
			)
		}
	}, [setState, shouldListen]);

	return [state, useCallback(dispatch, [])];
}

Pinned

  1. dogs-dashboard dogs-dashboard Public

    TypeScript

  2. next-secure-auth next-secure-auth Public

    Next.JS HttpOnly cookie POC

    TypeScript