-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ConcurrentJsObject.cs
154 lines (126 loc) · 3.68 KB
/
ConcurrentJsObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Linq;
using Tenray.Topaz.Core;
namespace Tenray.Topaz.API;
public partial class ConcurrentJsObject : IJsObject, IDictionary
{
private const string NullString = "null";
// ConcurrentDictionary enumeration key order is NOT guaranteed.
// Hence, serialization and deserialization becomes flaky. Find out a solution!
readonly ConcurrentDictionary<string, object> dictionary = new();
public object this[object key]
{
get
{
if (key == null)
key = NullString;
if (dictionary.TryGetValue(key.ToString(), out var value))
return value;
return Undefined.Value;
}
set
{
Add(key, ConvertJsonElementToConcurrentJsObject(value));
}
}
public bool IsFixedSize => false;
public bool IsReadOnly => false;
public ICollection Keys => ((IDictionary)dictionary).Keys;
public ICollection Values => ((IDictionary)dictionary).Values;
public int Count => dictionary.Count;
public bool IsSynchronized => false;
public object SyncRoot => null;
public void Add(object key, object value)
{
if (key == null)
key = NullString;
dictionary.AddOrUpdate(key.ToString(), value,
(key, oldValue) =>
{
return value;
});
}
public void Clear()
{
dictionary.Clear();
}
public bool Contains(object key)
{
if (key == null)
key = NullString;
return dictionary.ContainsKey(key.ToString());
}
public void CopyTo(Array array, int index)
{
((IDictionary)dictionary).CopyTo(array, index);
}
public IDictionaryEnumerator GetEnumerator()
{
return ((IDictionary)dictionary).GetEnumerator();
}
public void Remove(object key)
{
if (key == null)
key = NullString;
((IDictionary)dictionary).Remove(key.ToString());
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IDictionary)dictionary).GetEnumerator();
}
public bool TryGetValue(object key, out object value)
{
if (key == null)
key = NullString;
return dictionary.TryGetValue(key.ToString(), out value);
}
public IEnumerable GetObjectKeys()
{
return GetObjectKeysInternal();
}
protected virtual IEnumerable GetObjectKeysInternal()
{
return Keys;
}
void IJsObject.UnwrapObject(ScriptExecutor scriptExecutor)
{
var keys = dictionary.Keys.ToArray();
foreach (var key in keys)
{
var value = scriptExecutor.GetValue(dictionary[key]);
Add(key, value);
}
}
public void SetValue(object key, object value)
{
if (key == null)
key = NullString;
dictionary.AddOrUpdate(key.ToString(), value,
(key, oldValue) =>
{
return value;
});
}
bool IJsObject.IsPrototypeProperty(object member)
{
if (member == null)
return false;
if (member is not string memberName)
return false;
return IsPrototypePropertyInternal(memberName);
}
protected virtual bool IsPrototypePropertyInternal(string memberName)
{
return false;
}
#pragma warning disable IDE1006 // Naming Styles
public virtual string toString()
{
return "[object Object]";
}
public bool hasOwnProperty(object key) => Contains(key);
public bool hasOwn(object key) => Contains(key);
#pragma warning restore IDE1006 // Naming Styles
}