@dan-uni/dan-any-plugin-detaolu 0.0.2

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.
@@ -0,0 +1,338 @@
1
+ /* eslint-disable getter-return */
2
+ /* eslint-disable unicorn/catch-error-name */
3
+ /* eslint-disable unicorn/prefer-number-properties */
4
+ /* eslint-disable block-scoped-var */
5
+ /* eslint-disable object-shorthand */
6
+ /* eslint-disable no-setter-return */
7
+ /* eslint-disable prefer-template */
8
+ /* eslint-disable unicorn/new-for-builtins */
9
+ /* eslint-disable unicorn/throw-new-error */
10
+ /* eslint-disable no-console */
11
+ /* eslint-disable unicorn/consistent-function-scoping */
12
+ /* eslint-disable no-unused-expressions */
13
+ /* eslint-disable vars-on-top */
14
+ /* eslint-disable import/no-mutable-exports */
15
+ /* eslint-disable import/no-default-export */
16
+ /** @nocollapse */ var Module = function (moduleArg = {}) {
17
+ var moduleRtn
18
+
19
+ var f = moduleArg,
20
+ g,
21
+ m = new Promise((a) => {
22
+ g = a
23
+ })
24
+ '_begin_chunk _check_similar _begin_index_lock _malloc _memory ___indirect_function_table onRuntimeInitialized'
25
+ .split(' ')
26
+ .forEach((a) => {
27
+ Object.getOwnPropertyDescriptor(m, a) ||
28
+ Object.defineProperty(m, a, {
29
+ get: () =>
30
+ q(
31
+ 'You are getting ' +
32
+ a +
33
+ ' on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js',
34
+ ),
35
+ set: () =>
36
+ q(
37
+ 'You are setting ' +
38
+ a +
39
+ ' on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js',
40
+ ),
41
+ })
42
+ })
43
+ var r = (a) => console.log(a),
44
+ t = (a) => console.error(a)
45
+ r = () => {}
46
+ t = (a) => {
47
+ console.error(a)
48
+ throw Error('wasm error: ' + a)
49
+ }
50
+ function v(a, b) {
51
+ if (!a) throw b
52
+ }
53
+ function q(a) {
54
+ throw Error(a)
55
+ }
56
+ var B, C, D, E
57
+ function H() {
58
+ var a = E.buffer
59
+ new Int8Array(a)
60
+ B = new Int16Array(a)
61
+ C = new Uint8Array(a)
62
+ new Uint16Array(a)
63
+ new Int32Array(a)
64
+ D = new Uint32Array(a)
65
+ new Float32Array(a)
66
+ new Float64Array(a)
67
+ new BigInt64Array(a)
68
+ new BigUint64Array(a)
69
+ }
70
+ v(
71
+ Math.imul,
72
+ 'This browser does not support Math.imul(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill',
73
+ )
74
+ v(
75
+ Math.fround,
76
+ 'This browser does not support Math.fround(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill',
77
+ )
78
+ v(
79
+ Math.clz32,
80
+ 'This browser does not support Math.clz32(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill',
81
+ )
82
+ v(
83
+ Math.trunc,
84
+ 'This browser does not support Math.trunc(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill',
85
+ )
86
+ var I = new Int16Array(1),
87
+ J = new Int8Array(I.buffer)
88
+ I[0] = 25459
89
+ if (115 !== J[0] || 99 !== J[1])
90
+ throw 'Runtime error: expected the system to be little-endian! (Run with -sSUPPORT_BIG_ENDIAN to bypass)'
91
+ if (f.ENVIRONMENT)
92
+ throw Error(
93
+ 'Module.ENVIRONMENT has been deprecated. To force the environment, use the ENVIRONMENT compile-time option (for example, -sENVIRONMENT=web or -sENVIRONMENT=node)',
94
+ )
95
+ function K(a) {
96
+ return (
97
+ 'FS_createPath' === a ||
98
+ 'FS_createDataFile' === a ||
99
+ 'FS_createPreloadedFile' === a ||
100
+ 'FS_unlink' === a ||
101
+ 'addRunDependency' === a ||
102
+ 'FS_createLazyFile' === a ||
103
+ 'FS_createDevice' === a ||
104
+ 'removeRunDependency' === a
105
+ )
106
+ }
107
+ function L(a, b) {
108
+ 'undefined' == typeof globalThis ||
109
+ Object.getOwnPropertyDescriptor(globalThis, a) ||
110
+ Object.defineProperty(globalThis, a, {
111
+ configurable: !0,
112
+ get() {
113
+ b()
114
+ },
115
+ })
116
+ }
117
+ function M(a, b) {
118
+ L(a, () => {
119
+ N(`\`${a}\` is not longer defined by emscripten. ${b}`)
120
+ })
121
+ }
122
+ M('buffer', 'Please use HEAP8.buffer or wasmMemory.buffer')
123
+ M('asm', 'Please use wasmExports instead')
124
+ function O(a) {
125
+ Object.getOwnPropertyDescriptor(f, a) ||
126
+ Object.defineProperty(f, a, {
127
+ configurable: !0,
128
+ get() {
129
+ var b = `'${a}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the Emscripten FAQ)`
130
+ K(a) &&
131
+ (b +=
132
+ '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you')
133
+ q(b)
134
+ },
135
+ })
136
+ }
137
+ var N = (a) => {
138
+ N.g || (N.g = {})
139
+ N.g[a] || ((N.g[a] = 1), t(a))
140
+ },
141
+ P = 'undefined' != typeof TextDecoder ? new TextDecoder() : void 0,
142
+ R = [null, [], []],
143
+ S = {
144
+ _abort_js: () => q('native code called abort()'),
145
+ emscripten_resize_heap: (a) => {
146
+ var b = C.length
147
+ a >>>= 0
148
+ v(a > b)
149
+ if (268435456 < a)
150
+ return (
151
+ t(
152
+ `Cannot enlarge memory, requested ${a} bytes, but the limit is ${268435456} bytes!`,
153
+ ),
154
+ !1
155
+ )
156
+ for (var c = 1; 4 >= c; c *= 2) {
157
+ var h = b * (1 + 1 / c)
158
+ h = Math.min(h, a + 100663296)
159
+ var k = Math,
160
+ n = k.min
161
+ h = Math.max(a, h)
162
+ v(65536, 'alignment argument is required')
163
+ k = n.call(k, 268435456, 65536 * Math.ceil(h / 65536))
164
+ a: {
165
+ n = k
166
+ h = E.buffer
167
+ var F = ((n - h.byteLength + 65535) / 65536) | 0
168
+ try {
169
+ E.grow(F)
170
+ H()
171
+ var w = 1
172
+ break a
173
+ } catch (x) {
174
+ t(
175
+ `growMemory: Attempted to grow heap from ${h.byteLength} bytes to ${n} bytes, but got error: ${x}`,
176
+ )
177
+ }
178
+ w = void 0
179
+ }
180
+ if (w) return !0
181
+ }
182
+ t(
183
+ `Failed to grow the heap from ${b} bytes to ${k} bytes, not enough memory!`,
184
+ )
185
+ return !1
186
+ },
187
+ fd_close: () => {
188
+ q('fd_close called without SYSCALLS_REQUIRE_FILESYSTEM')
189
+ },
190
+ fd_seek: function () {
191
+ return 70
192
+ },
193
+ fd_write: (a, b, c, h) => {
194
+ for (var k = 0, n = 0; n < c; n++) {
195
+ var F = D[b >> 2],
196
+ w = D[(b + 4) >> 2]
197
+ b += 8
198
+ for (var x = 0; x < w; x++) {
199
+ var y = a,
200
+ e = C[F + x],
201
+ z = R[y]
202
+ v(z)
203
+ if (0 === e || 10 === e) {
204
+ y = 1 === y ? r : t
205
+ e = z
206
+ for (var l = 0, p = l + NaN, u = l; e[u] && !(u >= p); ) ++u
207
+ if (16 < u - l && e.buffer && P) e = P.decode(e.subarray(l, u))
208
+ else {
209
+ for (p = ''; l < u; ) {
210
+ var d = e[l++]
211
+ if (d & 128) {
212
+ var G = e[l++] & 63
213
+ if (192 == (d & 224))
214
+ p += String.fromCharCode(((d & 31) << 6) | G)
215
+ else {
216
+ var Q = e[l++] & 63
217
+ if (224 == (d & 240)) d = ((d & 15) << 12) | (G << 6) | Q
218
+ else {
219
+ if (240 != (d & 248)) {
220
+ var V = N
221
+ var A = d
222
+ v('number' === typeof A)
223
+ A = '0x' + (A >>> 0).toString(16).padStart(8, '0')
224
+ V(
225
+ 'Invalid UTF-8 leading byte ' +
226
+ A +
227
+ ' encountered when deserializing a UTF-8 string in wasm memory to a JS string!',
228
+ )
229
+ }
230
+ d =
231
+ ((d & 7) << 18) | (G << 12) | (Q << 6) | (e[l++] & 63)
232
+ }
233
+ 65536 > d
234
+ ? (p += String.fromCharCode(d))
235
+ : ((d -= 65536),
236
+ (p += String.fromCharCode(
237
+ 55296 | (d >> 10),
238
+ 56320 | (d & 1023),
239
+ )))
240
+ }
241
+ } else p += String.fromCharCode(d)
242
+ }
243
+ e = p
244
+ }
245
+ y(e)
246
+ z.length = 0
247
+ } else z.push(e)
248
+ }
249
+ k += w
250
+ }
251
+ D[h >> 2] = k
252
+ return 0
253
+ },
254
+ }
255
+ f.stringToUTF16 = (a, b, c) => {
256
+ v(
257
+ 0 == b % 2,
258
+ 'Pointer passed to stringToUTF16 must be aligned to two bytes!',
259
+ )
260
+ v(
261
+ 'number' == typeof c,
262
+ 'stringToUTF16(str, outPtr, maxBytesToWrite) is missing the third parameter that specifies the length of the output buffer!',
263
+ )
264
+ c ??= 2147483647
265
+ if (2 > c) return 0
266
+ c -= 2
267
+ var h = b
268
+ c = c < 2 * a.length ? c / 2 : a.length
269
+ for (var k = 0; k < c; ++k) ((B[b >> 1] = a.charCodeAt(k)), (b += 2))
270
+ B[b >> 1] = 0
271
+ return b - h
272
+ }
273
+ 'writeI53ToI64 writeI53ToI64Clamped writeI53ToI64Signaling writeI53ToU64Clamped writeI53ToU64Signaling readI53FromI64 readI53FromU64 convertI32PairToI53 convertI32PairToI53Checked convertU32PairToI53 stackSave stackRestore stackAlloc getTempRet0 setTempRet0 zeroMemory strError inetPton4 inetNtop4 inetPton6 inetNtop6 readSockaddr writeSockaddr emscriptenLog readEmAsmArgs jstoi_q getExecutableName listenOnce autoResumeAudioContext getDynCaller dynCall callUserCallback asmjsMangle asyncLoad mmapAlloc HandleAllocator getNativeTypeSize STACK_SIZE STACK_ALIGN POINTER_SIZE ASSERTIONS getCFunc ccall cwrap uleb128Encode sigToWasmTypes generateFuncType convertJsFunctionToWasm getEmptyTableSlot updateTableMap getFunctionAddress addFunction removeFunction reallyNegative unSign strLen reSign formatString setValue getValue stringToUTF8Array stringToUTF8 lengthBytesUTF8 intArrayFromString intArrayToString AsciiToString stringToAscii UTF16ToString lengthBytesUTF16 UTF32ToString stringToUTF32 lengthBytesUTF32 stringToNewUTF8 stringToUTF8OnStack writeArrayToMemory registerKeyEventCallback maybeCStringToJsString findEventTarget getBoundingClientRect fillMouseEventData registerMouseEventCallback registerWheelEventCallback registerUiEventCallback registerFocusEventCallback fillDeviceOrientationEventData registerDeviceOrientationEventCallback fillDeviceMotionEventData registerDeviceMotionEventCallback screenOrientation fillOrientationChangeEventData registerOrientationChangeEventCallback fillFullscreenChangeEventData registerFullscreenChangeEventCallback JSEvents_requestFullscreen JSEvents_resizeCanvasForFullscreen registerRestoreOldStyle hideEverythingExceptGivenElement restoreHiddenElements setLetterbox softFullscreenResizeWebGLRenderTarget doRequestFullscreen fillPointerlockChangeEventData registerPointerlockChangeEventCallback registerPointerlockErrorEventCallback requestPointerLock fillVisibilityChangeEventData registerVisibilityChangeEventCallback registerTouchEventCallback fillGamepadEventData registerGamepadEventCallback registerBeforeUnloadEventCallback fillBatteryEventData battery registerBatteryEventCallback setCanvasElementSize getCanvasElementSize jsStackTrace getCallstack convertPCtoSourceLocation getEnvStrings checkWasiClock wasiRightsToMuslOFlags wasiOFlagsToMuslOFlags initRandomFill randomFill safeSetTimeout setImmediateWrapped safeRequestAnimationFrame clearImmediateWrapped polyfillSetImmediate registerPostMainLoop registerPreMainLoop getPromise makePromise idsToPromises makePromiseCallback isLeapYear ydayFromDate arraySum addDays getSocketFromFD getSocketAddress ALLOC_NORMAL ALLOC_STACK allocate writeStringToMemory writeAsciiToMemory setErrNo demangle stackTrace'
274
+ .split(' ')
275
+ .forEach(function (a) {
276
+ L(a, () => {
277
+ var b = `\`${a}\` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line`,
278
+ c = a
279
+ c.startsWith('_') || (c = '$' + a)
280
+ b += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${c}')`
281
+ K(a) &&
282
+ (b +=
283
+ '. Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you')
284
+ N(b)
285
+ })
286
+ O(a)
287
+ })
288
+ 'run addOnPreRun addOnInit addOnPreMain addOnExit addOnPostRun addRunDependency removeRunDependency out err callMain abort wasmMemory wasmExports HEAPF32 HEAPF64 HEAP_DATA_VIEW HEAP8 HEAPU8 HEAP16 HEAPU16 HEAP32 HEAPU32 HEAP64 HEAPU64 writeStackCookie checkStackCookie INT53_MAX INT53_MIN bigintToI53Checked ptrToString getHeapMax growMemory ENV ERRNO_CODES DNS Protocols Sockets timers warnOnce readEmAsmArgsArray jstoi_s alignMemory wasmTable noExitRuntime freeTableIndexes functionsInTableMap PATH PATH_FS UTF8Decoder UTF8ArrayToString UTF8ToString UTF16Decoder JSEvents specialHTMLTargets findCanvasEventTarget currentFullscreenStrategy restoreOldWindowedStyle UNWIND_CACHE promiseMap MONTH_DAYS_REGULAR MONTH_DAYS_LEAP MONTH_DAYS_REGULAR_CUMULATIVE MONTH_DAYS_LEAP_CUMULATIVE SYSCALLS allocateUTF8 allocateUTF8OnStack print printErr'
289
+ .split(' ')
290
+ .forEach(O)
291
+ var T,
292
+ U,
293
+ W = { env: S, wasi_snapshot_preview1: S }
294
+ if (!f.wasm)
295
+ throw 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM'
296
+ WebAssembly.instantiate(f.wasm, W).then(
297
+ (a) => {
298
+ a = a.instance.exports
299
+ f._begin_chunk = a.begin_chunk
300
+ f._begin_index_lock = a.begin_index_lock
301
+ f._check_similar = a.check_similar
302
+ f._malloc = a.malloc
303
+ T = a.emscripten_stack_init
304
+ U = a.emscripten_stack_get_end
305
+ E = a.memory
306
+ v(E)
307
+ H()
308
+ T()
309
+ var b = U()
310
+ v(0 == (b & 3))
311
+ 0 == b && (b += 4)
312
+ D[b >> 2] = 34821223
313
+ D[(b + 4) >> 2] = 2310721022
314
+ a.__wasm_call_ctors()
315
+ g(f)
316
+ r(
317
+ 'ready() called, and INVOKE_RUN=0. The runtime is now ready for you to call run() to invoke application _main(). You can also override ready() in a --pre-js file to get this signal as a callback',
318
+ )
319
+ },
320
+ (a) => {
321
+ console.error(a)
322
+ },
323
+ )
324
+ moduleRtn = m
325
+ for (const a of Object.keys(f))
326
+ a in moduleArg ||
327
+ Object.defineProperty(moduleArg, a, {
328
+ configurable: !0,
329
+ get() {
330
+ q(
331
+ `Access to module property ('${a}') is no longer possible via the module constructor argument; Instead, use the result of the module constructor.`,
332
+ )
333
+ },
334
+ })
335
+
336
+ return moduleRtn
337
+ }
338
+ export default Module
Binary file
@@ -0,0 +1,95 @@
1
+ import type { Config } from '.'
2
+ import type { int, Stats } from './types'
3
+
4
+ // @ts-expect-error
5
+ import generated_promise from './similarity-gen'
6
+
7
+ let module: any = null
8
+ let ptr_buf: number
9
+
10
+ const MAX_STRING_LEN = 16005
11
+
12
+ export async function init(wasm_module: ArrayBuffer) {
13
+ module = await generated_promise({ wasm: wasm_module })
14
+ ptr_buf = module._malloc(MAX_STRING_LEN * 2 + 7)
15
+ if (ptr_buf % 2)
16
+ // align to ushort
17
+ ptr_buf++
18
+ }
19
+
20
+ export function begin_chunk(config: Config) {
21
+ try {
22
+ module._begin_chunk(
23
+ ptr_buf,
24
+ config.MAX_DIST,
25
+ config.MAX_COSINE,
26
+ config.TRIM_PINYIN,
27
+ config.CROSS_MODE,
28
+ )
29
+ } catch (error) {
30
+ throw new Error(`wasm error (begin_chunk):\n${error}`)
31
+ }
32
+ }
33
+
34
+ export function begin_index_lock() {
35
+ try {
36
+ module._begin_index_lock()
37
+ } catch (error) {
38
+ throw new Error(`wasm error (begin_index_lock):\n${error}`)
39
+ }
40
+ }
41
+
42
+ enum CombinedReason {
43
+ combined_identical = 0,
44
+ combined_edit_distance = 1,
45
+ combined_pinyin_distance = 2,
46
+ combined_cosine_distance = 3,
47
+ }
48
+
49
+ export function detect_similarity(
50
+ str: string,
51
+ mode: number,
52
+ index_l: int,
53
+ S: Stats,
54
+ ): null | { reason: string; idx_diff: int } {
55
+ try {
56
+ module.stringToUTF16(str, ptr_buf, MAX_STRING_LEN * 2)
57
+ } catch (error) {
58
+ throw new Error(`wasm error (write str buf): ${str}\n${error}`)
59
+ }
60
+
61
+ let ret: number
62
+ try {
63
+ ret = module._check_similar(mode, index_l)
64
+ } catch (error) {
65
+ throw new Error(`wasm error (similar): ${str}\n${error}`)
66
+ }
67
+
68
+ if (ret === 0)
69
+ // fast path for CombinedReason.not_combined
70
+ return null
71
+
72
+ ret = ret >>> 0 // to unsigned
73
+ const reason: CombinedReason = ret >>> 30
74
+ const dist = (ret >>> 19) & ((1 << 11) - 1)
75
+ const idx_diff = ret & ((1 << 19) - 1)
76
+
77
+ let reason_str
78
+ if (reason === CombinedReason.combined_identical) {
79
+ S.combined_identical++
80
+ reason_str = '=='
81
+ } else if (reason === CombinedReason.combined_edit_distance) {
82
+ S.combined_edit_distance++
83
+ reason_str = `≤${dist}`
84
+ } else if (reason === CombinedReason.combined_cosine_distance) {
85
+ S.combined_cosine_distance++
86
+ reason_str = `${dist}%`
87
+ } else if (reason === CombinedReason.combined_pinyin_distance) {
88
+ S.combined_pinyin_distance++
89
+ reason_str = `P≤${dist}`
90
+ } else {
91
+ throw new Error(`similarity wasm returned unknown reason: ${ret}`)
92
+ }
93
+
94
+ return { reason: reason_str, idx_diff }
95
+ }
@@ -0,0 +1,196 @@
1
+ // import type { Config } from '../utils/dm-merge'
2
+
3
+ import type { UniDM } from '@dan-uni/dan-any'
4
+
5
+ export type int = number
6
+ export type float = number
7
+ export type AnyObject = { [k: string]: any }
8
+
9
+ export interface DanmuObject {
10
+ time_ms: int
11
+ mode: int
12
+ // fontsize: float
13
+ // color: int
14
+ // sender_hash: string
15
+ content: string
16
+ // sendtime: int
17
+ // weight: int
18
+ // id: string
19
+ pool: int
20
+ // danuni
21
+ // danuni_sender: string
22
+ danuni_dan: UniDM
23
+
24
+ // extra: {
25
+ // // for protobuf ingress
26
+ // proto_attr?: int | null
27
+ // proto_action?: string | null
28
+ // proto_animation?: string | null
29
+ // proto_colorful?: int | null
30
+ // proto_oid?: int | null
31
+ // proto_dmfrom?: int | null
32
+ // }
33
+ }
34
+
35
+ export interface DanmuObjectPeer extends DanmuObject {
36
+ pakku: {
37
+ sim_reason: string
38
+ }
39
+ }
40
+
41
+ export interface DanmuObjectRepresentative extends DanmuObject {
42
+ pakku: {
43
+ peers: DanmuObjectPeer[]
44
+ desc: string[]
45
+ disp_str: string
46
+ }
47
+ }
48
+
49
+ export interface DanmuChunk<ObjectType extends DanmuObject> {
50
+ objs: ObjectType[]
51
+
52
+ // extra: {
53
+ // // for protobuf ingress
54
+ // proto_segidx?: int
55
+ // proto_colorfulsrc?: AnyObject[]
56
+
57
+ // // for xml ingress
58
+ // xml_maxlimit?: string
59
+ // xml_chatid?: string
60
+ // }
61
+ }
62
+
63
+ export interface DanmuCluster {
64
+ peers: DanmuObjectPeer[]
65
+ desc: string[]
66
+ chosen_str: string
67
+ }
68
+
69
+ export interface DanmuClusterPtr {
70
+ peers_ptr: [int, string][] // index and sim_reason
71
+ desc: string[]
72
+ chosen_str: string
73
+ // danuni
74
+ danuni_count: int
75
+ danuni_dans: DanmuObject[]
76
+ // danuni_senders: string[]
77
+ }
78
+
79
+ export interface DanmuClusterOutput {
80
+ clusters: DanmuClusterPtr[]
81
+ stats: Stats
82
+ }
83
+
84
+ export class Stats {
85
+ // type = 'done' as const
86
+ // download_time_ms = 0
87
+ // parse_time_ms = 0
88
+ // userscript_time_ms = 0
89
+
90
+ combined_identical = 0
91
+ combined_edit_distance = 0
92
+ combined_pinyin_distance = 0
93
+ combined_cosine_distance = 0
94
+
95
+ // deleted_dispval = 0
96
+ deleted_blacklist = 0
97
+ deleted_blacklist_each: { [k: string]: int } = {}
98
+
99
+ ignored_whitelist = 0
100
+ // ignored_script = 0
101
+ ignored_type = 0
102
+
103
+ // modified_enlarge = 0
104
+ // modified_shrink = 0
105
+ // modified_scroll = 0
106
+
107
+ num_taolu_matched = 0
108
+ // num_total_danmu = 0
109
+ // num_onscreen_danmu = 0
110
+ // num_max_combo = 0
111
+ // num_max_dispval = 0
112
+
113
+ // notify(tabid: int, config: Config) {
114
+ // save_state({ ['STATS_' + tabid]: this }).then(() => {
115
+ // let text =
116
+ // config.POPUP_BADGE === 'count'
117
+ // ? '' + (this.num_total_danmu - this.num_onscreen_danmu)
118
+ // : config.POPUP_BADGE === 'percent'
119
+ // ? `${this.num_total_danmu ? Math.max(0, 100 - (100 * this.num_onscreen_danmu) / this.num_total_danmu).toFixed(0) : 0}%`
120
+ // : config.POPUP_BADGE === 'dispval'
121
+ // ? '' + Math.ceil(this.num_max_dispval)
122
+ // : /* off */ null
123
+ // void chrome.runtime.sendMessage({
124
+ // type: 'update_badge',
125
+ // tabid: tabid,
126
+ // text: text,
127
+ // bgcolor: '#008800',
128
+ // })
129
+ // })
130
+ // return this
131
+ // }
132
+
133
+ // update_from(x: Stats) {
134
+ // for (let k of [
135
+ // 'combined_identical',
136
+ // 'combined_edit_distance',
137
+ // 'combined_pinyin_distance',
138
+ // 'combined_cosine_distance',
139
+ // 'deleted_dispval',
140
+ // 'deleted_blacklist',
141
+ // 'ignored_whitelist',
142
+ // 'ignored_type',
143
+ // 'ignored_script',
144
+ // 'modified_enlarge',
145
+ // 'modified_shrink',
146
+ // 'modified_scroll',
147
+ // 'num_taolu_matched',
148
+ // ]) {
149
+ // // @ts-ignore
150
+ // this[k] += x[k]
151
+ // }
152
+
153
+ // for (let k of ['num_max_combo', 'num_max_dispval']) {
154
+ // // @ts-ignore
155
+ // this[k] = Math.max(this[k], x[k])
156
+ // }
157
+ // for (let [k, v] of Object.entries(x.deleted_blacklist_each)) {
158
+ // this.deleted_blacklist_each[k] = (this.deleted_blacklist_each[k] || 0) + v
159
+ // }
160
+ // }
161
+ }
162
+
163
+ export class Queue<T> {
164
+ storage: { [k: int]: T }
165
+ index_l: int
166
+ index_r: int // [l, r)
167
+ constructor(init: T[] = []) {
168
+ this.storage = { ...init }
169
+ this.index_l = 0
170
+ this.index_r = init.length
171
+ }
172
+ push(item: T) {
173
+ this.storage[this.index_r++] = item
174
+ }
175
+ pop() {
176
+ delete this.storage[this.index_l++]
177
+ }
178
+ peek(): T | null {
179
+ if (this.index_l === this.index_r) return null
180
+ return this.storage[this.index_l]
181
+ }
182
+ size() {
183
+ return this.index_r - this.index_l
184
+ }
185
+ [Symbol.iterator]() {
186
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
187
+ const self = this
188
+ let index = self.index_l
189
+ return {
190
+ next() {
191
+ if (index >= self.index_r) return { done: true, value: undefined }
192
+ return { done: false, value: self.storage[index++] }
193
+ },
194
+ } as IterableIterator<T>
195
+ }
196
+ }