Skip to content

Commit ecde023

Browse files
committed
✨ Feature: add createContext for each upload process
1 parent 9950259 commit ecde023

File tree

3 files changed

+109
-61
lines changed

3 files changed

+109
-61
lines changed

src/core/Lifecycle.ts

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,125 +2,128 @@ import { EventEmitter } from 'events'
22
import { ILifecyclePlugins, IPicGo, IPlugin, Undefinable } from '../types'
33
import { handleUrlEncode } from '../utils/common'
44
import { IBuildInEvent } from '../utils/enum'
5+
import { createContext } from '../utils/createContext'
56

67
class Lifecycle extends EventEmitter {
7-
private ctx: IPicGo
8+
private readonly ctx: IPicGo
89

910
constructor (ctx: IPicGo) {
1011
super()
1112
this.ctx = ctx
1213
}
1314

1415
async start (input: any[]): Promise<IPicGo> {
16+
// ensure every upload process has an unique context
17+
const ctx = createContext(this.ctx)
1518
try {
1619
// images input
1720
if (!Array.isArray(input)) {
1821
throw new Error('Input must be an array.')
1922
}
20-
this.ctx.input = input
21-
this.ctx.output = []
23+
ctx.input = input
24+
ctx.output = []
2225

2326
// lifecycle main
24-
await this.beforeTransform()
25-
await this.doTransform()
26-
await this.beforeUpload()
27-
await this.doUpload()
28-
await this.afterUpload()
29-
return this.ctx
27+
await this.beforeTransform(ctx)
28+
await this.doTransform(ctx)
29+
await this.beforeUpload(ctx)
30+
await this.doUpload(ctx)
31+
await this.afterUpload(ctx)
32+
return ctx
3033
} catch (e) {
31-
this.ctx.log.warn('failed')
32-
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, -1)
33-
this.ctx.emit(IBuildInEvent.FAILED, e)
34-
this.ctx.log.error(e)
35-
if (this.ctx.getConfig<Undefinable<string>>('debug')) {
34+
ctx.log.warn('failed')
35+
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, -1)
36+
ctx.emit(IBuildInEvent.FAILED, e)
37+
ctx.log.error(e)
38+
if (ctx.getConfig<Undefinable<string>>('debug')) {
3639
throw e
3740
}
38-
return this.ctx
41+
return ctx
3942
}
4043
}
4144

42-
private async beforeTransform (): Promise<IPicGo> {
43-
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 0)
44-
this.ctx.emit(IBuildInEvent.BEFORE_TRANSFORM, this.ctx)
45-
this.ctx.log.info('Before transform')
46-
await this.handlePlugins(this.ctx.helper.beforeTransformPlugins)
47-
return this.ctx
45+
private async beforeTransform (ctx: IPicGo): Promise<IPicGo> {
46+
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 0)
47+
ctx.emit(IBuildInEvent.BEFORE_TRANSFORM, ctx)
48+
ctx.log.info('Before transform')
49+
await this.handlePlugins(ctx.helper.beforeTransformPlugins, ctx)
50+
return ctx
4851
}
4952

50-
private async doTransform (): Promise<IPicGo> {
51-
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 30)
52-
const type = this.ctx.getConfig<Undefinable<string>>('picBed.transformer') || 'path'
53+
private async doTransform (ctx: IPicGo): Promise<IPicGo> {
54+
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 30)
55+
const type = ctx.getConfig<Undefinable<string>>('picBed.transformer') || 'path'
5356
let currentTransformer = type
54-
let transformer = this.ctx.helper.transformer.get(type)
57+
let transformer = ctx.helper.transformer.get(type)
5558
if (!transformer) {
56-
transformer = this.ctx.helper.transformer.get('path')
59+
transformer = ctx.helper.transformer.get('path')
5760
currentTransformer = 'path'
58-
this.ctx.log.warn(`Can't find transformer - ${type}, switch to default transformer - path`)
61+
ctx.log.warn(`Can't find transformer - ${type}, switch to default transformer - path`)
5962
}
60-
this.ctx.log.info(`Transforming... Current transformer is [${currentTransformer}]`)
61-
await transformer?.handle(this.ctx)
62-
return this.ctx
63+
ctx.log.info(`Transforming... Current transformer is [${currentTransformer}]`)
64+
await transformer?.handle(ctx)
65+
return ctx
6366
}
6467

