Skip to content

Commit

Permalink
problem: mutex used before creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Oct 16, 2023
1 parent fc57747 commit cce9c69
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Mutex } from "async-mutex";

export const MAX_STATECHANGE_EVENT_AGE = 86_400; //seconds

export const ignitionPubkey =
Expand Down Expand Up @@ -32,3 +34,5 @@ export const nostrocketIgnitionTag = [
export const rocketNameValidator = /^\w{5,20}$/;

export const hexPubkeyValidator = /^\w{64}$/;

export const changeStateMutex = new Mutex()
60 changes: 58 additions & 2 deletions src/lib/stores/problems.ts
Original file line number Diff line number Diff line change
@@ -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<Map<string, NDKEvent>>(new Map());
export function getProblemEvent(id:string):NDKEvent|undefined {
Expand Down Expand Up @@ -151,4 +152,59 @@ export function GetFulltextFromTextEvent(e:NDKEvent | undefined):string {
})
}
return val
}
}


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()
})
})
60 changes: 3 additions & 57 deletions src/lib/stores/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<NDKEvent>(
{ "#e": [rootEventID], kinds: allNostrocketEventKinds }, //"#e": [ignitionEvent] , authors: [ignitionPubkey] kinds: allNostrocketEventKinds, "#e": [mainnetRoot]
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cce9c69

Please sign in to comment.