Skip to content

Commit fa1a43f

Browse files
committed
fix(modules): remove redundant code, add more flags
the define jsdoc flags handle code stripping the functions as is
1 parent 53a592a commit fa1a43f

10 files changed

Lines changed: 243 additions & 176 deletions

File tree

apps/web/content/docs/modules/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ entirely by dead code elimination.
6363
"name": "@gwigz/slua-tstl-plugin",
6464
"define": {
6565
"CONFIG_YAML_PARSER": true,
66-
"YIELD_DATASERVER": true,
66+
"YIELD_DATASERVER_AGENT": true,
6767
"YIELD_HTTP": true,
6868
},
6969
},

apps/web/content/docs/modules/yield.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ elimination.
4343
{
4444
"name": "@gwigz/slua-tstl-plugin",
4545
"define": {
46-
"YIELD_DATASERVER": true,
46+
"YIELD_DATASERVER_AGENT": true,
4747
"YIELD_HTTP": true,
4848
// omitted flags default to false, code is stripped
4949
},
@@ -63,19 +63,19 @@ export type API = {
6363
waitFor: typeof waitFor,
6464
/** Yield the current coroutine for `seconds` seconds. */
6565
sleep: typeof sleep,
66-
/** Request agent data and yield until the result arrives. @remarks `YIELD_DATASERVER` */
66+
/** Request agent data and yield until the result arrives. @remarks `YIELD_DATASERVER_AGENT` */
6767
requestAgentData: typeof requestAgentData,
68-
/** Request a display name and yield until the result arrives. @remarks `YIELD_DATASERVER` */
68+
/** Request a display name and yield until the result arrives. @remarks `YIELD_DATASERVER_DISPLAY_NAME` */
6969
requestDisplayName: typeof requestDisplayName,
70-
/** Request simulator data and yield until the result arrives. @remarks `YIELD_DATASERVER` */
70+
/** Request simulator data and yield until the result arrives. @remarks `YIELD_DATASERVER_SIM` */
7171
requestSimulatorData: typeof requestSimulatorData,
72-
/** Request inventory data and yield until the result arrives. @remarks `YIELD_DATASERVER` */
72+
/** Request inventory data and yield until the result arrives. @remarks `YIELD_DATASERVER_INVENTORY` */
7373
requestInventoryData: typeof requestInventoryData,
74-
/** Read a single notecard line and yield until the result arrives. @remarks `YIELD_DATASERVER` */
74+
/** Read a single notecard line and yield until the result arrives. @remarks `YIELD_DATASERVER_NOTECARD` */
7575
readNotecardLine: typeof readNotecardLine,
76-
/** Read an entire notecard by fetching lines until EOF. Returns an array of lines. @remarks `YIELD_DATASERVER` */
76+
/** Read an entire notecard by fetching lines until EOF. Returns an array of lines. @remarks `YIELD_DATASERVER_NOTECARD` */
7777
readNotecard: typeof readNotecard,
78-
/** Search notecard text for a pattern and yield until the count arrives. @remarks `YIELD_DATASERVER` */
78+
/** Search notecard text for a pattern and yield until the count arrives. @remarks `YIELD_DATASERVER_TEXT_COUNT` */
7979
findNotecardTextCount: typeof findNotecardTextCount,
8080
/** Read a key-value pair from the experience KV store. @remarks `YIELD_KV` */
8181
kvRead: typeof kvRead,

examples/sim-wide-relay/dist/coordinator.slua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ local config = {
3636
REZ_BATCH_SIZE = 20,
3737
}
3838

39+
---
40+
-- @internal Run `fn` in a new coroutine. The coroutine starts immediately.
41+
--
42+
-- Shared by `config` and `yield` modules so the create + resume
43+
-- boilerplate isn't duplicated in the Lua output.
44+
local function spawn(fn)
45+
local co = coroutine.create(fn)
46+
47+
coroutine.resume(co)
48+
49+
return co
50+
end
51+
3952
---
4053
-- @internal Shared dataserver yield pattern, registers a handler filtered
4154
-- by request ID, yields, returns the data string.
@@ -214,13 +227,13 @@ end
214227
local function loadConfig(notecard, options, callback)
215228
local config = options.config
216229
local ____type = options.type or "yml"
217-
local co = coroutine.create(function()
230+
231+
spawn(function()
218232
local lines = readNotecardLines(notecard)
219233

220234
applyFromLines(config, lines, ____type)
221235
callback()
222236
end)
223-
coroutine.resume(co)
224237
end
225238

226239
--- Watch for changes to a settings notecard and re-parse on update.
@@ -261,8 +274,7 @@ local function onConfigChanged(notecard, options, callback)
261274
end
262275

263276
lastKey = currentKey
264-
265-
local co = coroutine.create(function()
277+
spawn(function()
266278
for key in pairs(snapshot) do
267279
config[key] = snapshot[key]
268280
end
@@ -272,7 +284,6 @@ local function onConfigChanged(notecard, options, callback)
272284
applyFromLines(config, lines, ____type)
273285
callback()
274286
end)
275-
coroutine.resume(co)
276287
end)
277288
end
278289

examples/sim-wide-relay/dist/dialog.slua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ local NOTECARD_NAME = "settings.yml"
1717
--- Note, these are defaults, use the `settings.yml` notecard to override them
1818
local config = { DIALOG_CHANNEL = -1731704570, RATE_LIMIT = 5, PENDING_TIMEOUT = 30 }
1919

20+
---
21+
-- @internal Run `fn` in a new coroutine. The coroutine starts immediately.
22+
--
23+
-- Shared by `config` and `yield` modules so the create + resume
24+
-- boilerplate isn't duplicated in the Lua output.
25+
local function spawn(fn)
26+
local co = coroutine.create(fn)
27+
28+
coroutine.resume(co)
29+
30+
return co
31+
end
32+
2033
---
2134
-- @internal Shared dataserver yield pattern, registers a handler filtered
2235
-- by request ID, yields, returns the data string.
@@ -195,13 +208,13 @@ end
195208
local function loadConfig(notecard, options, callback)
196209
local config = options.config
197210
local ____type = options.type or "yml"
198-
local co = coroutine.create(function()
211+
212+
spawn(function()
199213
local lines = readNotecardLines(notecard)
200214

201215
applyFromLines(config, lines, ____type)
202216
callback()
203217
end)
204-
coroutine.resume(co)
205218
end
206219

207220
--- Watch for changes to a settings notecard and re-parse on update.
@@ -242,8 +255,7 @@ local function onConfigChanged(notecard, options, callback)
242255
end
243256

244257
lastKey = currentKey
245-
246-
local co = coroutine.create(function()
258+
spawn(function()
247259
for key in pairs(snapshot) do
248260
config[key] = snapshot[key]
249261
end
@@ -253,7 +265,6 @@ local function onConfigChanged(notecard, options, callback)
253265
applyFromLines(config, lines, ____type)
254266
callback()
255267
end)
256-
coroutine.resume(co)
257268
end)
258269
end
259270

examples/sim-wide-relay/dist/listener.slua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ local config = {
2828
DEDUP_MAX = 50,
2929
}
3030

31+
---
32+
-- @internal Run `fn` in a new coroutine. The coroutine starts immediately.
33+
--
34+
-- Shared by `config` and `yield` modules so the create + resume
35+
-- boilerplate isn't duplicated in the Lua output.
36+
local function spawn(fn)
37+
local co = coroutine.create(fn)
38+
39+
coroutine.resume(co)
40+
41+
return co
42+
end
43+
3144
---
3245
-- @internal Shared dataserver yield pattern, registers a handler filtered
3346
-- by request ID, yields, returns the data string.
@@ -206,13 +219,13 @@ end
206219
local function loadConfig(notecard, options, callback)
207220
local config = options.config
208221
local ____type = options.type or "yml"
209-
local co = coroutine.create(function()
222+
223+
spawn(function()
210224
local lines = readNotecardLines(notecard)
211225

212226
applyFromLines(config, lines, ____type)
213227
callback()
214228
end)
215-
coroutine.resume(co)
216229
end
217230

218231
--- Watch for changes to a settings notecard and re-parse on update.
@@ -253,8 +266,7 @@ local function onConfigChanged(notecard, options, callback)
253266
end
254267

255268
lastKey = currentKey
256-
257-
local co = coroutine.create(function()
269+
spawn(function()
258270
for key in pairs(snapshot) do
259271
config[key] = snapshot[key]
260272
end
@@ -264,7 +276,6 @@ local function onConfigChanged(notecard, options, callback)
264276
applyFromLines(config, lines, ____type)
265277
callback()
266278
end)
267-
coroutine.resume(co)
268279
end)
269280
end
270281

examples/sim-wide-relay/dist/sender.slua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ local config = {
2727
MAX_BLOCKS = 10,
2828
}
2929

30+
---
31+
-- @internal Run `fn` in a new coroutine. The coroutine starts immediately.
32+
--
33+
-- Shared by `config` and `yield` modules so the create + resume
34+
-- boilerplate isn't duplicated in the Lua output.
35+
local function spawn(fn)
36+
local co = coroutine.create(fn)
37+
38+
coroutine.resume(co)
39+
40+
return co
41+
end
42+
3043
---
3144
-- @internal Shared dataserver yield pattern, registers a handler filtered
3245
-- by request ID, yields, returns the data string.
@@ -205,13 +218,13 @@ end
205218
local function loadConfig(notecard, options, callback)
206219
local config = options.config
207220
local ____type = options.type or "yml"
208-
local co = coroutine.create(function()
221+
222+
spawn(function()
209223
local lines = readNotecardLines(notecard)
210224

211225
applyFromLines(config, lines, ____type)
212226
callback()
213227
end)
214-
coroutine.resume(co)
215228
end
216229

217230
--- Watch for changes to a settings notecard and re-parse on update.
@@ -252,8 +265,7 @@ local function onConfigChanged(notecard, options, callback)
252265
end
253266

254267
lastKey = currentKey
255-
256-
local co = coroutine.create(function()
268+
spawn(function()
257269
for key in pairs(snapshot) do
258270
config[key] = snapshot[key]
259271
end
@@ -263,7 +275,6 @@ local function onConfigChanged(notecard, options, callback)
263275
applyFromLines(config, lines, ____type)
264276
callback()
265277
end)
266-
coroutine.resume(co)
267278
end)
268279
end
269280

packages/create/src/templates/snippets.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { Extras } from "../prompts.js"
22

33
const YIELD_FLAGS = [
4-
"YIELD_DATASERVER",
4+
"YIELD_DATASERVER_AGENT",
5+
"YIELD_DATASERVER_DISPLAY_NAME",
6+
"YIELD_DATASERVER_SIM",
7+
"YIELD_DATASERVER_INVENTORY",
8+
"YIELD_DATASERVER_NOTECARD",
9+
"YIELD_DATASERVER_TEXT_COUNT",
510
"YIELD_KV",
611
"YIELD_DIALOG",
712
"YIELD_HTTP",
@@ -46,7 +51,12 @@ export function flagsDtsContent(extras: Extras): string {
4651
}
4752

4853
if (extras.yield) {
49-
lines.push("declare const YIELD_DATASERVER: boolean")
54+
lines.push("declare const YIELD_DATASERVER_AGENT: boolean")
55+
lines.push("declare const YIELD_DATASERVER_DISPLAY_NAME: boolean")
56+
lines.push("declare const YIELD_DATASERVER_SIM: boolean")
57+
lines.push("declare const YIELD_DATASERVER_INVENTORY: boolean")
58+
lines.push("declare const YIELD_DATASERVER_NOTECARD: boolean")
59+
lines.push("declare const YIELD_DATASERVER_TEXT_COUNT: boolean")
5060
lines.push("declare const YIELD_KV: boolean")
5161
lines.push("declare const YIELD_DIALOG: boolean")
5262
lines.push("declare const YIELD_HTTP: boolean")

packages/modules/src/testing/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ const GLOBAL_KEYS = [
268268
"coroutine",
269269
"CONFIG_YAML_PARSER",
270270
"CONFIG_LLJSON_PARSER",
271-
"YIELD_DATASERVER",
271+
"YIELD_DATASERVER_AGENT",
272+
"YIELD_DATASERVER_DISPLAY_NAME",
273+
"YIELD_DATASERVER_SIM",
274+
"YIELD_DATASERVER_INVENTORY",
275+
"YIELD_DATASERVER_NOTECARD",
276+
"YIELD_DATASERVER_TEXT_COUNT",
272277
"YIELD_KV",
273278
"YIELD_DIALOG",
274279
"YIELD_HTTP",
@@ -310,7 +315,12 @@ export function setup(): void {
310315
g.coroutine = { ...mockCoroutine }
311316
g.CONFIG_YAML_PARSER = true
312317
g.CONFIG_LLJSON_PARSER = false
313-
g.YIELD_DATASERVER = true
318+
g.YIELD_DATASERVER_AGENT = true
319+
g.YIELD_DATASERVER_DISPLAY_NAME = true
320+
g.YIELD_DATASERVER_SIM = true
321+
g.YIELD_DATASERVER_INVENTORY = true
322+
g.YIELD_DATASERVER_NOTECARD = true
323+
g.YIELD_DATASERVER_TEXT_COUNT = true
314324
g.YIELD_KV = true
315325
g.YIELD_DIALOG = true
316326
g.YIELD_HTTP = true

packages/modules/src/yield/flags.d.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
11
/**
2-
* Compile-time flag: enable dataserver yield wrappers.
2+
* Compile-time flag: enable {@link requestAgentData}.
33
*
4-
* Guards: {@link requestAgentData}, {@link requestDisplayName},
5-
* {@link requestSimulatorData}, {@link requestInventoryData},
6-
* {@link readNotecardLine}, {@link readNotecard}, {@link findNotecardTextCount}.
4+
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
5+
* by this flag is stripped from the Lua output when set to `false`.
6+
*/
7+
declare const YIELD_DATASERVER_AGENT: boolean
8+
9+
/**
10+
* Compile-time flag: enable {@link requestDisplayName}.
11+
*
12+
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
13+
* by this flag is stripped from the Lua output when set to `false`.
14+
*/
15+
declare const YIELD_DATASERVER_DISPLAY_NAME: boolean
16+
17+
/**
18+
* Compile-time flag: enable {@link requestSimulatorData}.
19+
*
20+
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
21+
* by this flag is stripped from the Lua output when set to `false`.
22+
*/
23+
declare const YIELD_DATASERVER_SIM: boolean
24+
25+
/**
26+
* Compile-time flag: enable {@link requestInventoryData}.
27+
*
28+
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
29+
* by this flag is stripped from the Lua output when set to `false`.
30+
*/
31+
declare const YIELD_DATASERVER_INVENTORY: boolean
32+
33+
/**
34+
* Compile-time flag: enable {@link readNotecardLine}, {@link readNotecard}.
35+
*
36+
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
37+
* by this flag is stripped from the Lua output when set to `false`.
38+
*/
39+
declare const YIELD_DATASERVER_NOTECARD: boolean
40+
41+
/**
42+
* Compile-time flag: enable {@link findNotecardTextCount}.
743
*
844
* @define Set via `@gwigz/slua-tstl-plugin` `define` option. Code guarded
945
* by this flag is stripped from the Lua output when set to `false`.
1046
*/
11-
declare const YIELD_DATASERVER: boolean
47+
declare const YIELD_DATASERVER_TEXT_COUNT: boolean
1248

1349
/**
1450
* Compile-time flag: enable experience KV store yield wrappers.

0 commit comments

Comments
 (0)