From 0615ca9aed025e194012afc5029bf0f7574ea613 Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 30 Apr 2023 20:18:50 -0300 Subject: [PATCH] amigx y cosas --- src/doc.ts | 3 ++- src/lib/Amigxs.svelte | 35 ++++++++++++++++++++++++---- src/lib/Others.svelte | 31 ++++++++---------------- src/lib/helpers/readableText.js | 21 +++++++++++++++++ src/lib/helpers/recursiveReadable.js | 34 +++++++++++++++++++++++++++ src/lib/stores/friends.ts | 19 +++++++++++++-- 6 files changed, 115 insertions(+), 28 deletions(-) create mode 100644 src/lib/helpers/readableText.js create mode 100644 src/lib/helpers/recursiveReadable.js diff --git a/src/doc.ts b/src/doc.ts index e1a2f8c..1a6fdb4 100644 --- a/src/doc.ts +++ b/src/doc.ts @@ -80,5 +80,6 @@ export type Done = { export function getData(userY: UserY) { const ydoing: Y.Array = userY.ydoc.getArray("doing"); const ydone: Y.Array = userY.ydoc.getArray("done"); - return { ydoing, ydone }; + const yname: Y.Text = userY.ydoc.getText("name"); + return { ydoing, ydone, yname }; } diff --git a/src/lib/Amigxs.svelte b/src/lib/Amigxs.svelte index 3986cdc..da6a5e3 100644 --- a/src/lib/Amigxs.svelte +++ b/src/lib/Amigxs.svelte @@ -1,6 +1,11 @@ + changeName(e.currentTarget.value)} + placeholder="tu nombre" +/>
@@ -27,7 +50,11 @@
    - {#each $friends as friend} -
  • {friend.room}
  • + {#each $allData as friend} + {#if friend.name} +
  • {friend.name} ({friend.id.room})
  • + {:else} +
  • {friend.id.room}
  • + {/if} {/each}
diff --git a/src/lib/Others.svelte b/src/lib/Others.svelte index 532a62c..bf960cd 100644 --- a/src/lib/Others.svelte +++ b/src/lib/Others.svelte @@ -1,37 +1,26 @@ -{#if allDoing} +{#if allData}
    - {#each $allDoing as doing} - {#if doing.$d.length > 0} + {#each $allData as data} + {#if data.doing.length > 0}
  • - {doing.name} - {#each doing.$d as d, i} + {data.name || data.id.room} + {#each data.doing as d, i} {#if i !== 0} y {/if} {d.description} hace diff --git a/src/lib/helpers/readableText.js b/src/lib/helpers/readableText.js new file mode 100644 index 0000000..961c79b --- /dev/null +++ b/src/lib/helpers/readableText.js @@ -0,0 +1,21 @@ +import * as Y from "yjs"; + +/** @typedef {import('svelte/store').Readable & { y: Y.Text }} YReadableText */ + +/** + * @param {Y.Text} ytext + * @returns {YReadableText} + */ +export function readableText(ytext) { + return { + subscribe(run) { + const cb = () => run(ytext.toString()); + cb(); + ytext.observe(cb); + return () => { + ytext.unobserve(cb); + }; + }, + y: ytext, + }; +} diff --git a/src/lib/helpers/recursiveReadable.js b/src/lib/helpers/recursiveReadable.js new file mode 100644 index 0000000..2b03f61 --- /dev/null +++ b/src/lib/helpers/recursiveReadable.js @@ -0,0 +1,34 @@ +// /** +// * @template Type +// * @typedef {{ +// [Property in keyof Type]: Type[Property] extends import("svelte/store").Readable ? Type[Property]['subscribe']['arguments'] : Type[Property]; +// }} Resolved */ + +import * as Y from "yjs"; + +/** + * @typedef {{ + [key: string]: import("svelte/store").Readable +}} DataStores */ +/** + * @template {DataStores} T + * @typedef {{ + [Property in keyof T]: Parameters[0]>[0] +}} Data */ + +import { derived } from "svelte/store"; + +/** + * @template {DataStores} T + * @param {T} thing + * @returns {import('svelte/store').Readable>} + */ +export function deriveObj(thing) { + return derived(Object.values(thing), (values) => { + const keys = Object.keys(thing); + // prettier-ignore + return /** @type {Data} */(Object.fromEntries( + values.map(($v, i) => [keys[i], $v]) + )) + }); +} diff --git a/src/lib/stores/friends.ts b/src/lib/stores/friends.ts index 14f8ff3..3339654 100644 --- a/src/lib/stores/friends.ts +++ b/src/lib/stores/friends.ts @@ -1,5 +1,8 @@ -import { writable } from "svelte/store"; -import type { UserIdentifier } from "../../doc"; +import { derived, writable } from "svelte/store"; +import { readableArray } from "svelt-yjs/src/main"; +import { getData, getUserY, type UserIdentifier } from "../../doc"; +import { readableText } from "../helpers/readableText"; +import { deriveObj } from "../helpers/recursiveReadable"; type Type = UserIdentifier[]; @@ -9,3 +12,15 @@ const save = (v: Type) => (localStorage.friends = JSON.stringify(v)); const store = writable(start); store.subscribe(save); export default store; + +export const friendsData = derived(store, ($friends) => + $friends + .map((id) => ({ id, ...getUserY(id) })) + .map((y) => ({ ...y, ...getData(y) })) + .map((y) => ({ + name: readableText(y.yname), + doing: readableArray(y.ydoing), + done: readableArray(y.ydone), + })) + .map((d) => deriveObj(d)) +);