Skip to content

Commit 2e528ee

Browse files
committed
chore(simple-state): update syntax
1 parent 05043cb commit 2e528ee

File tree

1 file changed

+31
-23
lines changed
  • templates/hyperweb/src/simple-state

1 file changed

+31
-23
lines changed
+31-23
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
export interface State {
2-
get(key: string): string;
3-
set(key: string, value: any): void;
4-
}
1+
export default class Contract {
2+
private state: { value: number } = { value: 0 };
53

6-
export function reset(state: State) {
7-
const newValue = 0;
8-
state.set('value', newValue);
9-
return newValue
10-
}
11-
export function inc(state: State, { x }: { x: number }) {
12-
const oldValue = Number(state.get('value')) || 0;
13-
const newValue = oldValue + x;
14-
state.set('value', newValue);
15-
return newValue
16-
}
17-
export function dec(state: State, { x }: { x: number }) {
18-
const oldValue = Number(state.get('value')) || 0;
19-
const newValue = oldValue - x;
20-
state.set('value', newValue);
21-
return newValue
22-
}
4+
constructor() { }
5+
6+
reset(): number {
7+
this.state.value = 0;
8+
return this.state.value;
9+
}
10+
11+
init(): number {
12+
this.state.value = 0;
13+
return this.state.value;
14+
}
15+
16+
inc(): number {
17+
const oldValue = this.state.value || 0;
18+
const newValue = oldValue + 1;
19+
this.state.value = newValue;
20+
return this.state.value;
21+
}
22+
23+
dec(): number {
24+
const oldValue = this.state.value ?? 0;
25+
const newValue = oldValue - 1;
26+
this.state.value = newValue;
27+
return newValue;
28+
}
29+
30+
read(): number {
31+
return this.state.value;
32+
}
2333

24-
export function read(state: State) {
25-
return state.get('value');
2634
}

0 commit comments

Comments
 (0)