Преглед изворни кода

Move private API to own file

JC Brand пре 2 година
родитељ
комит
885a3d92b5

+ 2 - 35
src/headless/core.js

@@ -5,46 +5,13 @@
 import './shared/constants.js';
 import _converse from './shared/_converse';
 import advancedFormat from 'dayjs/plugin/advancedFormat';
-import connection_api from './shared/connection/api.js';
+import api from './shared/api/index.js';
 import dayjs from 'dayjs';
 import i18n from './shared/i18n';
-import { settings_api } from './shared/settings/api.js';
-import send_api from './shared/api/send.js';
-import user_api from './shared/api/user.js';
-import events_api from './shared/api/events.js';
-import promise_api from './shared/api/promise.js';
 
 export { converse } from './shared/api/public.js';
 export { _converse };
 export { i18n };
+export { api };
 
 dayjs.extend(advancedFormat);
-
-/**
- * ### The private API
- *
- * The private API methods are only accessible via the closured {@link _converse}
- * object, which is only available to plugins.
- *
- * These methods are kept private (i.e. not global) because they may return
- * sensitive data which should be kept off-limits to other 3rd-party scripts
- * that might be running in the page.
- *
- * @namespace _converse.api
- * @memberOf _converse
- */
-export const api = _converse.api = {
-    connection: connection_api,
-    settings: settings_api,
-    ...send_api,
-    ...user_api,
-    ...events_api,
-    ...promise_api,
-};
-
-
-_converse.shouldClearCache = () => (
-    !_converse.config.get('trusted') ||
-    api.settings.get('clear_cache_on_logout') ||
-    _converse.isTestEnv()
-);

+ 3 - 2
src/headless/plugins/chat/utils.js

@@ -1,7 +1,8 @@
+import log from '@converse/headless/log.js';
 import { _converse, api, converse } from '@converse/headless/core.js';
 import { isArchived, isHeadline, isServerMessage, } from '@converse/headless/shared/parsers';
 import { parseMessage } from './parsers.js';
-import log from '@converse/headless/log.js';
+import { shouldClearCache } from '@converse/headless/utils/core.js';
 
 const { Strophe, u } = converse.env;
 
