Sha256: 09e697dda72b9057fe7b9ce078cda806d921127a31ad6760c001aecc9073059b

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

with_data_isolation = (data) -> (fn) ->
  original = TwitterCldr.data
  TwitterCldr.data = data
  result = fn()
  TwitterCldr.data = original
  result

get_with_data_isolation = (data) -> (target, property_key, receiver) ->
  isolate_data = with_data_isolation(data)

  isolate_data(->
    result = Reflect.get(target, property_key, receiver)

    if typeof result == 'function'
      fn = result

      result = (args...) -> isolate_data(-> Reflect.apply(fn, target, args))

    result
  )

proxy_or_primitive = (value, handlers) ->
  if value instanceof Object
    new Proxy(value, handlers)
  else
    value

TwitterCldr.clone = (data) ->
  if !data
    throw new Error('Cannot create a clone with no data')

  get = get_with_data_isolation(data)
  isolate_data = with_data_isolation(data)

  proxy_or_primitive(
    root,
    get: (target, property_key, receiver) ->
      switch property_key
        when 'set_data'
          return -> throw new Error('Cannot set data on a TwitterCldr clone')
        else
          isolate_data(
            -> proxy_or_primitive(
              Reflect.get(target, property_key, receiver), {
                get,
                apply: (args...) ->
                  isolate_data(
                    -> Reflect.apply(args...)
                  )
                construct: (args...) ->
                  isolate_data(
                    -> proxy_or_primitive(Reflect.construct(args...), { get })
                  )
              }
            )
          )
    set: () -> throw new Error('Cannot set properties on a TwitterCldr clone')
  )

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr_js-3.3.0 lib/twitter_cldr/js/mustache/implementation/clone/clone.coffee