From cce9c69834d38ce916ce4ba401a7654ad99933f9 Mon Sep 17 00:00:00 2001 From: gsovereignty Date: Mon, 16 Oct 2023 13:58:55 +0800 Subject: [PATCH] problem: mutex used before creation --- src/lib/settings.ts | 4 +++ src/lib/stores/problems.ts | 60 ++++++++++++++++++++++++++++++++++++-- src/lib/stores/state.ts | 60 ++------------------------------------ 3 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/lib/settings.ts b/src/lib/settings.ts index 9c72377..c96e2d9 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -1,3 +1,5 @@ +import { Mutex } from "async-mutex"; + export const MAX_STATECHANGE_EVENT_AGE = 86_400; //seconds export const ignitionPubkey = @@ -32,3 +34,5 @@ export const nostrocketIgnitionTag = [ export const rocketNameValidator = /^\w{5,20}$/; export const hexPubkeyValidator = /^\w{64}$/; + +export const changeStateMutex = new Mutex() \ No newline at end of file diff --git a/src/lib/stores/problems.ts b/src/lib/stores/problems.ts index 0ce4adf..d25580e 100644 --- a/src/lib/stores/problems.ts +++ b/src/lib/stores/problems.ts @@ -1,7 +1,8 @@ import type { NDKEvent, NDKFilter } from "@nostr-dev-kit/ndk"; import { get, writable, type Writable } from "svelte/store"; import ndk from "./ndk"; -import { consensusTipState } from "./state"; +import { consensusTipState, labelledTag } from "./state"; +import { changeStateMutex } from "$lib/settings"; export const problemEvents = writable>(new Map()); export function getProblemEvent(id:string):NDKEvent|undefined { @@ -151,4 +152,59 @@ export function GetFulltextFromTextEvent(e:NDKEvent | undefined):string { }) } return val -} \ No newline at end of file +} + + +problemEvents.subscribe(()=>{ + changeStateMutex.acquire().then(()=>{ + consensusTipState.update(state=>{ + state.Problems.forEach(problem=>{ + //get the commit event and popuate status etc + if (problem.Head) { + let commitID = labelledTag(problem.Head, "commit", "e") + if (commitID) { + let commitEvent = getProblemEvent(commitID)//get(problemEvents).get(commitID) + if (commitEvent) { + let s = commitEvent.tagValue("s") + if (s) { + problem.Status = s + } + let previous = labelledTag(commitEvent, "previous", "e") + if (previous) { + if (!problem.CommitHistory) { + problem.CommitHistory = []; + } + if (!problem.CommitHistory.includes(previous)) { + problem.CommitHistory.push(previous) + } + } + let textEventID = labelledTag(commitEvent, "text", "e") + if (textEventID) { + let textEvent = getProblemEvent(textEventID) + if (textEvent) { + let title = labelledTag(textEvent, "title", "t") + if (title) { + problem.Title = title.length <= 100? title : problem.Title + } + let summary = labelledTag(textEvent, "summary", "t") + if (summary) { + problem.Summary = summary.length <= 280? summary : problem.Summary + } + let fulltext = labelledTag(textEvent, "full", "t") + if (fulltext) { + problem.FullText = fulltext + } + } + } + } + } + //get the text event + //populate text content + } + + }) + return state + }) + changeStateMutex.release() + }) +}) \ No newline at end of file diff --git a/src/lib/stores/state.ts b/src/lib/stores/state.ts index adfd55c..9c6094c 100644 --- a/src/lib/stores/state.ts +++ b/src/lib/stores/state.ts @@ -12,13 +12,14 @@ import { } from "svelte/store"; import { allNostrocketEventKinds } from "../kinds"; import { + changeStateMutex, ignitionPubkey, - rootEventID, nostrocketIgnitionEvent, + rootEventID, } from "../settings"; import { Nostrocket, Problem, type Account } from "../types"; import ndk, { ndk_profiles } from "./ndk"; -import { fetchEventsAndUpsertStore, getProblemEvent, problemEvents } from "./problems"; +import { fetchEventsAndUpsertStore, problemEvents } from "./problems"; import { profiles } from "./profiles"; export function FUCKYOUVITE(): NDKUser { //todo, vite issue fixed, update everywhere that uses this @@ -30,7 +31,6 @@ const $ndk_profiles = getStore(ndk_profiles); let r: Nostrocket = new Nostrocket(JSON.stringify("")); export const consensusTipState = writable(r); //this is the latest nostrocket state, built from consensus events signed by participants with votepower -let changeStateMutex = new Mutex(); export const anek = $ndk.storeSubscribe( { "#e": [rootEventID], kinds: allNostrocketEventKinds }, //"#e": [ignitionEvent] , authors: [ignitionPubkey] kinds: allNostrocketEventKinds, "#e": [mainnetRoot] @@ -351,60 +351,6 @@ consensusTipState.subscribe(state=>{ }) -problemEvents.subscribe(()=>{ - changeStateMutex.acquire().then(()=>{ - consensusTipState.update(state=>{ - state.Problems.forEach(problem=>{ - //get the commit event and popuate status etc - if (problem.Head) { - let commitID = labelledTag(problem.Head, "commit", "e") - if (commitID) { - let commitEvent = getProblemEvent(commitID)//get(problemEvents).get(commitID) - if (commitEvent) { - let s = commitEvent.tagValue("s") - if (s) { - problem.Status = s - } - let previous = labelledTag(commitEvent, "previous", "e") - if (previous) { - if (!problem.CommitHistory) { - problem.CommitHistory = []; - } - if (!problem.CommitHistory.includes(previous)) { - problem.CommitHistory.push(previous) - } - } - let textEventID = labelledTag(commitEvent, "text", "e") - if (textEventID) { - let textEvent = getProblemEvent(textEventID) - if (textEvent) { - let title = labelledTag(textEvent, "title", "t") - if (title) { - problem.Title = title.length <= 100? title : problem.Title - } - let summary = labelledTag(textEvent, "summary", "t") - if (summary) { - problem.Summary = summary.length <= 280? summary : problem.Summary - } - let fulltext = labelledTag(textEvent, "full", "t") - if (fulltext) { - problem.FullText = fulltext - } - } - } - } - } - //get the text event - //populate text content - } - - }) - return state - }) - changeStateMutex.release() - }) -}) - //todo make a new Problem object which contains a nested tree of problems. //0. sort all problems by ID //1. iterate and add all problems with no parents to the tree, iterate again and add problems with parents under their parent. Pop from list each time.