view src/Patch.ts @ 150:3ce355e4f388 default tip

bye jest; hi vitest. new working test for styles.ts
author drewp@bigasterisk.com
date Mon, 08 May 2023 17:27:44 -0700
parents cf642d395be4
children
line wrap: on
line source

import { Quad, Store } from "n3";
import { Stream } from "rdf-js";

export enum PatchDirection {
  ADD = "+",
  DEL = "-",
}

export class Patch {
  quads: Quad[] = [];

  constructor(public direction: PatchDirection) {}

  toString(): string {
    return `Patch ${this.direction} ${this.quads.length}`;
  }

  public async streamImport(quadStream: Stream): Promise<void> {
    return new Promise((resolve, reject) => {
      quadStream.on("data", (quad) => {
        this.quads.push(quad);
      });
      quadStream.on("error", reject);
      quadStream.on("end", resolve);
    });
  }

  public applyToStore(s: Store) {
    if (this.direction == PatchDirection.ADD) {
      s.addQuads(this.quads);
    } else {
      s.removeQuads(this.quads);
    }
  }
  public isEmpty(): boolean {
    return this.quads.length == 0;
  }
}