Skip to content

Commit

Permalink
Merge pull request #891 from obgnail/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
obgnail authored Dec 14, 2024
2 parents 284581b + e67e508 commit ace8a3d
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 267 deletions.
55 changes: 26 additions & 29 deletions plugin/global/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,38 +174,44 @@ class utils {
}

/** @description param fn cannot be an async function that returns promiseLike object */
static singleflight = (fn, timeout = 1000) => {
let timer, result
static memorize = fn => {
const cache = {}
const isAsync = this.isAsyncFunction(fn)
return function (...args) {
if (!timer) {
timer = setTimeout(() => {
clearTimeout(timer)
timer = null
result = null
}, timeout)

result = isAsync
? Promise.resolve(fn(...args)).catch(e => Promise.reject(e))
: fn(...args)
const key = JSON.stringify(args)
if (cache[key]) {
return cache[key]
}
const result = isAsync
? Promise.resolve(fn(...args)).catch(e => Promise.reject(e))
: fn(...args)
cache[key] = result
return result
}
}

/** @description param fn cannot be an async function that returns promiseLike object */
static memorize = fn => {
const cache = {};
const isAsync = this.isAsyncFunction(fn);
/**
* @description param fn cannot be an async function that returns promiseLike object
* @description strict FIFO not guaranteed. cache.delete() may alter internal state, affecting iterators. But I dont care
**/
static memorizeFIFO = (fn, capacity = 100) => {
const cache = new Map()
const isAsync = this.isAsyncFunction(fn)
return function (...args) {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key]
const key = JSON.stringify(args)
const cacheEntry = cache.get(key)
if (cacheEntry) {
cache.delete(key)
cache.set(key, cacheEntry)
return cacheEntry
}
const result = isAsync
? Promise.resolve(fn(...args)).catch(e => Promise.reject(e))
: fn(...args)
cache[key] = result
cache.set(key, result)
if (capacity > 0 && cache.size > capacity) {
cache.delete(cache.keys().next().value)
}
return result
}
}
Expand All @@ -223,15 +229,6 @@ class utils {
/** @description try not to use it */
static sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

static defer = () => {
const obj = {};
obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve;
obj.reject = reject;
});
return obj;
}

/**
* @example merge({ a: [{ b: 2 }] }, { a: [{ c: 2 }] }) -> { a: [{ c: 2 }] }
* @example merge({ o: { a: 3 } }, { o: { b: 4 } }) -> { o: { a: 3, b: 4 } }
Expand Down
11 changes: 5 additions & 6 deletions plugin/global/styles/search_multi.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
border-radius: 2px;
padding-left: 5px;
padding-right: 80px;
padding-right: 55px;
}

#plugin-search-multi-input svg {
Expand All @@ -42,18 +42,17 @@
display: inline-flex;
flex-direction: row-reverse;
align-items: center;
top: 53%;
top: 52%;
transform: translateY(-50%);
right: 6px;
padding: 2px 1px;
right: 5px;
line-height: 80%;
}

#plugin-search-multi-input .option-btn {
border-radius: 3px;
margin-left: 3px;
margin-left: 2px;
cursor: pointer;
padding: 1px 3px;
padding: 0 3px;
}

#plugin-search-multi-input .option-btn:nth-child(2) {
Expand Down
20 changes: 13 additions & 7 deletions plugin/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ class helpPlugin extends BasePlugin {

process = () => {
this.utils.eventHub.addEventListener(this.utils.eventHub.eventType.allPluginsHadInjected, () => {
this.updater = this.utils.getPlugin("updater");
if (!this.updater) return;
const arg_name = "升级插件" + (this.version ? `(当前版本:${this.version})` : "");
this.callArgs.unshift({ arg_name: arg_name, arg_value: "update_plugin" });
this.preferences = this.utils.getPlugin("preferences")
if (this.preferences) {
this.callArgs.unshift({ arg_name: "启停插件", arg_value: "preferences" })
}
this.updater = this.utils.getPlugin("updater")
if (this.updater) {
const arg_name = "升级插件" + (this.version ? `(当前版本:${this.version})` : "")
this.callArgs.unshift({ arg_name: arg_name, arg_value: "update_plugin" })
}
})
}

Expand Down Expand Up @@ -183,16 +188,17 @@ class helpPlugin extends BasePlugin {
json_rpc: () => this.utils.showInFinder(this.utils.joinPath("./plugin/json_rpc/请读我.md")),
github_picture_bed: () => this.utils.openUrl("https://github.com/obgnail/typora_image_uploader"),
update_plugin: () => this.updater && this.updater.call(),
preferences:() => this.preferences && this.preferences.call(),
uninstall_plugin: () => this.uninstall(),
environment: () => this.showEnv(),
donate: () => this.donate(),
about: () => this.about(),
}
const func = map[type];
func && func();
const func = map[type]
func && func()
}
}

module.exports = {
plugin: helpPlugin,
};
}
Loading

0 comments on commit ace8a3d

Please sign in to comment.