removing delta methods from store

This commit is contained in:
Roland Osborne 2022-09-29 16:00:48 -07:00
parent ad781e6c81
commit 40a29e17c3
5 changed files with 52 additions and 82 deletions

View File

@ -440,13 +440,9 @@ export function useCardContext() {
const { guid } = session.current; const { guid } = session.current;
return await store.actions.getCardChannelTopicItems(guid, cardId, channelId); return await store.actions.getCardChannelTopicItems(guid, cardId, channelId);
}, },
getChannelTopicDeltaItems: async (cardId, channelId, revision) => { setChannelTopicItem: async (cardId, channelId, topicId, topic) => {
const { guid } = session.current; const { guid } = session.current;
return await store.actions.getCardChannelTopicDeltaItems(guid, cardId, channelId, revision); return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, topic);
},
setChannelTopicItem: async (cardId, channelId, topicId, channelRevision, topic) => {
const { guid } = session.current;
return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, channelRevision, topic);
}, },
clearChannelTopicItem: async (cardId, channelId, topicId) => { clearChannelTopicItem: async (cardId, channelId, topicId) => {
const { guid } = session.current; const { guid } = session.current;
@ -457,39 +453,32 @@ export function useCardContext() {
return await store.actions.clearCardChannelTopicItems(guid, cardId, channelId); return await store.actions.clearCardChannelTopicItems(guid, cardId, channelId);
}, },
getChannelTopic: async (cardId, channelId, topicId) => { getChannelTopic: async (cardId, channelId, topicId) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await getContactChannelTopic(profile.node, detail.token, channelId, topicId); return await getContactChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId);
}, },
getChannelTopics: async (cardId, channelId, revision) => { getChannelTopics: async (cardId, channelId, revision) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await getContactChannelTopics(profile.node, detail.token, channelId, revision); return await getContactChannelTopics(profile.node, `${profile.guid}.${detail.token}`, channelId, revision);
}, },
getChannelTopicAssetUrl: (cardId, channelId, topicId, assetId) => { getChannelTopicAssetUrl: (cardId, channelId, topicId, assetId) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return getContactChannelTopicAssetUrl(profile.node, detail.token, channelId, topicId, assetId); return getContactChannelTopicAssetUrl(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, assetId);
}, },
addChannelTopic: async (cardId, channelId, message, assets) => { addChannelTopic: async (cardId, channelId, message, assets) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await addChannelTopic(profile.node, detail.token, channelId, message, assets); return await addChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, message, assets);
}, },
setChannelTopicSubject: async (cardId, channelId, topicId, data) => { setChannelTopicSubject: async (cardId, channelId, topicId, data) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await setContactChannelTopicSubject(profile.node, detail.token, channelId, topicId, data); return await setContactChannelTopicSubject(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, data);
}, },
removeChannel: async (cardId, channelId) => { removeChannel: async (cardId, channelId) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await removeChannel(profile.node, detail.token, channelId); return await removeChannel(profile.node, `${profile.guid}.${detail.token}`, channelId);
}, },
removeChannelTopic: async (cardId, channelId, topicId) => { removeChannelTopic: async (cardId, channelId, topicId) => {
const { guid } = session.current;
const { detail, profile } = getCard(cardId); const { detail, profile } = getCard(cardId);
return await removeChannelTopic(profile.node, detail.token, channelId, topicId); return await removeChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId);
}, },
} }

View File

