Skip to content

Commit b7b1a30

Browse files
committed
ViewState
fix state updates js cleanup
1 parent a57c90e commit b7b1a30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+536
-425
lines changed

client/dist/action.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { Meta, ViewId, RequestId, EncodedAction } from "./message";
1+
import { Meta, ViewId, RequestId, EncodedAction, ViewState } from "./message";
22
export type ActionMessage = {
33
viewId: ViewId;
44
action: EncodedAction;
55
requestId: RequestId;
6+
state?: ViewState;
67
meta: Meta[];
78
form: URLSearchParams | undefined;
89
};
9-
export declare function actionMessage(id: ViewId, action: EncodedAction, reqId: RequestId, form?: FormData): ActionMessage;
10+
export declare function actionMessage(id: ViewId, action: EncodedAction, state: ViewState | undefined, reqId: RequestId, form?: FormData): ActionMessage;
1011
export declare function toSearch(form?: FormData): URLSearchParams | undefined;
1112
export declare function renderActionMessage(msg: ActionMessage): string;
1213
export declare function renderForm(form: URLSearchParams | undefined): string;

client/dist/hyperbole.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/dist/hyperbole.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/dist/message.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type Meta = {
55
export type ViewId = string;
66
export type RequestId = number;
77
export type EncodedAction = string;
8+
export type ViewState = string;
89
type RemoteEvent = {
910
name: string;
1011
detail: any;

client/src/action.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { takeWhileMap } from "./lib"
3-
import { Meta, ViewId, RequestId, EncodedAction } from "./message"
3+
import { Meta, ViewId, RequestId, EncodedAction, ViewState } from "./message"
44
import * as message from "./message"
55

66

@@ -9,20 +9,21 @@ export type ActionMessage = {
99
viewId: ViewId
1010
action: EncodedAction
1111
requestId: RequestId
12+
state?: ViewState
1213
meta: Meta[]
1314
form: URLSearchParams | undefined
1415
}
1516

1617

1718

1819

19-
export function actionMessage(id: ViewId, action: EncodedAction, reqId: RequestId, form?: FormData): ActionMessage {
20+
export function actionMessage(id: ViewId, action: EncodedAction, state: ViewState | undefined, reqId: RequestId, form?: FormData): ActionMessage {
2021
let meta: Meta[] = [
2122
{ key: "Cookie", value: decodeURI(document.cookie) },
2223
{ key: "Query", value: window.location.search }
2324
]
2425

25-
return { viewId: id, action, requestId: reqId, meta, form: toSearch(form) }
26+
return { viewId: id, action, state, requestId: reqId, meta, form: toSearch(form) }
2627
}
2728

2829
export function toSearch(form?: FormData): URLSearchParams | undefined {
@@ -42,10 +43,15 @@ export function renderActionMessage(msg: ActionMessage): string {
4243
"|ACTION|",
4344
"ViewId: " + msg.viewId,
4445
"Action: " + msg.action,
45-
"RequestId: " + msg.requestId
4646
]
4747

4848

49+
if (msg.state) {
50+
header.push("State: " + msg.state)
51+
}
52+
53+
header.push("RequestId: " + msg.requestId)
54+
4955
return [
5056
header.join('\n'),
5157
message.renderMetas(msg.meta),

client/src/index.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { patch, create } from "omdomdom/lib/omdomdom.es.js"
22
import { SocketConnection, Update, Redirect } from './sockets'
33
import { listenChange, listenClick, listenDblClick, listenFormSubmit, listenLoad, listenTopLevel, listenInput, listenKeydown, listenKeyup, listenMouseEnter, listenMouseLeave } from './events'
44
import { actionMessage, ActionMessage, Request, newRequest } from './action'
5-
import { ViewId, Metadata, parseMetadata } from './message'
5+
import { ViewId, Metadata, parseMetadata, ViewState } from './message'
66
import { setQuery } from "./browser"
77
import { parseResponse, Response, LiveUpdate } from './response'
88

@@ -46,8 +46,10 @@ async function runAction(target: HyperView, action: string, form?: FormData) {
4646
target.classList.add("hyp-loading")
4747
}, 100)
4848

49+
let state = target.dataset.state
50+
4951
let req = newRequest()
50-
let msg = actionMessage(target.id, action, req.requestId, form)
52+
let msg = actionMessage(target.id, action, state, req.requestId, form)
5153

5254
// Set the requestId
5355
target.activeRequest = req
@@ -110,6 +112,7 @@ function handleUpdate(res: Update): HyperView {
110112
// Patch the node
111113
const old: VNode = create(target)
112114
let next: VNode = create(update.content)
115+
let state = (next.attributes as any)["data-state"]
113116
next.attributes = old.attributes
114117
patch(next, old)
115118

@@ -118,21 +121,24 @@ function handleUpdate(res: Update): HyperView {
118121
let newTarget = document.getElementById(target.id)
119122
dispatchContent(newTarget)
120123

121-
if (newTarget) {
122-
// execute the metadata, anything that doesn't interrupt the dom update
123-
runMetadata(res.meta, newTarget)
124-
125-
// now way for these to bubble)
126-
listenLoad(newTarget)
127-
listenMouseEnter(newTarget)
128-
listenMouseLeave(newTarget)
129-
fixInputs(newTarget)
130-
enrichHyperViews(newTarget)
131-
}
132-
else {
124+
if (!newTarget) {
133125
console.warn("Target Missing: ", target.id)
126+
return target
134127
}
135128

129+
// re-add state attribute
130+
newTarget.dataset.state = state
131+
132+
// execute the metadata, anything that doesn't interrupt the dom update
133+
runMetadata(res.meta, newTarget)
134+
135+
// now way for these to bubble)
136+
listenLoad(newTarget)
137+
listenMouseEnter(newTarget)
138+
listenMouseLeave(newTarget)
139+
fixInputs(newTarget)
140+
enrichHyperViews(newTarget)
141+
136142
return target
137143
}
138144
// catch (err) {

client/src/message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type Meta = { key: string, value: string }
77
export type ViewId = string
88
export type RequestId = number
99
export type EncodedAction = string
10+
export type ViewState = string
1011

1112
type RemoteEvent = { name: string, detail: any }
1213

examples/Example/App.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import Example.Page.State.Actions qualified as Actions
5858
import Example.Page.State.Effects qualified as Effects
5959
import Example.Page.State.Query qualified as Query
6060
import Example.Page.State.Sessions qualified as Sessions
61+
import Example.Page.State.View qualified as SView
6162
import Example.Page.Test qualified as Test
6263
import Example.Page.Todos.Todo qualified as Todo
6364
import Example.Page.Todos.TodoCSS qualified as TodoCSS
@@ -121,7 +122,7 @@ exampleApp config users count chats = do
121122
runApp = runReader config . runTodosSession . runUsersIO users . runDebugIO . runConcurrent . runRandom . runOAuth2 config.oauth config.manager
122123

123124
router :: forall es. (Hyperbole :> es, OAuth2 :> es, Todos :> es, Users :> es, Debug :> es, Concurrent :> es, IOE :> es, GenRandom :> es, Reader AppConfig :> es) => AppRoute -> Eff es Response
124-
router Chat = runReader chats $ runPage $ Chat.page
125+
router Chat = runReader chats $ runPage Chat.page
125126
router (Hello h) = runPage $ hello h
126127
router (Contacts (Contact uid)) = Contact.response uid
127128
router (Contacts ContactsAll) = runPage Contacts.page
@@ -141,6 +142,7 @@ exampleApp config users count chats = do
141142
case r of
142143
StateRoot -> redirect $ routeUri (State Actions)
143144
Actions -> runPage Actions.page
145+
StateView -> runPage SView.page
144146
Effects -> runReader count $ runPage Effects.page
145147
Sessions -> runPage Sessions.page
146148
Query -> runPage Query.page

examples/Example/AppRoute.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ instance Route DataRoute where
6868
data StateRoute
6969
= StateRoot
7070
| Actions
71+
| StateView
7172
| Effects
7273
| Query
7374
| Sessions
@@ -110,6 +111,7 @@ routeTitle (Hello _) = "Hello World"
110111
routeTitle (Contacts ContactsAll) = "Contacts"
111112
routeTitle (State Effects) = "Effects"
112113
routeTitle (State StateRoot) = "State"
114+
routeTitle (State StateView) = "Built-in State"
113115
routeTitle (State Actions) = "Managing State"
114116
routeTitle (State Query) = "Query"
115117
routeTitle (State Sessions) = "Sessions"

examples/Example/Page/Advanced.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ controlView = do
5858

5959
targetView :: View Controls ()
6060
targetView = do
61-
target Message $ do
61+
target Message () $ do
6262
button (SetMessage "Targeted!") ~ btn $ "Target SetMessage"

0 commit comments

Comments
 (0)