Skip to content

Commit 9e710de

Browse files
committed
refactor: convert standalone functions to method syntax
Convert various standalone functions to method syntax for better organization and consistency across the codebase. This includes functions in Mocket, Logger, and Value modules. Remove redundant Show trait implementations and clean up generated code. Simplify Object access by using tuple struct field access (self.0) instead of inner() method.
1 parent 12af437 commit 9e710de

9 files changed

Lines changed: 80 additions & 67 deletions

File tree

src/index.mbt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn template_to_regex(template : String) -> String {
8686
}
8787

8888
///|
89-
pub fn on(
89+
pub fn Mocket::on(
9090
self : Mocket,
9191
event : String,
9292
path : String,
@@ -128,7 +128,7 @@ pub fn on(
128128
}
129129

130130
///|
131-
pub fn get(
131+
pub fn Mocket::get(
132132
self : Mocket,
133133
path : String,
134134
handler : async (HttpEvent) -> HttpBody noraise,
@@ -137,7 +137,7 @@ pub fn get(
137137
}
138138

139139
///|
140-
pub fn post(
140+
pub fn Mocket::post(
141141
self : Mocket,
142142
path : String,
143143
handler : async (HttpEvent) -> HttpBody noraise,
@@ -146,7 +146,7 @@ pub fn post(
146146
}
147147

148148
///|
149-
pub fn patch(
149+
pub fn Mocket::patch(
150150
self : Mocket,
151151
path : String,
152152
handler : async (HttpEvent) -> HttpBody noraise,
@@ -155,7 +155,7 @@ pub fn patch(
155155
}
156156

157157
///|
158-
pub fn connect(
158+
pub fn Mocket::connect(
159159
self : Mocket,
160160
path : String,
161161
handler : async (HttpEvent) -> HttpBody noraise,
@@ -164,7 +164,7 @@ pub fn connect(
164164
}
165165

166166
///|
167-
pub fn put(
167+
pub fn Mocket::put(
168168
self : Mocket,
169169
path : String,
170170
handler : async (HttpEvent) -> HttpBody noraise,
@@ -173,7 +173,7 @@ pub fn put(
173173
}
174174

175175
///|
176-
pub fn delete(
176+
pub fn Mocket::delete(
177177
self : Mocket,
178178
path : String,
179179
handler : async (HttpEvent) -> HttpBody noraise,
@@ -182,7 +182,7 @@ pub fn delete(
182182
}
183183

184184
///|
185-
pub fn head(
185+
pub fn Mocket::head(
186186
self : Mocket,
187187
path : String,
188188
handler : async (HttpEvent) -> HttpBody noraise,
@@ -191,7 +191,7 @@ pub fn head(
191191
}
192192

193193
///|
194-
pub fn options(
194+
pub fn Mocket::options(
195195
self : Mocket,
196196
path : String,
197197
handler : async (HttpEvent) -> HttpBody noraise,
@@ -200,7 +200,7 @@ pub fn options(
200200
}
201201

202202
///|
203-
pub fn trace(
203+
pub fn Mocket::trace(
204204
self : Mocket,
205205
path : String,
206206
handler : async (HttpEvent) -> HttpBody noraise,
@@ -209,7 +209,7 @@ pub fn trace(
209209
}
210210

211211
///|
212-
pub fn all(
212+
pub fn Mocket::all(
213213
self : Mocket,
214214
path : String,
215215
handler : async (HttpEvent) -> HttpBody noraise,
@@ -219,7 +219,7 @@ pub fn all(
219219

220220
///|
221221
// 创建路由组
222-
pub fn group(
222+
pub fn Mocket::group(
223223
self : Mocket,
224224
base_path : String,
225225
configure : (Mocket) -> Unit,

src/js/object.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn Object::from_value_unchecked(value : Value) -> Object {
1111

1212
///|
1313
pub fn Object::to_value(self : Object) -> Value {
14-
self.inner()
14+
self.0
1515
}
1616

1717
///|
@@ -52,10 +52,10 @@ pub fn[K, V] Object::from_iter2(it : Iter2[K, V]) -> Object {
5252

5353
///|
5454
pub fn[K, V] Object::op_get(self : Object, key : K) -> V {
55-
self.inner().get_ffi(Value::cast_from(key)).cast()
55+
self.0.get_ffi(Value::cast_from(key)).cast()
5656
}
5757

5858
///|
5959
pub fn[K, V] Object::op_set(self : Object, key : K, value : V) -> Unit {
60-
self.inner().set_ffi(Value::cast_from(key), Value::cast_from(value))
60+
self.0.set_ffi(Value::cast_from(key), Value::cast_from(value))
6161
}

src/js/pkg.generated.mbti

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ async fn[T, E : Error] suspend(((T) -> Unit, (E) -> Unit) -> Unit) -> T raise E
2727
// Errors
2828
pub suberror Error_ Value
2929
fn Error_::cause(Self) -> Value?
30-
fn Error_::output(Self, &Logger) -> Unit // from trait `Show`
31-
fn Error_::to_string(Self) -> String // from trait `Show`
3230
fn[T] Error_::wrap(() -> Value, map_ok? : (Value) -> T) -> T raise Self
3331
impl Show for Error_
3432

@@ -187,7 +185,6 @@ fn[Arg, Result] Value::new(Self, Array[Arg]) -> Result
187185
fn[Arg, Result] Value::new_with_index(Self, Int, Array[Arg]) -> Result
188186
fn[Arg, Result] Value::new_with_string(Self, String, Array[Arg]) -> Result
189187
fn[Arg, Result] Value::new_with_symbol(Self, Symbol, Array[Arg]) -> Result
190-
fn Value::output(Self, &Logger) -> Unit // from trait `Show`
191188
fn[T] Value::set_with_index(Self, Int, T) -> Unit
192189
fn[T] Value::set_with_string(Self, String, T) -> Unit
193190
fn[T] Value::set_with_symbol(Self, Symbol, T) -> Unit

src/js/value.mbt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ pub impl Show for Value with output(self, logger) {
2020
pub let globalThis : Value = get_globalThis()
2121

2222
///|
23-
pub fn[T] get_with_string(self : Value, key : String) -> T {
23+
pub fn[T] Value::get_with_string(self : Value, key : String) -> T {
2424
self.get_ffi(Value::cast_from(key)).cast()
2525
}
2626

2727
///|
28-
pub fn[T] get_with_symbol(self : Value, key : Symbol) -> T {
28+
pub fn[T] Value::get_with_symbol(self : Value, key : Symbol) -> T {
2929
self.get_ffi(Value::cast_from(key)).cast()
3030
}
3131

3232
///|
33-
pub fn[T] get_with_index(self : Value, index : Int) -> T {
33+
pub fn[T] Value::get_with_index(self : Value, index : Int) -> T {
3434
self.get_ffi(Value::cast_from(index)).cast()
3535
}
3636

3737
///|
38-
pub fn[T] set_with_string(self : Value, key : String, value : T) -> Unit {
38+
pub fn[T] Value::set_with_string(self : Value, key : String, value : T) -> Unit {
3939
self.set_ffi(Value::cast_from(key), Value::cast_from(value))
4040
}
4141

4242
///|
43-
pub fn[T] set_with_symbol(self : Value, key : Symbol, value : T) -> Unit {
43+
pub fn[T] Value::set_with_symbol(self : Value, key : Symbol, value : T) -> Unit {
4444
self.set_ffi(Value::cast_from(key), Value::cast_from(value))
4545
}
4646

4747
///|
48-
pub fn[T] set_with_index(self : Value, index : Int, value : T) -> Unit {
48+
pub fn[T] Value::set_with_index(self : Value, index : Int, value : T) -> Unit {
4949
self.set_ffi(Value::cast_from(index), Value::cast_from(value))
5050
}
5151

@@ -144,9 +144,9 @@ pub impl @json.FromJson for Value with from_json(json : Json, path) {
144144
Object(kvs) => {
145145
let acc = Object::new()
146146
for k, v in kvs {
147-
acc.inner().set_with_string(k, (@json.from_json(v, path~) : Value))
147+
acc.0.set_with_string(k, (@json.from_json(v, path~) : Value))
148148
}
149-
acc.inner()
149+
acc.0
150150
}
151151
}
152152
}
@@ -174,59 +174,63 @@ extern "js" fn Value::to_json_string_ffi(self : Value) -> Value =
174174
#| (self) => JSON.stringify(self)
175175

176176
///|
177-
pub extern "js" fn is_bool(self : Value) -> Bool =
177+
pub extern "js" fn Value::is_bool(self : Value) -> Bool =
178178
#| (value) => Object.is(typeof value, 'boolean')
179179

180180
///|
181-
pub extern "js" fn is_null(self : Value) -> Bool =
181+
pub extern "js" fn Value::is_null(self : Value) -> Bool =
182182
#| (n) => Object.is(n, null)
183183

184184
///|
185-
pub extern "js" fn is_undefined(self : Value) -> Bool =
185+
pub extern "js" fn Value::is_undefined(self : Value) -> Bool =
186186
#| (n) => Object.is(n, undefined)
187187

188188
///|
189-
pub extern "js" fn is_number(self : Value) -> Bool =
189+
pub extern "js" fn Value::is_number(self : Value) -> Bool =
190190
#| (n) => Object.is(typeof n, "number")
191191

192192
///|
193-
pub extern "js" fn is_string(self : Value) -> Bool =
193+
pub extern "js" fn Value::is_string(self : Value) -> Bool =
194194
#| (n) => Object.is(typeof n, "string")
195195

196196
///|
197-
pub extern "js" fn is_object(self : Value) -> Bool =
197+
pub extern "js" fn Value::is_object(self : Value) -> Bool =
198198
#| (n) => Object.is(typeof n, "object")
199199

200200
///|
201-
pub extern "js" fn is_symbol(self : Value) -> Bool =
201+
pub extern "js" fn Value::is_symbol(self : Value) -> Bool =
202202
#| (n) => Object.is(typeof n, "symbol")
203203

204204
///|
205205
extern "js" fn get_globalThis() -> Value =
206206
#| () => globalThis
207207

208208
///|
209-
extern "js" fn get_ffi(self : Value, key : Value) -> Value =
209+
extern "js" fn Value::get_ffi(self : Value, key : Value) -> Value =
210210
#| (obj, key) => obj[key]
211211

212212
///|
213-
extern "js" fn set_ffi(self : Value, key : Value, value : Value) =
213+
extern "js" fn Value::set_ffi(self : Value, key : Value, value : Value) =
214214
#| (obj, key, value) => { obj[key] = value }
215215

216216
///|
217-
extern "js" fn apply_ffi(self : Value, key : Value, args : Value) -> Value =
217+
extern "js" fn Value::apply_ffi(
218+
self : Value,
219+
key : Value,
220+
args : Value,
221+
) -> Value =
218222
#| (self, key, args) => self[key](...args)
219223

220224
///|
221-
extern "js" fn apply_self_ffi(self : Value, args : Value) -> Value =
225+
extern "js" fn Value::apply_self_ffi(self : Value, args : Value) -> Value =
222226
#| (self, args) => self(...args)
223227

224228
///|
225-
extern "js" fn new_ffi(self : Value, key : Value, args : Value) -> Value =
229+
extern "js" fn Value::new_ffi(self : Value, key : Value, args : Value) -> Value =
226230
#| (self, key, args) => new self[key](...args)
227231

228232
///|
229-
extern "js" fn new_self_ffi(self : Value, args : Value) -> Value =
233+
extern "js" fn Value::new_self_ffi(self : Value, args : Value) -> Value =
230234
#| (self, args) => new self(...args)
231235

232236
///|
@@ -239,7 +243,10 @@ extern "js" fn Value::undefined() -> Value =
239243

240244
///|
241245
/// @param self The constructor function that accepts this as the first argument
242-
pub extern "js" fn extends(self : Value, parent_constructor : Value) -> Value =
246+
pub extern "js" fn Value::extends(
247+
self : Value,
248+
parent_constructor : Value,
249+
) -> Value =
243250
#|(f, parent) => {
244251
#| return class extends parent {
245252
#| constructor(...args) {

0 commit comments

Comments
 (0)