Skip to content

Commit

Permalink
Move creation of browserStorage into a separate method
Browse files Browse the repository at this point in the history
This is in anticipation of the localforage/IndexDB merge
  • Loading branch information
jcbrand committed Oct 11, 2019
1 parent 877e077 commit 7d72a28
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 73 deletions.
3 changes: 1 addition & 2 deletions src/converse-bookmark-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
import "backbone.nativeview";
import "@converse/headless/converse-muc";
import BrowserStorage from "backbone.browserStorage";
import { OrderedListView } from "backbone.overview";
import converse from "@converse/headless/converse-core";
import tpl_bookmark from "templates/bookmark.html";
Expand Down Expand Up @@ -255,7 +254,7 @@ converse.plugins.add('converse-bookmark-views', {
const storage = _converse.config.get('storage'),
id = `converse.room-bookmarks${_converse.bare_jid}-list-model`;
this.list_model = new _converse.BookmarksList({'id': id});
this.list_model.browserStorage = new BrowserStorage[storage](id);
this.list_model.browserStorage = _converse.createStore(id, storage);

const render = () => {
this.render();
Expand Down
3 changes: 1 addition & 2 deletions src/converse-chatboxviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ converse.plugins.add('converse-chatboxviews', {
/* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active.
*/
this.forEach(v => v.close());
return this;
return Promise.all(this.map(v => v.close({'name': 'closeAllChatBoxes'})));
}
});

Expand Down
3 changes: 1 addition & 2 deletions src/converse-emoji-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import "@converse/headless/converse-emoji";
import { debounce, find } from "lodash";
import BrowserStorage from "backbone.browserStorage";
import bootstrap from "bootstrap.native";
import tpl_emoji_button from "templates/emoji_button.html";
import tpl_emojis from "templates/emojis.html";
Expand Down Expand Up @@ -103,7 +102,7 @@ converse.plugins.add('converse-emoji-views', {
const storage = _converse.config.get('storage'),
id = `converse.emoji-${_converse.bare_jid}`;
_converse.emojipicker = new _converse.EmojiPicker({'id': id});
_converse.emojipicker.browserStorage = new BrowserStorage[storage](id);
_converse.emojipicker.browserStorage = _converse.createStore(id, storage);
_converse.emojipicker.fetch();
}
this.emoji_picker_view = new _converse.EmojiPickerView({'model': _converse.emojipicker});
Expand Down
5 changes: 2 additions & 3 deletions src/converse-minimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @module converse-minimize
*/
import "converse-chatview";
import BrowserStorage from "backbone.browserStorage";
import { Overview } from "backbone.overview";
import converse from "@converse/headless/converse-core";
import tpl_chatbox_minimize from "templates/chatbox_minimize.html";
Expand Down Expand Up @@ -459,11 +458,11 @@ converse.plugins.add('converse-minimize', {

initToggle () {
const storage = _converse.config.get('storage'),
id = `converse.minchatstoggle${_converse.bare_jid}`;
id = `converse.minchatstoggle-${_converse.bare_jid}`;
this.toggleview = new _converse.MinimizedChatsToggleView({
'model': new _converse.MinimizedChatsToggle({'id': id})
});
this.toggleview.model.browserStorage = new BrowserStorage[storage](id);
this.toggleview.model.browserStorage = _converse.createStore(id, storage);
this.toggleview.model.fetch();
},

Expand Down
12 changes: 6 additions & 6 deletions src/converse-muc-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "converse-modal";
import "backbone.vdomview";
import "formdata-polyfill";
import "@converse/headless/utils/muc";
import BrowserStorage from "backbone.browserStorage";
import { OrderedListView } from "backbone.overview";
import converse from "@converse/headless/converse-core";
import tpl_add_chatroom_modal from "templates/add_chatroom_modal.html";
Expand Down Expand Up @@ -124,11 +123,12 @@ converse.plugins.add('converse-muc-views', {
if (this.roomspanel && u.isInDOM(this.roomspanel.el)) {
return this.roomspanel;
}
const id = `converse.roomspanel${_converse.bare_jid}`;

this.roomspanel = new _converse.RoomsPanel({
'model': new (_converse.RoomsPanelModel.extend({
'id': `converse.roomspanel${_converse.bare_jid}`, // Required by web storage
'browserStorage': new BrowserStorage[_converse.config.get('storage')](
`converse.roomspanel${_converse.bare_jid}`)
id,
'browserStorage': _converse.createStore(id, _converse.config.get('storage'))
}))()
});
this.roomspanel.model.fetch();
Expand Down Expand Up @@ -933,7 +933,7 @@ converse.plugins.add('converse-muc-views', {
_converse.router.navigate('');
}
this.model.leave();
_converse.ChatBoxView.prototype.close.apply(this, arguments);
return _converse.ChatBoxView.prototype.close.apply(this, arguments);
},

updateOccupantsToggle () {
Expand Down Expand Up @@ -2200,7 +2200,7 @@ converse.plugins.add('converse-muc-views', {
* @method _converse.api.roomviews.close
* @param {(String[]|String)} jids The JID or array of JIDs of the chatroom(s)
*/
'close' (jids) {
close (jids) {
let views;
if (jids === undefined) {
views = _converse.chatboxviews;
Expand Down
9 changes: 4 additions & 5 deletions src/converse-omemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @module converse-omemo
*/
import "converse-profile";
import BrowserStorage from "backbone.browserStorage";
import converse from "@converse/headless/converse-core";
import tpl_toolbar_omemo from "templates/toolbar_omemo.html";

Expand Down Expand Up @@ -988,7 +987,7 @@ converse.plugins.add('converse-omemo', {
this.devices = new _converse.Devices();
const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`;
const storage = _converse.config.get('storage');
this.devices.browserStorage = new BrowserStorage[storage](id);
this.devices.browserStorage = _converse.createStore(id, storage);
this.fetchDevices();
},

Expand Down Expand Up @@ -1017,7 +1016,7 @@ converse.plugins.add('converse-omemo', {
this._devices_promise = new Promise(resolve => {
this.devices.fetch({
'success': c => resolve(this.onDevicesFound(c)),
'error': e => { _converse.log(e, Strophe.LogLevel.ERROR); resolve(); }
'error': (m, e) => { _converse.log(e, Strophe.LogLevel.ERROR); resolve(); }
});
});
}
Expand Down Expand Up @@ -1175,7 +1174,7 @@ converse.plugins.add('converse-omemo', {
const storage = _converse.config.get('storage'),
id = `converse.omemosession-${_converse.bare_jid}`;
_converse.omemo_store = new _converse.OMEMOStore({'id': id});
_converse.omemo_store.browserStorage = new BrowserStorage[storage](id);
_converse.omemo_store.browserStorage = _converse.createStore(id, storage);
}
return _converse.omemo_store.fetchSession();
}
Expand All @@ -1187,7 +1186,7 @@ converse.plugins.add('converse-omemo', {
_converse.devicelists = new _converse.DeviceLists();
const storage = _converse.config.get('storage'),
id = `converse.devicelists-${_converse.bare_jid}`;
_converse.devicelists.browserStorage = new BrowserStorage[storage](id);
_converse.devicelists.browserStorage = _converse.createStore(id, storage);

try {
await fetchOwnDevices();
Expand Down
5 changes: 2 additions & 3 deletions src/converse-roomslist.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* rooms in the "Rooms Panel" of the ControlBox.
*/
import "@converse/headless/converse-muc";
import BrowserStorage from "backbone.browserStorage";
import { OrderedListView } from "backbone.overview";
import converse from "@converse/headless/converse-core";
import tpl_rooms_list from "templates/rooms_list.html";
Expand Down Expand Up @@ -175,7 +174,7 @@ converse.plugins.add('converse-roomslist', {
id = `converse.roomslist${_converse.bare_jid}`;

this.list_model = new _converse.RoomsList({'id': id});
this.list_model.browserStorage = new BrowserStorage[storage](id);
this.list_model.browserStorage = _converse.createStore(id, storage);
this.list_model.fetch();
this.render();
this.sortAndPositionAllItems();
Expand Down Expand Up @@ -271,7 +270,7 @@ converse.plugins.add('converse-roomslist', {
id = `converse.open-rooms-{_converse.bare_jid}`,
model = new _converse.OpenRooms();

model.browserStorage = new BrowserStorage[storage](id);
model.browserStorage = _converse.createStore(id, storage);
_converse.rooms_list_view = new _converse.RoomsListView({'model': model});
/**
* Triggered once the _converse.RoomsListView has been created and initialized.
Expand Down
3 changes: 1 addition & 2 deletions src/converse-rosterview.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import "@converse/headless/converse-chatboxes";
import "@converse/headless/converse-roster";
import "converse-modal";
import "formdata-polyfill";
import BrowserStorage from "backbone.browserStorage";
import { OrderedListView } from "backbone.overview";
import SHA1 from 'strophe.js/src/sha1';
import converse from "@converse/headless/converse-core";
Expand Down Expand Up @@ -807,7 +806,7 @@ converse.plugins.add('converse-rosterview', {
const model = new _converse.RosterFilter();
model.id = `_converse.rosterfilter${_converse.bare_jid}`;
const storage = _converse.config.get('storage');
model.browserStorage = new BrowserStorage[storage](this.filter.id);
model.browserStorage = _converse.createStore(this.filter.id, storage);
this.filter_view = new _converse.RosterFilterView({'model': model});
this.listenTo(this.filter_view.model, 'change', this.updateFilter);
this.filter_view.model.fetch();
Expand Down
5 changes: 2 additions & 3 deletions src/headless/converse-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Converse.js plugin which adds views for bookmarks specified in XEP-0048.
*/
import "@converse/headless/converse-muc";
import BrowserStorage from "backbone.browserStorage";
import converse from "@converse/headless/converse-core";

const { Backbone, Strophe, $iq, sizzle, _ } = converse.env;
Expand Down Expand Up @@ -110,7 +109,7 @@ converse.plugins.add('converse-bookmarks', {
const storage = _converse.config.get('storage');
const cache_key = `converse.room-bookmarks${_converse.bare_jid}`;
this.fetched_flag = cache_key+'fetched';
this.browserStorage = new BrowserStorage[storage](cache_key);
this.browserStorage = _converse.createStore(cache_key, storage);
},

async openBookmarkedRoom (bookmark) {
Expand All @@ -126,7 +125,7 @@ converse.plugins.add('converse-bookmarks', {
if (this.browserStorage.records.length > 0) {
this.fetch({
'success': () => deferred.resolve(),
'error': () => deferred.resolve()
'error': () => deferred.resolve()
});
} else if (! window.sessionStorage.getItem(this.fetched_flag)) {
// There aren't any cached bookmarks and the
Expand Down
7 changes: 3 additions & 4 deletions src/headless/converse-chatboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import "./converse-emoji";
import "./utils/form";
import { get, isObject, isString, propertyOf } from "lodash";
import BrowserStorage from "backbone.browserStorage";
import converse from "./converse-core";
import filesize from "filesize";

Expand Down Expand Up @@ -328,7 +327,7 @@ converse.plugins.add('converse-chatboxes', {
this.messages = new this.messagesCollection();
this.messages.chatbox = this;
const storage = _converse.config.get('storage');
this.messages.browserStorage = new BrowserStorage[storage](this.getMessagesCacheKey());
this.messages.browserStorage = _converse.createStore(this.getMessagesCacheKey(), storage);
this.listenTo(this.messages, 'change:upload', message => {
if (message.get('upload') === _converse.SUCCESS) {
_converse.api.send(this.createMessageStanza(message));
Expand Down Expand Up @@ -1147,8 +1146,8 @@ converse.plugins.add('converse-chatboxes', {
return;
}
const storage = _converse.config.get('storage');
this.browserStorage = new BrowserStorage[storage](
`converse.chatboxes-${_converse.bare_jid}`);
const id = `converse.chatboxes-${_converse.bare_jid}`;
this.browserStorage = _converse.createStore(id, storage);
this.registerMessageHandler();
this.fetch({
'add': true,
Expand Down
12 changes: 9 additions & 3 deletions src/headless/converse-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ _converse.isUniView = function () {
return _.includes(['mobile', 'fullscreen', 'embedded'], _converse.view_mode);
};

_converse.createStore = function (id, storage) {
const s = storage ? storage : _converse.storage[_converse.config.get('storage')];
return new BrowserStorage[s](id);
}


_converse.router = new Backbone.Router();

function initPlugins () {
Expand Down Expand Up @@ -411,7 +417,7 @@ function initClientConfig () {
'trusted': _converse.trusted && true || false,
'storage': _converse.trusted ? 'local' : 'session'
});
_converse.config.browserStorage = new BrowserStorage.session(id);
_converse.config.browserStorage = _converse.createStore(id, "session");
_converse.config.fetch();
/**
* Triggered once the XMPP-client configuration has been initialized.
Expand Down Expand Up @@ -641,7 +647,7 @@ async function initSession (jid) {
const id = `converse.session-${bare_jid}`;
if (!_converse.session || _converse.session.get('id') !== id) {
_converse.session = new Backbone.Model({id});
_converse.session.browserStorage = new BrowserStorage.session(id);
_converse.session.browserStorage = _converse.createStore(id, "session");
await new Promise(r => _converse.session.fetch({'success': r, 'error': r}));
if (_converse.session.get('active')) {
_converse.session.clear();
Expand Down Expand Up @@ -1199,7 +1205,7 @@ _converse.initialize = async function (settings, callback) {
} else {
const id = `converse.xmppstatus-${_converse.bare_jid}`;
_converse.xmppstatus = new this.XMPPStatus({'id': id});
_converse.xmppstatus.browserStorage = new BrowserStorage.session(id);
_converse.xmppstatus.browserStorage = _converse.createStore(id, "session");
_converse.xmppstatus.fetch({
'success': () => _converse.onStatusInitialized(reconnecting),
'error': () => _converse.onStatusInitialized(reconnecting),
Expand Down
34 changes: 13 additions & 21 deletions src/headless/converse-disco.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @description
* Converse plugin which add support for XEP-0030: Service Discovery
*/
import BrowserStorage from "backbone.browserStorage";
import converse from "./converse-core";
import sizzle from "sizzle";

Expand Down Expand Up @@ -44,32 +43,27 @@ converse.plugins.add('converse-disco', {
this.waitUntilFeaturesDiscovered = utils.getResolveablePromise();

this.dataforms = new _converse.Collection();
this.dataforms.browserStorage = new BrowserStorage.session(
`converse.dataforms-${this.get('jid')}`
);
let id = `converse.dataforms-${this.get('jid')}`;
this.dataforms.browserStorage = _converse.createStore(id, 'session');

this.features = new _converse.Collection();
this.features.browserStorage = new BrowserStorage.session(
`converse.features-${this.get('jid')}`
);
id = `converse.features-${this.get('jid')}`;
this.features.browserStorage = _converse.createStore(id, 'session');
this.listenTo(this.features, 'add', this.onFeatureAdded)

this.fields = new _converse.Collection();
this.fields.browserStorage = new BrowserStorage.session(
`converse.fields-${this.get('jid')}`
);
id = `converse.fields-${this.get('jid')}`;
this.fields.browserStorage = _converse.createStore(id, 'session');
this.listenTo(this.fields, 'add', this.onFieldAdded)

this.identities = new _converse.Collection();
this.identities.browserStorage = new BrowserStorage.session(
`converse.identities-${this.get('jid')}`
);
id = `converse.identities-${this.get('jid')}`;
this.identities.browserStorage = _converse.createStore(id, 'session');
this.fetchFeatures(options);

this.items = new _converse.DiscoEntities();
this.items.browserStorage = new BrowserStorage.session(
`converse.disco-items-${this.get('jid')}`
);
id = `converse.disco-items-${this.get('jid')}`;
this.items.browserStorage = _converse.createStore(id, 'session');
this.items.fetch();
},

Expand Down Expand Up @@ -267,7 +261,7 @@ converse.plugins.add('converse-disco', {
const id = `converse.stream-features-${bare_jid}`;
if (!_converse.stream_features || _converse.stream_features.browserStorage.id !== id) {
_converse.stream_features = new _converse.Collection();
_converse.stream_features.browserStorage = new BrowserStorage.session(id);
_converse.stream_features.browserStorage = _converse.createStore(id, "session");
_converse.stream_features.fetch({
success (collection) {
if (collection.length === 0 && _converse.connection.features) {
Expand Down Expand Up @@ -331,10 +325,8 @@ converse.plugins.add('converse-disco', {
_converse.connection.addHandler(onDiscoInfoRequest, Strophe.NS.DISCO_INFO, 'iq', 'get', null, null);

_converse.disco_entities = new _converse.DiscoEntities();
_converse.disco_entities.browserStorage = new BrowserStorage.session(
`converse.disco-entities-${_converse.bare_jid}`
);

const id = `converse.disco-entities-${_converse.bare_jid}`;
_converse.disco_entities.browserStorage = _converse.createStore(id, 'session');
const collection = await _converse.disco_entities.fetchEntities();
if (collection.length === 0 || !collection.get(_converse.domain)) {
// If we don't have an entity for our own XMPP server,
Expand Down
8 changes: 3 additions & 5 deletions src/headless/converse-muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import "./converse-disco";
import "./converse-emoji";
import "./utils/muc";
import BrowserStorage from "backbone.browserStorage";
import converse from "./converse-core";
import u from "./utils/form";

Expand Down Expand Up @@ -488,14 +487,13 @@ converse.plugins.add('converse-muc', {
this.features = new Backbone.Model(
_.assign({id}, _.zipObject(converse.ROOM_FEATURES, converse.ROOM_FEATURES.map(_.stubFalse)))
);
this.features.browserStorage = new BrowserStorage.session(id);
this.features.browserStorage = _converse.createStore(id, "session");
},

initOccupants () {
this.occupants = new _converse.ChatRoomOccupants();
this.occupants.browserStorage = new BrowserStorage.session(
`converse.occupants-${_converse.bare_jid}${this.get('jid')}`
);
const id = `converse.occupants-${_converse.bare_jid}${this.get('jid')}`;
this.occupants.browserStorage = _converse.createStore(id, 'session');
this.occupants.chatroom = this;
this.occupants.fetched = new Promise(resolve => {
this.occupants.fetch({
Expand Down
Loading

0 comments on commit 7d72a28

Please sign in to comment.