timelines: add createControlsForLookup
This commit is contained in:
		
							parent
							
								
									acde7609ba
								
							
						
					
					
						commit
						ad729f4f34
					
				
					 1 changed files with 26 additions and 28 deletions
				
			
		| 
						 | 
				
			
			@ -18,6 +18,30 @@ type Timeline<T extends mastodon.DefaultPaginationParams> = {
 | 
			
		|||
 | 
			
		||||
type TimelineParamsOf<T> = T extends Timeline<infer P> ? P : never;
 | 
			
		||||
 | 
			
		||||
function createControlsForLookup(
 | 
			
		||||
  lookup: ReactiveMap<string, TreeNode<mastodon.v1.Status>>,
 | 
			
		||||
) {
 | 
			
		||||
  return {
 | 
			
		||||
    get(id: string) {
 | 
			
		||||
      return lookup.get(id);
 | 
			
		||||
    },
 | 
			
		||||
    getPath(id: string) {
 | 
			
		||||
      const node = lookup.get(id);
 | 
			
		||||
      if (!node) return;
 | 
			
		||||
      const path = collectPath(node);
 | 
			
		||||
      for (const sym of path) {
 | 
			
		||||
        lookup.get(sym.value.id); // Track every node on the path
 | 
			
		||||
      }
 | 
			
		||||
      return path;
 | 
			
		||||
    },
 | 
			
		||||
    set(id: string, value: mastodon.v1.Status) {
 | 
			
		||||
      const node = untrack(() => lookup.get(id));
 | 
			
		||||
      if (!node) return;
 | 
			
		||||
      lookup.set(id, {...node, value});
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function createTimelineControlsForArray(
 | 
			
		||||
  status: () => mastodon.v1.Status[] | undefined,
 | 
			
		||||
): TimelineControls {
 | 
			
		||||
| 
						 | 
				
			
			@ -68,20 +92,7 @@ export function createTimelineControlsForArray(
 | 
			
		|||
 | 
			
		||||
  return {
 | 
			
		||||
    list: threads,
 | 
			
		||||
    get(id: string) {
 | 
			
		||||
      return lookup.get(id);
 | 
			
		||||
    },
 | 
			
		||||
    getPath(id: string) {
 | 
			
		||||
      const node = lookup.get(id);
 | 
			
		||||
      if (!node) return;
 | 
			
		||||
      return collectPath(node);
 | 
			
		||||
    },
 | 
			
		||||
    set(id: string, value: mastodon.v1.Status) {
 | 
			
		||||
      const node = untrack(() => lookup.get(id));
 | 
			
		||||
      if (!node) return;
 | 
			
		||||
      node.value = value;
 | 
			
		||||
      lookup.set(id, node);
 | 
			
		||||
    },
 | 
			
		||||
    ...createControlsForLookup(lookup),
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -307,20 +318,7 @@ export function createTimeline<
 | 
			
		|||
  return [
 | 
			
		||||
    {
 | 
			
		||||
      list: threads,
 | 
			
		||||
      get(id: string) {
 | 
			
		||||
        return lookup.get(id);
 | 
			
		||||
      },
 | 
			
		||||
      getPath(id: string) {
 | 
			
		||||
        const node = lookup.get(id);
 | 
			
		||||
        if (!node) return;
 | 
			
		||||
        return collectPath(node);
 | 
			
		||||
      },
 | 
			
		||||
      set(id: string, value: mastodon.v1.Status) {
 | 
			
		||||
        const node = untrack(() => lookup.get(id));
 | 
			
		||||
        if (!node) return;
 | 
			
		||||
        node.value = value;
 | 
			
		||||
        lookup.set(id, node);
 | 
			
		||||
      },
 | 
			
		||||
      ...createControlsForLookup(lookup),
 | 
			
		||||
    },
 | 
			
		||||
    chunk,
 | 
			
		||||
    { refetch },
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue