SkipLabs / skip

Skip is a framework for building reactive services
https://skiplabs.io
MIT License
155 stars 10 forks source link

Fixes #496: support for joinCollections #500

Open pikatchu opened 1 week ago

pikatchu commented 1 week ago

This diff implements the 'join' operation of two collections. It is assumed that:

If both those conditions are met, given a key, if the first collection defines an object of type V1 and the second collection defines an object V2 for the same key, the resulting collection will associate {...V1, ...V2} for that key. No value is associated if either side is missing.

jberdine commented 1 week ago

Summarizing offline discussion: It would be possible to give joinCollections the type

export function joinCollections<
  K extends Json,
  V1 extends JsonObject,
  V2 extends JsonObject,
>(
  col1: EagerCollection<K, V1>,
  col2: EagerCollection<K, V2>,
  allowMultipleValuesOnBothSides: boolean = false,
): EagerCollection<K, V1 & V2>

and propagate JsonObject down, eliminating the checks that each value is an object.

BUT, since typescript unsoundly treats method parameters as bivariant, calling joinCollections e.g. from createGraph where inputCollections: Record<string, EagerCollection<Json, Json>> will not detect mistakes where general Json values are provided where JsonObjects are required. So this PR's dynamic checks on each value are still needed. Thanks for nothing typescript.