personal web client for Bluesky
typescript solidjs bluesky atcute
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: some vs every guard

mary.my.id e26eb32e 0ad2f45f

verified
+35 -12
+35 -12
src/lib/hooks/guard.ts
··· 1 1 import { createMemo, createSignal, onCleanup } from 'solid-js'; 2 2 3 3 export type GuardFunction = () => boolean; 4 + type GuardKind = 'some' | 'every'; 4 5 5 - export const createGuard = (memoize = false) => { 6 + export const createGuard = (kind: GuardKind = 'some', memoize = false) => { 6 7 const [guards, setGuards] = createSignal<GuardFunction[]>([]); 7 8 8 - const isGuarded = createMemoMaybe(memoize, () => { 9 - const $guards = guards(); 10 - for (let idx = 0, len = $guards.length; idx < len; idx++) { 11 - const guard = $guards[idx]; 12 - if (guard()) { 13 - return true; 14 - } 15 - } 16 - 17 - return false; 18 - }); 9 + const isGuarded = createMemoMaybe(memoize, createIsGuarded(kind, guards)); 19 10 20 11 const addGuard = (guard: GuardFunction) => { 21 12 setGuards((guards) => guards.concat(guard)); ··· 23 14 }; 24 15 25 16 return [isGuarded, addGuard] as const; 17 + }; 18 + 19 + const createIsGuarded = (kind: GuardKind, guards: () => GuardFunction[]) => { 20 + if (kind === 'every') { 21 + return () => { 22 + const $guards = guards(); 23 + 24 + for (let idx = 0, len = $guards.length; idx < len; idx++) { 25 + const guard = $guards[idx]; 26 + if (!guard()) { 27 + return false; 28 + } 29 + } 30 + 31 + return true; 32 + }; 33 + } else if (kind === 'some') { 34 + return () => { 35 + const $guards = guards(); 36 + 37 + for (let idx = 0, len = $guards.length; idx < len; idx++) { 38 + const guard = $guards[idx]; 39 + if (guard()) { 40 + return true; 41 + } 42 + } 43 + 44 + return false; 45 + }; 46 + } 47 + 48 + return () => false; 26 49 }; 27 50 28 51 const createMemoMaybe = <T>(memoize: boolean, fn: () => T): (() => T) => {