zakscode/src/services/momentum.service.ts
ztimson 94c2f336f6
All checks were successful
Build Website / Build NPM Project (push) Successful in 19s
Build Website / Tag Version (push) Successful in 7s
Build Website / Build & Push Dockerfile (push) Successful in 42s
Zakscode is now using momentum
2024-12-30 12:05:38 -05:00

32 lines
1.2 KiB
TypeScript

import {environment} from '@/environments/environment';
import {PathEvent} from '@ztimson/utils';
import {ref, onUnmounted} from 'vue';
import {Momentum} from '@ztimson/momentum';
export function mRef<T>(event: string, get: (event?: PathEvent, ...args: any[]) => T | Promise<T>): any;
export function mRef<T>(event: string, init: () => T | Promise<T>, get: (event: PathEvent, ...args: any[]) => T | Promise<T>): any;
export function mRef<T>(event: string, fn1: (...args: any[]) => T | Promise<T>, fn2?: (event: PathEvent, ...args: any[]) => T | Promise<T>): any {
// Init
const resp = fn1(), promise = resp instanceof Promise;
const r = ref<T | null>(promise ? null : resp);
if(promise) resp.then(v => r.value = v);
// Get
const get = fn2 || fn1;
mWatch(event, async (event, ...args) => r.value = await get(<any>event, ...args));
return r;
}
export function mWatch(event: string, get: (event?: PathEvent, ...args: any[]) => any) {
const unsubscribe = momentum.on(event, get);
onUnmounted(() => unsubscribe());
}
export const momentum = new Momentum(environment.apiUrl, {
app: 'ZaksCode',
logLevel: 'ERROR',
persist: true,
socket: true
});
momentum.client.inject(true);