Skip to content

Instantly share code, notes, and snippets.

View VldMrgnn's full-sized avatar
🏠
working

Vlad VldMrgnn

🏠
working
  • Bucharest Romania
View GitHub Profile
@VldMrgnn
VldMrgnn / mdw-workersfetch.ts
Created January 18, 2025 04:55
Starfx Middleware for fetching data in main thread from the backend through the worker.
/*
* Showcase Example: Fetching Data via a Worker
*
* In this example, data is stored and fetched from a worker.
* It mimics the classic fetch behavior but instead passes the request through a worker.
* At request time, you can enhance the logic (e.g., add additional hashes)
* and at response time, perform parsing or other heavy-lifting operations.
*/
@VldMrgnn
VldMrgnn / server-express.ts
Last active November 19, 2024 15:19
sync a Starfx slice via y-websocket
import http from "http";
import wsUtils from "y-websocket/bin/utils";
import * as Y from "yjs";
import WebSocket from "ws";
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const docs = new Map < string, Y.Doc> ();
@VldMrgnn
VldMrgnn / clientside.ts
Last active September 23, 2024 19:38
persist the starfx store on remote
let pyDoc = new Y.Doc();
let provider: IndexeddbPersistence;
let wsProvider: WebsocketProvider;
const pxSync = (email, perseed) => {
return resource(function* (provide) {
if (!email) {
return;
}
@VldMrgnn
VldMrgnn / taskman.ts
Last active September 7, 2024 23:59
Implement a queue processing for server-side using Starfx
//this code is just for guidance.
import { each, main, on, resource, sleep } from "effection";
import {
createThunks,
keepAlive,
mdw,
parallel,
put,
run,
@VldMrgnn
VldMrgnn / parseRecursive.ts
Created August 19, 2024 13:41
parse Recursive unknown to latest Result<T>
import type { Result } from 'starfx';
export function isJSONString(str) {
try {
const obj = JSON.parse(str);
return typeof obj === 'object' && obj !== null;
} catch (error) {
return false;
}
}
@VldMrgnn
VldMrgnn / meta-selector.ts
Created August 18, 2024 09:46
Get the slices that are hit by a selector using a proxy
import { createSelector } from 'starfx';
/* SELECTORS */
/**
* Creates a proxy around an object to track accessed properties.
*
* @param {object} obj - The object to create a proxy for.
* @returns {[object, Set<string>]} An array containing the proxy object and a Set of accessed property names.
*/
function trackAccesses(obj) {
@VldMrgnn
VldMrgnn / meta-store.ts
Last active August 18, 2024 19:56
proxy to map the selector path
import { createSelector } from "starfx/store";
/* SELECTORS */
/**
* Creates a proxy around an object to track accessed properties.
*
* @param {object} obj - The object to create a proxy for.
* @returns {[object, Set<string>]} An array containing the proxy object and a Set of accessed property names.
*/
function trackAccesses(obj) {
@VldMrgnn
VldMrgnn / apis.ts
Created August 8, 2024 18:49
starfx apis example
import { createApi, createThunks, Err, mdw, Ok, put, sleep, spawn, take } from 'starfx';
import { service } from '@app/service/delphiService';
import { schema } from './schema';
import { mainWorker } from './workers/workerApi';
import type { Next, ApiCtx, Result } from 'starfx';
import type { ThunkCtx } from '@app/types';
@VldMrgnn
VldMrgnn / backend_express_endpoints.ts
Last active July 28, 2024 15:33
indexedDB starfx persistor with forwarding state to server for avoiding eviction
// we could store on filesystem as well...
export async function setPersisted(req: Request) {
const group = Object.values(req.session.groups || {}).find(
(x) => x.PERSEED === req.session.INSIDER
);
if (!group) {
console.log("setPersisted", "no group found");
return {};
}
@VldMrgnn
VldMrgnn / fetchrace.ts
Last active June 19, 2024 15:43
effection fetch timeout
function* fetchRace(endpoint:string, timeout: number) {
let result: Response | null = null;
function* fetcher(endpoint: string, signal: AbortSignal) {
try {
result = yield* call(() => fetch(endpoint, { signal }));
} catch (error) {
console.error("fetchRace error", error);
}
}
const controller = new AbortController();