Skip to content

Commit

Permalink
Merge pull request #117 from ipuppet/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ipuppet authored Dec 10, 2022
2 parents c885c05 + aede59d commit 21c2410
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 129 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": "1.8.1",
"version": "1.8.2",
"author": "ipuppet",
"module": false
},
Expand Down
2 changes: 1 addition & 1 deletion dist/CAIO-en.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/CAIO-zh-Hans.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/CAIO.js

Large diffs are not rendered by default.

99 changes: 68 additions & 31 deletions scripts/dao/action-manager-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class ActionManagerData {
// checkUserAction
this.checkUserAction()
// sync
this.sync()
$thread.background({
delay: this.#syncInterval,
handler: () => this.sync(true)
})
}

get actions() {
Expand All @@ -47,12 +50,14 @@ class ActionManagerData {
return this.#actions
}

actionsNeedReload() {
actionsNeedReload(needSync = false) {
this.#actions = undefined
$file.write({
data: $data({ string: JSON.stringify({ date: Date.now() }) }),
path: this.localSyncFile
})
if (needSync) {
$file.write({
data: $data({ string: JSON.stringify({ date: Date.now() }) }),
path: this.localSyncFile
})
}
}

importExampleAction() {
Expand Down Expand Up @@ -110,20 +115,57 @@ class ActionManagerData {
}
}

async downloadFiles(path) {
const list = $file.list(path)
for (let i = 0; i < list.length; i++) {
const subpath = path + "/" + list[i]
if ($file.isDirectory(subpath)) {
await this.downloadFiles(subpath)
} else {
const filename = subpath.substring(subpath.lastIndexOf("/") + 1)
if (filename.endsWith(".icloud")) {
await $file.download(subpath)
}
}
}
}

getSyncDate() {
const localSyncData = JSON.parse($file.read(this.localSyncFile).string)
const localSyncData = JSON.parse($file.read(this.localSyncFile)?.string ?? "{}")
return new Date(localSyncData.date)
}

async sync() {
checkSyncData() {
if (!$file.exists(this.localSyncFile)) {
$file.write({
data: $data({ string: JSON.stringify({ date: 0 }) }),
path: this.localSyncFile
})
}
if (!$file.exists(this.iCloudSyncFile)) {
if ($file.exists(this.iCloudSyncFileUndownloaded)) {
$file.download(this.iCloudSyncFileUndownloaded)
} else {
$file.write({
data: $data({ string: JSON.stringify({ date: 0 }) }),
path: this.iCloudSyncFile
})
}
}
}

async sync(loop = false) {
if (!this.kernel.setting.get("experimental.syncAction")) {
return
}
while (this.#syncLock) {
if (this.#syncLock) {
await $wait(this.#syncInterval)
return
}
this.#syncLock = true

this.checkSyncData()

let iCloudSyncData
if ($file.exists(this.iCloudSyncFileUndownloaded)) {
iCloudSyncData = await $file.download(this.iCloudSyncFileUndownloaded)
Expand All @@ -143,6 +185,7 @@ class ActionManagerData {
$file.delete(usetActionTempPath)
this.#mkdir(usetActionTempPath)
// 从 iCloud 复制到 temp
await this.downloadFiles(this.iCloudPath) // download first
await $file.copy({
src: this.iCloudPath,
dst: usetActionTempPath
Expand All @@ -157,6 +200,7 @@ class ActionManagerData {
this.kernel.print("iCloud data copy success")
// 通知更新 UI
await $wait(1)
this.actionsNeedReload()
$app.notify({
name: "actionSyncStatus",
object: { status: ActionManagerData.syncStatus.success }
Expand All @@ -170,15 +214,21 @@ class ActionManagerData {
})
// 停顿一个同步间隔
await $wait(this.#syncInterval)
// 通知更新 UI
$app.notify({
name: "actionSyncStatus",
object: { status: ActionManagerData.syncStatus.success }
})
}

// 解锁,进行下一次同步
this.#syncLock = false
await $wait(this.#syncInterval)
$thread.background({
delay: 0,
handler: () => this.sync()
})
if (loop) {
$thread.background({
delay: this.#syncInterval,
handler: () => this.sync(loop)
})
}
}

checkUserAction() {
Expand All @@ -193,19 +243,6 @@ class ActionManagerData {
dst: this.iCloudPath
})
}

if (!$file.exists(this.localSyncFile)) {
$file.write({
data: $data({ string: JSON.stringify({ date: 0 }) }),
path: this.localSyncFile
})
}
if (!$file.exists(this.iCloudSyncFile)) {
$file.write({
data: $data({ string: JSON.stringify({ date: 0 }) }),
path: this.iCloudSyncFile
})
}
}

getActionTypes() {
Expand Down Expand Up @@ -339,7 +376,7 @@ class ActionManagerData {
})
this.#saveFile(info.type, info.dir, "README.md", info.readme)

this.actionsNeedReload()
this.actionsNeedReload(true)
}

saveMainJs(info, content) {
Expand All @@ -355,7 +392,7 @@ class ActionManagerData {
data: $data({ string: JSON.stringify(order) }),
path: `${this.iCloudPath}/${type}/${this.actionOrderFile}`
})
this.actionsNeedReload()
this.actionsNeedReload(true)
}

move(from, to) {
Expand Down Expand Up @@ -394,13 +431,13 @@ class ActionManagerData {
})
}

this.actionsNeedReload()
this.actionsNeedReload(true)
}

delete(info) {
$file.delete(`${this.userActionPath}/${info.type}/${info.dir}`)
$file.delete(`${this.iCloudPath}/${info.type}/${info.dir}`)
this.actionsNeedReload()
this.actionsNeedReload(true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/libs/easy-jsbox.js

Large diffs are not rendered by default.

Loading

0 comments on commit 21c2410

Please sign in to comment.