|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { isAsyncFunction } from 'is-type-of'; |
| 3 | +import type { EggCore } from './egg.js'; |
| 4 | + |
| 5 | +export type SingletonCreateMethod = |
| 6 | + (config: Record<string, any>, app: EggCore, clientName: string) => unknown | Promise<unknown>; |
| 7 | + |
| 8 | +export interface SingletonOptions { |
| 9 | + name: string; |
| 10 | + app: EggCore; |
| 11 | + create: SingletonCreateMethod; |
| 12 | +} |
| 13 | + |
| 14 | +export class Singleton<T = any> { |
| 15 | + readonly clients = new Map<string, T>(); |
| 16 | + readonly app: EggCore; |
| 17 | + readonly create: SingletonCreateMethod; |
| 18 | + readonly name: string; |
| 19 | + readonly options: Record<string, any>; |
| 20 | + |
| 21 | + constructor(options: SingletonOptions) { |
| 22 | + assert(options.name, '[@eggjs/core/singleton] Singleton#constructor options.name is required'); |
| 23 | + assert(options.app, '[@eggjs/core/singleton] Singleton#constructor options.app is required'); |
| 24 | + assert(options.create, '[@eggjs/core/singleton] Singleton#constructor options.create is required'); |
| 25 | + assert(!(options.name in options.app), `[@eggjs/core/singleton] ${options.name} is already exists in app`); |
| 26 | + this.app = options.app; |
| 27 | + this.name = options.name; |
| 28 | + this.create = options.create; |
| 29 | + this.options = options.app.config[this.name] ?? {}; |
| 30 | + } |
| 31 | + |
| 32 | + init() { |
| 33 | + return isAsyncFunction(this.create) ? this.initAsync() : this.initSync(); |
| 34 | + } |
| 35 | + |
| 36 | + initSync() { |
| 37 | + const options = this.options; |
| 38 | + assert(!(options.client && options.clients), |
| 39 | + `[@eggjs/core/singleton] ${this.name} can not set options.client and options.clients both`); |
| 40 | + |
| 41 | + // alias app[name] as client, but still support createInstance method |
| 42 | + if (options.client) { |
| 43 | + const client = this.createInstance(options.client, options.name); |
| 44 | + this.#setClientToApp(client); |
| 45 | + this.#extendDynamicMethods(client); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // multi client, use app[name].getSingletonInstance(id) |
| 50 | + if (options.clients) { |
| 51 | + Object.keys(options.clients).forEach(id => { |
| 52 | + const client = this.createInstance(options.clients[id], id); |
| 53 | + this.clients.set(id, client); |
| 54 | + }); |
| 55 | + this.#setClientToApp(this); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // no config.clients and config.client |
| 60 | + this.#setClientToApp(this); |
| 61 | + } |
| 62 | + |
| 63 | + async initAsync() { |
| 64 | + const options = this.options; |
| 65 | + assert(!(options.client && options.clients), |
| 66 | + `[@eggjs/core/singleton] ${this.name} can not set options.client and options.clients both`); |
| 67 | + |
| 68 | + // alias app[name] as client, but still support createInstance method |
| 69 | + if (options.client) { |
| 70 | + const client = await this.createInstanceAsync(options.client, options.name); |
| 71 | + this.#setClientToApp(client); |
| 72 | + this.#extendDynamicMethods(client); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // multi client, use app[name].getInstance(id) |
| 77 | + if (options.clients) { |
| 78 | + await Promise.all(Object.keys(options.clients).map((id: string) => { |
| 79 | + return this.createInstanceAsync(options.clients[id], id) |
| 80 | + .then(client => this.clients.set(id, client)); |
| 81 | + })); |
| 82 | + this.#setClientToApp(this); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // no config.clients and config.client |
| 87 | + this.#setClientToApp(this); |
| 88 | + } |
| 89 | + |
| 90 | + #setClientToApp(client: unknown) { |
| 91 | + Reflect.set(this.app, this.name, client); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @deprecated please use `getSingletonInstance(id)` instead |
| 96 | + */ |
| 97 | + get(id: string) { |
| 98 | + return this.clients.get(id)!; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get singleton instance by id |
| 103 | + */ |
| 104 | + getSingletonInstance(id: string) { |
| 105 | + return this.clients.get(id)!; |
| 106 | + } |
| 107 | + |
| 108 | + createInstance(config: Record<string, any>, clientName: string) { |
| 109 | + // async creator only support createInstanceAsync |
| 110 | + assert(!isAsyncFunction(this.create), |
| 111 | + `[@eggjs/core/singleton] ${this.name} only support synchronous creation, please use createInstanceAsync`); |
| 112 | + // options.default will be merge in to options.clients[id] |
| 113 | + config = { |
| 114 | + ...this.options.default, |
| 115 | + ...config, |
| 116 | + }; |
| 117 | + return (this.create as SingletonCreateMethod)(config, this.app, clientName) as T; |
| 118 | + } |
| 119 | + |
| 120 | + async createInstanceAsync(config: Record<string, any>, clientName: string) { |
| 121 | + // options.default will be merge in to options.clients[id] |
| 122 | + config = { |
| 123 | + ...this.options.default, |
| 124 | + ...config, |
| 125 | + }; |
| 126 | + return await this.create(config, this.app, clientName) as T; |
| 127 | + } |
| 128 | + |
| 129 | + #extendDynamicMethods(client: any) { |
| 130 | + assert(!client.createInstance, '[@eggjs/core/singleton] singleton instance should not have createInstance method'); |
| 131 | + assert(!client.createInstanceAsync, '[@eggjs/core/singleton] singleton instance should not have createInstanceAsync method'); |
| 132 | + |
| 133 | + try { |
| 134 | + let extendable = client; |
| 135 | + // Object.preventExtensions() or Object.freeze() |
| 136 | + if (!Object.isExtensible(client) || Object.isFrozen(client)) { |
| 137 | + // eslint-disable-next-line no-proto |
| 138 | + extendable = client.__proto__ || client; |
| 139 | + } |
| 140 | + extendable.createInstance = this.createInstance.bind(this); |
| 141 | + extendable.createInstanceAsync = this.createInstanceAsync.bind(this); |
| 142 | + } catch (err) { |
| 143 | + this.app.coreLogger.warn( |
| 144 | + '[@eggjs/core/singleton] %s dynamic create is disabled because of client is un-extendable', |
| 145 | + this.name); |
| 146 | + this.app.coreLogger.warn(err); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments