Adds or updates a thread in the DB. If the newly given thread is a deleted one, it will get deleted.
(thread: ThreadDataWithDeleteInfo<TM, CM>)
| 107 | |
| 108 | /** Adds or updates a thread in the DB. If the newly given thread is a deleted one, it will get deleted. */ |
| 109 | public upsert(thread: ThreadDataWithDeleteInfo<TM, CM>): void { |
| 110 | this.signal.mutate(() => { |
| 111 | thread = sanitizeThread(thread); |
| 112 | |
| 113 | const id = thread.id; |
| 114 | |
| 115 | const toRemove = this.#byId.get(id); |
| 116 | if (toRemove) { |
| 117 | // Don't do anything if the existing thread is already deleted! |
| 118 | if (toRemove.deletedAt) return false; |
| 119 | |
| 120 | this.#asc.remove(toRemove); |
| 121 | this.#desc.remove(toRemove); |
| 122 | } |
| 123 | |
| 124 | if (!thread.deletedAt) { |
| 125 | this.#asc.add(thread); |
| 126 | this.#desc.add(thread); |
| 127 | } |
| 128 | this.#byId.set(id, thread); |
| 129 | return true; |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | /** Like .upsert(), except it won't update if a thread by this ID already exists. */ |
| 134 | // TODO Consider renaming this to just .upsert(). I'm not sure if we really |
no test coverage detected