Skip to content

Commit

Permalink
restructure new storage classes
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Apr 24, 2024
1 parent 6addda0 commit ff22e1e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/js/classes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export var App = function(name, version) {
this.setGistCredentials = function(gist, e) {
console.log("SET CREDENTIALS")
const { token, file } = gist;
self.storage = StorageJs("gist", {token, file});
data.storage.setCredentials(token, file);
};

// Ideally this dependencies should be injected by index.js
Expand Down
38 changes: 20 additions & 18 deletions src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const path = require('path');
const inkjs = require('inkjs');
import { Node } from './node';
import { Utils } from './utils';
import { getFileType, FILETYPE } from './storage';
import { getFileType, FILETYPE, DBStorage, StorageJs } from './storage';

export const data = {
db: DBStorage('yarn-DB', 'Yarn-persistence'),
storage: StorageJs("gist"),
appInstanceStates: ko.observable([]),
restoreFromLocalStorage: ko.observable(true),
// All the bellow go into appInstanceStates, which controls r/w of app states to local storage (for file tabs feature)
Expand Down Expand Up @@ -185,7 +187,7 @@ export const data = {
updatedStates[app.settings.selectedFileTab()] = data.getCurrentAppState();
data.appInstanceStates(updatedStates);
//storage.setItem('appStates', JSON.stringify(data.appInstanceStates()));
app.storage.db.save('appStates', JSON.stringify(data.appInstanceStates()))
data.db.save('appStates', JSON.stringify(data.appInstanceStates()))
app.ui.dispatchEvent('yarnSavedStateToLocalStorage');
}, 700),
loadAppStateFromLocalStorage: async function() {
Expand All @@ -198,7 +200,7 @@ export const data = {
storage.clear(); //TODO remove later
}

const appStatesData = await app.storage.db.getDbValue('appStates');
const appStatesData = await data.db.getDbValue('appStates');
//const appStates = JSON.parse(storage.getItem('appStates')); // appStateS <- new key
const appStates = JSON.parse(appStatesData); // appStateS <- new key

Expand Down Expand Up @@ -956,13 +958,13 @@ export const data = {

tryOpenFile: function() /// Refactor to send signal to the main process
{
app.storage.openLocalFile().then(yarnData => {
data.storage.openLocalFile().then(yarnData => {
data.addDocumentState({
editingName: app.storage.fileName,
editingType: app.storage.fileType,
editingName: data.storage.fileName,
editingType: data.storage.fileType,
yarnData,
});
data.loadData(yarnData, app.storage.fileType, true);
data.loadData(yarnData, data.storage.fileType, true);
});
},

Expand Down Expand Up @@ -1061,13 +1063,13 @@ export const data = {
},

trySaveGist: function(gists) {
if (gists.hasGistSettings()) {
gists.getGistFile().then(gist => {
if (data.storage.hasGistSettings()) {
data.storage.getGistFile().then(gist => {
const gistFiles = Object.keys(gist.body.files);
console.log(gistFiles);
data.promptFileNameAndFormat(({ editingName, yarnData }) => {
data.editingName(editingName);
gists.editGist(gists.file, editingName, yarnData);
data.storage.editGist(gists.file, editingName, yarnData);
Swal.fire(
'Saved!',
`The Yarn has been saved to gist ${gists.file}`,
Expand Down Expand Up @@ -1099,10 +1101,10 @@ export const data = {
},

tryOpenGist: function(gists) {
if (gists.hasGistSettings()) {
if (data.storage.hasGistSettings()) {
const previouslyOpenedGist =
data.lastStorageHost() === 'GIST' ? data.editingName() : '';
gists.getGistFile().then(({ inputOptions, filesInGist }) => {
data.storage.getGistFile().then(({ inputOptions, filesInGist }) => {
Swal.fire({
title: '🐙 Open file from a gist',
input: 'select',
Expand All @@ -1117,7 +1119,7 @@ export const data = {
if (value) {
const content = filesInGist[value].content;
const rawUrl = filesInGist[value].raw_url;
gists.getContentOrRaw(content, rawUrl).then(content => {
data.storage.getContentOrRaw(content, rawUrl).then(content => {
data.openGist(content, value);
});
}
Expand All @@ -1134,15 +1136,15 @@ export const data = {
},

tryAppend: function() {
app.storage.openLocalFile().then(yarnData => {
data.loadData(yarnData, app.storage.fileType, false);
data.storage.openLocalFile().then(yarnData => {
data.loadData(yarnData, data.storage.fileType, false);
});
},

trySave: function(type) {
const fileName =
(data.editingName() || '').replace(/\.[^/.]+$/, '') + '.' + type;
app.storage.saveAsFile(fileName, data.getSaveData).then(result => {
data.storage.saveAsFile(fileName, data.getSaveData).then(result => {
data.setNewFileStats(result.chosenFileName, '', 'LOCAL');
data.editingType(result.type);
});
Expand All @@ -1152,7 +1154,7 @@ export const data = {
if (!data.isDocumentDirty()) return;

if (data.lastStorageHost() === 'GIST') {
const storage = app.storage;
const storage = data.storage;
storage.getGistFile().then(gist => {
data.getSaveData(data.editingType()).then(yarnData => {
data.getSaveData(data.editingType());
Expand All @@ -1169,7 +1171,7 @@ export const data = {
} else if (!data.editingPath()) {
// file access api (web + electron)
data.getSaveData(data.editingType()).then(saveData => {
app.storage.saveToCurrentFile(saveData, data.editingName());
data.storage.saveToCurrentFile(saveData, data.editingName());
});
} else if (data.editingPath().length > 0 && data.editingType().length > 0) {
data.getSaveData(data.editingType()).then(saveData => {
Expand Down
2 changes: 1 addition & 1 deletion src/js/classes/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Settings = function(app) {
ko.extenders.persist = function(target, option) {
target.subscribe(function(newValue) {
storage.setItem(option, newValue);
app.storage.db.save(option, newValue)
app.data.db.save(option, newValue);
});
return target;
};
Expand Down
36 changes: 18 additions & 18 deletions src/js/classes/storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const idb = require('idb');
/////////// Persist via DB api ////////////////
const DBStorage = function(dbName = 'my-db', objectStoreName = 'preferences') {
export const DBStorage = function(
dbName = 'my-db',
objectStoreName = 'preferences'
) {
// requires https://unpkg.com/idb@5/build/iife/index-min.js
return {
dbName,
Expand All @@ -25,16 +28,9 @@ const DBStorage = function(dbName = 'my-db', objectStoreName = 'preferences') {
},
});
},
getDb: function() {
getDbValue: function(key = this.dbName) {
return this.openDatabase().then(_db => {
this.db = _db;
return _db;
});
},
getDbValue: function(key = this.dbName) {
if (this.db) return this.load(key);

return this.getDb().then(_db => {
return this.load(key);
});
},
Expand Down Expand Up @@ -68,10 +64,9 @@ export const getFileType = filename => {
return FILETYPE.UNKNOWN;
};

export const StorageJs = (type = 'gist', credentials) => {
export const StorageJs = (type = 'gist') => {
if (type === 'gist') {
return {
db: DBStorage('yarn-DB', 'Yarn-persistence'),
getFileType,
FILETYPE,
lastStorageHost: 'GIST', // or LOCAL
Expand Down Expand Up @@ -206,8 +201,13 @@ export const StorageJs = (type = 'gist', credentials) => {
setLastStorageHost: function(newHost) {
this.lastStorageHost = newHost;
},
token: credentials.token,
file: credentials.file,
token: undefined,
gistId: undefined,
setCredentials: function(token, gistId){
//console.log("using gist credentials", {token, gistId})
this.token = token
this.gistId = gistId
},
filesInGist: {},
getFilesInGist: function(fileKey) {
if (!fileKey) return this.filesInGist;
Expand Down Expand Up @@ -244,10 +244,10 @@ export const StorageJs = (type = 'gist', credentials) => {
});
},
hasGistSettings: function() {
return this.file && this.file.length > 0;
return this.gistId && this.gistId.length > 0;
},
getGistFile: function() {
return this.getGist(this.file);
return this.getGist(this.gistId);
},
getContentOrRaw: function(content, rawUrl) {
// sometimes github comes back empty handed for content, but has raw_url
Expand All @@ -270,12 +270,12 @@ export const StorageJs = (type = 'gist', credentials) => {
});
},
editGist: function(gistId, fileName, content) {
console.log({ gistId, fileName, content, credentials });
console.log({ gistId, fileName, content });
return fetch('https://api.github.com/gists/' + gistId, {
method: 'POST',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${credentials.token}`,
Authorization: `Bearer ${this.token}`,
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify({
Expand All @@ -289,7 +289,7 @@ export const StorageJs = (type = 'gist', credentials) => {
});
},
editGistFile: function(fileName, content) {
return this.editGist(this.file, fileName, content);
return this.editGist(this.gistId, fileName, content);
},
FILETYPE,
};
Expand Down
2 changes: 1 addition & 1 deletion src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export var Plugins = function(app) {

// register plugins stored on a gist - todo cache all this
if (app.settings.gistPluginsFile() !== null) {
app.storage.getGist(app.settings.gistPluginsFile()).then(({fileList}) => {
app.data.storage.getGist(app.settings.gistPluginsFile()).then(({fileList}) => {
console.log({ fileList });
fileList.forEach(gistFile => {
if (gistFile.language === 'JavaScript') {
Expand Down

0 comments on commit ff22e1e

Please sign in to comment.