Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Fix: Resolve problem of Block-scoped variable on cache code #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/target/typescript_nodeserver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ END
@io << "\n"
end
@io << "\n"
@io << ident ident "let cacheKey: string | null = null, decodedKey: string | null = null, cacheExpirationSeconds: number | null = null, cacheVersion: number | null = null;\n"
@io << ident ident "if (cacheConfig.#{op.pretty_name}) {\n"
@io << ident ident ident "try {\n"
@io << ident ident ident ident "const {key, expirationSeconds, version} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n"
@io << ident ident ident ident "if (!key) throw \"\";\n"
@io << ident ident ident ident "cacheKey = crypto.createHash(\"sha256\").update(JSON.stringify(key)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100); decodedKey = JSON.stringify(key); cacheExpirationSeconds = expirationSeconds; cacheVersion = version;\n"
@io << ident ident ident ident "const cache = await hook.getCache(cacheKey, version);console.log(JSON.stringify(cache));\n"
@io << ident ident ident ident "const {key: _cacheKey, expirationSeconds: _cacheExpirationSeconds, version: _cacheVersion} = await cacheConfig.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n"
@io << ident ident ident ident "if (!_cacheKey) throw \"\";\n"
@io << ident ident ident ident "const cacheKeyEncoded = crypto.createHash(\"sha256\").update(JSON.stringify(_cacheKey)+ \"-#{op.pretty_name}\").digest(\"hex\").substr(0, 100);\n"
@io << ident ident ident ident "const decodedKey = JSON.stringify(_cacheKey);\n"
@io << ident ident ident ident "const cache = await hook.getCache(cacheKeyEncoded, _cacheVersion);\n"
@io << ident ident ident ident "if (cache && (!cache.expirationDate || cache.expirationDate > new Date())) return cache.ret;\n"
@io << ident ident ident "} catch(e) {console.log(JSON.stringify(e));}\n"
@io << ident ident "}\n"
@io << ident ident "const ret = await fn.#{op.pretty_name}(#{(["ctx"] + op.args.map(&.name)).join(", ")});\n"
@io << ident ident op.return_type.typescript_check_decoded("ret", "\"#{op.pretty_name}.ret\"")
@io << ident ident "const encodedRet = " + op.return_type.typescript_encode("ret") + ";\n"
@io << ident ident "if (cacheKey !== null && cacheVersion !== null) hook.setCache(cacheKey, cacheExpirationSeconds ? new Date(new Date().getTime() + (cacheExpirationSeconds * 1000)) : null, cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n"
@io << ident ident "if (cacheKeyEncoded !== null && _cacheVersion !== null) hook.setCache(cacheKeyEncoded, _cacheExpirationSeconds ? new Date(new Date().getTime() + (_cacheExpirationSeconds * 1000)) : null, _cacheVersion, decodedKey!, \"#{op.pretty_name}\", encodedRet);\n"
@io << ident ident "return encodedRet"
@io << ident "},\n"
end
Expand Down