6eaf3c8c92
* feat(workspaces): drop createdByUserId from the dataschema * feat(workspaces): repositories WIP * merge * protect against removing last admin in workspace * quick impl and stub tests * add tests * services * unit tests for role services * feat(workspaces): authorize project creation if workspace specified * feat(workspaces): emit project created event * feat(workspaces): assign roles on project create in workspace * feat(workspaces): update project roles when user added to workspace * feat(workspaces): stencil gql resolvers * fix(workspaces): lol lmao * fix(workspaces): perform automatic project role update in service function * fix(workspaces): also delete roles * fix(workspaces): broke tests again oops * fix(workspaces): update `onProjectCreated` listener to use new repo method * fix(workspaces): use service function in event listener * fix(workspaces): get workspace projects via existing stream repo functions * fix(workspaces): roles mapping in domain, use enum * feat(workspaces): stencil gql api and resolvers * fix(workspaces): repair type reference in tests * fix(workspaces): consolidate files, use different existing stream-getter * fix(workspaces): more specific error * fix(workspaces): roles and scopes * fix(workspaces): yield per page * fix(workspaces): some test dry * fix(workspaces): superdry * fix(workspaces): add scopes * fix(workspaces): classic * feat(workspaces): create workspace mutation * feat(workspaces): I'm sure everything will be fine * fix(workspaces): yep * fix(workspaces): successful gql e2e test * feat(workspaces): update workspace resolver * chore(workspaces): update resolver test * feat(workspaces): some retrieval resolvers * chore(workspaces): tests for query resolvers * fix(chore): revert temp test command change * fix(workspaces): test structure and gql types * fix(workspaces): validate user authz to perform some operations * fix(workspaces): use existing test infrastructure * fix(workspaces): stop `isPublic` check if authorizing a workspace resource * fix(workspaces): better test hygiene --------- Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
127 lines
3.8 KiB
TypeScript
127 lines
3.8 KiB
TypeScript
import {
|
|
WorkspaceEventsPayloads,
|
|
workspaceEventNamespace
|
|
} from '@/modules/workspacesCore/domain/events'
|
|
import { MaybeAsync } from '@speckle/shared'
|
|
import { UnionToIntersection } from 'type-fest'
|
|
|
|
import EventEmitter from 'eventemitter2'
|
|
|
|
type EventWildcard = '*'
|
|
|
|
type TestEvents = {
|
|
['test.string']: string
|
|
['test.number']: number
|
|
}
|
|
|
|
// we should only ever extend this type, other helper types will be derived from this
|
|
type EventsByNamespace = {
|
|
test: TestEvents
|
|
[workspaceEventNamespace]: WorkspaceEventsPayloads
|
|
}
|
|
|
|
type EventTypes = UnionToIntersection<EventsByNamespace[keyof EventsByNamespace]>
|
|
|
|
// generated union to collect all event
|
|
type EventNamesByNamespace = {
|
|
[Namespace in keyof EventsByNamespace]: keyof EventsByNamespace[Namespace]
|
|
}
|
|
|
|
// generated type for a top level wildcard one level nested wildcards per namespace and each possible event
|
|
type EventSubscriptionKey =
|
|
| EventWildcard
|
|
| `${keyof EventNamesByNamespace}.${EventWildcard}`
|
|
| {
|
|
[Namespace in keyof EventNamesByNamespace]: EventNamesByNamespace[Namespace]
|
|
}[keyof EventNamesByNamespace]
|
|
|
|
// generated flatten of each specific event name with the emitted event type
|
|
type EventPayloadsMap = UnionToIntersection<
|
|
EventPayloadsByNamespaceMap[keyof EventPayloadsByNamespaceMap]
|
|
>
|
|
|
|
type EventNames = keyof EventPayloadsMap
|
|
|
|
type EventPayloadsByNamespaceMap = {
|
|
// for each event namespace
|
|
[Key in keyof EventsByNamespace]: {
|
|
// for each event
|
|
[EventName in keyof EventsByNamespace[Key]]: {
|
|
// create a type with they original event as the payload, and the eventName
|
|
eventName: EventName
|
|
payload: EventsByNamespace[Key][EventName]
|
|
}
|
|
}
|
|
}
|
|
|
|
type EventPayload<T extends EventSubscriptionKey> = T extends EventWildcard
|
|
? // if event key is "*", get all events from the flat object
|
|
EventPayloadsMap[keyof EventPayloadsMap]
|
|
: // else if, the key is a "namespace.*" wildcard
|
|
T extends `${infer Namespace}.${EventWildcard}`
|
|
? // the Namespace needs to extend the keys of the type, otherwise we never
|
|
Namespace extends keyof EventPayloadsByNamespaceMap
|
|
? // get the union type of all possible events in a namespace
|
|
EventPayloadsByNamespaceMap[Namespace][keyof EventPayloadsByNamespaceMap[Namespace]]
|
|
: never
|
|
: // else if, the key is a "namespace.event" concrete key
|
|
T extends keyof EventPayloadsMap
|
|
? EventPayloadsMap[T]
|
|
: never
|
|
|
|
export function initializeEventBus() {
|
|
const emitter = new EventEmitter({ wildcard: true })
|
|
|
|
return {
|
|
/**
|
|
* Emit a module event. This function must be awaited to ensure all listeners
|
|
* execute. Any errors thrown in the listeners will bubble up and throw from
|
|
* the part of code that triggers this emit() call.
|
|
*/
|
|
emit: async <EventName extends EventNames>(args: {
|
|
eventName: EventName
|
|
payload: EventTypes[EventName]
|
|
}): Promise<unknown[]> => {
|
|
// curate the proper payload here and eventName object here, before emitting
|
|
return emitter.emitAsync(args.eventName, args)
|
|
},
|
|
|
|
/**
|
|
* Listen for module events. Any errors thrown here will bubble out of where
|
|
* emit() was invoked.
|
|
*
|
|
* @returns Callback for stopping listening
|
|
*/
|
|
listen: <K extends EventSubscriptionKey>(
|
|
eventName: K,
|
|
// we should add some error type object here with a type discriminator
|
|
handler: (event: EventPayload<K>) => MaybeAsync<unknown>
|
|
) => {
|
|
emitter.on(eventName, handler, {
|
|
async: true,
|
|
promisify: true
|
|
})
|
|
|
|
return () => {
|
|
emitter.removeListener(eventName, handler)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Destroy event emitter
|
|
*/
|
|
destroy() {
|
|
emitter.removeAllListeners()
|
|
}
|
|
}
|
|
}
|
|
|
|
export type EventBus = ReturnType<typeof initializeEventBus>
|
|
|
|
let eventBus: EventBus
|
|
|
|
export function getEventBus(): EventBus {
|
|
if (!eventBus) eventBus = initializeEventBus()
|
|
return eventBus
|
|
}
|