Skip to content

Commit 60c0289

Browse files
committed
chore: use penlight class for all the class def
1 parent 66b03f7 commit 60c0289

16 files changed

+144
-158
lines changed

lua/react/components/buffer-component.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local create_effect = core.create_effect
99
--- @field private effect react.Effect
1010
--- @field private node function
1111
--- @field private subscriber function
12-
--- @field private components Set
12+
--- @field private components react.Set
1313
--- @field private text string
1414

1515
local BufferComponent = class()
@@ -33,7 +33,7 @@ function BufferComponent:_init(args)
3333
3434
local Component = require('react.component')
3535
36-
Component:new({
36+
Component({
3737
component = App,
3838
subscriber = on_change
3939
})
@@ -53,7 +53,7 @@ function BufferComponent:_init(args)
5353

5454
-- List of components that will be referred when one of them are
5555
-- user functional components and it's being updated
56-
self.components = Set:new()
56+
self.components = Set()
5757

5858
-- holds the latest text of this component
5959
self.text = ''

lua/react/core/effect-events.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--- @enum EffectEvents
1+
--- @enum react.EffectEvents
22
return {
33

44
-- runs before / after each render

lua/react/core/effect.lua

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ local log = require('react.util.log')
99

1010
--- @class react.Effect
1111
--- @field private first_render boolean true until first render is completed
12-
--- @field private signals Set holds all the signals associated with this effect
12+
--- @field private signals react.Set holds all the signals associated with this effect
1313
--- @field private signal_pointer number a signal can request previously created signals from the
1414
--- effect. When a signal is requested, the pointer will be incremented.
1515
--- @field private events Event event object to handle events within the effect
1616
--- @field private callback function to be called on an render/re-render event
1717

1818
local Effect = class()
1919

20-
Effect.context = Stack:new()
20+
Effect.context = Stack()
2121

2222
function Effect:_init(callback)
2323
log.debug('creating new effect')
@@ -27,10 +27,6 @@ function Effect:_init(callback)
2727
'Callback function should be passed to effect'
2828
)
2929

30-
-- local o = {}
31-
-- setmetatable(o, self)
32-
-- self.__index = self
33-
3430
local context_push_callback = function()
3531
self.events:dispatch(EffectEvents.BEFORE_RENDER)
3632

@@ -48,9 +44,9 @@ function Effect:_init(callback)
4844
end
4945

5046
self.first_render = true
51-
self.signals = Set:new()
47+
self.signals = Set()
5248
self.signal_pointer = 1
53-
self.events = Event:new()
49+
self.events = Event()
5450

5551
-- wrap the callback only for the first render to identify the initial
5652
-- render

lua/react/core/signal.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local log = require('react.util.log')
55

66
--- @class react.Signal
77
--- @field private value table value of the store
8-
--- @field private publisher Publisher
8+
--- @field private publisher react.Publisher
99
local Signal = class()
1010

1111
function Signal:_init(value)
@@ -22,7 +22,7 @@ function Signal:_init(value)
2222
log.debug('creating signal with initial value:: ', value)
2323

2424
self.value = value
25-
self.publisher = Publisher:new()
25+
self.publisher = Publisher()
2626
end
2727

2828
--- Returns the signal value

lua/react/stores/dict/dict-node.lua

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
local class = require('react.util.class')
12
local Effect = require('react.core.effect')
23
local List = require('react.util.list')
34
local helper = require('react.stores.dict.helper')
45
local log = require('react.util.log')
56

6-
--- @alias PublisherMap { key: string, effects: Set, children: PublisherMap[] }
7-
--- @alias DictNodeConstructor { publishers: PublisherMap, value: any, path?: List, curr_publisher_node?: PublisherMap }
7+
--- @alias PublisherMap { key: string, effects: react.Set, children: PublisherMap[] }
8+
--- @alias DictNodeConstructor { publishers: PublisherMap, value: any, path?: react.List, curr_publisher_node?: PublisherMap }
89

910
--- @class DictNode
1011
--- @field value any value of the dict node
11-
local M = {}
12+
local M = class()
13+
14+
function M:_init() end
1215

1316
--- @param opt DictNodeConstructor
1417
function M:new(opt)
@@ -20,7 +23,7 @@ function M:new(opt)
2023
local obj = setmetatable({}, {
2124
publishers = opt.publishers,
2225
value = opt.value,
23-
path = opt.path or List:new(),
26+
path = opt.path or List(),
2427
curr_publishers_node = opt.curr_publisher_node or opt.publishers,
2528

2629
__index = self.index,
@@ -42,7 +45,6 @@ function M.index(self, key)
4245
local curr_pub_node = helper.publisher_path_traversal(publishers, curr_path)
4346
local indexed_value = value[key]
4447

45-
4648
log.debug(('indexed value for key "%s" is'):format(key), indexed_value)
4749

4850
-- if there is an effect, add effect to store, and store to effect
@@ -67,7 +69,12 @@ function M.index(self, key)
6769

6870
-- if the next value is a table then create new object of dict
6971
if type(indexed_value) == 'table' then
70-
log.debug('creating new dict node for path ', curr_path.list, ' with value', indexed_value)
72+
log.debug(
73+
'creating new dict node for path ',
74+
curr_path.list,
75+
' with value',
76+
indexed_value
77+
)
7178

7279
return M:new({
7380
publishers = publishers,

lua/react/stores/dict/helper.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
--- Traverse through the table and find
77
--- @param publisher_map PublisherMap
8-
--- @param path List
8+
--- @param path react.List
99
function M.publisher_path_traversal(publisher_map, path)
1010
local curr_pub_node = publisher_map
1111

@@ -14,7 +14,7 @@ function M.publisher_path_traversal(publisher_map, path)
1414
curr_pub_node.children[key] = {
1515
-- adding key for debugging purposes
1616
key = key,
17-
effects = Set:new(),
17+
effects = Set(),
1818
children = {},
1919
}
2020
end
@@ -48,7 +48,7 @@ function M.dispatch_and_remove_children(root_pub_node)
4848
end
4949

5050
function M.get_all_effects_in_pub_node(root_pub_node)
51-
local effects = Set:new()
51+
local effects = Set()
5252

5353
local capture_children_effects = nil
5454

lua/react/stores/dict/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ return function(init_value)
88
assert(Effect.context:is_empty(), 'Store can not be created inside an effect or component')
99

1010
local publishers = {
11-
effects = Set:new(),
11+
effects = Set(),
1212
children = {},
1313
}
1414

lua/react/stores/list.lua

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
local class = require('react.util.class')
12
local Set = require('react.util.set')
23
local Publisher = require('react.util.publisher')
34
local Effect = require('react.core.effect')
45

5-
local M = {
6-
list = Set:new(),
7-
element_publishers = Set:new(),
8-
publisher = Publisher:new(),
9-
}
10-
11-
function M:new(o)
12-
o = o or {}
13-
setmetatable(o, self)
14-
self.__index = self
15-
return o
16-
end
6+
local M = class()
7+
8+
M.list = Set()
9+
M.element_publishers = Set()
10+
M.publisher = Publisher()
1711

1812
function M:get(index)
1913
self:reg_subscriber()

lua/react/util/event.lua

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1+
local class = require('react.util.class')
12
local Set = require('react.util.set')
23

34
--- @class Event
4-
--- @field private listeners_map table<string, Set>
5-
local M = {}
5+
--- @field private listeners_map table<string, react.Set>
6+
local M = class()
67

7-
function M:new()
8-
local o = {
9-
listeners_map = {},
10-
}
11-
12-
setmetatable(o, self)
13-
self.__index = self
14-
15-
return o
8+
function M:_init()
9+
self.listeners_map = {}
1610
end
1711

1812
--- Add a listener function to a given event
@@ -21,7 +15,7 @@ end
2115
--- @param opt nil|{once: boolean} additional options
2216
function M:add_listener(event, listener, opt)
2317
if not self.listeners_map[event] then
24-
self.listeners_map[event] = Set:new()
18+
self.listeners_map[event] = Set()
2519
end
2620

2721
if opt and not opt.once then

lua/react/util/list.lua

+25-29
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,52 @@
1-
---@class List
1+
local class = require('react.util.class')
2+
3+
---@class react.List
24
---@field protected list any[]
3-
local M = {}
5+
local List = class()
46

57
---@param list any[] | nil
6-
function M:new(list)
7-
local o = {
8-
list = list or {},
9-
}
10-
11-
setmetatable(o, self)
12-
self.__index = self
13-
14-
return o
8+
function List:_init(list)
9+
self.list = list or {}
1510
end
1611

1712
---Returns the value at given index
1813
---@param index number index of the param
1914
---@returns any
20-
function M:get(index)
15+
function List:get(index)
2116
return self.list[index]
2217
end
2318

2419
---Sets the value at given index to given value
2520
---@param index number index to set the value at
2621
---@param value any value to set
27-
function M:set(index, value)
22+
function List:set(index, value)
2823
self.list[index] = value
2924
return 0
3025
end
3126

3227
---Returns an iterator of the list
3328
---@returns function
34-
function M:iter()
29+
function List:iter()
3530
return ipairs(self.list)
3631
end
3732

3833
---Append new value to the list
3934
---@param value any value to append
40-
function M:add(value)
35+
function List:add(value)
4136
table.insert(self.list, value)
4237
end
4338

4439
---Remove existing value from the list by index
4540
---@param index number
4641
---@returns any value that got removed from the list
47-
function M:remove(index)
42+
function List:remove(index)
4843
return table.remove(self.list, index)
4944
end
5045

5146
---Remove existing value from the list by the value
5247
---@param value any value to remove from the list
5348
---@returns any value that got removed from the list
54-
function M:remove_by_value(value)
49+
function List:remove_by_value(value)
5550
local index = self:find_index(value)
5651

5752
if index < 0 then
@@ -64,14 +59,14 @@ function M:remove_by_value(value)
6459
end
6560

6661
---Remove all the values from the list
67-
function M:remove_all()
62+
function List:remove_all()
6863
self.list = {}
6964
end
7065

7166
---Returns the index of a given value if exists
7267
---Returns -1 if the value does not exist
7368
---@return number
74-
function M:find_index(value)
69+
function List:find_index(value)
7570
for index, ele in ipairs(self.list) do
7671
if ele == value then
7772
return index
@@ -84,7 +79,7 @@ end
8479
---Returns true if the given value exists in the list
8580
---@param value any value to check the existence
8681
---@returns boolean
87-
function M:has(value)
82+
function List:has(value)
8883
for _, ele in ipairs(self.list) do
8984
if ele == value then
9085
return true
@@ -96,13 +91,13 @@ end
9691

9792
---Returns the size of the list
9893
---@returns number size of the list
99-
function M:length()
94+
function List:length()
10095
return #self.list
10196
end
10297

10398
---Concatenate the given list to this list
104-
---@param list List | any[]
105-
function M:concat(list)
99+
---@param list react.List | any[]
100+
function List:concat(list)
106101
if list.iter then
107102
for _, v in list:iter() do
108103
self:add(v)
@@ -117,20 +112,20 @@ end
117112
---Returns a string after joining the list by given string separator
118113
---@param separator string separator to join the list with
119114
---@returns string joined string
120-
function M:join(separator)
115+
function List:join(separator)
121116
return table.concat(self.list, separator)
122117
end
123118

124119
---Returns a clone of the current list
125120
---@returns List clone of the current list
126-
function M:clone()
127-
return M:new(M.deepcopy(self.list))
121+
function List:clone()
122+
return List(List.deepcopy(self.list))
128123
end
129124

130125
---@private
131126
---@param orig table original table to copy
132127
---@returns table clone of the table
133-
function M.deepcopy(orig, copies)
128+
function List.deepcopy(orig, copies)
134129
copies = copies or {}
135130
local orig_type = type(orig)
136131
local copy
@@ -141,7 +136,8 @@ function M.deepcopy(orig, copies)
141136
copy = {}
142137
copies[orig] = copy
143138
for orig_key, orig_value in next, orig, nil do
144-
copy[M.deepcopy(orig_key, copies)] = M.deepcopy(orig_value, copies)
139+
copy[List.deepcopy(orig_key, copies)] =
140+
List.deepcopy(orig_value, copies)
145141
end
146142
-- setmetatable(copy, M.deepcopy(getmetatable(orig), copies))
147143
end
@@ -152,4 +148,4 @@ function M.deepcopy(orig, copies)
152148
return copy
153149
end
154150

155-
return M
151+
return List

0 commit comments

Comments
 (0)