mirror of
https://github.com/pockethost/pockethost.git
synced 2025-11-26 23:45:57 +00:00
enh(pockethost): add event handlers to MothershipMirrorService
This commit is contained in:
parent
a3a8a215f2
commit
7d9ccf0639
@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
createEvent,
|
||||||
EDGE_APEX_DOMAIN,
|
EDGE_APEX_DOMAIN,
|
||||||
InstanceFields,
|
InstanceFields,
|
||||||
InstanceId,
|
InstanceId,
|
||||||
@ -18,6 +19,11 @@ export type MothershipMirrorServiceConfig = SingletonBaseConfig & {
|
|||||||
export const MothershipMirrorService = mkSingleton(async (config: MothershipMirrorServiceConfig) => {
|
export const MothershipMirrorService = mkSingleton(async (config: MothershipMirrorServiceConfig) => {
|
||||||
const { dbg, error } = (config.logger ?? LoggerService()).create(`MothershipMirrorService`)
|
const { dbg, error } = (config.logger ?? LoggerService()).create(`MothershipMirrorService`)
|
||||||
|
|
||||||
|
const [onInstanceUpserted, fireInstanceUpserted] = createEvent<InstanceFields>()
|
||||||
|
const [onInstanceDeleted, fireInstanceDeleted] = createEvent<InstanceId>()
|
||||||
|
const [onUserUpserted, fireUserUpserted] = createEvent<UserFields>()
|
||||||
|
const [onUserDeleted, fireUserDeleted] = createEvent<UserId>()
|
||||||
|
|
||||||
const client = config.client
|
const client = config.client
|
||||||
|
|
||||||
const mirror: {
|
const mirror: {
|
||||||
@ -37,7 +43,7 @@ export const MothershipMirrorService = mkSingleton(async (config: MothershipMirr
|
|||||||
}
|
}
|
||||||
|
|
||||||
const upsertInstance = (record: InstanceFields) => {
|
const upsertInstance = (record: InstanceFields) => {
|
||||||
dbg(`upsertInstance`, { record })
|
dbg(`upsertInstance ${record.id}`)
|
||||||
deleteInstance(record.id)
|
deleteInstance(record.id)
|
||||||
const canonicalId = `${record.id}.${EDGE_APEX_DOMAIN()}`
|
const canonicalId = `${record.id}.${EDGE_APEX_DOMAIN()}`
|
||||||
const canonicalSubdomain = `${record.subdomain}.${EDGE_APEX_DOMAIN()}`
|
const canonicalSubdomain = `${record.subdomain}.${EDGE_APEX_DOMAIN()}`
|
||||||
@ -64,7 +70,7 @@ export const MothershipMirrorService = mkSingleton(async (config: MothershipMirr
|
|||||||
}
|
}
|
||||||
|
|
||||||
const upsertUser = (record: UserFields) => {
|
const upsertUser = (record: UserFields) => {
|
||||||
dbg(`upsertUser`, { record })
|
dbg(`upsertUser ${record.id}`)
|
||||||
deleteUser(record.id)
|
deleteUser(record.id)
|
||||||
mirror.users[record.id] = record
|
mirror.users[record.id] = record
|
||||||
}
|
}
|
||||||
@ -76,35 +82,39 @@ export const MothershipMirrorService = mkSingleton(async (config: MothershipMirr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.collection(`instances`).subscribe<InstanceFields>(`*`, (e) => {
|
client
|
||||||
const { action, record } = e
|
.collection(`instances`)
|
||||||
if (action === `create` || action === `update`) {
|
.subscribe<InstanceFields>(`*`, (e) => {
|
||||||
dbg(`instance`, { action, record })
|
const { action, record } = e
|
||||||
upsertInstance(record)
|
if (action === `create` || action === `update`) {
|
||||||
}
|
dbg(`instance`, { action, record })
|
||||||
if (action === `delete`) {
|
upsertInstance(record)
|
||||||
dbg(`instance`, { action, record })
|
fireInstanceUpserted(record)
|
||||||
deleteInstance(record.id)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
client.collection(`users`).subscribe<UserFields>(`*`, (e) => {
|
|
||||||
const deleteUser = (id: UserId) => {
|
|
||||||
const oldUser = mirror.users[id]
|
|
||||||
if (oldUser) {
|
|
||||||
delete mirror.users[oldUser.id]
|
|
||||||
}
|
}
|
||||||
}
|
if (action === `delete`) {
|
||||||
const { action, record } = e
|
dbg(`instance`, { action, record })
|
||||||
if (action === `create` || action === `update`) {
|
deleteInstance(record.id)
|
||||||
dbg(`user`, { action, record })
|
fireInstanceDeleted(record.id)
|
||||||
upsertUser(record)
|
}
|
||||||
}
|
})
|
||||||
if (action === `delete`) {
|
.catch(error)
|
||||||
dbg(`user`, { action, record })
|
|
||||||
deleteUser(record.id)
|
client
|
||||||
}
|
.collection(`users`)
|
||||||
})
|
.subscribe<UserFields>(`*`, (e) => {
|
||||||
|
const { action, record } = e
|
||||||
|
if (action === `create` || action === `update`) {
|
||||||
|
dbg(`user`, { action, record })
|
||||||
|
upsertUser(record)
|
||||||
|
fireUserUpserted(record)
|
||||||
|
}
|
||||||
|
if (action === `delete`) {
|
||||||
|
dbg(`user`, { action, record })
|
||||||
|
deleteUser(record.id)
|
||||||
|
fireUserDeleted(record.id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error)
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
dbg(`init`)
|
dbg(`init`)
|
||||||
@ -146,6 +156,12 @@ export const MothershipMirrorService = mkSingleton(async (config: MothershipMirr
|
|||||||
async getUser(id: UserId) {
|
async getUser(id: UserId) {
|
||||||
return mirror.users[id]
|
return mirror.users[id]
|
||||||
},
|
},
|
||||||
|
onInstanceUpserted,
|
||||||
|
onInstanceDeleted,
|
||||||
|
onUserDeleted,
|
||||||
|
onUserUpserted,
|
||||||
|
getInstances: () => Object.values(mirror.instancesById),
|
||||||
|
getUsers: () => Object.values(mirror.users),
|
||||||
}
|
}
|
||||||
return api
|
return api
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user