* @zh 应用效果 * @en Apply effect * * @param definition - @zh 效果定义 @en Effect definition * @param sourceId - @zh 来源 ID @en Source ID * @param initialData - @zh 初始数据 @en Initial data * @returns @zh 效果实例或 null @en Effect instance or null
(
definition: IEffectDefinition,
sourceId?: string,
initialData?: Record<string, unknown>
)
| 127 | * @returns @zh 效果实例或 null @en Effect instance or null |
| 128 | */ |
| 129 | apply( |
| 130 | definition: IEffectDefinition, |
| 131 | sourceId?: string, |
| 132 | initialData?: Record<string, unknown> |
| 133 | ): IEffectInstance | null { |
| 134 | // Handle exclusive tags - remove conflicting effects |
| 135 | if (definition.exclusiveTags) { |
| 136 | for (const tag of definition.exclusiveTags) { |
| 137 | this.removeByTag(tag); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Check for existing effect of same type |
| 142 | const existingIds = this._effectsByType.get(definition.typeId); |
| 143 | if (existingIds && existingIds.size > 0) { |
| 144 | const existingId = existingIds.values().next().value as string; |
| 145 | const existing = this._effects.get(existingId); |
| 146 | |
| 147 | if (existing) { |
| 148 | return this._handleStacking(existing, definition); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Create new effect instance |
| 153 | const instance = this._createInstance(definition, sourceId, initialData); |
| 154 | |
| 155 | // Add to collections |
| 156 | this._effects.set(instance.instanceId, instance); |
| 157 | |
| 158 | if (!this._effectsByType.has(definition.typeId)) { |
| 159 | this._effectsByType.set(definition.typeId, new Set()); |
| 160 | } |
| 161 | this._effectsByType.get(definition.typeId)!.add(instance.instanceId); |
| 162 | |
| 163 | for (const tag of definition.tags) { |
| 164 | if (!this._effectsByTag.has(tag)) { |
| 165 | this._effectsByTag.set(tag, new Set()); |
| 166 | } |
| 167 | this._effectsByTag.get(tag)!.add(instance.instanceId); |
| 168 | } |
| 169 | |
| 170 | // Call handler |
| 171 | const handler = this._handlers.get(definition.typeId); |
| 172 | handler?.onApply?.(instance, this._target); |
| 173 | |
| 174 | // Emit event |
| 175 | this._emitEvent('applied', instance); |
| 176 | |
| 177 | return instance; |
| 178 | } |
| 179 | |
| 180 | private _createInstance( |
| 181 | definition: IEffectDefinition, |
no test coverage detected