Skip to content

Commit

Permalink
chore: Update version to 2.3.6 and optimize actions caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ipuppet committed Sep 27, 2024
1 parent 73bb7c2 commit f68b8b9
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 49 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"info": {
"name": "CAIO",
"version": "2.3.5",
"version": "2.3.6",
"author": "ipuppet",
"module": false
},
Expand Down
2 changes: 1 addition & 1 deletion dist/CAIO.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/action/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class Action {
* @returns
*/
getAction(category, name, data) {
const dir = this.#kernel.actions.getActionDirByName(name)
const dir = this.#kernel.actions.getActionDir(category, name)
return this.#kernel.actions.getAction(category, dir, data)
}

Expand Down
4 changes: 3 additions & 1 deletion scripts/app-main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { UIKit, ViewController, TabBarController } = require("./libs/easy-jsbox")
const { UIKit, ViewController, TabBarController, FileManager } = require("./libs/easy-jsbox")
const { AppKernelBase } = require("./app")

const compatibility = require("./compatibility")
Expand All @@ -13,6 +13,8 @@ class AppKernel extends AppKernelBase {
this.query = $context.query

settingMethods(this)

this.fileManager = new FileManager()
}
}

Expand Down
35 changes: 23 additions & 12 deletions scripts/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { UIKit, Kernel, Logger, FileStorage, Setting, FileManager } = require("./libs/easy-jsbox")
const { UIKit, Kernel, Logger, FileStorage, Setting } = require("./libs/easy-jsbox")
const SettingStructure = require("./setting/setting")
const { Storage } = require("./dao/storage")
const Clips = require("./ui/clips/clips")
Expand All @@ -13,6 +13,8 @@ class AppKernelBase extends Kernel {
})

#storage
#clips
#actions

constructor() {
super()
Expand All @@ -30,35 +32,44 @@ class AppKernelBase extends Kernel {
fileStorage: this.fileStorage,
structure: SettingStructure
})
this.initComponents()
}

get logFile() {
return "caio.log"
}

get isWebdavEnabled() {
return this.setting.get("webdav.status")
}

/**
* @type {Storage}
*/
get storage() {
if (!this.#storage) {
this.logger.info("init storage")
this.#storage = new Storage(this)
}
return this.#storage
}

