-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
130 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,5 @@ | ||
import Exotic from "../../types/Exotic"; | ||
import { getPayload } from "../../utils"; | ||
import { encode as encodeValue } from "../../utils"; | ||
|
||
export default function encode( | ||
scope: Exotic.Emulator, | ||
value: any, | ||
): Exotic.payload { | ||
const isObject = value !== null && typeof value === "object"; | ||
const isArray = Array.isArray(value); | ||
const result: Exotic.payload = getPayload(scope, value); | ||
|
||
if (isObject) { | ||
const copy = isArray ? [] : {}; | ||
|
||
for (const key in value) { | ||
copy[key] = encode(scope, value[key]); | ||
} | ||
|
||
result.value = copy; | ||
} | ||
|
||
return result; | ||
export default function encode(value: any): any { | ||
return encodeValue(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import Exotic from "../../types/Exotic"; | ||
import { findProxy, map } from "../../utils"; | ||
import decode from "../../utils/decode"; | ||
|
||
export default function parent( | ||
value?: Exotic.traceable, | ||
): undefined | Exotic.Proxy { | ||
const proxy = findProxy(value); | ||
if (!proxy) return; | ||
const { origin } = map.proxies.get(proxy); | ||
return origin && origin.proxy; | ||
const { origin, scope } = map.proxies.get(proxy); | ||
const decodedOrigin = decode(scope, origin) as Exotic.proxy.origin; | ||
return decodedOrigin && decodedOrigin.proxy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Exotic from "../types/Exotic"; | ||
import findProxy from "./findProxy"; | ||
import findProxyById from "./findProxyById"; | ||
|
||
function isPayload(value: any): boolean { | ||
if (!Array.isArray(value)) return false; | ||
if (!value.length || value.length > 2) return false; | ||
const [noBreak] = value; | ||
if (noBreak !== "") return false; | ||
return true; | ||
} | ||
|
||
export default function decode(scope: Exotic.Emulator, value: any): any { | ||
const isObject = value !== null && typeof value === "object"; | ||
const isArray = Array.isArray(value); | ||
const proxy = findProxy(value); | ||
const payload = isPayload(value); | ||
|
||
let result = value; | ||
|
||
if (proxy) { | ||
result = proxy; | ||
} else if (payload) { | ||
result = findProxyById(scope, value[1]); | ||
} else if (isObject) { | ||
const copy = isArray ? [] : {}; | ||
|
||
for (const key in value) { | ||
copy[key] = decode(scope, value[key]); | ||
} | ||
|
||
result = copy; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Emulator from "../Emulator"; | ||
import encode from "./encode"; | ||
|
||
const $ = new Emulator(); | ||
|
||
describe("(function) encode", () => { | ||
it("Returns a payload object from a proxy", () => { | ||
const proxy = $.use(); | ||
const payload = encode(proxy); | ||
|
||
expect(payload).toEqual(["", 1]); | ||
}); | ||
|
||
it("Returns a payload object from a non proxy value", () => { | ||
const value = null; | ||
const payload = encode(value); | ||
|
||
expect(payload).toEqual(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Exotic from "../types/Exotic"; | ||
import findProxy from "./findProxy"; | ||
import map from "./map"; | ||
|
||
export default function encode(value: any): any { | ||
const isObject = value !== null && typeof value === "object"; | ||
const isArray = Array.isArray(value); | ||
const proxy = findProxy(value); | ||
|
||
let result = value; | ||
|
||
if (proxy) { | ||
result = ["", map.proxies.get(proxy).id] as Exotic.ProxyPayload; | ||
} else if (isObject) { | ||
const copy = isArray ? [] : {}; | ||
|
||
for (const key in value) { | ||
copy[key] = encode(value[key]); | ||
} | ||
|
||
result = copy; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Exotic from "../types/Exotic"; | ||
import map from "./map"; | ||
import proxyIterator from "./proxyIterator"; | ||
|
||
export default function findProxyById( | ||
scope: Exotic.Emulator, | ||
id: number, | ||
): void | Exotic.Proxy { | ||
for (const proxy of proxyIterator()) { | ||
const { scope: proxyScope, id: proxyId } = map.proxies.get(proxy); | ||
if (scope === proxyScope && id === proxyId) { | ||
return proxy; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters