Skip to content

Commit

Permalink
Merge pull request #922 from permaweb/twilson63/fix-ao-receive
Browse files Browse the repository at this point in the history
fix: update ao receive and onReply to use x-reference
  • Loading branch information
twilson63 authored Jul 28, 2024
2 parents 94ab018 + 2fc60c1 commit 71ee890
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
78 changes: 32 additions & 46 deletions dev-cli/container/src/ao.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
local oldao = ao or {}
local ao = {
_version = "0.0.5",
_version = "0.0.6",
id = oldao.id or "",
_module = oldao._module or "",
authorities = oldao.authorities or {},
reference = oldao.reference or 0,
outbox = oldao.outbox or {Output = {}, Messages = {}, Spawns = {}, Assignments = {}},
outbox = oldao.outbox or
{Output = {}, Messages = {}, Spawns = {}, Assignments = {}},
nonExtractableTags = {
'Data-Protocol', 'Variant', 'From-Process', 'From-Module', 'Type',
'From', 'Owner', 'Anchor', 'Target', 'Data', 'Tags'
},
nonForwardableTags = {
'Data-Protocol', 'Variant', 'From-Process', 'From-Module', 'Type',
'From', 'Owner', 'Anchor', 'Target', 'Tags',
'TagArray', 'Hash-Chain', 'Timestamp', 'Nonce', 'Epoch', 'Signature',
'Forwarded-By', 'Pushed-For', 'Read-Only', 'Cron', 'Block-Height',
'Reference', 'Id', 'Reply-To'
'From', 'Owner', 'Anchor', 'Target', 'Tags', 'TagArray', 'Hash-Chain',
'Timestamp', 'Nonce', 'Epoch', 'Signature', 'Forwarded-By',
'Pushed-For', 'Read-Only', 'Cron', 'Block-Height', 'Reference', 'Id',
'Reply-To'
}
}

Expand Down Expand Up @@ -54,7 +55,7 @@ function ao.clone(obj, seen)
-- Handle non-tables and previously-seen tables.
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end

-- New table; mark it as seen and copy recursively.
local s = seen or {}
local res = {}
Expand All @@ -75,10 +76,8 @@ end
function ao.sanitize(msg)
local newMsg = ao.clone(msg)

for k,_ in pairs(newMsg) do
if _includes(ao.nonForwardableTags)(k) then
newMsg[k] = nil
end
for k, _ in pairs(newMsg) do
if _includes(ao.nonForwardableTags)(k) then newMsg[k] = nil end
end

return newMsg
Expand Down Expand Up @@ -114,7 +113,9 @@ function ao.log(txt)
end

-- clears outbox
function ao.clearOutbox() ao.outbox = {Output = {}, Messages = {}, Spawns = {}, Assignments = {}} end
function ao.clearOutbox()
ao.outbox = {Output = {}, Messages = {}, Spawns = {}, Assignments = {}}
end

function ao.send(msg)
assert(type(msg) == 'table', 'msg should be a table')
Expand Down Expand Up @@ -153,15 +154,11 @@ function ao.send(msg)

-- If running in an environment without the AOS Handlers module, do not add
-- the onReply and receive functions to the message.
if not Handlers then
return message
end
if not Handlers then return message end

-- clone message info and add to outbox
local extMessage = {}
for k, v in pairs(message) do
extMessage[k] = v
end
for k, v in pairs(message) do extMessage[k] = v end

-- add message to outbox
table.insert(ao.outbox.Messages, extMessage)
Expand All @@ -179,21 +176,14 @@ function ao.send(msg)
end

-- Add a one-time callback that runs the user's (matching) resolver on reply
Handlers.once(
{ From = from, ["X-Reference"] = message.Reference },
resolver
)
Handlers.once({From = from, ["X-Reference"] = ao.reference},
resolver)
end

message.receive = function(...)
local from = message.Target
if select("#", ...) == 1 then
from = select(1, ...)
end
return Handlers.receive({
From = from,
["X-Reference"] = message.Reference
})
if select("#", ...) == 1 then from = select(1, ...) end
return Handlers.receive({From = from, ["X-Reference"] = ao.reference})
end

return message
Expand Down Expand Up @@ -241,28 +231,24 @@ function ao.spawn(module, msg)

-- If running in an environment without the AOS Handlers module, do not add
-- the after and receive functions to the spawn.
if not Handlers then
return spawn
end
if not Handlers then return spawn end

-- clone spawn info and add to outbox
local extSpawn = {}
for k, v in pairs(spawn) do
extSpawn[k] = v
end
for k, v in pairs(spawn) do extSpawn[k] = v end

table.insert(ao.outbox.Spawns, extSpawn)

-- add 'after' callback to returned table
--local result = {}
spawn.onReply =
function(callback)
Handlers.once(
{ Action = "Spawned", From = ao.id, ["Reference"] = spawnRef },
callback
)
end
-- local result = {}
spawn.onReply = function(callback)
Handlers.once({
Action = "Spawned",
From = ao.id,
["Reference"] = spawnRef
}, callback)
end

spawn.receive = function()
return Handlers.receive({
Action = "Spawned",
Expand All @@ -271,7 +257,7 @@ function ao.spawn(module, msg)
})

end

return spawn
end

Expand Down Expand Up @@ -305,4 +291,4 @@ function ao.result(result)
}
end

return ao
return ao
4 changes: 2 additions & 2 deletions dev-cli/src/versions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
export const VERSION = {
"CLI": "0.0.61",
"IMAGE": "0.0.41"
"CLI": "0.0.62",
"IMAGE": "0.0.42"
}

0 comments on commit 71ee890

Please sign in to comment.