stevekrouse
05/18/2023, 6:07 PMohhhhfudge
05/18/2023, 11:24 PMjsx
const notifyIfResourceChanged = () => {
const data = await checkSomeResource();
if(data !== @me.lastData) console.email('Something changed!');
@me.lastData = data;
}
Sadly, ^this is no longer possible due to the recent security updates :
https://blog.val.town/blog/restricted-library-mode
Instead, I achieve the same thing API mode:
jsx
// public state val
const PUBLIC_STATE = {};
// public state updater, called via API mode
const setPublicState = (key: string, value: any) => {
const keys = key.split("/");
let current = @joey.PUBLIC_STATE;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (!current[key]) {
current[key] = {};
}
current = current[key];
}
current[keys[keys.length - 1]] = value;
};
^ This allows users to update update PUBLIC_STATE deeply:
api(@joey.setPublicState, 'a/b/c', 5)
, which would result in { a: { b: { c: 5 } } }
Here’s an example that will email you whenever your repo get’s a new star on Github, you can just call this function directly (and schedule if desired), without having to fork it to get state setters working:
https://www.val.town/v/joey.emailOnGithubStar
jsx
// edited for brevity
const emailOnGithubStar = (userName:string, projectName:string) => {
const stars = await @joey.getGithubStarsForRepo(userName, projectName);
if (stars !== @joey.PUBLIC_STATE.STAR_COUNTS[project]) console.email('New star!');
await api(@joey.setPublicState, `STAR_COUNTS/${project}`, stars);
}
wilt
05/19/2023, 4:25 PMstevekrouse
05/19/2023, 5:52 PMNeedle
05/19/2023, 6:01 PMnitehawk
05/19/2023, 7:14 PMzeturk
05/20/2023, 5:10 AMhttps://cdn.discordapp.com/attachments/1020432421243592717/1109347400314802216/Screenshot_2023-05-20_at_1.09.26_AM.png▾
Jesse
05/21/2023, 3:32 AMfetch
to make HTTP requests
> - Buffer
- to encode/decode strings
> - dynamic import
from npm and https
> - [Contact us](https://docs.val.town/01c8eb9c534b4899802f3a9e31d540ab) if you’d like something else added 😊
However, there are other non-ECMAScript APIs which are indeed present, but are unrecognized according to the compiler diagnostic emitter — for example TextEncoder
(see screenshot):
ts
const untitled_nmb5TU39 = (text: string): Uint8Array => {
return new TextEncoder().encode(text); /* Error
~~~~~~~~~~~
Cannot find name 'TextEncoder'. */
};
Using it in another val:
ts
const untitled_zu6PjEPZ = [...@me.untitled_nmb5TU39("hello world")];
outputs:
[
104,
101,
108,
108,
111,
32,
119,
111,
114,
108,
100
]
https://cdn.discordapp.com/attachments/1020432421243592717/1109685172581957632/image.png▾
Jesse
05/21/2023, 3:39 AMexport
keyword and not using it, which is confusing. It doesn't appear to be necessary, and the auto-wrapped IIFEs (https://docs.val.town/vals#37b420a14a6141b093c193580dee145a) don't include it. Perhaps this can become more consistent in the future.Needle
05/21/2023, 5:13 AMJesse
05/21/2023, 12:07 PMJesse
05/21/2023, 1:38 PMsetTimeout
unavailable? If we need to implement a short-delayed retry behavior, it would be useful. This alternative hack works, but surely you don't want that kind of wasted CPU usage...
ts
// Not allowed:
function delay(ms: number): Promise<void> {
return new Promise((res) => setTimeout(res, ms));
}
// But this works 😈:
async function delay(ms: number): Promise<void> {
const futureTime = Date.now() + ms;
while (Date.now() < futureTime) {/* Warm the CPU 🫠 */}
}
darrinm
05/23/2023, 12:55 AMgetValues
that returns an object @me.values
and fetch getValues
from a web page. I'm seeing response times in the range from 2.2-6.1s. Is this what I should expect?Needle
05/23/2023, 12:55 AMstevekrouse
05/23/2023, 1:51 PMconsole.email
https://twitter.com/stevekrouse/status/1661000255523913729?s=20
#2 - folder & full code search https://twitter.com/stevekrouse/status/1661006102664998915?s=20Needle
05/23/2023, 3:53 PMstevekrouse
05/23/2023, 7:32 PMstevekrouse
05/23/2023, 9:23 PMNeedle
05/23/2023, 9:54 PMNeedle
05/24/2023, 2:59 PMrobert
05/24/2023, 3:50 PMNeedle
05/24/2023, 7:46 PMstevekrouse
05/24/2023, 7:49 PMhttps://cdn.discordapp.com/attachments/1020432421243592717/1111018162968866896/Screenshot_2023-05-24_at_15.44.472x.png▾
El
05/24/2023, 9:09 PMrobert
05/25/2023, 5:52 PMnitehawk
05/25/2023, 11:30 PMJackTheRapper
05/26/2023, 2:21 PMlet body = { email: email };
const response = await fetch(
"https://api.val.town/v1/run/politelyinvinciblepointer.handleForm",
{
method: "POST",
body: JSON.stringify(body),
}
);
console.log(response);
I get HTTP 500.
The email variable is a simple string.
Thanksstevekrouse
05/26/2023, 2:37 PMNeedle
05/28/2023, 2:40 PM