Skip to content

Commit

Permalink
+++
Browse files Browse the repository at this point in the history
  • Loading branch information
Offirmo committed Oct 5, 2024
1 parent d5984cc commit 7924ca7
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,61 @@ window.addEventListener('load', (event) => {
dumpScreen()
dumpVisualViewPort()

// TODO layout viewport https://developer.mozilla.org/en-US/docs/Glossary/Layout_viewport
// TODO get root element
// TODO layout viewport?
// https://developer.mozilla.org/en-US/docs/Glossary/Layout_viewport
// through root element?

console.group('Multi screens')
console.group('Screen:', getBoundingRect‿str(getBoundingRectꓽScreen()))
console.group('Available screen:', getBoundingRect‿str(getBoundingRectꓽScreenⵧavailable(), getBoundingRectꓽScreen()))
console.group('Browser window')
console.log('Browser window:', 'todo')
// TODO frames
console.group('Visual viewport (dynamic)')
console.log('Visual viewport (dynamic):', 'todo')
console.groupEnd()
console.groupEnd()
console.groupEnd()
console.groupEnd()
console.groupEnd()
})

function getBoundingRectꓽScreen(s = globalThis.top.screen) {
return {
w: s.width,
h: s.height,
}
}
function getBoundingRectꓽScreenⵧavailable(s = globalThis.top.screen) {
return {
w: s.availWidth,
h: s.availHeight,
}
}

function hasꓽdimensions(rect) {
return rect && (rect.hasOwnProperty('w') || rect.hasOwnProperty('h'))
}

function getBoundingRect‿str(rect, parentRect) {
let result = ''

if (hasꓽdimensions(rect)) {
result += `${rect.w}×${rect.h}`

if (hasꓽdimensions(parentRect)) {
const surfaceⵧparent = parentRect.w * parentRect.h
const surface = rect.w * rect.h
const ratio = surface / surfaceⵧparent
result += `[${Math.round(ratio * 100)}%]`
}
}

if (rect && (rect.hasOwnProperty('x') || rect.hasOwnProperty('y'))) {
result += `(${rect.x},${rect.y})`
}



return result
}
13 changes: 11 additions & 2 deletions stack--current/5-incubator/active/view--chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
"sideEffects": false,
"type": "module",
"source": "src/index.js",
"module": "src/index.js",
"main": "src/index.js",
"exports": {
".": {
"import": "./dist/src.es2023.esm/index.js"
},
"./primitives-console-basic": {
"import": "./dist/src.es2023.esm/__fixtures/primitives--console.js"
}
},
"module": "dist/src.es2023.esm/index.js",
"main": "dist/src.es2023.cjs/index.js",
"typings": "dist/src.es2023.esm/index.d.ts",

