summaryrefslogtreecommitdiff
path: root/server/src/server.ts
blob: 09735186fa749ede64595b1cb79413115d4c647b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Context } from './context.model'
import { bindOperations } from './operations'
import { DatabaseChange } from '../../lib/database-change.model'
import WebSocket from 'ws'

import { Database } from 'naive-core'

type CleanupRoutine = () => Promise<void>

export const runServer = async (ctx: Context): Promise<CleanupRoutine> => {
  const db = new Database()
  await db.init()
  const wss = new WebSocket.Server({ port: ctx.wsPort })

  const socketCleanup = () =>
    new Promise(resolve => {
      wss.close(resolve)
    })

  const operationCleanup = await bindOperations(
    ctx,
    db,
    async (dbChange: DatabaseChange) => {
      const changeBuff = JSON.stringify(dbChange)
      // TODO figure out if `client` cares about
      // `dbChange` instead of just assuming that
      // `client` cares because `subscribe` has been
      // called at least once
      for (let client of wss.clients) {
        client.send(changeBuff)
      }
    }
  )
  return async () => {
    Promise.all([operationCleanup(), socketCleanup()])
  }
}