@ -184,13 +184,9 @@ export function useChannelContext() {
const { guid } = session.current; const { guid } = session.current;
return await store.actions.getChannelTopicItems(guid, channelId); return await store.actions.getChannelTopicItems(guid, channelId);
}, },
getTopicDeltaItems: async (channelId, revision) => { setTopicItem: async (channelId, topicId, topic) => {
const { guid } = session.current; const { guid } = session.current;
return await store.actions.getChannelTopicDeltaItems(guid, channelId, revision); return await store.actions.setChannelTopicItem(guid, channelId, topicId, topic);
},
setTopicItem: async (channelId, topicId, channelRevision, topic) => {
const { guid } = session.current;
return await store.actions.setChannelTopicItem(guid, channelId, topicId, channelRevision, topic);
}, },
clearTopicItem: async (channelId, topicId) => { clearTopicItem: async (channelId, topicId) => {
const { guid } = session.current; const { guid } = session.current;

View File

@ -102,42 +102,47 @@ export function useConversationContext() {
if (item && (item.revision !== revision.current || item.syncRevision != syncRevision.current)) { if (item && (item.revision !== revision.current || item.syncRevision != syncRevision.current)) {
syncing.current = true; syncing.current = true;
// set channel details try {
if (detailRevision.current != item.detailRevision) { // set channel details
if (detailRevision.current != item.detailRevision) {
if (curView === setView.current) {
setChannel(item);
detailRevision.current = item.detailRevision;
}
}
// set channel topics
if (syncRevision.current != item.syncRevision) {
if (syncRevision.current) {
const topics = await getTopicDeltaItems(cardId, channelId);
}
else {
const topics = await getTopicItems(cardId, channelId);
}
if (curView === setView.current) {
syncRevision.current = item.syncRevision;
}
}
// sync from server to store
if (item.topicRevision !== item.syncRevision) {
const res = await getTopics(cardId, channelId, item.syncRevision)
}
// update revision
if (curView === setView.current) { if (curView === setView.current) {
setChannel(item); revision.current = item.revision;
detailRevision.current = item.detailRevision; //TODO set to synced state
} }
}
// set channel topics syncing.current = false;
if (syncRevision.current != item.syncRevision) { sync();
if (syncRevision.current) {
const topics = await getTopicDeltaItems(cardId, channelId);
}
else {
const topics = await getTopicItems(cardId, channelId);
}
if (curView === setView.current) {
syncRevision.current = item.syncRevision;
}
} }
catch(err) {
// sync from server to store console.log(err);
if (item.topicRevision !== item.syncRevision) { syncing.current = false;
const res = await getTopics(cardId, channelId, item.syncRevision) //TODO set to unsynced state
res.topics.forEach(topic => {
console.log(topic.data);
});
} }
// update revision
if (curView === setView.current) {
revision.current = item.revision;
}
syncing.current = false;
sync();
} }
} }
} }

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react'; import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage"; import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'databag_v036.db'; const DATABAG_DB = 'databag_v037.db';
export function useStoreContext() { export function useStoreContext() {
const [state, setState] = useState({}); const [state, setState] = useState({});
@ -13,10 +13,10 @@ export function useStoreContext() {
const initSession = async (guid) => { const initSession = async (guid) => {
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_${guid} (channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, read_revision integer, unique(channel_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_${guid} (channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, read_revision integer, unique(channel_id))`);
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_topic_${guid} (channel_id text, topic_id text, channel_revision integer, revision integer, detail_revision integer, detail text, unique(channel_id, topic_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_topic_${guid} (channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(channel_id, topic_id))`);
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_${guid} (card_id text, revision integer, detail_revision integer, profile_revision integer, detail text, profile text, notified_view integer, notified_article integer, notified_profile integer, notified_channel integer, offsync integer, blocked integer, unique(card_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_${guid} (card_id text, revision integer, detail_revision integer, profile_revision integer, detail text, profile text, notified_view integer, notified_article integer, notified_profile integer, notified_channel integer, offsync integer, blocked integer, unique(card_id))`);
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_${guid} (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, blocked integer, read_revision integer, unique(card_id, channel_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_${guid} (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, blocked integer, read_revision integer, unique(card_id, channel_id))`);
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_topic_${guid} (card_id text, channel_id text, topic_id text, channel_revision integer, revision integer, detail_revision integer, detail text, unique(card_id, channel_id, topic_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_topic_${guid} (card_id text, channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(card_id, channel_id, topic_id))`);
} }
const actions = { const actions = {
@ -230,16 +230,7 @@ export function useStoreContext() {
detail: decodeObject(topic.detail), detail: decodeObject(topic.detail),
})); }));
}, },
getChannelTopicDeltaItems: async (guid, channelId, revision) => { setChannelTopicItem: async (guid, channelId, topicId, topic) => {
const values = await getAppValues(db.current, `SELECT topic_id, revision, detail_revision, detail FROM channel_topic_${guid} WHERE channel_id=? AND channel_revision>?`, [channelId, revision]);
return values.map(topic => ({
topicId: topic.topic_id,
revision: topic.revision,
detailRevision: topic.detail_revision,
detail: decodeObject(topic.detail),
}));
},
setChannelTopicItem: async (guid, channelId, topicId, channelRevision, topic) => {
const { id, revision, data } = channel; const { id, revision, data } = channel;
await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?);`, [channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]); await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?);`, [channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]);
}, },
@ -311,16 +302,7 @@ export function useStoreContext() {
detail: decodeObject(topic.detail), detail: decodeObject(topic.detail),
})); }));
}, },
getCardChannelTopicDeltaItems: async (guid, cardId, channelId, revision) => { setCardChannelTopicItem: async (guid, cardId, channelId, topicId, topic) => {
const values = await getAppValues(db.current, `SELECT topic_id, revision, detail_revision, detail FROM card_channel_topic_${guid} WHERE card_id=? AND channel_id=? AND channel_revision>?`, [cardId, channelId, revision]);
return values.map(topic => ({
topicId: topic.topic_id,
revision: topic.revision,
detailRevision: topic.detail_revision,
detail: decodeObject(topic.detail),
}));
},
setCardChannelTopicItem: async (guid, cardId, channelId, topicId, channelRevision, topic) => {
const { id, revision, data } = channel; const { id, revision, data } = channel;
await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (card_id, channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?, ?);`, [cardId, channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]); await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (card_id, channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?, ?);`, [cardId, channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]);
}, },

View File

@ -23,8 +23,6 @@ export function Profile() {
const { state, actions } = useProfile(); const { state, actions } = useProfile();
console.log(state.imageSource);
const setVisible = async (visible) => { const setVisible = async (visible) => {
try { try {
await actions.setVisible(visible); await actions.setVisible(visible);