v0 · Developer PreviewSorus is under active development. APIs may change between releases.

Equality and hashing

Example

A refetched Model.Schema row under a held key is merged into the held one: a component that reads the room list reruns when a plain field such as name changes, and one that reads $topic reruns when the topic does.

ts
import { Box, Collection, Model, Realm } from "@frondruntime/sorus-effect";
import { Schema } from "effect";
import { app } from "./realm.ts";
import { Room } from "./snapshotsEffect.ts";

const $rooms = Box.schema(app, Schema.Array(Room), [], { name: "rooms" });

const receiveRooms = (json: unknown) => {
  const fresh = Model.decode(app, Schema.Array(Room))(json);
  Realm.update(app, (read, write) => {
    // A held id keeps its Room, and the new topic goes into its $topic box.
    write($rooms, Collection.reconcile(read($rooms), fresh, { keyOf: (room) => room.id }));
  });
};

Unread defines its own [Equal.symbol] and [Hash.symbol]: equal by room, whatever its countedAt, so a refetched count lands in the held $count.

ts
import { Box, Collection, Model, Realm } from "@frondruntime/sorus-effect";
import { Equal, Hash, Schema } from "effect";
import { app } from "./realm.ts";

class Unread extends Model.Schema<Unread>("Unread")({
  roomId: Schema.String,
  // When the server counted; Equal ignores it.
  countedAt: Schema.Number,
  $count: Model.boxField(Schema.Number),
}) {
  // One count per room, whatever countedAt says. The key alone: Effect caches the hash.
  [Equal.symbol](that: unknown): boolean {
    return that instanceof Unread && that.realm === this.realm && this.roomId === that.roomId;
  }

  [Hash.symbol](): number {
    return Hash.string(this.roomId);
  }
}

const $unread = Box.schema(app, Schema.Array(Unread), [], { name: "unread" });

const receiveUnread = (json: unknown) => {
  const fresh = Model.decode(app, Schema.Array(Unread))(json);
  Realm.update(app, (read, write) => {
    // A room already held keeps its Unread instance; the new count is written into its box.
    write(
      $unread,
      Collection.reconcile(read($unread), fresh, { keyOf: (unread) => unread.roomId }),
    );
  });
};

Rules

  • A schema box with Schema.Unknown, Schema.Any, or Schema.ObjectKeyword compares with Object.is, because a structural compare would hash the whole value.
  • A Model.Schema instance compares its plain fields by value and its boxes and realm by identity, so two decodes of a model with a $ field are not equal.
  • A refetched instance under a held key is merged when both are of one class and realm and their plain fields are equal, or, when the class defines [Equal.symbol], that method finds them equal. Any other difference replaces the instance.
  • Define [Hash.symbol] beside [Equal.symbol], on plain fields such as the key: Effect’s Equal.equals compares hashes first and caches them.

Answers

Constructor Compares with, by default
Box.make, Collection.make, Computed.make Object.is, row by row for a collection. A computed value keeps it over a Box.schema.
Box.schema, Model.boxField The schema’s equivalence (Schema.toEquivalence).
Model.collectionField A merge for a Model.Schema row, the row schema’s equivalence otherwise.
Collection.reconcile A merge for a Model.Schema row, a schema class’s equivalence, else Equal.equals. Given equals, only equals, with no merge.

See also