i think i finally fixed the state management awfulness
This commit is contained in:
parent
6953b49563
commit
40be540527
18 changed files with 402 additions and 417 deletions
|
@ -2,7 +2,7 @@ import { Instance, server_types } from './instance.js';
|
|||
import * as api from './api.js';
|
||||
import { get, writable } from 'svelte/store';
|
||||
|
||||
let client = writable(false);
|
||||
export const client = writable(false);
|
||||
|
||||
const save_name = "campfire";
|
||||
|
||||
|
@ -22,15 +22,6 @@ export class Client {
|
|||
};
|
||||
}
|
||||
|
||||
static get() {
|
||||
let current = get(client);
|
||||
if (current && current.app) return client;
|
||||
let new_client = new Client();
|
||||
new_client.load();
|
||||
client.set(new_client);
|
||||
return client;
|
||||
}
|
||||
|
||||
async init(host) {
|
||||
if (host.startsWith("https://")) host = host.substring(8);
|
||||
const url = `https://${host}/api/v1/instance`;
|
||||
|
@ -76,30 +67,30 @@ export class Client {
|
|||
console.error("Failed to obtain access token");
|
||||
return false;
|
||||
}
|
||||
this.app.token = token;
|
||||
client.set(this);
|
||||
return token;
|
||||
}
|
||||
|
||||
async revokeToken() {
|
||||
return await api.revokeToken();
|
||||
}
|
||||
|
||||
async verifyCredentials() {
|
||||
async getUser() {
|
||||
// already known
|
||||
if (this.user) return this.user;
|
||||
|
||||
// cannot provide- not logged in
|
||||
if (!this.app || !this.app.token) {
|
||||
this.user = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// logged in- attempt to retrieve using token
|
||||
const data = await api.verifyCredentials();
|
||||
if (!data) {
|
||||
this.user = false;
|
||||
return false;
|
||||
}
|
||||
await client.update(async c => {
|
||||
c.user = await api.parseUser(data);
|
||||
console.log(`Logged in as @${c.user.username}@${c.user.host}`);
|
||||
});
|
||||
return this.user;
|
||||
const user = await api.parseUser(data);
|
||||
console.log(`Logged in as @${user.username}@${user.host}`);
|
||||
return user;
|
||||
}
|
||||
|
||||
async getTimeline(last_post_id) {
|
||||
|
@ -110,6 +101,10 @@ export class Client {
|
|||
return await api.getPost(post_id, parent_replies, child_replies);
|
||||
}
|
||||
|
||||
async getPostContext(post_id) {
|
||||
return await api.getPostContext(post_id);
|
||||
}
|
||||
|
||||
async boostPost(post_id) {
|
||||
return await api.boostPost(post_id);
|
||||
}
|
||||
|
@ -199,7 +194,7 @@ export class Client {
|
|||
console.warn("Failed to log out correctly; ditching the old tokens anyways.");
|
||||
}
|
||||
localStorage.removeItem(save_name);
|
||||
client.set(false);
|
||||
client.set(new Client());
|
||||
console.log("Logged out successfully.");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue