Counter
A small shared counter: state, a participant action, and a standard button.
What it demonstrates: State validation · participant actions · views.
Run this example
All learning examples are available in your developer world on scape.wtf. To edit this example, clone the public Scape tools repository and run its project with Node.js 22+:
sh
git clone https://github.com/scape-wtf/scape-tools.git
cd scape-tools
npm install
cd examples/gizmos/counter
npm run build
npm test
npm exec -- scape gizmo dev1
2
3
4
5
6
7
2
3
4
5
6
7
This connects to https://scape.wtf. Follow connection and approval. To make an independent project, copy this example directory outside the checkout, then run npm install, npm run build, npm test and npm exec -- scape gizmo dev there. Its manifest uses published SDK and CLI dependencies. The project's npm run dev script is a shortcut for scape gizmo dev.
Try changing it
Add a decrement action. Keep the state a safe integer and reject unexpected payload fields.
Source
This code is included directly from the working example when the site builds.
src/definition.ts
ts
/** Counter owns the shared count and controls; Scape supplies persistence and permissions. */
import { defineObject, exactKeys, ObjectActionError, record, requirePayload } from '@scape-wtf/sdk';
/** Start here: state, validation, two actions, and a declarative view. */
export const counter = defineObject<{ count: number }>({
type: 'scape.counter',
version: 1,
emoji: '🔢',
label: 'Counter',
hint: 'Bump to add one',
interaction: { bump: { name: 'increment', payload: {} } },
react: (state, _previous, action) =>
action.name === 'increment' ? { feedback: { durationMs: 900, number: state.count } } : null,
initial: () => ({ count: 0 }),
valid: (state): state is { count: number } =>
record(state) &&
exactKeys(state, ['count']) &&
Number.isSafeInteger(state.count) &&
Number(state.count) >= 0 &&
Number(state.count) <= 9999,
actions: {
increment: {
permission: 'participant',
run: (state, payload) => {
requirePayload(payload, []);
if (state.count === 9999)
throw new ObjectActionError(409, 'Reset the counter before adding more');
return { count: state.count + 1 };
},
},
reset: {
permission: 'editor',
run: (_state, payload) => {
requirePayload(payload, []);
return { count: 0 };
},
},
},
view: (state, viewer) => ({
title: `${state.count}`,
description: 'Bump into the counter to add one. The object editor can reset the count.',
controls: [
...(viewer.canEdit
? [
{
id: 'reset',
label: 'Reset count',
confirm: 'Clear the count?',
action: { name: 'reset', payload: {} },
},
]
: []),
],
}),
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
src/project.ts
ts
/** Project entry: exports counter to Scape's SDK loader. */
import { defineProject } from '@scape-wtf/sdk';
import { counter } from './definition.js';
export default defineProject({
name: 'Counter',
description:
'Count together with a shared counter. Bump to add one and see the animated total; editors can reset it.',
objects: [counter],
});1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