@@ -13,7 +14,7 @@ export function openChat (jid) {
 }
 
 export async function onClearSession () {
-    if (_converse.shouldClearCache()) {
+    if (shouldClearCache()) {
         await Promise.all(
             _converse.chatboxes.map(c => c.messages && c.messages.clearStore({ 'silent': true }))
         );

+ 2 - 1
src/headless/plugins/roster/utils.js

@@ -3,6 +3,7 @@ import { Model } from '@converse/skeletor/src/model.js';
 import { RosterFilter } from '@converse/headless/plugins/roster/filter.js';
 import { _converse, api, converse } from "@converse/headless/core";
 import { initStorage } from '@converse/headless/utils/storage.js';
+import { shouldClearCache } from '@converse/headless/utils/core.js';
 
 const { $pres } = converse.env;
 
@@ -88,7 +89,7 @@ async function clearPresences () {
  */
 export async function onClearSession () {
     await clearPresences();
-    if (_converse.shouldClearCache()) {
+    if (shouldClearCache()) {
         if (_converse.rostergroups) {
             await _converse.rostergroups.clearStore();
             delete _converse.rostergroups;

+ 2 - 1
src/headless/plugins/status/index.js

@@ -5,6 +5,7 @@
 import XMPPStatus from './status.js';
 import status_api from './api.js';
 import { _converse, api, converse } from '@converse/headless/core';
+import { shouldClearCache } from '@converse/headless/utils/core.js';
 import {
     addStatusToMUCJoinPresence,
     initStatus,
@@ -52,7 +53,7 @@ converse.plugins.add('converse-status', {
         });
 
         api.listen.on('clearSession', () => {
-            if (_converse.shouldClearCache() && _converse.xmppstatus) {
+            if (shouldClearCache() && _converse.xmppstatus) {
                 _converse.xmppstatus.destroy();
                 delete _converse.xmppstatus;
                 api.promises.add(['statusInitialized']);

+ 2 - 1
src/headless/plugins/vcard/utils.js

@@ -1,6 +1,7 @@
 import log from "@converse/headless/log";
 import { _converse, api, converse } from "../../core.js";
 import { initStorage } from '@converse/headless/utils/storage.js';
+import { shouldClearCache } from '@converse/headless/utils/core.js';
 
 const { Strophe, $iq, u } = converse.env;
 
@@ -163,7 +164,7 @@ export async function initVCardCollection () {
 
 
 export function clearVCardsSession () {
-    if (_converse.shouldClearCache()) {
+    if (shouldClearCache()) {
         api.promises.add('VCardsInitialized');
         if (_converse.vcards) {
             _converse.vcards.clearStore();

+ 4 - 0
src/headless/shared/_converse.js

@@ -8,6 +8,7 @@ import { TimeoutError } from './errors.js';
 import { createStore, getDefaultStore } from '../utils/storage.js';
 import { getInitSettings } from './settings/utils.js';
 import { getOpenPromise } from '@converse/openpromise';
+import { shouldClearCache } from '../utils/core.js';
 
 
 /**
@@ -18,8 +19,11 @@ import { getOpenPromise } from '@converse/openpromise';
  */
 const _converse = {
     log,
+    shouldClearCache, // TODO: Should be moved to utils with next major release
+
     CONNECTION_STATUS,
     VERSION_NAME,
+
     templates: {},
     promises: {
         'initialized': getOpenPromise()

+ 31 - 0
src/headless/shared/api/index.js

@@ -0,0 +1,31 @@
+import _converse from '../_converse.js';
+import connection_api from '../connection/api.js';
+import events_api from '../api/events.js';
+import promise_api from '../api/promise.js';
+import send_api from '../api/send.js';
+import user_api from '../api/user.js';
+import { settings_api } from '../settings/api.js';
+
+/**
+ * ### The private API
+ *
+ * The private API methods are only accessible via the closured {@link _converse}
+ * object, which is only available to plugins.
+ *
+ * These methods are kept private (i.e. not global) because they may return
+ * sensitive data which should be kept off-limits to other 3rd-party scripts
+ * that might be running in the page.
+ *
+ * @namespace _converse.api
+ * @memberOf _converse
+ */
+const api = _converse.api = {
+    connection: connection_api,
+    settings: settings_api,
+    ...send_api,
+    ...user_api,
+    ...events_api,
+    ...promise_api,
+};
+
+export default api;

+ 9 - 1
src/headless/utils/core.js

@@ -48,6 +48,13 @@ export function isUniView () {
     return ['mobile', 'fullscreen', 'embedded'].includes(settings_api.get("view_mode"));
 }
 
+export function shouldClearCache () {
+    const { api } = _converse;
+    return !_converse.config.get('trusted') ||
+        api.settings.get('clear_cache_on_logout') ||
+        _converse.isTestEnv();
+}
+
 
 export async function tearDown () {
     await _converse.api.trigger('beforeTearDown', {'synchronous': true});
@@ -65,7 +72,7 @@ export async function tearDown () {
 export function clearSession () {
     _converse.session?.destroy();
     delete _converse.session;
-    _converse.shouldClearCache() && _converse.api.user.settings.clear();
+    shouldClearCache() && _converse.api.user.settings.clear();
     /**
      * Synchronouse event triggered once the user session has been cleared,
      * for example when the user has logged out or when Converse has
@@ -624,6 +631,7 @@ export function saveWindowState (ev) {
 
 
 export default Object.assign({
+    shouldClearCache,
     waitUntil, // TODO: remove. Only the API should be used
     isErrorObject,
     getRandomInt,

+ 3 - 2
src/plugins/omemo/index.js

@@ -13,7 +13,8 @@ import Devices from './devices.js';
 import OMEMOStore from './store.js';
 import log from '@converse/headless/log';
 import omemo_api from './api.js';
-import { _converse, api, converse } from '@converse/headless/core';
+import { _converse, api, converse } from '@converse/headless/core.js';
+import { shouldClearCache } from '@converse/headless/utils/core.js';
 import {
     createOMEMOMessageStanza,
     encryptFile,
@@ -109,7 +110,7 @@ converse.plugins.add('converse-omemo', {
 
         api.listen.on('clearSession', () => {
             delete _converse.omemo_store
-            if (_converse.shouldClearCache() && _converse.devicelists) {
+            if (shouldClearCache() && _converse.devicelists) {
                 _converse.devicelists.clearStore();
                 delete _converse.devicelists;
             }