Skip to content

Commit

Permalink
feat: Event group (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleysin authored Feb 16, 2024
1 parent 5435a52 commit c682b26
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
67 changes: 63 additions & 4 deletions example/src/inputs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { SizedBoxUiElement, Radio, Padding, Container, Row, GestureDetector, EdgeInsetsUtils, LocalExecutedAction, UpdateEvent, NavigationEvent, Column, RadioGroupContext, CustomEvent } = require("duit_js");
const { SizedBoxUiElement, Radio, Padding, Container, Row, GestureDetector, EdgeInsetsUtils, LocalExecutedAction, UpdateEvent, NavigationEvent, Column, RadioGroupContext, CustomEvent, CreateSequencedEventGroup, CreateUpdateEvent, CreateCommonEventGroup, CreateCustomEvent } = require("duit_js");
const { PaddingUiElement } = require("duit_js");
const { CheckBoxUiElement } = require("duit_js");
const { RowUiElement } = require("duit_js");
Expand Down Expand Up @@ -150,10 +150,69 @@ function inputExample() {
)
),
new SizedBoxUiElement({height: 24}),
new TextUiElement({data: ""}, "text1", null, true),
new TextUiElement({data: ""}, "text2", null, true),
new TextUiElement({data: ""}, "text3", null, true),
new TextUiElement({data: ""}, "text1", true),
new TextUiElement({data: ""}, "text2", true),
new TextUiElement({data: ""}, "text3", true),
new ElevatedButtonUiElement({}, "custom_action_button", new LocalExecutedAction(new CustomEvent("event1", {}))).addChild(new TextUiElement({data: "Custom event 1 runner"})),
new ElevatedButtonUiElement({}, "Sequence", new LocalExecutedAction(CreateSequencedEventGroup([
{
event: CreateUpdateEvent({
text1: {
data: "text1"
},
text2: {
data: "text2"
},
text3: {
data: "text3"
},
}),
delay: 1000,
},
{
event: CreateUpdateEvent({
text1: {
data: "text1 changed"
},
text2: {
data: "text2 changed"
},
text3: {
data: "text3 changed"
},
}),
delay: 1000,
},
{
event: CreateUpdateEvent({
text1: {
data: "text1 changed twice"
},
text2: {
data: "text2 changed twice"
},
text3: {
data: "text3 changed twice"
},
}),
delay: 0,
},
]))).addChild(new TextUiElement({data: "Run sequence"})),
new ElevatedButtonUiElement({}, "CommonGroup", new LocalExecutedAction(CreateCommonEventGroup([
CreateUpdateEvent({
text1: {
data: "common text1"
},
text2: {
data: "common text2"
},
text3: {
data: "common text3"
},
}),
CreateCustomEvent("event1"),
CreateCustomEvent("event2"),
]))).addChild(new TextUiElement({data: "Run group"})),
]
)

Expand Down
36 changes: 36 additions & 0 deletions src/lib/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ enum ServerEventType {
navigation = "navigation",
openUrl = "openUrl",
custom = "custom",
sequenced = "sequenced",
grouped = "grouped",
}

export abstract class ServerEvent {
Expand Down Expand Up @@ -52,6 +54,32 @@ export class OpenUrlEvent extends ServerEvent {
}
}

interface SequencedEvent {
event: ServerEvent;
//delay in ms
delay: number;
}

export class CommonEventGroup extends ServerEvent {
type = ServerEventType.grouped as const;
events: ServerEvent[];

constructor(events: ServerEvent[]) {
super();
this.events = events;
}
}

export class SequencedEventGroup extends ServerEvent {
type = ServerEventType.sequenced as const;
events: SequencedEvent[];

constructor(events: SequencedEvent[]) {
super();
this.events = events;
}
}

export class CustomEvent extends ServerEvent {
type = ServerEventType.custom as const;
key: string;
Expand Down Expand Up @@ -82,4 +110,12 @@ export const CreateOpenUrlEvent = (url: string) => {

export const CreateCustomEvent = (key: string, extra?: Record<string, any>) => {
return new CustomEvent(key, extra);
}

export const CreateSequencedEventGroup = (events: SequencedEvent[]) => {
return new SequencedEventGroup(events);
}

export const CreateCommonEventGroup = (events: ServerEvent[]) => {
return new CommonEventGroup(events);
}

0 comments on commit c682b26

Please sign in to comment.