65-
private async beforeUpload (): Promise<IPicGo> {
66-
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 60)
67-
this.ctx.log.info('Before upload')
68-
this.ctx.emit(IBuildInEvent.BEFORE_UPLOAD, this.ctx)
69-
await this.handlePlugins(this.ctx.helper.beforeUploadPlugins)
70-
return this.ctx
68+
private async beforeUpload (ctx: IPicGo): Promise<IPicGo> {
69+
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 60)
70+
ctx.log.info('Before upload')
71+
ctx.emit(IBuildInEvent.BEFORE_UPLOAD, ctx)
72+
await this.handlePlugins(ctx.helper.beforeUploadPlugins, ctx)
73+
return ctx
7174
}
7275

73-
private async doUpload (): Promise<IPicGo> {
74-
let type = this.ctx.getConfig<Undefinable<string>>('picBed.uploader') || this.ctx.getConfig<Undefinable<string>>('picBed.current') || 'smms'
75-
let uploader = this.ctx.helper.uploader.get(type)
76+
private async doUpload (ctx: IPicGo): Promise<IPicGo> {
77+
let type = ctx.getConfig<Undefinable<string>>('picBed.uploader') || ctx.getConfig<Undefinable<string>>('picBed.current') || 'smms'
78+
let uploader = ctx.helper.uploader.get(type)
7679
let currentTransformer = type
7780
if (!uploader) {
7881
type = 'smms'
7982
currentTransformer = 'smms'
80-
uploader = this.ctx.helper.uploader.get('smms')
81-
this.ctx.log.warn(`Can't find uploader - ${type}, switch to default uploader - smms`)
83+
uploader = ctx.helper.uploader.get('smms')
84+
ctx.log.warn(`Can't find uploader - ${type}, switch to default uploader - smms`)
8285
}
83-
this.ctx.log.info(`Uploading... Current uploader is [${currentTransformer}]`)
84-
await uploader?.handle(this.ctx)
85-
for (const outputImg of this.ctx.output) {
86+
ctx.log.info(`Uploading... Current uploader is [${currentTransformer}]`)
87+
await uploader?.handle(ctx)
88+
for (const outputImg of ctx.output) {
8689
outputImg.type = type
8790
}
88-
return this.ctx
91+
return ctx
8992
}
9093

91-
private async afterUpload (): Promise<IPicGo> {
92-
this.ctx.emit(IBuildInEvent.AFTER_UPLOAD, this.ctx)
93-
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 100)
94-
await this.handlePlugins(this.ctx.helper.afterUploadPlugins)
94+
private async afterUpload (ctx: IPicGo): Promise<IPicGo> {
95+
ctx.emit(IBuildInEvent.AFTER_UPLOAD, ctx)
96+
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 100)
97+
await this.handlePlugins(ctx.helper.afterUploadPlugins, ctx)
9598
let msg = ''
96-
const length = this.ctx.output.length
99+
const length = ctx.output.length
97100
for (let i = 0; i < length; i++) {
98-
msg += handleUrlEncode(this.ctx.output[i].imgUrl)
101+
msg += handleUrlEncode(ctx.output[i].imgUrl)
99102
if (i !== length - 1) {
100103
msg += '\n'
101104
}
102-
delete this.ctx.output[i].base64Image
103-
delete this.ctx.output[i].buffer
105+
delete ctx.output[i].base64Image
106+
delete ctx.output[i].buffer
104107
}
105-
this.ctx.emit(IBuildInEvent.FINISHED, this.ctx)
106-
this.ctx.log.success(`\n${msg}`)
107-
return this.ctx
108+
ctx.emit(IBuildInEvent.FINISHED, ctx)
109+
ctx.log.success(`\n${msg}`)
110+
return ctx
108111
}
109112

110-
private async handlePlugins (lifeCyclePlugins: ILifecyclePlugins): Promise<IPicGo> {
113+
private async handlePlugins (lifeCyclePlugins: ILifecyclePlugins, ctx: IPicGo): Promise<IPicGo> {
111114
const plugins = lifeCyclePlugins.getList()
112115
const pluginNames = lifeCyclePlugins.getIdList()
113116
const lifeCycleName = lifeCyclePlugins.getName()
114117
await Promise.all(plugins.map(async (plugin: IPlugin, index: number) => {
115118
try {
116-
this.ctx.log.info(`${lifeCycleName}: ${pluginNames[index]} running`)
117-
await plugin.handle(this.ctx)
119+
ctx.log.info(`${lifeCycleName}: ${pluginNames[index]} running`)
120+
await plugin.handle(ctx)
118121
} catch (e) {
119-
this.ctx.log.error(`${lifeCycleName}: ${pluginNames[index]} error`)
122+
ctx.log.error(`${lifeCycleName}: ${pluginNames[index]} error`)
120123
throw e
121124
}
122125
}))
123-
return this.ctx
126+
return ctx
124127
}
125128
}
126129

src/core/PicGo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class PicGo extends EventEmitter implements IPicGo {
115115

116116
getConfig<T> (name?: string): T {
117117
if (!name) {
118+
console.log(this._config)
118119
return this._config as unknown as T
119120
} else {
120121
return get(this._config, name)

src/utils/createContext.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { IPicGo } from '../types'
2+
3+
/**
4+
* create an unique context for each upload process
5+
* @param ctx
6+
*/
7+
export const createContext = (ctx: IPicGo): IPicGo => {
8+
return {
9+
configPath: ctx.configPath,
10+
baseDir: ctx.baseDir,
11+
log: ctx.log,
12+
cmd: ctx.cmd,
13+
output: [],
14+
input: [],
15+
pluginLoader: ctx.pluginLoader,
16+
pluginHandler: ctx.pluginHandler,
17+
Request: ctx.Request,
18+
helper: ctx.helper,
19+
VERSION: ctx.VERSION,
20+
GUI_VERSION: ctx.GUI_VERSION,
21+
request: ctx.request,
22+
getConfig: ctx.getConfig.bind(ctx),
23+
saveConfig: ctx.saveConfig.bind(ctx),
24+
removeConfig: ctx.removeConfig.bind(ctx),
25+
setConfig: ctx.setConfig.bind(ctx),
26+
unsetConfig: ctx.unsetConfig.bind(ctx),
27+
upload: ctx.upload.bind(ctx),
28+
addListener: ctx.addListener.bind(ctx),
29+
on: ctx.on.bind(ctx),
30+
once: ctx.once.bind(ctx),
31+
removeListener: ctx.removeListener.bind(ctx),
32+
off: ctx.off.bind(ctx),
33+
removeAllListeners: ctx.removeAllListeners.bind(ctx),
34+
setMaxListeners: ctx.setMaxListeners.bind(ctx),
35+
getMaxListeners: ctx.getMaxListeners.bind(ctx),
36+
listeners: ctx.listeners.bind(ctx),
37+
rawListeners: ctx.rawListeners.bind(ctx),
38+
emit: ctx.emit.bind(ctx),
39+
listenerCount: ctx.listenerCount.bind(ctx),
40+
prependListener: ctx.prependListener.bind(ctx),
41+
prependOnceListener: ctx.prependOnceListener.bind(ctx),
42+
eventNames: ctx.eventNames.bind(ctx)
43+
}
44+
}

0 commit comments

Comments
 (0)