lenix 1.7.8 → 1.7.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/cam.ts +10 -1
- package/client/client.ts +20 -10
- package/nui/on.ts +21 -10
- package/package.json +1 -1
package/client/cam.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import type { Vec4 } from '../shared/types.ts'
|
|
4
4
|
|
|
5
|
+
const cams = new Set<number>()
|
|
6
|
+
|
|
5
7
|
export interface BaseCamDetails {
|
|
6
8
|
/**
|
|
7
9
|
* Fade-out duration in milliseconds.
|
|
@@ -102,6 +104,7 @@ const create = ({
|
|
|
102
104
|
false,
|
|
103
105
|
rotationOrder
|
|
104
106
|
)
|
|
107
|
+
cams.add(cam)
|
|
105
108
|
|
|
106
109
|
toggleCam({
|
|
107
110
|
cam,
|
|
@@ -132,10 +135,16 @@ const destroy = ({
|
|
|
132
135
|
fadeIn,
|
|
133
136
|
fadeOut
|
|
134
137
|
})
|
|
138
|
+
cams.delete(cam)
|
|
135
139
|
}
|
|
136
140
|
|
|
137
|
-
// TODO: delete cams on resource stop
|
|
138
141
|
export const cam = {
|
|
139
142
|
create,
|
|
140
143
|
destroy
|
|
141
144
|
}
|
|
145
|
+
|
|
146
|
+
on('onResourceStop', (resource: string) => {
|
|
147
|
+
if (resource !== GetCurrentResourceName()) return
|
|
148
|
+
|
|
149
|
+
cams.forEach(cam => destroy({ cam }))
|
|
150
|
+
})
|
package/client/client.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { repeat } from '../shared/repeat.ts'
|
|
2
2
|
import type { Vec3, Vec4 } from '../shared/types.ts'
|
|
3
3
|
|
|
4
|
+
type JsonPrimitive = string | number | boolean | null
|
|
5
|
+
type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue }
|
|
6
|
+
|
|
4
7
|
export const client = {
|
|
5
8
|
entity: {
|
|
6
9
|
/**
|
|
@@ -154,24 +157,31 @@ export const client = {
|
|
|
154
157
|
},
|
|
155
158
|
|
|
156
159
|
/* */
|
|
157
|
-
/* TODO: support booleans and numbers, and maybe even json */
|
|
158
160
|
storage: {
|
|
159
161
|
set: <Storage>(
|
|
160
162
|
...[key, value]: {
|
|
161
|
-
[K in keyof Storage]:
|
|
163
|
+
[K in keyof Storage]: Storage[K] extends JsonValue
|
|
164
|
+
? [key: Extract<K, string>, value: Storage[K]]
|
|
165
|
+
: never
|
|
162
166
|
}[keyof Storage]
|
|
163
|
-
) => SetResourceKvp(key, value),
|
|
167
|
+
) => SetResourceKvp(key, JSON.stringify(value)),
|
|
164
168
|
|
|
165
169
|
get: <Storage, K extends Extract<keyof Storage, string>>(
|
|
166
170
|
key: K,
|
|
167
|
-
init?:
|
|
168
|
-
): Storage[K] extends
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
init?: Storage[K] extends JsonValue ? Storage[K] : never
|
|
172
|
+
): Storage[K] extends JsonValue ? Storage[K] : never => {
|
|
173
|
+
const value = GetResourceKvpString(key)
|
|
174
|
+
|
|
175
|
+
if (value === null) {
|
|
176
|
+
if (init === undefined) {
|
|
177
|
+
throw new Error(`Storage<${key}> was never assigned`)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
SetResourceKvp(key, JSON.stringify(init))
|
|
181
|
+
return init as Storage[K] extends JsonValue ? Storage[K] : never
|
|
173
182
|
}
|
|
174
|
-
|
|
183
|
+
|
|
184
|
+
return JSON.parse(value) as Storage[K] extends JsonValue ? Storage[K] : never
|
|
175
185
|
},
|
|
176
186
|
|
|
177
187
|
delete: <Storage>(
|
package/nui/on.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import type { Event } from '../shared/types.ts'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const events = new Map<string, Set<(...params: unknown[]) => void>>()
|
|
4
4
|
|
|
5
|
-
// TODO: remove the event
|
|
6
5
|
// deno-lint-ignore no-window no-window-prefix no-unused-vars
|
|
7
|
-
const handler =
|
|
6
|
+
const handler = (event: MessageEvent) => {
|
|
8
7
|
const { id, params } = event.data
|
|
9
|
-
const
|
|
10
|
-
if (!
|
|
8
|
+
const callbacks = events.get(id)
|
|
9
|
+
if (!callbacks) return console.error(`Event<${id}> does not exist yet`)
|
|
11
10
|
|
|
12
11
|
try {
|
|
13
|
-
|
|
12
|
+
callbacks.forEach(callback => callback(...(params ?? [])))
|
|
14
13
|
} catch (e) {
|
|
15
14
|
throw new Error(`Error occured while receiving event<${id}>. \n${e}`)
|
|
16
15
|
}
|
|
17
|
-
}
|
|
16
|
+
}
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
19
|
* Registers a typed browser-side handler for events sent from the game client.
|
|
@@ -24,6 +23,18 @@ export const onEvent = <
|
|
|
24
23
|
>(
|
|
25
24
|
id: T[0],
|
|
26
25
|
cb: (...params: T[1]) => void
|
|
27
|
-
): void => {
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
): () => void => {
|
|
27
|
+
if (events.size === 0) window.addEventListener('message', handler)
|
|
28
|
+
|
|
29
|
+
const callbacks = events.get(id) ?? new Set<(...params: T[1]) => void>()
|
|
30
|
+
callbacks.add(cb)
|
|
31
|
+
events.set(id, callbacks)
|
|
32
|
+
|
|
33
|
+
return () => {
|
|
34
|
+
callbacks.delete(cb)
|
|
35
|
+
if (callbacks.size === 0) events.delete(id)
|
|
36
|
+
if (events.size === 0) {
|
|
37
|
+
window.removeEventListener('message', handler)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|