Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Nov 7, 2023
1 parent 3da1e1a commit c03f6a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
5 changes: 4 additions & 1 deletion packages/stores/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
class A {
constructor() {
this.m = new Map();
this.m.set("klj", 0);
this.m.set(
new Uint8Array([4, 42, 2, 4, 2, 4, 2, 23, 3, 4, 4, 55, 6]),
0
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/stores/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@holochain-open-dev/stores",
"version": "0.7.9",
"version": "0.7.10",
"description": "Re-export of svelte/store, with additional utilities to build reusable holochain-open-dev modules",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down
41 changes: 29 additions & 12 deletions packages/stores/src/elements/visualize-store-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cloneDeepWith from "lodash-es/cloneDeepWith.js";
import "@scoped-elements/cytoscape";
import { EdgeDefinition, NodeDefinition } from "cytoscape";
import { encodeHashToBase64 } from "@holochain/client";
import "@shoelace-style/shoelace/dist/components/relative-time/relative-time.js";

import { Derived } from "../derived.js";
import { deriveStore } from "../async-derived.js";
Expand All @@ -33,14 +34,15 @@ function getObjectId(object: any): number {

export function buildAndJoinTree(
store: Readable<any>
): Readable<TreeNode<{ id: number; value: any }>> {
): Readable<TreeNode<{ id: number; value: any; lastUpdated: number }>> {
return deriveStore(store, (value) => {
const deps = (store as Derived<any>).derivedFrom || [];
const childStores = deps.map((c) => buildAndJoinTree(c));
return derived(childStores, (children) => ({
node: {
id: getObjectId(store),
value,
lastUpdated: Date.now(),
},
children,
}));
Expand Down Expand Up @@ -90,21 +92,21 @@ export function valueTreeToElements(
data: {
id: `${valueTree.node.id.toString()}-content`,
parent: valueTree.node.id.toString(),
label,
label: label.toString().slice(0, 20),
},
}
);

return elements;
}

export function findValueInTree(
tree: TreeNode<{ id: number; value: any }>,
export function findNodeInTree(
tree: TreeNode<{ id: number; value: any; lastUpdated: number }>,
id: number
): any {
if (tree.node.id === id) return tree.node.value;
): { id: number; value: any; lastUpdated: number } {
if (tree.node.id === id) return tree.node;
for (const c of tree.children) {
const value = findValueInTree(c, id);
const value = findNodeInTree(c, id);
if (value) {
return value;
}
Expand All @@ -129,7 +131,7 @@ export class VisualizeStoreTree extends LitElement {

get selectedValue() {
return cloneDeepWith(
findValueInTree(this._subscriber.value, this.selectedStore),
findNodeInTree(this._subscriber.value, this.selectedStore).value,
(value) => {
if (value instanceof Map) {
return Object.fromEntries(value);
Expand Down Expand Up @@ -199,10 +201,25 @@ node > node {
<div class="row" style="flex-basis: 300px">
${this.selectedStore
? html`
<json-viewer
.data=${this.selectedValue}
style="flex: 1;"
></json-viewer>
<div
style="display: flex; flex-direction: column; flex: 1; gap: 16px"
>
<json-viewer
style="flex: 1"
.data=${this.selectedValue}
></json-viewer>
<div style="display: flex; flex-direction: row;">
<span>Last updated: </span>
<sl-relative-time
.date=${new Date(
findNodeInTree(
this._subscriber.value,
this.selectedStore
).lastUpdated
)}
></sl-relative-time>
</div>
</div>
`
: html`
<span style="align-self: center"
Expand Down
6 changes: 3 additions & 3 deletions packages/stores/src/holochain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function latestVersionOfEntryStore<
const fetch = async () => {
try {
const nlatestVersion = await fetchLatestVersion();
if (latestVersion) {
if (nlatestVersion) {
if (!isEqual(latestVersion, nlatestVersion)) {
latestVersion = nlatestVersion;
set({
Expand All @@ -155,7 +155,7 @@ export function latestVersionOfEntryStore<
}
};
fetch();
const interval = setInterval(() => fetchLatestVersion(), 4000);
const interval = setInterval(() => fetch(), 4000);
const unsubs = client.onSignal(async (signal) => {
if (
signal.type === "EntryUpdated" &&
Expand Down Expand Up @@ -275,7 +275,7 @@ export function deletesForEntryStore<S extends ActionCommittedSignal<any, any>>(
}
};
await fetch();
const interval = setInterval(() => fetchDeletes(), 4000);
const interval = setInterval(() => fetch(), 4000);
const unsubs = client.onSignal(async (signal) => {
if (
signal.type === "EntryDeleted" &&
Expand Down

0 comments on commit c03f6a9

Please sign in to comment.