Zakscode is now using momentum
This commit is contained in:
31
src/services/momentum.service.ts
Normal file
31
src/services/momentum.service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user