Skip to content

Commit

Permalink
优化性能
Browse files Browse the repository at this point in the history
  • Loading branch information
ipuppet committed Aug 3, 2023
1 parent 36611f8 commit b1ecf41
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 197 deletions.
9 changes: 4 additions & 5 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const config = JSON.parse(fs.readFileSync(path.join(__dirname, "config.json"), "

const outputName = config.info.name
const distEntryPath = path.join(__dirname, `dist/${outputName}.js`)
const entryFile = "main.js"
const entryFileContent = fs.readFileSync(path.join(__dirname, "main.js"), "utf-8")

const entryFile = "main.build.js"
const entryFilePath = path.join(__dirname, entryFile)
const entryFileContent = fs.readFileSync(entryFilePath, "utf-8")

function injectContent() {
const stringsFolder = path.join(__dirname, "strings")
const stringsFiles = fs.readdirSync(stringsFolder)
const localizedText = {}

stringsFiles.forEach(fileName => {
if (path.extname(fileName) !== ".strings") {
return
Expand All @@ -33,7 +33,6 @@ function injectContent() {
}
})
})

const stringsText = `$app.strings = ${JSON.stringify(localizedText)};`

const configSettings = Object.keys(config.settings)
Expand Down Expand Up @@ -154,7 +153,7 @@ async function build() {
}
} finally {
// 恢复文件内容
fs.writeFileSync(entryFilePath, entryFileContent)
fs.unlinkSync(entryFilePath)
fs.writeFileSync(packageJsonPath, packageJsonContent)
}
}
Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"info": {
"name": "CAIO",
"version": "1.10.8",
"version": "1.11.0",
"author": "ipuppet",
"module": false
},
Expand All @@ -13,4 +13,4 @@
"keyboardToolbarEnabled": true,
"rotateDisabled": false
}
}
}
29 changes: 29 additions & 0 deletions dist/CAIO-en.json

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions dist/CAIO-zh-Hans.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/CAIO.js

Large diffs are not rendered by default.

38 changes: 35 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
// run app
const app = require("./scripts/app")
app.run()
let AppInstance

switch ($app.env) {
case $env.app:
case $env.action:
AppInstance = require("./scripts/app")
break
case $env.today:
case $env.keyboard:
AppInstance = require("./scripts/app-lite")
break
case $env.widget:
AppInstance = require("./scripts/widget")
break

default:
$intents.finish("不支持在此环境中运行")
$ui.render({
views: [
{
type: "label",
props: {
text: "不支持在此环境中运行",
align: $align.center
},
layout: $layout.fill
}
]
})
break
}

if (AppInstance) {
AppInstance.run()
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"license": "MIT",
"scripts": {
"build": "node build.js"
}
{
"license": "MIT",
"scripts": {
"build": "node build.js"
}
}
71 changes: 71 additions & 0 deletions scripts/app-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { Kernel, Logger, FileStorage, Setting, FileManager } = require("./libs/easy-jsbox")
const SettingStructure = require("./setting/setting")
const { Storage } = require("./dao/storage")
const Clips = require("./ui/clips")
const ActionManager = require("./ui/components/action-manager")

/**
* @typedef {AppKernelBase} AppKernelBase
*/
class AppKernelBase extends Kernel {
static fileStorage = new FileStorage()

logPath = "logs"
logFile = "caio.log"
logFilePath = FileStorage.join(this.logPath, this.logFile)

#storage

constructor() {
super()
// FileStorage
this.fileStorage = AppKernelBase.fileStorage
// Logger
this.logger = new Logger()
this.logger.printToFile(this.fileStorage, this.logFilePath)
// Setting
this.setting = new Setting({
fileStorage: this.fileStorage,
structure: SettingStructure
})
this.setting.setReadonly()
this.initComponents()
}

get storage() {
if (!this.#storage) {
this.print("init storage")
this.#storage = new Storage(this)
}
return this.#storage
}

error(message) {
if (this.fileStorage.exists(this.logFilePath)) {
const logFileSize = this.fileStorage.readSync(this.logFilePath)?.info?.size ?? 0
if (logFileSize > 1024 * 10) {
const dist = FileStorage.join(this.logPath, `caio.${Date.now()}.log`)
this.fileStorage.move(this.logFilePath, dist)
}
}

if (message instanceof Error) {
message = `${message}\n${message.stack}`
}
super.error(message)
this.logger.error(message)
}

initComponents() {
// Clips
this.clips = new Clips(this)
// ActionManager
this.actionManager = new ActionManager(this)
// FileManager
this.fileManager = new FileManager()
}
}

module.exports = {
AppKernelBase
}
58 changes: 58 additions & 0 deletions scripts/app-lite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { AppKernelBase } = require("./app-base")

/**
* @typedef {AppKernel} AppKernel
*/
class AppKernel extends AppKernelBase {
constructor() {
super()

this.setting.setReadonly()
}

addOpenInJsboxButton() {
this.useJsboxNav()
this.setNavButtons([
{
image: $image("assets/icon.png"),
handler: () => this.openInJsbox()
}
])
}
}

class AppUI {
// 小组件模式下不初始化 AppKernel
static kernel = $app.env !== $env.widget ? new AppKernel() : undefined

static renderKeyboardUI() {
this.kernel.addOpenInJsboxButton()

const Keyboard = require("./ui/keyboard")
const keyboard = new Keyboard(this.kernel)

this.kernel.KeyboardRender(keyboard.getView())
}

static renderTodayUI() {
this.kernel.addOpenInJsboxButton()

const Today = require("./ui/today")
const today = new Today(this.kernel)

this.kernel.UIRender(today.getView())
}
}

module.exports = {
run: () => {
//AppUI.renderKeyboardUI();return
//AppUI.renderTodayUI();return

if ($app.env === $env.today) {
AppUI.renderTodayUI()
} else if ($app.env === $env.keyboard) {
AppUI.renderKeyboardUI()
}
}
}
Loading

0 comments on commit b1ecf41

Please sign in to comment.