ctx
Every handler but view, status, and canInvite gets ctx as its last argument.
A handler's effects, like events and notifications, happen together after it returns, and only if it neither refuses
nor throws. Otherwise none of them happens and state is unchanged. If Tinylib can't carry out one effect, it skips
that effect alone and shows the error on your developer page and in npx tinylib-sdk dev. An input
that would leave the room's state or data past its limit is the exception: none of its effects happen.
rules.js.at.Use it instead of Date.now(), which doesn't follow npx tinylib-sdk dev when it moves the clock forward to test
timers.
migrate, it's fromVersion.create it's the creator alone. In join it includes the new member, and in leave it no longer includes the member who left.create, the member who acted in an action, the new member in join, the member who left in leave, and the member who came or went in presence. Null in a timer's handler, in beat, and in migrate.Math.random(), drawn on Tinylib's servers. Members can't predict it, so use it for dice and shuffles.ctx.data(userId) is a member's data, as MemberValues, where userId is a current member's id, or the leaving member's in leave. ctx.data.room is the room's data, as RoomData.Throws
- TypeError
- When
userIdisn't a string.
refused, with sentence as the error's message. A try/catch around it doesn't stop the refusal.Parameters
- sentencestring
- A sentence for the user. Past 300 characters, it's cut.
Throws
- TypeError
- When it's called outside
createand actions, the inputs a client waits on. The input then fails, even when atry/catchcatches the error.
event to the members in to who have the room open, through room.onEvent, after this input's views. Nobody else ever gets it, so use events for one-off moments like a sound, and put what members must see in view. An id that isn't a member's, or an event past 64 KB, is skipped.Throws
- TypeError
- When
eventcan't be saved as JSON. - When
toisn't aRecipientsvalue, or is'user'wherectx.useris null.
- When
to. Tapping it opens the room at the kind's address. It needs "notifications" in uses; without it, nothing is sent.It reaches the member only when they don't have the room open anywhere, haven't turned off your app's Rooms switch, and haven't muted the room. Guests never get one. A member has one notification from the room at a time, and a newer one replaces it. Opening or leaving the room clears it there and on the member's Android phones; another browser clears it once the room opens in it.
An id that isn't a member's is skipped, and so is a notification whose title or text is too long; nothing is cut to fit. Past 60 per hour to one member, or 300 per hour from the room, Tinylib drops the room's notifications without an error.
Parameters
- tostring
- The id of a current member.
- noticeNotice
- What the notification says.
- notice.titlestring
- The notification's first line, up to 80 characters.
- notice.textstringoptional
- The line under the title, up to 300 characters.
Throws
- TypeError
- When
toisn't a string,noticeisn't an object, or itstitleortextisn't a string.
Throws
- TypeError
- When
textisn't a string.
timers[name] with data at time at. Each name holds one timer: setting it again moves it and replaces its data. For several deadlines, like one clock per player, keep them in state and set the timer for the soonest.An at in the past goes off as soon as it can. A timer set in a timer's handler or in beat waits at least
1 second. The rules can't read a timer back, so keep any deadline your client shows in
state.
Timers count toward the size of the room's state. They don't keep a room going: one unused for its kind's idleMs
ends, and ending a room cancels its timers. A timer with no handler by its name fails when it goes off, and one
whose handler throws doesn't run again.
Parameters
- namestring
- The name of its handler in
timers. - atnumber
- Milliseconds since the Unix epoch, as
ctx.nowgives them. - dataunknownoptional
- Any JSON value, passed to the timer's handler.
Throws
- TypeError
- When
nameisn't a string,atisn't a finite number, ordatacan't be saved as JSON.
name, if it's set.Throws
- TypeError
- When
nameisn't a string.
id after this input, then runs leave for them with why as 'removed'. They can join again like anyone else.An id that isn't a member's is skipped. Removing the last member ends the room.
Throws
- TypeError
- When
idisn't a string.
leave included. Members keep the room in their past rooms, with its last status and its room data.To play again, reset state instead.
ctx.emit reaches: a member's id, a list of ids, 'everyone' for every current member, or 'user' for ctx.user.rulesKeys declares in tinylib.json. Only the rules write these keys, and any room of your app can, so a member's wins can add up across rooms.A write is skipped when:
rulesKeysdoesn't declare its key, or the key is longer than 128 bytes.- It's for someone who isn't a member, other than the leaving member in
leave. - It would take the user's data your rules write past 64 KB.
Methods
- get(key)
- Returns a copy of the value under
key, including writes made earlier in this input, orundefined. - keys(prefix?)
- Returns the declared keys that have a value and start with
prefix, sorted. - set(key, value, options?)
- Sets
keytovalue. - remove(key)
- Removes
key. - add(key, by, options?)
- Adds
byto the number underkey, starting from 0 when it holds no number. - max(key, value, options?)
- Sets
keytovalueifvalueis larger orkeyholds no number, even when other rooms write the key at the same time. - min(key, value, options?)
- Sets
keytovalueifvalueis smaller orkeyholds no number, even when other rooms write the key at the same time.
key, including writes made earlier in this input, or undefined.Throws
- TypeError
- When
keyisn't a string or isn't declared inrulesKeys. - When the id passed to
ctx.dataisn't a current member's, or the leaving member's inleave.
- When
prefix, sorted. Without prefix, returns every one.Throws
- TypeError
- When the id passed to
ctx.dataisn't a current member's, or the leaving member's inleave.
key to value. With audience: 'friends', the member's friends who have the app can read it. Without audience, the key keeps its audience, or 'me' for a new key.Throws
- TypeError
- When
keyisn't a string,valueisundefinedor can't be saved as JSON, oraudienceisn't'me'or'friends'.
by to the number under key, starting from 0 when it holds no number. When rooms add to the same key at the same time, every add counts. audience works as in set.Throws
- TypeError
- When
keyisn't a string,byisn't a finite number, oraudienceisn't'me'or'friends'.
key to value if value is larger or key holds no number, even when other rooms write the key at the same time. audience works as in set.Throws
- TypeError
- When
keyisn't a string,valueisn't a finite number, oraudienceisn't'me'or'friends'.
key to value if value is smaller or key holds no number, even when other rooms write the key at the same time. audience works as in set.Throws
- TypeError
- When
keyisn't a string,valueisn't a finite number, oraudienceisn't'me'or'friends'.
tinylib.rooms.data, so keep secrets in state, behind view.Methods
- get(key)
- Returns a copy of the value under
key, including writes made earlier in this input, orundefined. - keys(prefix?)
- Returns the keys that start with
prefix, sorted. - set(key, value)
- Sets
keytovalue. - remove(key)
- Removes
key.
key, including writes made earlier in this input, or undefined.Throws
- TypeError
- When
keyisn't a string.
prefix, sorted. Without prefix, returns every key.key to value. When the input's writes would leave the room's data past 256 KB, the whole input changes nothing, as the full-room rule says.Throws
- TypeError
- When
keyisn't a string, orvalueisundefinedor can't be saved as JSON.
Member as ctx.members lists them.Properties
- joinedAtnumber
- When the member joined, in milliseconds on Tinylib's clock.
- presentboolean
- True while the member has the room open on a device, and as they come in, before their page connects. After they close it everywhere, it stays true for 5 seconds, until
presenceruns withpresentfalse.