"peerDependencies": {
"tslib": "^2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as RichText from '@offirmo-private/rich-text-format'

import {
type StepIterator,
type StepIteratorTNext,
type StepIteratorYieldResult,
} from '../loop/types.js'
import { getꓽInputStepⵧconfirmation } from '../steps/bases.js'

/////////////////////////////////////////////////

type ContentType = string | RichText.Document

/////////////////////////////////////////////////

class ChatGenerator implements StepIterator<ContentType> {
next(p: StepIteratorTNext<ContentType>) {
const step = getꓽInputStepⵧconfirmation<string>({})
return {
value: step,
done: false,
} satisfies StepIteratorYieldResult<ContentType>
}
}

/////////////////////////////////////////////////

export default ChatGenerator
7 changes: 5 additions & 2 deletions stack--current/5-incubator/active/view--chat/src/loop/demo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import generator_func from '../__fixtures/tour.js'
import generator_func_tour from '../__fixtures/tour.js'
import InfiniteChatGenerator from '../__fixtures/infinite.js'

import { ChatPrimitivesConsole } from '../__fixtures/primitives--console.js'

import { create } from './index.js'

const chat = create({
DEBUG: false,
gen_next_step: generator_func() as any,
//gen_next_step: generator_func_tour(),
gen_next_step: new InfiniteChatGenerator(),
primitives: new ChatPrimitivesConsole(),
})

Expand Down
13 changes: 9 additions & 4 deletions stack--current/5-incubator/active/view--chat/src/loop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import assert from 'tiny-invariant'

import { type ChatPrimitives } from '../primitives/types.js'
import { type Step, StepType, type TaskProgressStep } from '../steps/index.js'
import { StepsGenerator } from './types.js'
import { type StepIterator } from './types.js'
import { create_dummy_progress_promise } from '../utils/index.js'

/////////////////////////////////////////////////

interface Options<ContentType> {
gen_next_step: StepsGenerator<ContentType>
gen_next_step: StepIterator<ContentType>
primitives: ChatPrimitives<ContentType>

// TODO review! merge?
Expand All @@ -35,6 +35,7 @@ function create<ContentType>({
DEBUG_to_prettified_str = (x: any) => x, // work with browser
}: Options<ContentType>) {
if (DEBUG) console.log(`↘ ${LIB}.create()`)
let infinite_loop_limit = 10 // temp while proto

async function start() {
if (DEBUG) console.log(`↘ ${LIB}.start()`)
Expand Down Expand Up @@ -92,6 +93,10 @@ function create<ContentType>({

async function execute_step(step: Step<ContentType>) {
if (DEBUG) console.log('↘ ${LIB}.execute_step(', DEBUG_to_prettified_str(step), ')')
infinite_loop_limit--
if (infinite_loop_limit < 0) {
throw new Error('Too many steps, exiting to avoid infinite loop!')
}


//const step = normalize_step(raw_step)
Expand All @@ -100,6 +105,7 @@ function create<ContentType>({

case StepType.simple_message:
await primitives.display_message({ msg: step.msg })
step.callback?.()
break

case StepType.perceived_labor: {
Expand Down Expand Up @@ -138,8 +144,7 @@ function create<ContentType>({
}),
})

if (step.callback)
step.callback(success, result || error)
step.callback?.(success, result || error)
break
}

Expand Down
29 changes: 16 additions & 13 deletions stack--current/5-incubator/active/view--chat/src/loop/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { type Step } from '../steps/index.js'

/////////////////////////////////////////////////

// what is yielded by the generator
type StepsGeneratorYield<ContentType> = Step<ContentType>
type TYield<ContentType> = Step<ContentType> | Promise<Step<ContentType>>

// what is eventually returned by the generator
type StepsGeneratorReturn<ContentType> = Step<ContentType> | Promise<Step<ContentType>>
type TReturn<ContentType> = any

// parameters of the generator.next() method
type StepsGeneratorNext<ContentType> = unknown
type TNext<ContentType> = {
last_step: Step<ContentType> | undefined,
last_answer: any | undefined,
}

// all together
type StepsGenerator<ContentType> = Generator<
StepsGeneratorYield<ContentType>,
StepsGeneratorReturn<ContentType>,
StepsGeneratorNext<ContentType>
>
type StepIterator<ContentType> = Iterator<TYield<ContentType>, TReturn<ContentType>, TNext<ContentType>>

/////////////////////////////////////////////////
// helper types for implementation
type StepIteratorTNext<ContentType> = TNext<ContentType>
type StepIteratorYieldResult<ContentType> = IteratorYieldResult<TYield<ContentType>>
type StepIteratorReturnResult<ContentType> = IteratorReturnResult<TReturn<ContentType>>

export {
type StepsGenerator,
type StepIteratorTNext,
type StepIteratorYieldResult,
type StepIteratorReturnResult,

type StepIterator,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ export type StepType = Enum<typeof StepType> // eslint-disable-line no-redeclare
// TODO more async?? callbacks? everything?

interface BaseStep {
// TODO needed?

// always a callback after the step is done, even for a trivial one
// for ex. a notification system may want to mark a notif as "read" after it's been displayed
callback?: (...p: any[]) => void
}

interface SimpleMessageStep<ContentType> extends BaseStep {
type: typeof StepType.simple_message

msg: ContentType | string

callback?: () => void
}

// TODO is it redundant with progress?
Expand All @@ -38,7 +43,7 @@ interface PerceivedLaborStep<ContentType> extends BaseStep {
duration_ms?: number
msg_after?: ContentType | string

// callback?
callback?: () => void
}

interface TaskProgressStep<ContentType, T = any> extends BaseStep {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@offirmo-private/state-utils": "*",
"@offirmo-private/timestamps": "*",
"@offirmo-private/ts-types": "*",
"@offirmo-private/view--chat": "*",
"@tbrpg/state": "*",
"@tbrpg/ui--rich-text": "^0",
"tiny-invariant": "^1",
Expand Down
Loading

0 comments on commit 7924ca7

Please sign in to comment.