From 05aa3fd5bcbc49c2a6efc90fad49162e0cc2fee8 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Sun, 22 Oct 2023 10:27:52 +0900 Subject: [PATCH] chore!: remove onceWriteDoc() Fixes #15, this is no longer needed by @mapeo/core, since this is done outside of the indexer --- index.js | 41 ------------- test/index-callback.js | 131 ----------------------------------------- 2 files changed, 172 deletions(-) delete mode 100644 test/index-callback.js diff --git a/index.js b/index.js index 93c8795..b5f2a40 100644 --- a/index.js +++ b/index.js @@ -49,9 +49,6 @@ export class DbApi { #docDefaults #tableInfo - /** @type {Map>>} */ - #listeners = new Map() - /** * @param {import('better-sqlite3').Database} db * @param {object} options @@ -123,36 +120,6 @@ export class DbApi { } } this.#writeDocSql.run(flattenedDoc) - - const { versionId } = doc - if (this.#listeners.has(versionId)) { - process.nextTick(() => { - const set = this.#listeners.get(versionId) - if (set) { - for (const listener of set.values()) { - listener(doc) - } - this.#listeners.delete(versionId) - } - }) - } - } - /** - * @param {string} versionId - * @param {IndexCallback} listener - */ - onceWriteDoc(versionId, listener) { - if (!this.#listeners.has(versionId)) { - this.#listeners.set(versionId, new Set()) - } - - const set = this.#listeners.get(versionId) - if (set && set.has(listener)) { - return - } else if (set) { - set.add(listener) - this.#listeners.set(versionId, set) - } } /** * @param {string} docId @@ -257,14 +224,6 @@ export default class SqliteIndexer { isLinked(versionId) { return !!this.#dbApi.getBacklink(versionId) } - - /** - * @param {string} versionId - * @param {IndexCallback} listener - */ - onceWriteDoc(versionId, listener) { - this.#dbApi.onceWriteDoc(versionId, listener) - } } /** diff --git a/test/index-callback.js b/test/index-callback.js deleted file mode 100644 index 6b396fc..0000000 --- a/test/index-callback.js +++ /dev/null @@ -1,131 +0,0 @@ -// @ts-check -import test from 'tape' -import { create } from './utils.js' - -const docs = [ - { - docId: 'A', - seq: 1, - versionId: '1', - links: [], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 2, - versionId: '2', - links: ['1'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 3, - versionId: '3', - links: ['1'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 4, - versionId: '4', - links: ['2', '3'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 5, - versionId: '5', - links: ['4'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 6, - versionId: '6', - links: ['4'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 7, - versionId: '7', - links: ['4'], - updatedAt: '', - deleted: false, - }, - { - docId: 'A', - seq: 8, - versionId: '8', - links: ['5', '6'], - updatedAt: '', - deleted: false, - }, -] - -test('onceWriteDoc called for each doc', async (t) => { - t.plan(8) - /** - * @param {import('../index.js').IndexedDocument} doc - */ - function onWriteDoc(doc) { - t.ok(doc) - } - - const { indexer, cleanup, clear } = create() - for (const doc of docs) { - indexer.onceWriteDoc(doc.versionId, onWriteDoc) - } - indexer.batch(docs) - clear() - cleanup() -}) - -test('multiple listeners on the same version', async (t) => { - t.plan(3) - - const { indexer, cleanup, clear } = create() - - indexer.onceWriteDoc('1', function (doc) { - t.ok(doc) - }) - - indexer.onceWriteDoc('1', function (doc) { - t.ok(doc) - }) - - indexer.onceWriteDoc('1', function (doc) { - t.ok(doc) - }) - - indexer.batch(docs) - clear() - cleanup() -}) - -test('same listener used multiple times only called once', async (t) => { - t.plan(1) - - const { indexer, cleanup, clear } = create() - - /** - * @param {import('../index.js').IndexedDocument} doc - */ - function onWriteDoc(doc) { - t.ok(doc) - } - - indexer.onceWriteDoc('1', onWriteDoc) - indexer.onceWriteDoc('1', onWriteDoc) - indexer.onceWriteDoc('1', onWriteDoc) - - indexer.batch(docs) - clear() - cleanup() -})