Repository: appsmithorg/appsmith
File: app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/ApplicationPageServiceCEImpl.java (Java)

Code before:
                                return pageDTO;
                            })
                            .flatMap(pageDTO ->
                                    // save the last edit information as page is deleted from application
                                    applicationService
                                            .saveLastEditInformation(pageDTO.getApplicationId())
                                            .thenReturn(pageDTO));
                });
    }

    /**
     * This function walks through all the pages in the application. In each page, it walks through all the layouts.
     * In a layout, dsl and publishedDsl JSONObjects exist. Publish function is responsible for copying the dsl into
     * the publishedDsl.
     *
     * @param applicationId The id of the application that will be published.
     * @return Publishes a Boolean true, when the application has been published.
     */
    @Override
    public Mono<Application> publishWithoutPermissionChecks(String applicationId, boolean isPublishedManually) {
        return publishAndGetMetadata(applicationId, isPublishedManually)
                .flatMap(tuple2 -> {
                    ApplicationPublishingMetaDTO metaDTO = tuple2.getT2();
                    return sendApplicationPublishedEvent(metaDTO);
                })
                .doOnNext(application -> postApplicationPublishHookCoordinatorService
                        .executePostPublishHooks(applicationId)
                        .subscribe())
                .elapsed()
                .map(objects -> {
                    log.debug(
                            "Published application {} in {} ms", objects.getT2().getId(), objects.getT1());
                    return objects.getT2();
                });
    }

    protected Mono<Tuple2<Mono<Application>, ApplicationPublishingMetaDTO>> publishAndGetMetadata(
            String applicationId, boolean isPublishedManually) {
        /*
         * Please note that it is a cached Mono, hence please be careful with using this Mono to update / read data
         * when latest updated application object is desired.
         */
        Mono<Application> applicationMono = applicationService
                .findById(applicationId, applicationPermission.getEditPermission())
                .switchIfEmpty(Mono.error(
                        new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.APPLICATION, applicationId)))
                .cache();

        Mono<Theme> publishThemeMono =
                applicationMono.flatMap(application -> themeService.publishTheme(application.getId()));


Review comment (refactor):
Do you think we can add the subscription to the method itself? No matter who uses a postArtefactPublish utility, the event will always be asynchronous, so we should be able to abstract that into the method

Code after:
                                return pageDTO;
                            })
                            .flatMap(pageDTO ->
                                    // save the last edit information as page is deleted from application
                                    applicationService
                                            .saveLastEditInformation(pageDTO.getApplicationId())
                                            .thenReturn(pageDTO));
                });
    }

    /**
     * This function walks through all the pages in the application. In each page, it walks through all the layouts.
     * In a layout, dsl and publishedDsl JSONObjects exist. Publish function is responsible for copying the dsl into
     * the publishedDsl.
     *
     * @param applicationId The id of the application that will be published.
     * @return Publishes a Boolean true, when the application has been published.
     */
    @Override
    public Mono<Application> publishWithoutPermissionChecks(String applicationId, boolean isPublishedManually) {
        return publishAndGetMetadata(applicationId, isPublishedManually)
                .flatMap(tuple2 -> {
                    ApplicationPublishingMetaDTO metaDTO = tuple2.getT2();
                    return sendApplicationPublishedEvent(metaDTO);
                })
                .doOnNext(application ->
                        postApplicationPublishHookCoordinatorService.executePostPublishHooks(applicationId))
                .elapsed()
                .map(objects -> {
                    log.debug(
                            "Published application {} in {} ms", objects.getT2().getId(), objects.getT1());
                    return objects.getT2();
                });
    }

    protected Mono<Tuple2<Mono<Application>, ApplicationPublishingMetaDTO>> publishAndGetMetadata(
            String applicationId, boolean isPublishedManually) {
        /*
         * Please note that it is a cached Mono, hence please be careful with using this Mono to update / read data
         * when latest updated application object is desired.
         */
        Mono<Application> applicationMono = applicationService
                .findById(applicationId, applicationPermission.getEditPermission())
                .switchIfEmpty(Mono.error(
                        new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.APPLICATION, applicationId)))
                .cache();

        Mono<Theme> publishThemeMono =
                applicationMono.flatMap(application -> themeService.publishTheme(application.getId()));

        Set<CustomJSLibContextDTO> updatedPublishedJSLibDTOs = new HashSet<>();