get isWebdavEnabled() {
return this.setting.get("webdav.status")
/**
* @type {Clips}
*/
get clips() {
if (!this.#clips) {
this.#clips = new Clips(this)
}
return this.#clips
}

initComponents() {
// Clips
this.clips = new Clips(this)
// Actions
this.actions = new Actions(this)
// FileManager
this.fileManager = new FileManager()
/**
* @type {Actions}
*/
get actions() {
if (!this.#actions) {
this.#actions = new Actions(this)
}
return this.#actions
}
}

Expand Down
7 changes: 6 additions & 1 deletion scripts/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Compatibility {
}

class VersionActions {
version = 13
version = 14
userVersion = $cache.get("compatibility.version") ?? 0

/**
Expand All @@ -150,6 +150,7 @@ class VersionActions {
this.call(i)
}
this.compatibility.do().catch(e => this.kernel.logger.error(e))
this.kernel.logger.info(`compatibility: userVersion [${this.userVersion}] updated to [${this.version}]`)
}

// 修改版本
Expand Down Expand Up @@ -291,6 +292,10 @@ class VersionActions {
"assets/icon"
])
}

ver14() {
$cache.remove(this.kernel.actions.allActionsCacheKey)
}
}

/**
Expand Down
44 changes: 25 additions & 19 deletions scripts/dao/action-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ActionsData {
allActionsCacheKey = "allActions"

#actions
#allActions

/**
* @param {AppKernel} kernel
Expand All @@ -38,9 +37,10 @@ class ActionsData {
return this.#actions
}
get allActions() {
const allActions = $cache.get(this.allActionsCacheKey)
// 无分类的单层数组
if (!this.#allActions) this.#initActions()
return this.#allActions
if (!allActions || typeof allActions !== "object") this.#initActions()
return allActions
}

get isNew() {
Expand All @@ -59,23 +59,22 @@ class ActionsData {
items: this.getActions(category) // 过程中可能调用 saveOrder 导致 this.#allActions 被置为 undefined
}
})
this.#allActions = {}
const allActions = {}
this.#actions.map(category =>
category.items.forEach(item => {
this.#allActions[item.name] = item
allActions[category.category + item.dir] = item
})
)
$cache.set(this.allActionsCacheKey, this.#allActions)
this.kernel.logger.info(`init actions`)
$cache.set(this.allActionsCacheKey, allActions)
this.kernel.logger.info(`init action-data`)
}

importExampleAction() {
try {
Object.keys(__ACTIONS__).forEach(category => {
Object.keys(__ACTIONS__[category]).forEach(dir => {
const action = __ACTIONS__[category][dir]
const config = JSON.parse(action.config)
if (!this.exists(config.name)) {
if (!this.exists(category, dir)) {
this.importAction(action, category, false)
}
})
Expand All @@ -86,8 +85,7 @@ class ActionsData {
if ($file.isDirectory(actionCategoryPath)) {
const userActionCategoryPath = `${this.userActionPath}/${category}`
$file.list(actionCategoryPath).forEach(dir => {
const config = JSON.parse($file.read(`${actionCategoryPath}/${dir}/config.json`).string)
if (!this.exists(config.name)) {
if (!this.exists(category, dir)) {
$file.mkdir(userActionCategoryPath)
$file.copy({
src: `${actionCategoryPath}/${dir}`,
Expand Down Expand Up @@ -188,7 +186,7 @@ class ActionsData {

setNeedReload(animate) {
this.#actions = undefined
this.#allActions = undefined
$cache.remove(this.allActionsCacheKey)

// 通知更新 UI
try {
Expand All @@ -197,9 +195,7 @@ class ActionsData {
}

needUpload() {
this.setNeedReload() // Reload UI and data
// Update cache
$cache.set(this.allActionsCacheKey, this.allActions)
this.setNeedReload()
if (!this.webdavSync) return
this.webdavSync.needUpload()
}
Expand Down Expand Up @@ -380,8 +376,18 @@ class ActionsData {
return $text.MD5(name)
}

getActionDirByName(name) {
return this.allActions[name].dir
getActionDir(category, name) {
const md5 = this.initActionDirByName(name)
if ($file.isDirectory(this.getActionPath(category, md5))) {
return md5
}
for (const action of this.actions[category].items) {
if (action.name === name) {
return action.dir
}
}

return null
}

getAction(category, dir, data) {
Expand Down Expand Up @@ -551,8 +557,8 @@ class ActionsData {
this.needUpload(true)
}

exists(name) {
return this.allActions[name] !== undefined
exists(category, dir) {
return this.allActions[category + dir] !== undefined
}
}

Expand Down
3 changes: 1 addition & 2 deletions scripts/dao/clips-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class ClipsData {
this.tabItemsIndex.forEach((table, tabIndex) => {
if (!this.#allClips[tabIndex]) {
this.#allClips[tabIndex] = this.#initData(table)
this.kernel.logger.info(`init clips: ${table}`)
}
})
return this.#allClips
Expand All @@ -78,7 +77,6 @@ class ClipsData {
get clips() {
if (!this.#allClips[this.tabIndex]) {
this.#allClips[this.tabIndex] = this.#initData(this.table)
this.kernel.logger.info(`init clips: ${this.table}`)
}
return this.#allClips[this.tabIndex]
}
Expand Down Expand Up @@ -114,6 +112,7 @@ class ClipsData {
sorted.forEach((data, i) => {
this.clipsUUIDMap[data.uuid] = { tab: table, index: i }
})
this.kernel.logger.info(`init clip-data: ${this.table}`)
return this.#clipProxy(sorted)
} catch (error) {
this.kernel.logger.error(error)
Expand Down
2 changes: 1 addition & 1 deletion scripts/ui/action/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ActionEditor {
const sheetDone = async () => {
if (this.isNew) {
this.editingActionInfo.dir = this.data.initActionDirByName(this.editingActionInfo.name)
if (this.data.exists(this.editingActionInfo.name)) {
if (this.data.exists(this.editingActionInfo.category, this.editingActionInfo.dir)) {
const resp = await $ui.alert({
title: $l10n("UNABLE_CREATE_ACTION"),
message: $l10n("ACTION_NAME_ALREADY_EXISTS").replaceAll("${name}", this.editingActionInfo.name)
Expand Down
3 changes: 1 addition & 2 deletions scripts/ui/components/selectActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { Sheet } = require("../../libs/easy-jsbox")
*/

class SelectActions {
static shared = new SelectActions()
cacheKey

/**
Expand All @@ -26,7 +25,7 @@ class SelectActions {
return []
}

return actions.filter(action => this.kernel.actions.exists(action.name))
return actions.filter(action => this.kernel.actions.exists(action.category, action.dir))
}

addAction(action) {
Expand Down
12 changes: 4 additions & 8 deletions scripts/widget/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ const { TodayPinActions } = require("../ui/components/today-actions")
* @typedef {import("../app-main").AppKernel} AppKernel
*/
class ActionsWidget {
actions = []

/**
* @param {AppKernel} kernel
*/
constructor(kernel = {}) {
this.kernel = kernel
this.baseUrlScheme = `jsbox://run?name=${$addin.current.name}`

this.todayPinActions = new TodayPinActions(this.kernel)
this.actions = this.todayPinActions.getActions()
this.actions = TodayPinActions.shared.setKernel(this.kernel).getActions()
if (this.actions.length === 0) {
this.actions = $cache.get(this.kernel.actions.allActionsCacheKey)
if (!Array.isArray(this.actions)) {
$cache.set(this.kernel.actions.allActionsCacheKey, this.kernel.actions.allActions)
this.actions = this.kernel.actions.allActions
}
this.actions = Object.values(this.actions)
this.actions = Object.values(this.kernel.actions.allActions)
}
}

Expand Down

0 comments on commit f68b8b9

Please sign in to comment.