Rust implementation of NextGraph, a Decentralized and local-first web 3.0 ecosystem https://nextgraph.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nextgraph-rs/ng-app/src/store.ts

125 lines
4.0 KiB

// Copyright (c) 2022-2023 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
1 year ago
import { writable, readonly, derived } from "svelte/store";
import ng from "./api";
1 year ago
let all_branches = {};
export const opened_wallets = writable({});
/// { wallet:, id: }
export const active_wallet = writable(undefined);
export const wallets = writable({});
export const online = writable(false);
1 year ago
export const has_wallets = derived(wallets,($wallets) => Object.keys($wallets).length);
export const active_session = writable(undefined);
export const set_active_session = function(session) {
active_session.set(session.users);
1 year ago
};
export { writable, readonly, derived };
export const close_active_wallet = function() {
1 year ago
active_session.set(undefined);
active_wallet.update((w) => {
delete w.wallet;
return w;
1 year ago
});
}
const branch_commits = (nura, sub) => {
1 year ago
// console.log("branch_commits")
// const { subscribe, set, update } = writable([]); // create the underlying writable store
// let unsub = () => {};
// return {
// load: async () => {
// console.log("load")
// unsub = await ng.doc_sync_branch(nura, async (commit) => {
// console.log(commit);
// update( (old) => {old.unshift(commit); return old;} )
// });
// },
// subscribe: (run, invalid) => {
// console.log("sub")
// let upper_unsub = subscribe(run, invalid);
1 year ago
// return () => {
// upper_unsub();
// unsub();
// }
// }
// // set: (value) => {
// // localStorage.setItem(key, toString(value)); // save also to local storage as a string
// // return set(value);
// // },
// // update,
// };
1 year ago
return {
load: async () => {
1 year ago
let already_subscribed = all_branches[nura];
if (!already_subscribed) return;
if (already_subscribed.load) {
await already_subscribed.load();
already_subscribed.load = undefined;
}
},
subscribe: (run, invalid) => {
1 year ago
let already_subscribed = all_branches[nura];
if (!already_subscribed) {
const { subscribe, set, update } = writable([]); // create the underlying writable store
let count = 0;
let unsub = () => {};
already_subscribed = {
load: async () => {
unsub = await ng.doc_sync_branch(nura, async (commit) => {
console.log("GOT COMMIT", commit);
1 year ago
update( (old) => {old.unshift(commit); return old;} )
});
// this is in case decrease has been called before the load function returned.
if (count == 0) {unsub();}
1 year ago
},
increase: () => {
count += 1;
return readonly({subscribe});
},
decrease: () => {
count -= 1;
if (count == 0) {
unsub();
delete all_branches[nura];
}
},
}
all_branches[nura] = already_subscribed;
}
let new_store = already_subscribed.increase();
let read_unsub = new_store.subscribe(run, invalid);
return () => {
1 year ago
read_unsub();
already_subscribed.decrease();
}
1 year ago
}
1 year ago
}
};
export default branch_commits;