adding stire feature
This commit is contained in:
parent
0949c2b147
commit
40a9eccc75
1 changed files with 57 additions and 5 deletions
|
|
@ -5,17 +5,25 @@ this feature allows get object without making some created callback and local va
|
||||||
but, it can't allows mulit id and object
|
but, it can't allows mulit id and object
|
||||||
this feature should be upgraded
|
this feature should be upgraded
|
||||||
]]
|
]]
|
||||||
|
local wrap = coroutine.wrap;
|
||||||
local insert = table.insert;
|
local insert = table.insert;
|
||||||
local remove = table.remove;
|
local remove = table.remove;
|
||||||
|
|
||||||
|
local function catch(...)
|
||||||
|
local passed,err = pcall(...);
|
||||||
|
if not passed then
|
||||||
|
wran("[QUAD] Error occured while operating async task\n" .. tostring(err));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function module.init(shared)
|
function module.init(shared)
|
||||||
local new = {};
|
local new = {};
|
||||||
local items = shared.items;
|
local items = shared.items;
|
||||||
|
|
||||||
local objSpace = {};
|
local objSpace = {};
|
||||||
function objSpace:each(func)
|
function objSpace:each(func)
|
||||||
for i,v in ipairs(self) do
|
for i,v in ipairs(self) do
|
||||||
coroutine.resume(coroutine.create(func),i,v);
|
wrap(catch)(func,i,v);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function objSpace:eachSync(func)
|
function objSpace:eachSync(func)
|
||||||
|
|
@ -39,6 +47,15 @@ function module.init(shared)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function objSpace.__new()
|
||||||
|
return setmetatable({},objSpace);
|
||||||
|
end
|
||||||
|
function objSpace:__newIndex(key,value)
|
||||||
|
self:each(function (this)
|
||||||
|
this[key] = value;
|
||||||
|
end);
|
||||||
|
end
|
||||||
|
objSpace.__index = objSpace;
|
||||||
|
|
||||||
function new.getObjects(id)
|
function new.getObjects(id)
|
||||||
return items[id];
|
return items[id];
|
||||||
|
|
@ -47,16 +64,51 @@ function module.init(shared)
|
||||||
local item = items[id];
|
local item = items[id];
|
||||||
return item and item[1];
|
return item and item[1];
|
||||||
end
|
end
|
||||||
-- TODO: if item is exist already, ignore this call
|
--TODO: if item is exist already, ignore this call
|
||||||
function new.addObject(id,object)
|
function new.addObject(id,object)
|
||||||
local array = items[id];
|
local array = items[id];
|
||||||
if not array then
|
if not array then
|
||||||
array = setmetatable({},{__index = objSpace});
|
array = objSpace.__new();
|
||||||
items[id] = array;
|
items[id] = array;
|
||||||
end
|
end
|
||||||
insert(array, object);
|
insert(array, object);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local store = {};
|
||||||
|
function store:__index(key)
|
||||||
|
self.__self[key];
|
||||||
|
end
|
||||||
|
function store:__newindex(key,value)
|
||||||
|
self.__self[key] = value;
|
||||||
|
local event = self.__evt[key];
|
||||||
|
if event then
|
||||||
|
for _,v in ipairs(event) do
|
||||||
|
wrap(catch)(v,value,store);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function store:__call(key,func)
|
||||||
|
local register = self.__reg[key];
|
||||||
|
if not register then
|
||||||
|
local event = {}
|
||||||
|
self.__evt[key] = event;
|
||||||
|
register = function (efunc)
|
||||||
|
insert(event,efunc);
|
||||||
|
end;
|
||||||
|
self.__reg[key] = register;
|
||||||
|
end
|
||||||
|
|
||||||
|
if func then
|
||||||
|
register(func);
|
||||||
|
end
|
||||||
|
return {register,t="reg"};
|
||||||
|
end
|
||||||
|
function new.new()
|
||||||
|
return setmetatable(
|
||||||
|
{__self = {},__evt = {},__reg = {}},store
|
||||||
|
);
|
||||||
|
end
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue