export function warnIfAutoSubscribeEnabled()

in src/AutoSubscriptions.ts [396:425]


export function warnIfAutoSubscribeEnabled<T extends Function>(target: InstanceTarget,
        methodName: string,
        descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> {
    if (!Options.development) {
        // Disable warning for production.
        return descriptor;
    }

    const targetWithMetadata = instanceTargetToInstanceTargetWithMetadata(target);

    if (Options.development) {
        // Ensure the metadata is created for dev warnings
        getMethodMetadata(targetWithMetadata, methodName);
    }

    // Save the method being decorated. Note this might be another decorator method.
    const originalMethod = descriptor.value!!!;

    // Note: we need to be given 'this', so cannot use '=>' syntax.
    // Note: T might have other properties (e.g. T = { (): void; bar: number; }). We don't support that and need a cast.
    descriptor.value = function WarnIfAutoSubscribeEnabled(this: any, ...args: any[]) {
        assert(targetWithMetadata.__resubMetadata.__decorated, `Missing @AutoSubscribeStore class decorator: "${ methodName }"`);
        assert(!handlerWrapper || handlerWrapper.useAutoSubscriptions !== AutoOptions.Enabled || handlerWrapper.inAutoSubscribe,
            `Only Store methods with the @autoSubscribe decorator can be called right now (e.g. in _buildState): "${ methodName }"`);

        return originalMethod.apply(this, args);
    } as any as T;

    return descriptor;
}