Tinylib
DiscoverDocsSign in
Sign in
Tinylib
AboutDiscoverDocsMake an appPricingTermsPrivacyDeveloper TermsContact

Start

  • Quickstart
  • How Tinylib works

Guides

  • Data
  • Rooms and rules
  • Games
  • Friends
  • Places and links
  • Reminders and notifications
  • Publishing
  • Errors and offline
  • Platforms

Client API

  • tinylib
    • user
    • connection
    • now()
    • block()
    • report()
    • TinylibError
    • ErrorCode
  • tinylib.data
  • tinylib.rooms
  • tinylib.invitations
  • tinylib.friends
  • tinylib.places
  • tinylib.reminders
  • Types
  • tinylib-sdk/kits

Rules API

  • Kinds
  • ctx

Reference

  • tinylib.json
  • Command line
  • Limits

Plain text

  • llms.txt
  • llms-full.txt

tinylib

tinylib is a global in every page of your app, ready with the user's data before your own scripts run. It exists only inside Tinylib and at the address npx tinylib-sdk dev prints. The tinylib-sdk package declares its type, and Register adds your app's data and room types to it.

NameWhat it is
tinylib.user
What it is
The person using your app.
tinylib.connection
What it is
Whether the device can reach Tinylib's servers.
tinylib.now()
What it is
Returns the current time on Tinylib's clock, in milliseconds since the Unix epoch.
tinylib.places
What it is
Read and change the current screen's address, and share a link to it.
tinylib.data
What it is
Read and write the user's data, and watch it change.
tinylib.friends
What it is
List the user's friends who use your app, and read the data they share.
tinylib.rooms
What it is
Create and join rooms from your client, and act in them.
tinylib.invitations
What it is
Show the user's invitations to rooms in your app instead of Tinylib's sheet.
tinylib.reminders
What it is
Schedule, list, and cancel the user's reminders.
tinylib.block()
What it is
Opens Tinylib's sheet for the user to confirm blocking userId.
tinylib.report()
What it is
Opens Tinylib's sheet for the user to report userId to Tinylib.

tinylib.user

interface User
The person using your app.

Properties

idstringread-only
The user's id in your app. The same person has a different id in every app.
namestringread-only
The user's name. It can change and isn't unique, so tell users apart by id.
settings{ readonly notifications: boolean; readonly reminders: boolean }read-only
The user's switches for your app in Tinylib, which aren't the phone's permission to show notifications. notifications is the Rooms switch, for what your rules send with ctx.notify, and reminders is the Reminders switch. Both are true by default.
JavaScript
document.querySelector('#remind-me').hidden = !tinylib.user.settings.reminders
guestbooleanread-only
True when the user is a guest: someone without a Tinylib account, known only on the device they use. A guest has data, reminders and every room call, with the same id in every room of your app. A guest gets no notifications or invitations, and can't invite anyone, add friends, or report: Guests lists the calls that reject with refused for one.

Methods

onChange(fn)
Calls fn after name or settings changes.
manageSettings()
Opens Tinylib's sheet where the user turns your app's switches on and off.

tinylib.user.onChange()

tinylib.user.onChange(fn: () => void): () => void
Calls fn after name or settings changes.
JavaScript
tinylib.user.onChange(() => { document.querySelector('#name').textContent = tinylib.user.name })

Parameters

fn() => void
Called with no arguments.

Returns

() => void
A function that stops the calls.

Throws

TypeError
When fn isn't a function.

tinylib.user.manageSettings()

tinylib.user.manageSettings(): Promise<{ notifications: boolean; reminders: boolean }>
Opens Tinylib's sheet where the user turns your app's switches on and off. When your app's uses lists neither "notifications" nor "reminders", no sheet opens and the call resolves with settings at once. For a guest, Tinylib offers signing in instead, and the call resolves with settings unchanged.
JavaScript
const turnOn = document.querySelector('#turn-on-reminders') turnOn.addEventListener('click', async () => { try { const settings = await tinylib.user.manageSettings() turnOn.hidden = settings.reminders } catch (error) { toast(error.message) } })

Returns

Promise<{ notifications: boolean; reminders: boolean }>
The user's settings once the sheet closes.

Rejects with

offline

tinylib.connection

interface Connection
Whether the device can reach Tinylib's servers now. Errors and offline lists what works without a connection. While a room your app has open can't reach Tinylib, Tinylib shows so over your app, so your app doesn't show its own.

Properties

onlinebooleanread-only
True while the device can reach Tinylib's servers.

Methods

onChange(fn)
Calls fn after online changes.

tinylib.connection.onChange()

tinylib.connection.onChange(fn: () => void): () => void
Calls fn after online changes.
JavaScript
tinylib.connection.onChange(() => { document.querySelector('#new-game').disabled = !tinylib.connection.online })

Parameters

fn() => void
Called with no arguments.

Returns

() => void
A function that stops the calls.

Throws

TypeError
When fn isn't a function.

tinylib.now()

tinylib.now(): number
Returns the current time on Tinylib's clock, in milliseconds since the Unix epoch. Tinylib's servers and ctx.now in the rules use the same clock. Date.now() uses the device's clock, which can be wrong.
JavaScript
await tinylib.reminders.set('break', { title: 'Back to work', at: tinylib.now() + 15 * 60_000 })

Returns

number
The time in milliseconds.

tinylib.block()

tinylib.block(userId: string): Promise<boolean>
Opens Tinylib's sheet for the user to confirm blocking userId. Let users block and report says when your app must offer it.

A block keeps the two from reaching each other again, in every app: neither can invite the other, and neither can join a room the other is in. Rooms they're already in go on unchanged. Works for a guest.

JavaScript
if (await tinylib.block(player.id)) tinylib.places.go('')

Parameters

userIdstring
The id of someone the user has met in your app: someone who has been in a room of your app with them, who invited them to one or was invited by them, or a friend, when your app's uses has "friends".

Returns

Promise<boolean>
True if the user blocked them, or false if they canceled.

Rejects with

not_found
When userId isn't someone the user has met in your app.
refused
When userId is the user's own.
offline

tinylib.report()

tinylib.report(userId: string): Promise<boolean>
Opens Tinylib's sheet for the user to report userId to Tinylib.
JavaScript
await tinylib.report(player.id)

Parameters

userIdstring
The id of someone the user has met in your app, such as a member of one of their rooms.

Returns

Promise<boolean>
True if the user sent a report, or false if they canceled.

Rejects with

not_found
When userId isn't someone the user has met in your app.
refused
When userId is the user's own, or the user is a guest.
offline

TinylibError

interface TinylibError extends Error
The error a failed call rejects with, or a synchronous call throws: an Error whose name is 'TinylibError', with a code and a message you can show the user. There's no class to test with instanceof, so check code.
Extends Error.

Besides the codes each call's entry lists, any call that returns a promise can reject with failed when Tinylib's servers or the device fail.

A mistake in your own code, such as an argument of the wrong type, is a plain TypeError instead, with no code.

JavaScript
try { await room.act('move', 4) } catch (error) { if (error.code !== 'refused') throw error toast(error.message) }

Properties

codeErrorCode
What went wrong, as one of the ErrorCode values.

ErrorCode

type ErrorCode = | 'refused' | 'failed' | 'offline' | 'limit' | 'not_member' | 'ended' | 'not_found'
The code of a TinylibError.
CodeWhat happenedWhat to do
refused
What happened
Tinylib or your rules said no, and message says why: the rules called ctx.refuse, the call needs a Tinylib account and the user has none, your app's uses lacks what the call needs, or one person has blocked the other.
What to do
Show message.
failed
What happened
Something went wrong that the user didn't cause. Your rules threw or have no handler for the input, or Tinylib's servers or the device failed.
What to do
Show message. When your rules caused it, your developer page shows the error; otherwise, trying again later may work.
offline
What happened
The call needs Tinylib's servers, and the device has no connection.
What to do
Wait for tinylib.connection.onChange, then let the user try again. A request already on its way may have arrived.
limit
What happened
The call would pass one of the limits.
What to do
Show message. A rate allows more later, and a size or a count allows more once something is removed.
not_member
What happened
The user isn't a member of the room, or no room has that id. Tinylib doesn't say which.
What to do
Take the user out of the room's screen.
ended
What happened
The room has ended, or the user deleted their data in your app.
What to do
Take the user out of the room's screen.
not_found
What happened
No short code, link, invitation, or person matches what the call was given.
What to do
Ask the user to check the code or link they used.
PreviousPlatformsNexttinylib.data

On this page

  • user
  • onChange()
  • manageSettings()
  • connection
  • onChange()
  • now()
  • block()
  • report()
  • TinylibError
  • ErrorCode