Skip to content

Commit c3a0b8d

Browse files
committed
fixes array_schema:each() and related method. fixes #47
1 parent 0ed9ee2 commit c3a0b8d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

colyseus/serialization/schema/types/array_schema.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local array_schema = {}
88
function array_schema:new(obj)
99
obj = obj or {
1010
items = {},
11-
keys = {},
11+
dynamic_indexes = {},
1212
indexes = {},
1313
props = {},
1414
}
@@ -27,7 +27,7 @@ end
2727

2828
-- length
2929
function array_schema:length()
30-
return #self.items
30+
return #self.dynamic_indexes
3131
end
3232

3333
-- getter
@@ -60,7 +60,7 @@ function array_schema:set_by_index(index, dynamic_index, value)
6060

6161
-- insert key
6262
if self.items[dynamic_index] == nil then
63-
table.insert(self.keys, dynamic_index)
63+
table.insert(self.dynamic_indexes, dynamic_index)
6464
end
6565

6666
self.items[dynamic_index] = value
@@ -71,16 +71,16 @@ function array_schema:get_index(index)
7171
end
7272

7373
function array_schema:get_by_index(index)
74-
return self.items[self.keys[index]]
74+
return self.items[self.dynamic_indexes[index]]
7575
end
7676

7777
function array_schema:delete_by_index(index)
7878
local dynamic_index = self.indexes[index]
7979

8080
-- delete key
81-
for i, k in pairs(self.keys) do
81+
for i, k in pairs(self.dynamic_indexes) do
8282
if k == dynamic_index then
83-
table.remove(self.keys, i)
83+
table.remove(self.dynamic_indexes, i)
8484
break
8585
end
8686
end
@@ -93,6 +93,7 @@ function array_schema:clear(changes, refs)
9393
callback_helpers.remove_child_refs(self, changes, refs)
9494
self.indexes = {}
9595
self.items = {}
96+
self.dynamic_indexes = {}
9697
end
9798

9899
function array_schema:on_add(callback, trigger_all)
@@ -112,15 +113,15 @@ function array_schema:on_change(callback)
112113
end
113114

114115
function array_schema:each(cb)
115-
for _, dynamic_index in ipairs(self.indexes) do
116+
for _, dynamic_index in ipairs(self.dynamic_indexes) do
116117
cb(self.items[dynamic_index], dynamic_index)
117118
end
118119
end
119120

120121
function array_schema:clone()
121122
return array_schema:new({
122123
items = table.clone(self.items),
123-
keys = table.clone(self.keys),
124+
dynamic_indexes = table.clone(self.dynamic_indexes),
124125
indexes = table.clone(self.indexes),
125126
props = self.props,
126127
})

0 commit comments

Comments
 (0)