@aboutweb/proxyscope 中文文档教程

发布于 6年前 浏览 28 项目主页 更新于 3年前

ProxyScope

Bootstrap 将对象与 ES 代理相结合,并在其他代理工厂上重用它们。 这也将反映对原始对象所做的更改。 在使用不可变对象或混合对象时,代理也很有用。

请注意,此方法使用 代理,因此只能在现代环境中使用。 请查看 CanIUse 了解更多信息。

Install

npm i @aboutweb/proxyscope

注意此方法使用 Proxies 因此只能在现代环境中工作。 请查看 CanIUse 了解更多信息。

ProxyFactory(`…(traps || factory))

traps 为新代理

factory 合并的陷阱链 先前创建的工厂

返回具有 traps 属性的 factory 函数

factory(Object target | Array<Object target>, ...traps)

target 用作查找堆栈的对象或对象数组

traps 在创建新代理时组合

Example

import { default as proxyFactory } from "@aboutweb/proxyscope";
import otherFactory from "./otherfactory.js";

const traps = {
  has(target, property, receiver) {
    return this.findHost(property);
  },
  /*helper method*/
  findHost(property, root) {
    let host = this.stack.find(function(level) {
      return property in level;
    });

    if(host) {
      return host;
    }

    if(root === true) {
      return this.stack[0];
    }
  }
}

const factory = proxyFactory(traps, otherfactory);

export {
  factory as default,
  traps
}

ProxyScope.read

使用 target.stack 查找特性。

Example

import { read as proxyRead } from "@aboutweb/proxyscope";

let defaultConfig = {
  fu : "bar"
}

function someFn(config) {
  let proxy = proxyRead(config, defaultConfig);

  expect(proxy.fu).to.equal("bar");
  expect(proxy.bar).to.equal("baz");
}

someFn({
  bar : "baz"
});

ProxyScope.write

与 proxyRead 相同,但如果它们在级别链中的某处定义,也会转发写入。

Example

const l1 = {
  some : "value"
}

const l2 = {
  fu : "bar"
}

let proxy = proxyWrite(l1, l2);

proxy.fu = "hallo";

expect(l2.fu).to.equal("hallo");

ProxyScope.readDeep

甚至读取嵌套对象,通过为每个具有多个可能的 getter 的属性返回一个新代理来扩展代理读取

明智地使用,因为这将为每个具有多个可能值的属性查找创建一个代理。 因此,建议尽可能缓存查找。

Example

import { readDeep as proxyReadDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

const l3 = {
  other : true
}

let proxy = proxyReadDeep(l1, l2, l3);

//before caching or update cache afterwords
l3.nested = {
  deep : true
}

//cache the returned proxy
let nested = proxy.nested;

expect(nested.some).to.equal("value");
expect(nested.fu).to.equal("bar");
expect(nested.deep).to.equal(true);

ProxyScope.writeDeep

与 proxyReadDeep 相同,但也会写入嵌套对象

Example

import { writeDeep as proxyWriteDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

let proxy = proxyReadDeep(l1, l2);

//also use cache where possible
let nested = proxy.nested;

nested.some = "other";
nested.fu = "baz";

expect(l1.nested.some).to.equal("other");
expect(l2.nested.fu).to.equal("baz");

Licence

ISC

ProxyScope

Bootstrap for combining object be reference with ES proxy, and reusing them on other proxy-factories. Which will also reflect changes made to the original objects. Proxies can also be useful when using immutables or mixins.

Caution this method uses Proxies and therefor will only work in modern enviroments. Please view CanIUse for more information.

Install

npm i @aboutweb/proxyscope

Caution this method uses Proxies and therefor will only work in modern enviroments. Please view CanIUse for more information.

ProxyFactory(`…(traps || factory))

traps chain of traps merged for the new Proxy

factory a previous created factory

returns a factory function with a traps property

factory(Object target | Array<Object target>, ...traps)

target an object or array of object to use as stack for lookups

traps to combine while creating a new proxy

Example

import { default as proxyFactory } from "@aboutweb/proxyscope";
import otherFactory from "./otherfactory.js";

const traps = {
  has(target, property, receiver) {
    return this.findHost(property);
  },
  /*helper method*/
  findHost(property, root) {
    let host = this.stack.find(function(level) {
      return property in level;
    });

    if(host) {
      return host;
    }

    if(root === true) {
      return this.stack[0];
    }
  }
}

const factory = proxyFactory(traps, otherfactory);

export {
  factory as default,
  traps
}

ProxyScope.read

Uses the target.stack to look up properties.

Example

import { read as proxyRead } from "@aboutweb/proxyscope";

let defaultConfig = {
  fu : "bar"
}

function someFn(config) {
  let proxy = proxyRead(config, defaultConfig);

  expect(proxy.fu).to.equal("bar");
  expect(proxy.bar).to.equal("baz");
}

someFn({
  bar : "baz"
});

ProxyScope.write

same as proxyRead but will also forward writes if they are defined somewhere in the level chain.

Example

const l1 = {
  some : "value"
}

const l2 = {
  fu : "bar"
}

let proxy = proxyWrite(l1, l2);

proxy.fu = "hallo";

expect(l2.fu).to.equal("hallo");

ProxyScope.readDeep

read even nested objects, extends proxy-read by returning a new proxy for every property that has multiple possible getters

Use wisely, because this will create a proxy for every property lookup that has more then one possible value. Therefor it is recommended to cache lookups when possible.

Example

import { readDeep as proxyReadDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

const l3 = {
  other : true
}

let proxy = proxyReadDeep(l1, l2, l3);

//before caching or update cache afterwords
l3.nested = {
  deep : true
}

//cache the returned proxy
let nested = proxy.nested;

expect(nested.some).to.equal("value");
expect(nested.fu).to.equal("bar");
expect(nested.deep).to.equal(true);

ProxyScope.writeDeep

same as proxyReadDeep, but will also write to nested objects

Example

import { writeDeep as proxyWriteDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

let proxy = proxyReadDeep(l1, l2);

//also use cache where possible
let nested = proxy.nested;

nested.some = "other";
nested.fu = "baz";

expect(l1.nested.some).to.equal("other");
expect(l2.nested.fu).to.equal("baz");

Licence

ISC

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文