feat(ios): add intelligents button (#9281)
Co-authored-by: 砍砍 <git@qaq.wiki>
This commit is contained in:
13
packages/frontend/core/src/modules/ai-button/index.ts
Normal file
13
packages/frontend/core/src/modules/ai-button/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export { AIButtonProvider } from './provider/ai-button';
|
||||
export { AIButtonService } from './services/ai-button';
|
||||
|
||||
import type { Framework } from '@toeverything/infra';
|
||||
|
||||
import { AIButtonProvider } from './provider/ai-button';
|
||||
import { AIButtonService } from './services/ai-button';
|
||||
|
||||
export const configureAIButtonModule = (framework: Framework) => {
|
||||
framework.service(AIButtonService, container => {
|
||||
return new AIButtonService(container.getOptional(AIButtonProvider));
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { createIdentifier } from '@toeverything/infra';
|
||||
|
||||
export interface AIButtonProvider {
|
||||
presentAIButton: () => Promise<void>;
|
||||
dismissAIButton: () => Promise<void>;
|
||||
}
|
||||
|
||||
export const AIButtonProvider =
|
||||
createIdentifier<AIButtonProvider>('AIButtonProvider');
|
||||
@@ -0,0 +1,48 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import {
|
||||
effect,
|
||||
exhaustMapWithTrailing,
|
||||
fromPromise,
|
||||
Service,
|
||||
} from '@toeverything/infra';
|
||||
import {
|
||||
catchError,
|
||||
distinctUntilChanged,
|
||||
EMPTY,
|
||||
mergeMap,
|
||||
throttleTime,
|
||||
} from 'rxjs';
|
||||
|
||||
import type { AIButtonProvider } from '../provider/ai-button';
|
||||
|
||||
const logger = new DebugLogger('AIButtonService');
|
||||
|
||||
export class AIButtonService extends Service {
|
||||
constructor(private readonly aiButtonProvider?: AIButtonProvider) {
|
||||
super();
|
||||
}
|
||||
|
||||
presentAIButton = effect(
|
||||
distinctUntilChanged(),
|
||||
throttleTime<boolean>(1000), // throttle time to avoid frequent calls
|
||||
exhaustMapWithTrailing((present: boolean) => {
|
||||
return fromPromise(async () => {
|
||||
if (!this.aiButtonProvider) {
|
||||
return;
|
||||
}
|
||||
if (present) {
|
||||
await this.aiButtonProvider.presentAIButton();
|
||||
} else {
|
||||
await this.aiButtonProvider.dismissAIButton();
|
||||
}
|
||||
return;
|
||||
}).pipe(
|
||||
mergeMap(() => EMPTY),
|
||||
catchError(err => {
|
||||
logger.error('presentAIButton error', err);
|
||||
return EMPTY;
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user