| 19 | providedIn: 'root' |
| 20 | }) |
| 21 | export class StoryService { |
| 22 | // Used to manage app presentation state |
| 23 | hasStoryStarted = signal(false); |
| 24 | // Used to manage input the story description requests |
| 25 | premiseInput = signal(''); |
| 26 | // Used to manage input the story generation requests |
| 27 | storyInput = signal(''); |
| 28 | |
| 29 | // Only set this on the first request. |
| 30 | // Note: this approach is for demonstration purposes. Consider alternative approaches to |
| 31 | // session management in a production application. |
| 32 | sessionId = linkedSignal<string, string>({ |
| 33 | source: () => this.premiseResource.value().storyPremise, |
| 34 | computation: (_newPremise, previous) => !previous ? randomId() : previous.value |
| 35 | }); |
| 36 | |
| 37 | // Used to determine whether to show the update story button in the UserInputComponenet |
| 38 | hasPremiseUpdated = linkedSignal<string, boolean>({ |
| 39 | source: () => this.premiseResource.value().storyPremise, |
| 40 | computation: (newPremise, previous) => !!previous && newPremise !== previous!.source |
| 41 | }); |
| 42 | |
| 43 | // Only clear the session on initial page load |
| 44 | clearSession = linkedSignal<string, boolean>({ |
| 45 | source: () => this.premiseResource.value().storyPremise, |
| 46 | computation: (_newPremise, previous) => !previous |
| 47 | }); |
| 48 | |
| 49 | // A resource that requests update story premise information based on user input |
| 50 | premiseResource = resource({ |
| 51 | defaultValue: DEFAULT_PREMISE, |
| 52 | request: () => this.premiseInput(), |
| 53 | loader: ({request}): Promise<StoryPremise> => runFlow({ |
| 54 | url: DESCRIPTION_FLOW, |
| 55 | input: { |
| 56 | userInput: request, |
| 57 | sessionId: this.sessionId(), |
| 58 | clearSession: this.clearSession() |
| 59 | } |
| 60 | }) |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | interface StoryPremise { |
| 65 | storyPremise: string; |
nothing calls this directly
no test coverage detected