From d299491fb57e3b82e85f0cd942e9891adf3a2e3d Mon Sep 17 00:00:00 2001 From: Samuel Ralak Date: Fri, 20 Oct 2023 06:10:52 +0300 Subject: [PATCH] Problem: logic not totally clear Solution: Moved participant related code to SoftState context. Eliminated all redundant code from the NDKContext --- src/components/modals/AddIdentity.svelte | 1 - src/components/views/Identity.svelte | 60 ++++--------- .../components/identity/IdentityList.svelte | 26 +----- src/lib/consensus/state.ts | 8 +- src/lib/contexts/NDKContext.svelte | 88 ++----------------- src/lib/contexts/SoftStateContext.svelte | 51 +++++++++++ src/lib/stores/base-store.ts | 21 +++++ src/lib/stores/states/soft-state.ts | 28 ++++++ 8 files changed, 131 insertions(+), 152 deletions(-) create mode 100644 src/lib/contexts/SoftStateContext.svelte create mode 100644 src/lib/stores/base-store.ts create mode 100644 src/lib/stores/states/soft-state.ts diff --git a/src/components/modals/AddIdentity.svelte b/src/components/modals/AddIdentity.svelte index 7f78792..d71ea8b 100644 --- a/src/components/modals/AddIdentity.svelte +++ b/src/components/modals/AddIdentity.svelte @@ -26,7 +26,6 @@ let buttonDisabled = true; const profileData = writable(undefined); - const _ndk_profiles = get(ndk_profiles); function getProfile(pubkey) { if (pubkey.length == 64) { diff --git a/src/components/views/Identity.svelte b/src/components/views/Identity.svelte index 4b671cd..836942d 100644 --- a/src/components/views/Identity.svelte +++ b/src/components/views/Identity.svelte @@ -5,34 +5,33 @@ import Profile from "../Profile.svelte"; import AddIdentity from "../modals/AddIdentity.svelte"; import IdentityContext from "$lib/contexts/IdentityContext.svelte"; + import IdentityList from "$lib/components/identity/IdentityList.svelte"; import ndk from "$lib/stores/ndk"; - import IdentityResource from "$lib/resources/IdentityResource"; import {getContext, onDestroy} from "svelte"; - import {ignitionPubkey} from "$lib/settings"; + import softState from "$lib/stores/states/soft-state"; + import {writable} from "svelte/store"; + import SoftStateContext from "$lib/contexts/SoftStateContext.svelte"; const rocketMap = getContext('rocketMap') + const participants = writable([]) // TODO: Rename this to profiles - const identities = $ndk.storeSubscribe( - {kinds: [0], authors: [ignitionPubkey]}, - {closeOnEose: false}, - IdentityResource - ) + // const identities = $ndk.storeSubscribe( + // {kinds: [0], authors: [ignitionPubkey]}, + // {closeOnEose: false}, + // IdentityResource + // ) // subscribe to the rocketMap context // we can use this to get list of participants as well - rocketMap.subscribe((entries) => { - entries.forEach((entry) => { - console.log({entry}) - }) - }) - - onDestroy(() => { - identities.unsubscribe() - }) + // rocketMap.subscribe((entries) => { + // entries.forEach((entry) => { + // console.log({entry}) + // }) + // }) - +

These people have joined Nostrocket

@@ -47,27 +46,6 @@ - - - - - - - - todo: get user image from profile if we have pubkey, or use - silhouette if none. - - - - -

#{$nostrocketParticipantProfiles.length}

-
-
-
-
-
- {#each $nostrocketParticipantProfiles as p, i (p.profile.pubkey)} - - {/each} -
-
\ No newline at end of file + + + \ No newline at end of file diff --git a/src/lib/components/identity/IdentityList.svelte b/src/lib/components/identity/IdentityList.svelte index 9985400..1cae4f7 100644 --- a/src/lib/components/identity/IdentityList.svelte +++ b/src/lib/components/identity/IdentityList.svelte @@ -1,30 +1,10 @@ diff --git a/src/lib/consensus/state.ts b/src/lib/consensus/state.ts index 82ae7a3..f1e20c2 100644 --- a/src/lib/consensus/state.ts +++ b/src/lib/consensus/state.ts @@ -104,7 +104,7 @@ export const nostrocketParticipants = derived(consensusTipState, ($cts) => { return orderedList; }); -function recursiveList( +export function recursiveList( rocket: string, rootAccount: Account, state: Nostrocket, @@ -254,7 +254,7 @@ async function watchMempool() { }); } -function processMempool(currentState: Nostrocket): Nostrocket { +export function processMempool(currentState: Nostrocket): Nostrocket { let handled: NDKEvent[] = []; //let newState:Nostrocket = clone(currentState) mempool.singleIterator().forEach((e) => { @@ -292,7 +292,7 @@ function processMempool(currentState: Nostrocket): Nostrocket { return currentState; } -function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] { +export function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] { switch (e.kind) { case 15171971: //Problem ANCHOR @@ -304,7 +304,7 @@ function handleProblemEvent(e: NDKEvent, c: Nostrocket): [Nostrocket, boolean] { return [c, false]; } -function handleIdentityEvent( +export function handleIdentityEvent( e: NDKEvent, c: Nostrocket ): [Nostrocket, boolean] { diff --git a/src/lib/contexts/NDKContext.svelte b/src/lib/contexts/NDKContext.svelte index 29d5fac..494c492 100644 --- a/src/lib/contexts/NDKContext.svelte +++ b/src/lib/contexts/NDKContext.svelte @@ -1,88 +1,10 @@
diff --git a/src/lib/contexts/SoftStateContext.svelte b/src/lib/contexts/SoftStateContext.svelte new file mode 100644 index 0000000..53ad2a8 --- /dev/null +++ b/src/lib/contexts/SoftStateContext.svelte @@ -0,0 +1,51 @@ + + +
+ +
\ No newline at end of file diff --git a/src/lib/stores/base-store.ts b/src/lib/stores/base-store.ts new file mode 100644 index 0000000..f7bf63f --- /dev/null +++ b/src/lib/stores/base-store.ts @@ -0,0 +1,21 @@ +import {type Writable, writable} from 'svelte/store'; + +export default class BaseStore { + protected static instance: BaseStore; + protected data: Writable<{}>; + + protected constructor(input: {}) { + this.data = writable(input); // Initialize Svelte store + } + + static getInstance(input): BaseStore { + if (!BaseStore.instance) { + BaseStore.instance = new BaseStore(input); + } + return BaseStore.instance; + } + + get store() { + return this.data; + } +} diff --git a/src/lib/stores/states/soft-state.ts b/src/lib/stores/states/soft-state.ts new file mode 100644 index 0000000..efb2358 --- /dev/null +++ b/src/lib/stores/states/soft-state.ts @@ -0,0 +1,28 @@ +import {type Writable} from "svelte/store"; +import BaseStore from "$lib/stores/base-store"; +import {ignitionPubkey} from "$lib/settings"; + +interface Identity { + publicKeys: string[] +} + +interface ISoftState { + identity: Identity +} + +class SoftState extends BaseStore { + // override protected allows us to enforce type + protected data: Writable + + protected constructor(input: ISoftState) { + super(input) + } +} + +const _default: ISoftState = { + identity: { + publicKeys: [ignitionPubkey] + } +} + +export default SoftState.getInstance(_default).store